Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/butler/irc/string.rb,
lib/configuration.rb,
lib/butler/plugins.rb,
lib/ruby/string/chunks.rb,
lib/ruby/string/arguments.rb,
lib/ruby/string/unescaped.rb,
lib/ruby/string/post_arguments.rb

Overview

provides IRC-related methods for string-class

Defined Under Namespace

Classes: SingleQuoteException

Constant Summary collapse

COLORS =

Colors Lookup-table for mirc_translated_color

{
	'white'    => 0,
	'black'    => 1,
	'blue'     => 2,
	'green'    => 3,
	'red'      => 4,
	'brown'    => 5,
	'purple'   => 6,
	'orange'   => 7,
	'yellow'   => 8,
	'ltgreen'  => 9,
	'teal'     => 10,
	'ltcyan'   => 11,
	'ltblue'   => 12,
	'pink'     => 13,
	'grey'     => 14,
	'ltgrey'   => 15
}
Escapes =

convert " to “ and similar

Hash.new{|h,k|k}.update({
	'\\\\' => '\\',
	'\\"'  => '"',
	"\\'"  => "'",
	'\e'   => "\e",
	'\r'   => "\r",
	'\n'   => "\n",
	'\f'   => "\f",
	'\t'   => "\t",
	'\ '   => " ",
})

Instance Method Summary collapse

Instance Method Details

#arguments(unescape = true) ⇒ Object

parse a string of single/double/unquoted arguments (or any mix of) raises an exception if an odd number of quotes is matched examples:

'hello hello "ruby world"'.arguments # => ["hello", "hello", "ruby world"]
'hello hello ruby\ world'.arguments #  => ["hello", "hello", "ruby world"]
'hello hello ruby\ world'.arguments(false) #  => ["hello", "hello", "ruby\\ world"]


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ruby/string/arguments.rb', line 30

def arguments(unescape=true)
	args = scan(
		/
			# match a double quoted string
			"(?:\\.|[^\\"])*" |

			# match a single quoted string
			'(?:\\.|[^\\'])*' |

			# match non-quoted words (may contain escaped spaces)
			(?:\\.|[^\\'"\s])+ |

			# match a single " or '
			["']
		/x
	)
	if unescape
		args.map! { |arg| ((arg[0,1] == '"' && arg[1]) ? arg[1..-2] : arg).unescaped }
	else
		args.map! { |arg| ((arg[0,1] == '"' && arg[1]) ? arg[1..-2] : arg) }
	end
	if (sq = args.index('"')) || (sq = args.index("'")) then
		raise SingleQuoteException.new(args, sq)
	end
	args
end

#camelcaseObject

CamelCase a string, e.g. “foo_bar” becomes “FooBar”



20
21
22
# File 'lib/butler/plugins.rb', line 20

def camelcase
	scan(/[^_]+/).map { |s| s.capitalize }.join("")
end

#chunks(chunk_length) ⇒ Object

splits a string into chunks of length ‘len’



11
12
13
# File 'lib/ruby/string/chunks.rb', line 11

def chunks(chunk_length)
	scan(/.{1,#{chunk_length}}/)
end

#config_keyObject

 FIXME %2e instead of . is just ugly…



15
16
17
# File 'lib/configuration.rb', line 15

def config_key # FIXME %2e instead of . is just ugly...
	gsub(/\./, '%2e')
end

#mirc_formattedObject

provides mirc formatting: ![b]: bold ![i]: italic ![u]: underline ![r]: reverse ![c]: reset color ![cm]: set color, m=0-15 or (COLOR_CONSTANT) ![cm,n]: set color, m is foreground, n is background, m,n=0-15 or (COLOR_CONSTANT) ![o]: reset all effects Samples: ![bc(blue)]Bold blue text! and normal again ![bc2]Bold blue text! and normal again (same as above) ![bc(white),(black)]White bold text on black background! ![bi]Bold italic text!

Valid colors are: white (mirc-code: 0) black (mirc-code: 1) blue (mirc-code: 2) green (mirc-code: 3) red (mirc-code: 4) brown (mirc-code: 5) purple (mirc-code: 6) orange (mirc-code: 7) yellow (mirc-code: 8) ltgreen (mirc-code: 9) teal (mirc-code: 10) ltcyan (mirc-code: 11) ltblue (mirc-code: 12) pink (mirc-code: 13) grey (mirc-code: 14) ltgrey (mirc-code: 15)

Note: not every font/size combination displays bold/italic text.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/butler/irc/string.rb', line 125

def mirc_formatted # FIXME - copied code, ugly & slow
	self.gsub( /!\[(.*?)\]/ ) do |match|
		codes = $1.downcase
		repl = ""
		i = 0
		while i < codes.length
			case codes[i].chr
				when 'b'
					repl << 2.chr
				when 'o'
					repl << 15.chr
				when 'r'
					repl << 18.chr
				when 'u'
					repl << 31.chr
				when 'i'
					repl << 29.chr
				when '|'
					repl << 9.chr
				when 'c'
					bg = nil
					
					i, fg = mirc_translated_color( i+1, codes )
					i, bg = mirc_translated_color( i+1, codes ) if i < codes.length && codes[i].chr == ','
					
					repl << "" << ( fg || "" )
					repl << "," << bg if bg
					
					i -= 1
			end
				
			i += 1
		end
		repl
	end
end

#mirc_strippedObject

returns a string with formating codes in mirc-format stripped



83
84
85
# File 'lib/butler/irc/string.rb', line 83

def mirc_stripped
	return self.gsub(/(?:[\x02\x0f\x12\x1f\x1d\x09]|\cc\d{1,2}(?:,\d{1,2})?)/, "")
end

#mirc_translated_color(i, s) ⇒ Object

helper method for mirc_formatted, extracts color portions from ![c(color)] statements



163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/butler/irc/string.rb', line 163

def mirc_translated_color( i, s )
	return [ i, nil ] if i >= s.length
	
	if s[i].chr == '('
		j = s.index( ')', i )
		return [ j+1, "%02d" % COLORS[ s[i+1..j-1].downcase ] ]
	end
	
	j = i
	j += 1 while j < s.length && s[j].chr =~ /[0-9]/
	j += 1 if j == s.length
	return [ j, "%02d" % s[i..j-1].to_i ]
end

#post_arguments(unescaped = true) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ruby/string/post_arguments.rb', line 12

def post_arguments(unescaped=true)
	post   = []
	offset = -1
	found  = 0
	arguments = begin
		arguments(false)
	rescue SingleQuoteException => ex
		ex.pre+[ex.post.join(" ")]
	end
	arguments.each { |argument|
		post  << (unescaped ? self[offset+1..-1].unescaped : self[offset+1..-1])
		found  = index(argument, offset+1)
		offset = found+argument.length
	}
	post
end

#strip_user_prefixesObject

removes indicators from nicknames and channelnames



50
51
52
53
54
55
56
57
58
# File 'lib/butler/irc/string.rb', line 50

def strip_user_prefixes
	prefixes	= Butler::IRC::User::PREFIXES.dup
	index		= 0
	while (prefixes.has_key?(self[index,1]))
		prefixes.delete(self[index,1])
		index	+= 1
	end
	return self[index..-1]
end

#to_flagsObject

Converts string representation of user-prefixes to binary flags



74
75
76
77
78
79
80
# File 'lib/butler/irc/string.rb', line 74

def to_flags
	result	= 0
	0.upto(self.length) { |index|
		result |= Butler::IRC::User::PREFIXES[self[index,1]]
	}
	return result
end

#unescapedObject



14
15
16
# File 'lib/ruby/string/unescaped.rb', line 14

def unescaped
	gsub(/\\.|[^\\]/) { |m| Escapes[m] }
end

#user_prefixesObject

returns prefixes found in front of a nickname Sequential parsing since @@nickname is not valid.



62
63
64
65
66
67
68
69
70
71
# File 'lib/butler/irc/string.rb', line 62

def user_prefixes
	prefixes	= Butler::IRC::User::PREFIXES.dup
	index		= 0
	found		= 0
	while (prefixes.has_key?(self[index,1]))
		found |= prefixes.delete(self[index,1])
		index	+= 1
	end
	return found
end

#valid_channelname?Boolean

returns whether or not a string represents a valid channelname

Returns:

  • (Boolean)


32
33
34
# File 'lib/butler/irc/string.rb', line 32

def valid_channelname?
	self =~ /\A[&#!\+][^\x07\x0A\x0D,: ]{1,50}\z/
end

#valid_nickname?Boolean

returns if the the string represents a valid nickname this method does not take care of prefixes lik “@”, “+”, “-” see valid_user? for this funktionality or strip_user_prefixes

Returns:

  • (Boolean)


39
40
41
42
# File 'lib/butler/irc/string.rb', line 39

def valid_nickname?
	#self =~ /\A[0-9A-Za-z_][0-9A-Za-z_\-\|\\\[\]\{\}\^\`]*\z/
	self =~ /\A[0-9A-Za-z_\-\|\\\[\]\{\}\^\`]+\z/
end

#valid_user?Boolean

the same as valid_nickname? except that preceding @, + or - are ignored

Returns:

  • (Boolean)


45
46
47
# File 'lib/butler/irc/string.rb', line 45

def valid_user?
	strip_user_prefixes.valid_nickname?
end