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/mrbgems/mruby-string-ext/mrblib/string.rb

    r331 r439  
    11class 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
    212
    223  ##
     
    9677  #
    9778  def lstrip!
    98     raise RuntimeError, "can't modify frozen String" if frozen?
     79    raise FrozenError, "can't modify frozen String" if frozen?
    9980    s = self.lstrip
    10081    (s == self) ? nil : self.replace(s)
     
    11394  #
    11495  def rstrip!
    115     raise RuntimeError, "can't modify frozen String" if frozen?
     96    raise FrozenError, "can't modify frozen String" if frozen?
    11697    s = self.rstrip
    11798    (s == self) ? nil : self.replace(s)
     
    126107  #
    127108  def strip!
    128     raise RuntimeError, "can't modify frozen String" if frozen?
     109    raise FrozenError, "can't modify frozen String" if frozen?
    129110    s = self.strip
    130111    (s == self) ? nil : self.replace(s)
     
    143124  #
    144125  def casecmp(str)
    145     self.downcase <=> str.to_str.downcase
     126    self.downcase <=> str.__to_str.downcase
    146127  rescue NoMethodError
    147     raise TypeError, "no implicit conversion of #{str.class} into String"
     128    nil
     129  end
     130
     131  ##
     132  # call-seq:
     133  #   str.casecmp?(other)  -> true, false, or nil
     134  #
     135  # Returns true if str and other_str are equal after case folding,
     136  # false if they are not equal, and nil if other_str is not a string.
     137
     138  def casecmp?(str)
     139    c = self.casecmp(str)
     140    return nil if c.nil?
     141    return c == 0
    148142  end
    149143
     
    187181  #
    188182  def slice!(arg1, arg2=nil)
    189     raise RuntimeError, "can't modify frozen String" if frozen?
     183    raise FrozenError, "can't modify frozen String" if frozen?
    190184    raise "wrong number of arguments (for 1..2)" if arg1.nil? && arg2.nil?
    191185
     
    317311  end
    318312
     313  ##
     314  # Call the given block for each character of
     315  # +self+.
    319316  def each_char(&block)
    320317    return to_enum :each_char unless block
    321 
    322     split('').each do |i|
    323       block.call(i)
     318    pos = 0
     319    while pos < self.size
     320      block.call(self[pos])
     321      pos += 1
    324322    end
    325323    self
     
    353351    self
    354352  end
     353
     354  ##
     355  #  call-seq:
     356  #    string.lines                ->  array of string
     357  #    string.lines {|s| block}    ->  array of string
     358  #
     359  #  Returns strings per line;
     360  #
     361  #    a = "abc\ndef"
     362  #    a.lines    #=> ["abc\n", "def"]
     363  #
     364  #  If a block is given, it works the same as <code>each_line</code>.
     365  def lines(&blk)
     366    lines = self.__lines
     367    if blk
     368      lines.each do |line|
     369        blk.call(line)
     370      end
     371    end
     372    lines
     373  end
     374
     375  ##
     376  #  call-seq:
     377  #     str.upto(other_str, exclusive=false) {|s| block }   -> str
     378  #     str.upto(other_str, exclusive=false)                -> an_enumerator
     379  #
     380  #  Iterates through successive values, starting at <i>str</i> and
     381  #  ending at <i>other_str</i> inclusive, passing each value in turn to
     382  #  the block. The <code>String#succ</code> method is used to generate
     383  #  each value.  If optional second argument exclusive is omitted or is false,
     384  #  the last value will be included; otherwise it will be excluded.
     385  #
     386  #  If no block is given, an enumerator is returned instead.
     387  #
     388  #     "a8".upto("b6") {|s| print s, ' ' }
     389  #     for s in "a8".."b6"
     390  #       print s, ' '
     391  #     end
     392  #
     393  #  <em>produces:</em>
     394  #
     395  #     a8 a9 b0 b1 b2 b3 b4 b5 b6
     396  #     a8 a9 b0 b1 b2 b3 b4 b5 b6
     397  #
     398  #  If <i>str</i> and <i>other_str</i> contains only ascii numeric characters,
     399  #  both are recognized as decimal numbers. In addition, the width of
     400  #  string (e.g. leading zeros) is handled appropriately.
     401  #
     402  #     "9".upto("11").to_a   #=> ["9", "10", "11"]
     403  #     "25".upto("5").to_a   #=> []
     404  #     "07".upto("11").to_a  #=> ["07", "08", "09", "10", "11"]
     405  def upto(max, exclusive=false, &block)
     406    return to_enum(:upto, max, exclusive) unless block
     407    raise TypeError, "no implicit conversion of #{max.class} into String" unless max.kind_of? String
     408
     409    len = self.length
     410    maxlen = max.length
     411    # single character
     412    if len == 1 and maxlen == 1
     413      c = self.ord
     414      e = max.ord
     415      while c <= e
     416        break if exclusive and c == e
     417        yield c.chr(__ENCODING__)
     418        c += 1
     419      end
     420      return self
     421    end
     422    # both edges are all digits
     423    bi = self.to_i(10)
     424    ei = max.to_i(10)
     425    len = self.length
     426    if (bi > 0 or bi == "0"*len) and (ei > 0 or ei == "0"*maxlen)
     427      while bi <= ei
     428        break if exclusive and bi == ei
     429        s = bi.to_s
     430        s = s.rjust(len, "0") if s.length < len
     431        yield s
     432        bi += 1
     433      end
     434      return self
     435    end
     436    bs = self
     437    while true
     438      n = (bs <=> max)
     439      break if n > 0
     440      break if exclusive and n == 0
     441      yield bs
     442      break if n == 0
     443      bs = bs.succ
     444    end
     445    self
     446  end
    355447end
Note: See TracChangeset for help on using the changeset viewer.