* string.c (rb_str_lines): returns an Enumerator instead of an

array of lines.

* string.c (rb_str_bytes): a new method.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11067 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 2006-10-02 16:39:21 +00:00
parent 27841c008e
commit 4392e94d96
2 changed files with 27 additions and 23 deletions

View File

@ -1,3 +1,10 @@
Tue Oct 3 01:36:47 2006 Yukihiro Matsumoto <matz@ruby-lang.org>
* string.c (rb_str_lines): returns an Enumerator instead of an
array of lines.
* string.c (rb_str_bytes): a new method.
Mon Oct 2 23:47:55 2006 Nobuyoshi Nakada <nobu@ruby-lang.org> Mon Oct 2 23:47:55 2006 Nobuyoshi Nakada <nobu@ruby-lang.org>
* lib/test/unit/autorunner.rb (Test::Unit::AutoRunner::COLLECTORS): * lib/test/unit/autorunner.rb (Test::Unit::AutoRunner::COLLECTORS):

View File

@ -3550,37 +3550,33 @@ rb_str_split(VALUE str, const char *sep0)
/* /*
* call-seq: * call-seq:
* str.lines => anArray * str.lines => anEnumerator
* *
* Divides <i>str</i> into lines terminated by newlines. * Returns an enumerator that gives each line in the string.
* *
* "foo\nbar\n".lines #=> ["foo\n", "bar\n"] * "foo\nbar\n".lines.to_a #=> ["foo\n", "bar\n"]
* "foo\nb ar".lines #=> ["foo\n", "b ar"] * "foo\nb ar".lines.sort #=> ["b ar", "foo\n"]
*/ */
static VALUE static VALUE
rb_str_lines(VALUE str) rb_str_lines(VALUE str)
{ {
VALUE ary = rb_ary_new(); return rb_enumeratorize(str, ID2SYM(rb_intern("each_line")), 0, 0);
char *p = RSTRING_PTR(str); }
char *pend = p + RSTRING_LEN(str);
char *s = p;
VALUE line;
while (p < pend) { /*
if (*p == '\n') { * call-seq:
p++; * str.bytes => anEnumerator
line = rb_str_new(s, p-s); *
rb_ary_push(ary, line); * Returns an enumerator that gives each byte in the string.
s = p; *
} * "hello".bytes.to_a #=> [104, 101, 108, 108, 111]
p++; */
}
if (p == pend && s < p) { static VALUE
line = rb_str_new(s, p-s); rb_str_bytes(VALUE str)
rb_ary_push(ary, line); {
} return rb_enumeratorize(str, ID2SYM(rb_intern("each_byte")), 0, 0);
return ary;
} }
/* /*
@ -4889,6 +4885,7 @@ Init_String(void)
rb_define_method(rb_cString, "oct", rb_str_oct, 0); rb_define_method(rb_cString, "oct", rb_str_oct, 0);
rb_define_method(rb_cString, "split", rb_str_split_m, -1); rb_define_method(rb_cString, "split", rb_str_split_m, -1);
rb_define_method(rb_cString, "lines", rb_str_lines, 0); rb_define_method(rb_cString, "lines", rb_str_lines, 0);
rb_define_method(rb_cString, "bytes", rb_str_bytes, 0);
rb_define_method(rb_cString, "reverse", rb_str_reverse, 0); rb_define_method(rb_cString, "reverse", rb_str_reverse, 0);
rb_define_method(rb_cString, "reverse!", rb_str_reverse_bang, 0); rb_define_method(rb_cString, "reverse!", rb_str_reverse_bang, 0);
rb_define_method(rb_cString, "concat", rb_str_concat, 1); rb_define_method(rb_cString, "concat", rb_str_concat, 1);