Class: String
Instance Method Summary
collapse
Instance Method Details
#after(substr) ⇒ Object
6
7
8
9
10
|
# File 'lib/doh/core/string.rb', line 6
def after(substr)
loc = index(substr)
return nil if loc.nil?
mid(loc + substr.size)
end
|
#before(substr) ⇒ Object
18
19
20
21
22
|
# File 'lib/doh/core/string.rb', line 18
def before(substr)
loc = index(substr)
return nil if loc.nil?
firstn(loc)
end
|
#equals_ignore_whitespace(str2) ⇒ Object
66
67
68
|
# File 'lib/doh/core/string.rb', line 66
def equals_ignore_whitespace(str2)
self.normalize_all_whitespace == str2.normalize_all_whitespace
end
|
#firstn(limit) ⇒ Object
30
31
32
33
34
|
# File 'lib/doh/core/string.rb', line 30
def firstn(limit)
return nil if limit < 0
return '' if limit == 0
slice(Range.new(0, limit -1))
end
|
#gsub_between(beginstr, endstr, replace_str = nil) ⇒ Object
replaces with replace_str, or yields (and replaces with the returned value) the values found between each occurance of beginstr and endstr within the string an assumption is made that the string data between ‘beginstr’ and ‘endstr’ will not have either delimiter within it if it does, the result is undefined right now
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/doh/core/string.rb', line 46
def gsub_between(beginstr, endstr, replace_str = nil)
curpos = 0
result = self.dup
while true
begpos = result.index(beginstr, curpos)
endpos = result.index(endstr, begpos + beginstr.length) if begpos
if begpos && endpos
use_str = replace_str
if !replace_str
use_str = yield(result[(begpos+beginstr.length)..(endpos-1)])
end
result[(begpos+beginstr.length)..(endpos-1)] = use_str
curpos = begpos + beginstr.length + endstr.length + use_str.length
else
break
end
end
result
end
|
#hex_decode ⇒ Object
85
86
87
88
89
90
91
92
|
# File 'lib/doh/core/string.rb', line 85
def hex_decode
raise 'invalid hex string length' if (self.length % 2) != 0
byte_array = []
upcase.chars.each_slice(2) do |slice|
byte_array.push(hex_to_int(slice[0].getbyte(0))*16 + hex_to_int(slice[1].getbyte(0)))
end
byte_array.pack('c*')
end
|
#hex_encode ⇒ Object
79
80
81
82
83
|
# File 'lib/doh/core/string.rb', line 79
def hex_encode
bytes.to_a.collect do |byte|
"%02x" % byte
end.join
end
|
#lastn(limit) ⇒ Object
36
37
38
39
40
|
# File 'lib/doh/core/string.rb', line 36
def lastn(limit)
return nil if limit < 0
return '' if limit == 0
slice(Range.new(-limit, -1)) || self
end
|
#mid(first, count = nil) ⇒ Object
2
3
4
|
# File 'lib/doh/core/string.rb', line 2
def mid(first, count = nil)
slice(first, count || (size - first))
end
|
#normalize_all_whitespace ⇒ Object
this method strips the string, then combines all whitespace > 1 character into a single space
71
72
73
74
75
76
77
|
# File 'lib/doh/core/string.rb', line 71
def normalize_all_whitespace
result = self.strip.gsub("\n", ' ').gsub("\t", ' ')
while match = result.match(/\s\s/)
result = match.pre_match + ' ' + match.post_match
end
result
end
|
#rafter(substr) ⇒ Object
12
13
14
15
16
|
# File 'lib/doh/core/string.rb', line 12
def rafter(substr)
loc = rindex(substr)
return nil if loc.nil?
mid(loc + substr.size)
end
|
#rbefore(substr) ⇒ Object
24
25
26
27
28
|
# File 'lib/doh/core/string.rb', line 24
def rbefore(substr)
loc = rindex(substr)
return nil if loc.nil?
firstn(loc)
end
|