- Timestamp:
- Jan 21, 2018, 12:10:09 AM (6 years ago)
- Location:
- EcnlProtoTool/trunk/mruby-1.3.0
- Files:
-
- 1 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
EcnlProtoTool/trunk/mruby-1.3.0/mrbgems/mruby-string-ext/mrblib/string.rb
r321 r331 1 1 class String 2 3 ## 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 2 21 3 22 ## … … 27 46 a = 0 28 47 z = self.size - 1 29 a += 1 while " \f\n\r\t\v".include?(self[a]) and a <= z48 a += 1 while a <= z and " \f\n\r\t\v".include?(self[a]) 30 49 (z >= 0) ? self[a..z] : "" 31 50 end … … 44 63 a = 0 45 64 z = self.size - 1 46 z -= 1 while " \f\n\r\t\v\0".include?(self[z]) and a <= z65 z -= 1 while a <= z and " \f\n\r\t\v\0".include?(self[z]) 47 66 (z >= 0) ? self[a..z] : "" 48 67 end … … 60 79 a = 0 61 80 z = self.size - 1 62 a += 1 while " \f\n\r\t\v".include?(self[a]) and a <= z63 z -= 1 while " \f\n\r\t\v\0".include?(self[z]) and a <= z81 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]) 64 83 (z >= 0) ? self[a..z] : "" 65 84 end … … 77 96 # 78 97 def lstrip! 98 raise RuntimeError, "can't modify frozen String" if frozen? 79 99 s = self.lstrip 80 100 (s == self) ? nil : self.replace(s) … … 93 113 # 94 114 def rstrip! 115 raise RuntimeError, "can't modify frozen String" if frozen? 95 116 s = self.rstrip 96 117 (s == self) ? nil : self.replace(s) … … 105 126 # 106 127 def strip! 128 raise RuntimeError, "can't modify frozen String" if frozen? 107 129 s = self.strip 108 130 (s == self) ? nil : self.replace(s) … … 165 187 # 166 188 def slice!(arg1, arg2=nil) 189 raise RuntimeError, "can't modify frozen String" if frozen? 167 190 raise "wrong number of arguments (for 1..2)" if arg1.nil? && arg2.nil? 168 191 … … 189 212 idx = arg1 190 213 idx += self.size if arg1 < 0 191 validated = true if idx >=0 && arg1 < self.size 214 validated = true if idx >=0 && arg1 < self.size 192 215 end 193 216 if validated … … 236 259 # 237 260 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] 261 if idx == -1 262 return self << str 263 elsif idx < 0 264 idx += 1 265 end 266 self[idx, 0] = str 267 self 246 268 end 247 269 … … 258 280 # "hello".ljust(20, '1234') #=> "hello123412341234123" 259 281 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 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 287 end 288 289 ## 290 # call-seq: 291 # str.rjust(integer, padstr=' ') -> new_str 292 # 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>. 296 # 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 312 306 end 313 307 314 308 def chars(&block) 315 309 if block_given? 316 self.split(''). mapdo |i|310 self.split('').each do |i| 317 311 block.call(i) 318 312 end … … 322 316 end 323 317 end 324 alias each_char chars 318 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 325 327 326 328 def codepoints(&block) … … 328 330 329 331 if block_given? 330 self.split(''). mapdo|x|332 self.split('').each do|x| 331 333 block.call(x.ord) 332 334 end … … 337 339 end 338 340 alias each_codepoint codepoints 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 339 355 end
Note:
See TracChangeset
for help on using the changeset viewer.