diff options
| -rw-r--r-- | mrbgems/mruby-io/mrblib/io.rb | 6 | ||||
| -rw-r--r-- | mrbgems/mruby-io/src/io.c | 3 | ||||
| -rw-r--r-- | mrblib/string.rb | 47 | ||||
| -rw-r--r-- | src/string.c | 5 |
4 files changed, 41 insertions, 20 deletions
diff --git a/mrbgems/mruby-io/mrblib/io.rb b/mrbgems/mruby-io/mrblib/io.rb index f3c4de6fd..a6e3ef3de 100644 --- a/mrbgems/mruby-io/mrblib/io.rb +++ b/mrbgems/mruby-io/mrblib/io.rb @@ -226,12 +226,12 @@ class IO end end - def readline(arg = $/, limit = nil) + def readline(arg = "\n", limit = nil) case arg when String rs = arg when Fixnum - rs = $/ + rs = "\n" limit = arg else raise ArgumentError @@ -242,7 +242,7 @@ class IO end if rs == "" - rs = $/ + $/ + rs = "\n\n" end array = [] diff --git a/mrbgems/mruby-io/src/io.c b/mrbgems/mruby-io/src/io.c index eb9c4097b..784cdaf49 100644 --- a/mrbgems/mruby-io/src/io.c +++ b/mrbgems/mruby-io/src/io.c @@ -1328,7 +1328,4 @@ mrb_init_io(mrb_state *mrb) mrb_define_method(mrb, io, "closed?", mrb_io_closed, MRB_ARGS_NONE()); /* 15.2.20.5.2 */ mrb_define_method(mrb, io, "pid", mrb_io_pid, MRB_ARGS_NONE()); /* 15.2.20.5.2 */ mrb_define_method(mrb, io, "fileno", mrb_io_fileno, MRB_ARGS_NONE()); - - - mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$/"), mrb_str_new_cstr(mrb, "\n")); } diff --git a/mrblib/string.rb b/mrblib/string.rb index c26cdb1e2..0e7c8dc12 100644 --- a/mrblib/string.rb +++ b/mrblib/string.rb @@ -11,18 +11,43 @@ class String # and pass the respective line. # # 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.__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 + 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 - block.call(this[offset, this.size - offset]) if this.size > offset self end diff --git a/src/string.c b/src/string.c index c1c28ee8b..35c7f8e7c 100644 --- a/src/string.c +++ b/src/string.c @@ -1529,9 +1529,8 @@ mrb_str_chomp_bang(mrb_state *mrb, mrb_value str) * str.chomp(separator="\n") => new_str * * Returns a new <code>String</code> with the given record separator removed - * from the end of <i>str</i> (if present). If <code>$/</code> has not been - * changed from the default Ruby record separator, then <code>chomp</code> also - * removes carriage return characters (that is it will remove <code>\n</code>, + * from the end of <i>str</i> (if present). <code>chomp</code> also removes + * carriage return characters (that is it will remove <code>\n</code>, * <code>\r</code>, and <code>\r\n</code>). * * "hello".chomp #=> "hello" |
