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