Index: EcnlProtoTool/trunk/mruby-1.3.0/mrblib/string.rb
===================================================================
--- EcnlProtoTool/trunk/mruby-1.2.0/mrblib/string.rb	(revision 321)
+++ EcnlProtoTool/trunk/mruby-1.3.0/mrblib/string.rb	(revision 331)
@@ -10,11 +10,16 @@
   #
   # ISO 15.2.10.5.15
-  def each_line(&block)
+  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
-    while pos = self.index("\n", offset)
-      block.call(self[offset, pos + 1 - offset])
-      offset = pos + 1
-    end
-    block.call(self[offset, self.size - offset]) if self.size > offset
+    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
     self
   end
@@ -35,4 +40,6 @@
           when "'"
             post
+          when "1", "2", "3", "4", "5", "6", "7", "8", "9"
+            ""
           else
             self[j, 2]
@@ -52,21 +59,32 @@
   # ISO 15.2.10.5.18
   def gsub(*args, &block)
-    if args.size == 2
-      s = ""
-      i = 0
-      while j = index(args[0], i)
-        seplen = args[0].length
-        k = j + seplen
-        pre = self[0, j]
-        post = self[k, length-k]
-        s += self[i, j-i] + args[1].__sub_replace(pre, args[0], post)
-        i = k
-      end
-      s + self[i, length-i]
-    elsif args.size == 1 && block
-      split(args[0], -1).join(block.call(args[0]))
-    else
-      raise ArgumentError, "wrong number of arguments"
-    end
+    return to_enum(:gsub, *args) if args.length == 1 && !block
+    raise ArgumentError, "wrong number of arguments" unless (1..2).include?(args.length)
+
+    pattern, replace = *args
+    plen = pattern.length
+    if args.length == 2 && block
+      block = nil
+    end
+    if !replace.nil? || !block
+      replace = replace.to_str
+    end
+    offset = 0
+    result = []
+    while found = index(pattern, offset)
+      result << self[offset, found - offset]
+      offset = found + plen
+      result << if block
+        block.call(pattern).to_s
+      else
+        replace.__sub_replace(self[0, found], pattern, self[offset..-1] || "")
+      end
+      if plen == 0
+        result << self[offset, 1]
+        offset += 1
+      end
+    end
+    result << self[offset..-1] if offset < length
+    result.join
   end
 
@@ -79,4 +97,6 @@
   # ISO 15.2.10.5.19
   def gsub!(*args, &block)
+    raise RuntimeError, "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
@@ -105,13 +125,29 @@
   # ISO 15.2.10.5.36
   def sub(*args, &block)
-    if args.size == 2
-      pre, post = split(args[0], 2)
-      return self unless post # The sub target wasn't found in the string
-      pre + args[1].__sub_replace(pre, args[0], post) + post
-    elsif args.size == 1 && block
-      split(args[0], 2).join(block.call(args[0]))
+    unless (1..2).include?(args.length)
+      raise ArgumentError, "wrong number of arguments (given #{args.length}, expected 2)"
+    end
+
+    pattern, replace = *args
+    pattern = pattern.to_str
+    if args.length == 2 && block
+      block = nil
+    end
+    if !block
+      replace = replace.to_str
+    end
+    result = []
+    this = dup
+    found = index(pattern)
+    return this unless found
+    result << this[0, found]
+    offset = found + pattern.length
+    result << if block
+      block.call(pattern).to_s
     else
-      raise ArgumentError, "wrong number of arguments"
-    end
+      replace.__sub_replace(this[0, found], pattern, this[offset..-1] || "")
+    end
+    result << this[offset..-1] if offset < length
+    result.join
   end
 
@@ -124,4 +160,5 @@
   # ISO 15.2.10.5.37
   def sub!(*args, &block)
+    raise RuntimeError, "can't modify frozen String" if frozen?
     str = self.sub(*args, &block)
     return nil if str == self
@@ -160,5 +197,6 @@
     if anum == 2
       pos, value = args
-      if pos.kind_of? String
+      case pos
+      when String
         posnum = self.index(pos)
         if posnum
@@ -166,8 +204,15 @@
           a = self[(posnum + pos.length)..-1]
           self.replace([b, value, a].join(''))
-          return value
         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
@@ -178,6 +223,6 @@
         a = self[pos + 1..-1]
         self.replace([b, value, a].join(''))
-        return value
-      end
+      end
+      return value
     elsif anum == 3
       pos, len, value = args
