Index: EcnlProtoTool/trunk/mruby-2.1.1/mrblib/string.rb
===================================================================
--- EcnlProtoTool/trunk/mruby-1.3.0/mrblib/string.rb	(revision 331)
+++ EcnlProtoTool/trunk/mruby-2.1.1/mrblib/string.rb	(revision 439)
@@ -4,5 +4,7 @@
 # ISO 15.2.10
 class String
+  # ISO 15.2.10.3
   include Comparable
+
   ##
   # Calls the given block for each line
@@ -10,16 +12,41 @@
   #
   # ISO 15.2.10.5.15
-  def each_line(rs = "\n", &block)
-    return to_enum(:each_line, rs, &block) unless block
-    return block.call(self) if rs.nil?
-    rs = rs.to_str
-    offset = 0
-    rs_len = rs.length
-    this = dup
-    while pos = this.index(rs, offset)
-      block.call(this[offset, pos + rs_len - offset])
-      offset = pos + rs_len
-    end
-    block.call(this[offset, this.size - offset]) if this.size > offset
+  def each_line(separator = "\n", &block)
+    return to_enum(:each_line, separator) unless block
+
+    if separator.nil?
+      block.call(self)
+      return self
+    end
+    raise TypeError unless separator.is_a?(String)
+
+    paragraph_mode = false
+    if separator.empty?
+      paragraph_mode = true
+      separator = "\n\n"
+    end
+    start = 0
+    string = dup
+    self_len = length
+    sep_len = separator.length
+    should_yield_subclass_instances = self.class != String
+
+    while (pointer = string.index(separator, start))
+      pointer += sep_len
+      pointer += 1 while paragraph_mode && string[pointer] == "\n"
+      if should_yield_subclass_instances
+        block.call(self.class.new(string[start, pointer - start]))
+      else
+        block.call(string[start, pointer - start])
+      end
+      start = pointer
+    end
+    return self if start == self_len
+
+    if should_yield_subclass_instances
+      block.call(self.class.new(string[start, self_len - start]))
+    else
+      block.call(string[start, self_len - start])
+    end
     self
   end
@@ -68,5 +95,5 @@
     end
     if !replace.nil? || !block
-      replace = replace.to_str
+      replace.__to_str
     end
     offset = 0
@@ -97,23 +124,20 @@
   # ISO 15.2.10.5.19
   def gsub!(*args, &block)
-    raise RuntimeError, "can't modify frozen String" if frozen?
+    raise FrozenError, "can't modify frozen String" if frozen?
     return to_enum(:gsub!, *args) if args.length == 1 && !block
     str = self.gsub(*args, &block)
-    return nil if str == self
+    return nil unless self.index(args[0])
     self.replace(str)
   end
 
-  ##
-  # Calls the given block for each match of +pattern+
-  # If no block is given return an array with all
-  # matches of +pattern+.
-  #
-  # ISO 15.2.10.5.32
-  def scan(reg, &block)
-    ### *** TODO *** ###
-    unless Object.const_defined?(:Regexp)
-      raise NotImplementedError, "scan not available (yet)"
-    end
-  end
+#  ##
+#  # Calls the given block for each match of +pattern+
+#  # If no block is given return an array with all
+#  # matches of +pattern+.
+  ##
+#  # ISO 15.2.10.5.32
+#  def scan(pattern, &block)
+#    # TODO: String#scan is not implemented yet
+#  end
 
   ##
@@ -130,10 +154,10 @@
 
     pattern, replace = *args
-    pattern = pattern.to_str
+    pattern.__to_str
     if args.length == 2 && block
       block = nil
     end
-    if !block
-      replace = replace.to_str
+    unless block
+      replace.__to_str
     end
     result = []
@@ -160,20 +184,8 @@
   # ISO 15.2.10.5.37
   def sub!(*args, &block)
-    raise RuntimeError, "can't modify frozen String" if frozen?
+    raise FrozenError, "can't modify frozen String" if frozen?
     str = self.sub(*args, &block)
-    return nil if str == self
+    return nil unless self.index(args[0])
     self.replace(str)
-  end
-
-  ##
-  # Call the given block for each character of
-  # +self+.
-  def each_char(&block)
-    pos = 0
-    while pos < self.size
-      block.call(self[pos])
-      pos += 1
-    end
-    self
   end
 
@@ -181,4 +193,5 @@
   # Call the given block for each byte of +self+.
   def each_byte(&block)
+    return to_enum(:each_byte, &block) unless block
     bytes = self.bytes
     pos = 0
@@ -190,86 +203,15 @@
   end
 
-  ##
-  # Modify +self+ by replacing the content of +self+.
-  # The portion of the string affected is determined using the same criteria as +String#[]+.
-  def []=(*args)
-    anum = args.size
-    if anum == 2
-      pos, value = args
-      case pos
-      when String
-        posnum = self.index(pos)
-        if posnum
-          b = self[0, posnum.to_i]
-          a = self[(posnum + pos.length)..-1]
-          self.replace([b, value, a].join(''))
-        else
-          raise IndexError, "string not matched"
-        end
-      when Range
-        head = pos.begin
-        tail = pos.end
-        tail += self.length if tail < 0
-        unless pos.exclude_end?
-          tail += 1
-        end
-        return self[head, tail-head]=value
-      else
-        pos += self.length if pos < 0
-        if pos < 0 || pos > self.length
-          raise IndexError, "index #{args[0]} out of string"
-        end
-        b = self[0, pos.to_i]
-        a = self[pos + 1..-1]
-        self.replace([b, value, a].join(''))
-      end
-      return value
-    elsif anum == 3
-      pos, len, value = args
-      pos += self.length if pos < 0
-      if pos < 0 || pos > self.length
-        raise IndexError, "index #{args[0]} out of string"
-      end
-      if len < 0
-        raise IndexError, "negative length #{len}"
-      end
-      b = self[0, pos.to_i]
-      a = self[pos + len..-1]
-      self.replace([b, value, a].join(''))
-      return value
-    else
-      raise ArgumentError, "wrong number of arguments (#{anum} for 2..3)"
-    end
-  end
-
+  # those two methods requires Regexp that is optional in mruby
   ##
   # ISO 15.2.10.5.3
-  def =~(re)
-    raise TypeError, "type mismatch: String given" if re.respond_to? :to_str
-    re =~ self
-  end
+  #def =~(re)
+  # re =~ self
+  #end
 
   ##
   # ISO 15.2.10.5.27
-  def match(re, &block)
-    if re.respond_to? :to_str
-      if Object.const_defined?(:Regexp)
-        r = Regexp.new(re)
-        r.match(self, &block)
-      else
-        raise NotImplementedError, "String#match needs Regexp class"
-      end
-    else
-      re.match(self, &block)
-    end
-  end
+  #def match(re, &block)
+  #  re.match(self, &block)
+  #end
 end
-
-##
-# String is comparable
-#
-# ISO 15.2.10.3
-module Comparable; end
-class String
-  include Comparable
-end
