Changeset 439 for EcnlProtoTool/trunk/mruby-2.1.1/mrblib/string.rb
- Timestamp:
- Jul 9, 2020, 8:51:43 AM (4 years ago)
- Location:
- EcnlProtoTool/trunk/mruby-2.1.1
- Files:
-
- 1 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
EcnlProtoTool/trunk/mruby-2.1.1/mrblib/string.rb
r331 r439 4 4 # ISO 15.2.10 5 5 class String 6 # ISO 15.2.10.3 6 7 include Comparable 8 7 9 ## 8 10 # Calls the given block for each line … … 10 12 # 11 13 # ISO 15.2.10.5.15 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 16 offset = 0 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 14 def each_line(separator = "\n", &block) 15 return to_enum(:each_line, separator) unless block 16 17 if separator.nil? 18 block.call(self) 19 return self 20 end 21 raise TypeError unless separator.is_a?(String) 22 23 paragraph_mode = false 24 if separator.empty? 25 paragraph_mode = true 26 separator = "\n\n" 27 end 28 start = 0 29 string = dup 30 self_len = length 31 sep_len = separator.length 32 should_yield_subclass_instances = self.class != String 33 34 while (pointer = string.index(separator, start)) 35 pointer += sep_len 36 pointer += 1 while paragraph_mode && string[pointer] == "\n" 37 if should_yield_subclass_instances 38 block.call(self.class.new(string[start, pointer - start])) 39 else 40 block.call(string[start, pointer - start]) 41 end 42 start = pointer 43 end 44 return self if start == self_len 45 46 if should_yield_subclass_instances 47 block.call(self.class.new(string[start, self_len - start])) 48 else 49 block.call(string[start, self_len - start]) 50 end 24 51 self 25 52 end … … 68 95 end 69 96 if !replace.nil? || !block 70 replace = replace.to_str97 replace.__to_str 71 98 end 72 99 offset = 0 … … 97 124 # ISO 15.2.10.5.19 98 125 def gsub!(*args, &block) 99 raise RuntimeError, "can't modify frozen String" if frozen?126 raise FrozenError, "can't modify frozen String" if frozen? 100 127 return to_enum(:gsub!, *args) if args.length == 1 && !block 101 128 str = self.gsub(*args, &block) 102 return nil if str == self129 return nil unless self.index(args[0]) 103 130 self.replace(str) 104 131 end 105 132 106 ## 107 # Calls the given block for each match of +pattern+ 108 # If no block is given return an array with all 109 # matches of +pattern+. 110 # 111 # ISO 15.2.10.5.32 112 def scan(reg, &block) 113 ### *** TODO *** ### 114 unless Object.const_defined?(:Regexp) 115 raise NotImplementedError, "scan not available (yet)" 116 end 117 end 133 # ## 134 # # Calls the given block for each match of +pattern+ 135 # # If no block is given return an array with all 136 # # matches of +pattern+. 137 ## 138 # # ISO 15.2.10.5.32 139 # def scan(pattern, &block) 140 # # TODO: String#scan is not implemented yet 141 # end 118 142 119 143 ## … … 130 154 131 155 pattern, replace = *args 132 pattern = pattern.to_str156 pattern.__to_str 133 157 if args.length == 2 && block 134 158 block = nil 135 159 end 136 if !block137 replace = replace.to_str160 unless block 161 replace.__to_str 138 162 end 139 163 result = [] … … 160 184 # ISO 15.2.10.5.37 161 185 def sub!(*args, &block) 162 raise RuntimeError, "can't modify frozen String" if frozen?186 raise FrozenError, "can't modify frozen String" if frozen? 163 187 str = self.sub(*args, &block) 164 return nil if str == self188 return nil unless self.index(args[0]) 165 189 self.replace(str) 166 end167 168 ##169 # Call the given block for each character of170 # +self+.171 def each_char(&block)172 pos = 0173 while pos < self.size174 block.call(self[pos])175 pos += 1176 end177 self178 190 end 179 191 … … 181 193 # Call the given block for each byte of +self+. 182 194 def each_byte(&block) 195 return to_enum(:each_byte, &block) unless block 183 196 bytes = self.bytes 184 197 pos = 0 … … 190 203 end 191 204 192 ## 193 # Modify +self+ by replacing the content of +self+. 194 # The portion of the string affected is determined using the same criteria as +String#[]+. 195 def []=(*args) 196 anum = args.size 197 if anum == 2 198 pos, value = args 199 case pos 200 when String 201 posnum = self.index(pos) 202 if posnum 203 b = self[0, posnum.to_i] 204 a = self[(posnum + pos.length)..-1] 205 self.replace([b, value, a].join('')) 206 else 207 raise IndexError, "string not matched" 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 217 else 218 pos += self.length if pos < 0 219 if pos < 0 || pos > self.length 220 raise IndexError, "index #{args[0]} out of string" 221 end 222 b = self[0, pos.to_i] 223 a = self[pos + 1..-1] 224 self.replace([b, value, a].join('')) 225 end 226 return value 227 elsif anum == 3 228 pos, len, value = args 229 pos += self.length if pos < 0 230 if pos < 0 || pos > self.length 231 raise IndexError, "index #{args[0]} out of string" 232 end 233 if len < 0 234 raise IndexError, "negative length #{len}" 235 end 236 b = self[0, pos.to_i] 237 a = self[pos + len..-1] 238 self.replace([b, value, a].join('')) 239 return value 240 else 241 raise ArgumentError, "wrong number of arguments (#{anum} for 2..3)" 242 end 243 end 244 205 # those two methods requires Regexp that is optional in mruby 245 206 ## 246 207 # ISO 15.2.10.5.3 247 def =~(re) 248 raise TypeError, "type mismatch: String given" if re.respond_to? :to_str 249 re =~ self 250 end 208 #def =~(re) 209 # re =~ self 210 #end 251 211 252 212 ## 253 213 # ISO 15.2.10.5.27 254 def match(re, &block) 255 if re.respond_to? :to_str 256 if Object.const_defined?(:Regexp) 257 r = Regexp.new(re) 258 r.match(self, &block) 259 else 260 raise NotImplementedError, "String#match needs Regexp class" 261 end 262 else 263 re.match(self, &block) 264 end 265 end 214 #def match(re, &block) 215 # re.match(self, &block) 216 #end 266 217 end 267 268 ##269 # String is comparable270 #271 # ISO 15.2.10.3272 module Comparable; end273 class String274 include Comparable275 end
Note:
See TracChangeset
for help on using the changeset viewer.