Ignore:
Timestamp:
Jul 9, 2020, 8:51:43 AM (4 years ago)
Author:
coas-nagasima
Message:

mrubyを2.1.1に更新

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  
    44# ISO 15.2.10
    55class String
     6  # ISO 15.2.10.3
    67  include Comparable
     8
    79  ##
    810  # Calls the given block for each line
     
    1012  #
    1113  # 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
    2451    self
    2552  end
     
    6895    end
    6996    if !replace.nil? || !block
    70       replace = replace.to_str
     97      replace.__to_str
    7198    end
    7299    offset = 0
     
    97124  # ISO 15.2.10.5.19
    98125  def gsub!(*args, &block)
    99     raise RuntimeError, "can't modify frozen String" if frozen?
     126    raise FrozenError, "can't modify frozen String" if frozen?
    100127    return to_enum(:gsub!, *args) if args.length == 1 && !block
    101128    str = self.gsub(*args, &block)
    102     return nil if str == self
     129    return nil unless self.index(args[0])
    103130    self.replace(str)
    104131  end
    105132
    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
    118142
    119143  ##
     
    130154
    131155    pattern, replace = *args
    132     pattern = pattern.to_str
     156    pattern.__to_str
    133157    if args.length == 2 && block
    134158      block = nil
    135159    end
    136     if !block
    137       replace = replace.to_str
     160    unless block
     161      replace.__to_str
    138162    end
    139163    result = []
     
    160184  # ISO 15.2.10.5.37
    161185  def sub!(*args, &block)
    162     raise RuntimeError, "can't modify frozen String" if frozen?
     186    raise FrozenError, "can't modify frozen String" if frozen?
    163187    str = self.sub(*args, &block)
    164     return nil if str == self
     188    return nil unless self.index(args[0])
    165189    self.replace(str)
    166   end
    167 
    168   ##
    169   # Call the given block for each character of
    170   # +self+.
    171   def each_char(&block)
    172     pos = 0
    173     while pos < self.size
    174       block.call(self[pos])
    175       pos += 1
    176     end
    177     self
    178190  end
    179191
     
    181193  # Call the given block for each byte of +self+.
    182194  def each_byte(&block)
     195    return to_enum(:each_byte, &block) unless block
    183196    bytes = self.bytes
    184197    pos = 0
     
    190203  end
    191204
    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
    245206  ##
    246207  # 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
    251211
    252212  ##
    253213  # 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
    266217end
    267 
    268 ##
    269 # String is comparable
    270 #
    271 # ISO 15.2.10.3
    272 module Comparable; end
    273 class String
    274   include Comparable
    275 end
Note: See TracChangeset for help on using the changeset viewer.