diff options
author | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2006-10-02 16:39:21 +0000 |
---|---|---|
committer | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2006-10-02 16:39:21 +0000 |
commit | 4392e94d96e5278390c49f3fd1c1110dc33a3938 (patch) | |
tree | f8fdfbf1daf2fbf64d4abbbed9fb160df7740b2b | |
parent | 27841c008e0d22c4ebaed88af90f850c931da49d (diff) | |
download | ruby-4392e94d96e5278390c49f3fd1c1110dc33a3938.tar.gz |
* 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
-rw-r--r-- | ChangeLog | 7 | ||||
-rw-r--r-- | string.c | 43 |
2 files changed, 27 insertions, 23 deletions
@@ -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> * lib/test/unit/autorunner.rb (Test::Unit::AutoRunner::COLLECTORS): @@ -3550,37 +3550,33 @@ rb_str_split(VALUE str, const char *sep0) /* * 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\nb ar".lines #=> ["foo\n", "b ar"] + * "foo\nbar\n".lines.to_a #=> ["foo\n", "bar\n"] + * "foo\nb ar".lines.sort #=> ["b ar", "foo\n"] */ static VALUE rb_str_lines(VALUE str) { - VALUE ary = rb_ary_new(); - char *p = RSTRING_PTR(str); - char *pend = p + RSTRING_LEN(str); - char *s = p; - VALUE line; + return rb_enumeratorize(str, ID2SYM(rb_intern("each_line")), 0, 0); +} - while (p < pend) { - if (*p == '\n') { - p++; - line = rb_str_new(s, p-s); - rb_ary_push(ary, line); - s = p; - } - p++; - } - if (p == pend && s < p) { - line = rb_str_new(s, p-s); - rb_ary_push(ary, line); - } - return ary; +/* + * call-seq: + * str.bytes => anEnumerator + * + * Returns an enumerator that gives each byte in the string. + * + * "hello".bytes.to_a #=> [104, 101, 108, 108, 111] + */ + +static VALUE +rb_str_bytes(VALUE str) +{ + return rb_enumeratorize(str, ID2SYM(rb_intern("each_byte")), 0, 0); } /* @@ -4889,6 +4885,7 @@ Init_String(void) 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, "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_bang, 0); rb_define_method(rb_cString, "concat", rb_str_concat, 1); |