Changeset 331 for EcnlProtoTool/trunk/mruby-1.3.0/mrblib/string.rb
- 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/mrblib/string.rb
r321 r331 10 10 # 11 11 # ISO 15.2.10.5.15 12 def each_line(&block) 12 def each_line(rs = "\n", &block) 13 return to_enum(:each_line, rs, &block) unless block 14 return block.call(self) if rs.nil? 15 rs = rs.to_str 13 16 offset = 0 14 while pos = self.index("\n", offset) 15 block.call(self[offset, pos + 1 - offset]) 16 offset = pos + 1 17 end 18 block.call(self[offset, self.size - offset]) if self.size > offset 17 rs_len = rs.length 18 this = dup 19 while pos = this.index(rs, offset) 20 block.call(this[offset, pos + rs_len - offset]) 21 offset = pos + rs_len 22 end 23 block.call(this[offset, this.size - offset]) if this.size > offset 19 24 self 20 25 end … … 35 40 when "'" 36 41 post 42 when "1", "2", "3", "4", "5", "6", "7", "8", "9" 43 "" 37 44 else 38 45 self[j, 2] … … 52 59 # ISO 15.2.10.5.18 53 60 def gsub(*args, &block) 54 if args.size == 2 55 s = "" 56 i = 0 57 while j = index(args[0], i) 58 seplen = args[0].length 59 k = j + seplen 60 pre = self[0, j] 61 post = self[k, length-k] 62 s += self[i, j-i] + args[1].__sub_replace(pre, args[0], post) 63 i = k 64 end 65 s + self[i, length-i] 66 elsif args.size == 1 && block 67 split(args[0], -1).join(block.call(args[0])) 68 else 69 raise ArgumentError, "wrong number of arguments" 70 end 61 return to_enum(:gsub, *args) if args.length == 1 && !block 62 raise ArgumentError, "wrong number of arguments" unless (1..2).include?(args.length) 63 64 pattern, replace = *args 65 plen = pattern.length 66 if args.length == 2 && block 67 block = nil 68 end 69 if !replace.nil? || !block 70 replace = replace.to_str 71 end 72 offset = 0 73 result = [] 74 while found = index(pattern, offset) 75 result << self[offset, found - offset] 76 offset = found + plen 77 result << if block 78 block.call(pattern).to_s 79 else 80 replace.__sub_replace(self[0, found], pattern, self[offset..-1] || "") 81 end 82 if plen == 0 83 result << self[offset, 1] 84 offset += 1 85 end 86 end 87 result << self[offset..-1] if offset < length 88 result.join 71 89 end 72 90 … … 79 97 # ISO 15.2.10.5.19 80 98 def gsub!(*args, &block) 99 raise RuntimeError, "can't modify frozen String" if frozen? 100 return to_enum(:gsub!, *args) if args.length == 1 && !block 81 101 str = self.gsub(*args, &block) 82 102 return nil if str == self … … 105 125 # ISO 15.2.10.5.36 106 126 def sub(*args, &block) 107 if args.size == 2 108 pre, post = split(args[0], 2) 109 return self unless post # The sub target wasn't found in the string 110 pre + args[1].__sub_replace(pre, args[0], post) + post 111 elsif args.size == 1 && block 112 split(args[0], 2).join(block.call(args[0])) 127 unless (1..2).include?(args.length) 128 raise ArgumentError, "wrong number of arguments (given #{args.length}, expected 2)" 129 end 130 131 pattern, replace = *args 132 pattern = pattern.to_str 133 if args.length == 2 && block 134 block = nil 135 end 136 if !block 137 replace = replace.to_str 138 end 139 result = [] 140 this = dup 141 found = index(pattern) 142 return this unless found 143 result << this[0, found] 144 offset = found + pattern.length 145 result << if block 146 block.call(pattern).to_s 113 147 else 114 raise ArgumentError, "wrong number of arguments" 115 end 148 replace.__sub_replace(this[0, found], pattern, this[offset..-1] || "") 149 end 150 result << this[offset..-1] if offset < length 151 result.join 116 152 end 117 153 … … 124 160 # ISO 15.2.10.5.37 125 161 def sub!(*args, &block) 162 raise RuntimeError, "can't modify frozen String" if frozen? 126 163 str = self.sub(*args, &block) 127 164 return nil if str == self … … 160 197 if anum == 2 161 198 pos, value = args 162 if pos.kind_of? String 199 case pos 200 when String 163 201 posnum = self.index(pos) 164 202 if posnum … … 166 204 a = self[(posnum + pos.length)..-1] 167 205 self.replace([b, value, a].join('')) 168 return value169 206 else 170 207 raise IndexError, "string not matched" 171 208 end 209 when Range 210 head = pos.begin 211 tail = pos.end 212 tail += self.length if tail < 0 213 unless pos.exclude_end? 214 tail += 1 215 end 216 return self[head, tail-head]=value 172 217 else 173 218 pos += self.length if pos < 0 … … 178 223 a = self[pos + 1..-1] 179 224 self.replace([b, value, a].join('')) 180 return value181 end225 end 226 return value 182 227 elsif anum == 3 183 228 pos, len, value = args
Note:
See TracChangeset
for help on using the changeset viewer.