1 | class String
|
---|
2 |
|
---|
3 | ##
|
---|
4 | # call-seq:
|
---|
5 | # string.clear -> string
|
---|
6 | #
|
---|
7 | # Makes string empty.
|
---|
8 | #
|
---|
9 | # a = "abcde"
|
---|
10 | # a.clear #=> ""
|
---|
11 | #
|
---|
12 | def clear
|
---|
13 | self.replace("")
|
---|
14 | end
|
---|
15 |
|
---|
16 | ##
|
---|
17 | # call-seq:
|
---|
18 | # str.lstrip -> new_str
|
---|
19 | #
|
---|
20 | # Returns a copy of <i>str</i> with leading whitespace removed. See also
|
---|
21 | # <code>String#rstrip</code> and <code>String#strip</code>.
|
---|
22 | #
|
---|
23 | # " hello ".lstrip #=> "hello "
|
---|
24 | # "hello".lstrip #=> "hello"
|
---|
25 | #
|
---|
26 | def lstrip
|
---|
27 | a = 0
|
---|
28 | z = self.size - 1
|
---|
29 | a += 1 while " \f\n\r\t\v".include?(self[a]) and a <= z
|
---|
30 | (z >= 0) ? self[a..z] : ""
|
---|
31 | end
|
---|
32 |
|
---|
33 | ##
|
---|
34 | # call-seq:
|
---|
35 | # str.rstrip -> new_str
|
---|
36 | #
|
---|
37 | # Returns a copy of <i>str</i> with trailing whitespace removed. See also
|
---|
38 | # <code>String#lstrip</code> and <code>String#strip</code>.
|
---|
39 | #
|
---|
40 | # " hello ".rstrip #=> " hello"
|
---|
41 | # "hello".rstrip #=> "hello"
|
---|
42 | #
|
---|
43 | def rstrip
|
---|
44 | a = 0
|
---|
45 | z = self.size - 1
|
---|
46 | z -= 1 while " \f\n\r\t\v\0".include?(self[z]) and a <= z
|
---|
47 | (z >= 0) ? self[a..z] : ""
|
---|
48 | end
|
---|
49 |
|
---|
50 | ##
|
---|
51 | # call-seq:
|
---|
52 | # str.strip -> new_str
|
---|
53 | #
|
---|
54 | # Returns a copy of <i>str</i> with leading and trailing whitespace removed.
|
---|
55 | #
|
---|
56 | # " hello ".strip #=> "hello"
|
---|
57 | # "\tgoodbye\r\n".strip #=> "goodbye"
|
---|
58 | #
|
---|
59 | def strip
|
---|
60 | a = 0
|
---|
61 | z = self.size - 1
|
---|
62 | a += 1 while " \f\n\r\t\v".include?(self[a]) and a <= z
|
---|
63 | z -= 1 while " \f\n\r\t\v\0".include?(self[z]) and a <= z
|
---|
64 | (z >= 0) ? self[a..z] : ""
|
---|
65 | end
|
---|
66 |
|
---|
67 | ##
|
---|
68 | # call-seq:
|
---|
69 | # str.lstrip! -> self or nil
|
---|
70 | #
|
---|
71 | # Removes leading whitespace from <i>str</i>, returning <code>nil</code> if no
|
---|
72 | # change was made. See also <code>String#rstrip!</code> and
|
---|
73 | # <code>String#strip!</code>.
|
---|
74 | #
|
---|
75 | # " hello ".lstrip #=> "hello "
|
---|
76 | # "hello".lstrip! #=> nil
|
---|
77 | #
|
---|
78 | def lstrip!
|
---|
79 | s = self.lstrip
|
---|
80 | (s == self) ? nil : self.replace(s)
|
---|
81 | end
|
---|
82 |
|
---|
83 | ##
|
---|
84 | # call-seq:
|
---|
85 | # str.rstrip! -> self or nil
|
---|
86 | #
|
---|
87 | # Removes trailing whitespace from <i>str</i>, returning <code>nil</code> if
|
---|
88 | # no change was made. See also <code>String#lstrip!</code> and
|
---|
89 | # <code>String#strip!</code>.
|
---|
90 | #
|
---|
91 | # " hello ".rstrip #=> " hello"
|
---|
92 | # "hello".rstrip! #=> nil
|
---|
93 | #
|
---|
94 | def rstrip!
|
---|
95 | s = self.rstrip
|
---|
96 | (s == self) ? nil : self.replace(s)
|
---|
97 | end
|
---|
98 |
|
---|
99 | ##
|
---|
100 | # call-seq:
|
---|
101 | # str.strip! -> str or nil
|
---|
102 | #
|
---|
103 | # Removes leading and trailing whitespace from <i>str</i>. Returns
|
---|
104 | # <code>nil</code> if <i>str</i> was not altered.
|
---|
105 | #
|
---|
106 | def strip!
|
---|
107 | s = self.strip
|
---|
108 | (s == self) ? nil : self.replace(s)
|
---|
109 | end
|
---|
110 |
|
---|
111 | ##
|
---|
112 | # call-seq:
|
---|
113 | # str.casecmp(other_str) -> -1, 0, +1 or nil
|
---|
114 | #
|
---|
115 | # Case-insensitive version of <code>String#<=></code>.
|
---|
116 | #
|
---|
117 | # "abcdef".casecmp("abcde") #=> 1
|
---|
118 | # "aBcDeF".casecmp("abcdef") #=> 0
|
---|
119 | # "abcdef".casecmp("abcdefg") #=> -1
|
---|
120 | # "abcdef".casecmp("ABCDEF") #=> 0
|
---|
121 | #
|
---|
122 | def casecmp(str)
|
---|
123 | self.downcase <=> str.to_str.downcase
|
---|
124 | rescue NoMethodError
|
---|
125 | raise TypeError, "no implicit conversion of #{str.class} into String"
|
---|
126 | end
|
---|
127 |
|
---|
128 | def partition(sep)
|
---|
129 | raise TypeError, "type mismatch: #{sep.class} given" unless sep.is_a? String
|
---|
130 | n = index(sep)
|
---|
131 | unless n.nil?
|
---|
132 | m = n + sep.size
|
---|
133 | [ slice(0, n), sep, slice(m, size - m) ]
|
---|
134 | else
|
---|
135 | [ self, "", "" ]
|
---|
136 | end
|
---|
137 | end
|
---|
138 |
|
---|
139 | def rpartition(sep)
|
---|
140 | raise TypeError, "type mismatch: #{sep.class} given" unless sep.is_a? String
|
---|
141 | n = rindex(sep)
|
---|
142 | unless n.nil?
|
---|
143 | m = n + sep.size
|
---|
144 | [ slice(0, n), sep, slice(m, size - m) ]
|
---|
145 | else
|
---|
146 | [ "", "", self ]
|
---|
147 | end
|
---|
148 | end
|
---|
149 |
|
---|
150 | ##
|
---|
151 | # call-seq:
|
---|
152 | # str.slice!(fixnum) -> new_str or nil
|
---|
153 | # str.slice!(fixnum, fixnum) -> new_str or nil
|
---|
154 | # str.slice!(range) -> new_str or nil
|
---|
155 | # str.slice!(other_str) -> new_str or nil
|
---|
156 | #
|
---|
157 | # Deletes the specified portion from <i>str</i>, and returns the portion
|
---|
158 | # deleted.
|
---|
159 | #
|
---|
160 | # string = "this is a string"
|
---|
161 | # string.slice!(2) #=> "i"
|
---|
162 | # string.slice!(3..6) #=> " is "
|
---|
163 | # string.slice!("r") #=> "r"
|
---|
164 | # string #=> "thsa sting"
|
---|
165 | #
|
---|
166 | def slice!(arg1, arg2=nil)
|
---|
167 | raise "wrong number of arguments (for 1..2)" if arg1.nil? && arg2.nil?
|
---|
168 |
|
---|
169 | if !arg1.nil? && !arg2.nil?
|
---|
170 | idx = arg1
|
---|
171 | idx += self.size if arg1 < 0
|
---|
172 | if idx >= 0 && idx <= self.size && arg2 > 0
|
---|
173 | str = self[idx, arg2]
|
---|
174 | else
|
---|
175 | return nil
|
---|
176 | end
|
---|
177 | else
|
---|
178 | validated = false
|
---|
179 | if arg1.kind_of?(Range)
|
---|
180 | beg = arg1.begin
|
---|
181 | ed = arg1.end
|
---|
182 | beg += self.size if beg < 0
|
---|
183 | ed += self.size if ed < 0
|
---|
184 | ed -= 1 if arg1.exclude_end?
|
---|
185 | validated = true
|
---|
186 | elsif arg1.kind_of?(String)
|
---|
187 | validated = true
|
---|
188 | else
|
---|
189 | idx = arg1
|
---|
190 | idx += self.size if arg1 < 0
|
---|
191 | validated = true if idx >=0 && arg1 < self.size
|
---|
192 | end
|
---|
193 | if validated
|
---|
194 | str = self[arg1]
|
---|
195 | else
|
---|
196 | return nil
|
---|
197 | end
|
---|
198 | end
|
---|
199 | unless str.nil? || str == ""
|
---|
200 | if !arg1.nil? && !arg2.nil?
|
---|
201 | idx = arg1 >= 0 ? arg1 : self.size+arg1
|
---|
202 | str2 = self[0...idx] + self[idx+arg2..-1].to_s
|
---|
203 | else
|
---|
204 | if arg1.kind_of?(Range)
|
---|
205 | idx = beg >= 0 ? beg : self.size+beg
|
---|
206 | idx2 = ed>= 0 ? ed : self.size+ed
|
---|
207 | str2 = self[0...idx] + self[idx2+1..-1].to_s
|
---|
208 | elsif arg1.kind_of?(String)
|
---|
209 | idx = self.index(arg1)
|
---|
210 | str2 = self[0...idx] + self[idx+arg1.size..-1] unless idx.nil?
|
---|
211 | else
|
---|
212 | idx = arg1 >= 0 ? arg1 : self.size+arg1
|
---|
213 | str2 = self[0...idx] + self[idx+1..-1].to_s
|
---|
214 | end
|
---|
215 | end
|
---|
216 | self.replace(str2) unless str2.nil?
|
---|
217 | end
|
---|
218 | str
|
---|
219 | end
|
---|
220 |
|
---|
221 | ##
|
---|
222 | # call-seq:
|
---|
223 | # str.insert(index, other_str) -> str
|
---|
224 | #
|
---|
225 | # Inserts <i>other_str</i> before the character at the given
|
---|
226 | # <i>index</i>, modifying <i>str</i>. Negative indices count from the
|
---|
227 | # end of the string, and insert <em>after</em> the given character.
|
---|
228 | # The intent is insert <i>aString</i> so that it starts at the given
|
---|
229 | # <i>index</i>.
|
---|
230 | #
|
---|
231 | # "abcd".insert(0, 'X') #=> "Xabcd"
|
---|
232 | # "abcd".insert(3, 'X') #=> "abcXd"
|
---|
233 | # "abcd".insert(4, 'X') #=> "abcdX"
|
---|
234 | # "abcd".insert(-3, 'X') #=> "abXcd"
|
---|
235 | # "abcd".insert(-1, 'X') #=> "abcdX"
|
---|
236 | #
|
---|
237 | def insert(idx, str)
|
---|
238 | pos = idx.to_i
|
---|
239 | pos += self.size + 1 if pos < 0
|
---|
240 |
|
---|
241 | raise IndexError, "index #{idx.to_i} out of string" if pos < 0 || pos > self.size
|
---|
242 |
|
---|
243 | return self + str if pos == -1
|
---|
244 | return str + self if pos == 0
|
---|
245 | return self[0..pos - 1] + str + self[pos..-1]
|
---|
246 | end
|
---|
247 |
|
---|
248 | ##
|
---|
249 | # call-seq:
|
---|
250 | # str.ljust(integer, padstr=' ') -> new_str
|
---|
251 | #
|
---|
252 | # If <i>integer</i> is greater than the length of <i>str</i>, returns a new
|
---|
253 | # <code>String</code> of length <i>integer</i> with <i>str</i> left justified
|
---|
254 | # and padded with <i>padstr</i>; otherwise, returns <i>str</i>.
|
---|
255 | #
|
---|
256 | # "hello".ljust(4) #=> "hello"
|
---|
257 | # "hello".ljust(20) #=> "hello "
|
---|
258 | # "hello".ljust(20, '1234') #=> "hello123412341234123"
|
---|
259 | def ljust(idx, padstr = ' ')
|
---|
260 | if idx <= self.size
|
---|
261 | return self
|
---|
262 | end
|
---|
263 | newstr = self.dup
|
---|
264 | newstr << padstr
|
---|
265 | while newstr.size <= idx
|
---|
266 | newstr << padstr
|
---|
267 | end
|
---|
268 | return newstr.slice(0,idx)
|
---|
269 | end
|
---|
270 |
|
---|
271 | # str.upto(other_str, exclusive=false) {|s| block } -> str
|
---|
272 | # str.upto(other_str, exclusive=false) -> an_enumerator
|
---|
273 | #
|
---|
274 | # Iterates through successive values, starting at <i>str</i> and
|
---|
275 | # ending at <i>other_str</i> inclusive, passing each value in turn to
|
---|
276 | # the block. The <code>String#succ</code> method is used to generate
|
---|
277 | # each value. If optional second argument exclusive is omitted or is false,
|
---|
278 | # the last value will be included; otherwise it will be excluded.
|
---|
279 | #
|
---|
280 | # If no block is given, an enumerator is returned instead.
|
---|
281 | #
|
---|
282 | # "a8".upto("b6") {|s| print s, ' ' }
|
---|
283 | # for s in "a8".."b6"
|
---|
284 | # print s, ' '
|
---|
285 | # end
|
---|
286 | #
|
---|
287 | # <em>produces:</em>
|
---|
288 | #
|
---|
289 | # a8 a9 b0 b1 b2 b3 b4 b5 b6
|
---|
290 | # a8 a9 b0 b1 b2 b3 b4 b5 b6
|
---|
291 | #
|
---|
292 | # If <i>str</i> and <i>other_str</i> contains only ascii numeric characters,
|
---|
293 | # both are recognized as decimal numbers. In addition, the width of
|
---|
294 | # string (e.g. leading zeros) is handled appropriately.
|
---|
295 | #
|
---|
296 | # "9".upto("11").to_a #=> ["9", "10", "11"]
|
---|
297 | # "25".upto("5").to_a #=> []
|
---|
298 | # "07".upto("11").to_a #=> ["07", "08", "09", "10", "11"]
|
---|
299 | #
|
---|
300 | def upto(other_str, excl=false, &block)
|
---|
301 | return to_enum :upto, other_str, excl unless block
|
---|
302 |
|
---|
303 | str = self
|
---|
304 | n = self.<=>other_str
|
---|
305 | return self if n > 0 || (self == other_str && excl)
|
---|
306 | while true
|
---|
307 | block.call(str)
|
---|
308 | return self if !excl && str == other_str
|
---|
309 | str = str.succ
|
---|
310 | return self if excl && str == other_str
|
---|
311 | end
|
---|
312 | end
|
---|
313 |
|
---|
314 | def chars(&block)
|
---|
315 | if block_given?
|
---|
316 | self.split('').map do |i|
|
---|
317 | block.call(i)
|
---|
318 | end
|
---|
319 | self
|
---|
320 | else
|
---|
321 | self.split('')
|
---|
322 | end
|
---|
323 | end
|
---|
324 | alias each_char chars
|
---|
325 |
|
---|
326 | def codepoints(&block)
|
---|
327 | len = self.size
|
---|
328 |
|
---|
329 | if block_given?
|
---|
330 | self.split('').map do|x|
|
---|
331 | block.call(x.ord)
|
---|
332 | end
|
---|
333 | self
|
---|
334 | else
|
---|
335 | self.split('').map{|x| x.ord}
|
---|
336 | end
|
---|
337 | end
|
---|
338 | alias each_codepoint codepoints
|
---|
339 | end
|
---|