ruby-changes:3235
From: ko1@a...
Date: 27 Dec 2007 11:03:07 +0900
Subject: [ruby-changes:3235] nobu - Ruby:r14728 (ruby_1_8): * string.c (rb_str_lines, rb_str_bytes): ditto.
nobu 2007-12-27 11:02:47 +0900 (Thu, 27 Dec 2007) New Revision: 14728 Modified files: branches/ruby_1_8/ChangeLog branches/ruby_1_8/string.c Log: * string.c (rb_str_lines, rb_str_bytes): ditto. http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/ChangeLog?r1=14728&r2=14727 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/string.c?r1=14728&r2=14727 Index: ruby_1_8/ChangeLog =================================================================== --- ruby_1_8/ChangeLog (revision 14727) +++ ruby_1_8/ChangeLog (revision 14728) @@ -1,8 +1,10 @@ -Thu Dec 27 11:01:17 2007 Nobuyoshi Nakada <nobu@r...> +Thu Dec 27 11:02:45 2007 Nobuyoshi Nakada <nobu@r...> * intern.h, string.c (rb_str_set_len): added for upgrading path from 1.8 to 1.9. [ruby-dev:32807] + * string.c (rb_str_lines, rb_str_bytes): ditto. + Thu Dec 27 10:47:32 2007 Technorama Ltd. <oss-ruby@t...> * ext/openssl/ossl_ssl.c: Only show a warning if the default Index: ruby_1_8/string.c =================================================================== --- ruby_1_8/string.c (revision 14727) +++ ruby_1_8/string.c (revision 14728) @@ -3762,8 +3762,99 @@ } +static VALUE str_enumerator _((VALUE, VALUE, int, VALUE *)); +static VALUE +str_enumerator(str, sym, argc, argv) + VALUE str, sym; + int argc; + VALUE *argv; +{ + static VALUE enumerator; + static ID new; + int nargc; + VALUE *nargv, result; + volatile VALUE args; + + if (!enumerator) { + rb_require("enumerator"); + enumerator = rb_path2class("Enumerable::Enumerator"); + new = rb_intern("new"); + } + args = rb_ary_new2(nargc = argc + 2); + RBASIC(args)->klass = 0; + nargv = RARRAY_PTR(args); + nargv[0] = str; + nargv[1] = sym; + MEMCPY(nargv + 2, argv, VALUE, argc); + result = rb_funcall2(enumerator, new, nargc, nargv); + rb_ary_clear(args); + return result; +} + + /* + * Document-method: lines * call-seq: + * str.lines(separator=$/) => anEnumerator + * str.lines(separator=$/) {|substr| block } => str + * + * Returns an enumerator that gives each line in the string. If a block is + * given, it iterates over each line in the string. + * + * "foo\nbar\n".lines.to_a #=> ["foo\n", "bar\n"] + * "foo\nb ar".lines.sort #=> ["b ar", "foo\n"] + */ + +static VALUE +rb_str_lines(argc, argv, str) + int argc; + VALUE *argv; + VALUE str; +{ + if (rb_block_given_p()) { + return rb_str_each_line(argc, argv, str); + } + else { + static VALUE each_line; + + if (!each_line) each_line = ID2SYM(rb_intern("each_line")); + return str_enumerator(str, each_line, argc, argv); + } +} + + +/* + * Document-method: bytes + * call-seq: + * str.bytes => anEnumerator + * str.bytes {|fixnum| block } => str + * + * Returns an enumerator that gives each byte in the string. If a block is + * given, it iterates over each byte in the string. + * + * "hello".bytes.to_a #=> [104, 101, 108, 108, 111] + */ + +static VALUE +rb_str_bytes(argc, argv, str) + int argc; + VALUE *argv; + VALUE str; +{ + if (rb_block_given_p()) { + return rb_str_each_byte(argc, argv, str); + } + else { + static VALUE each_byte; + + if (!each_byte) each_byte = ID2SYM(rb_intern("each_byte")); + return str_enumerator(str, each_byte, argc, argv); + } +} + + +/* + * call-seq: * str.chop! => str or nil * * Processes <i>str</i> as for <code>String#chop</code>, returning <i>str</i>, @@ -4754,6 +4845,9 @@ rb_define_method(rb_cString, "each", rb_str_each_line, -1); rb_define_method(rb_cString, "each_byte", rb_str_each_byte, 0); + rb_define_method(rb_cString, "lines", rb_str_lines, -1); + rb_define_method(rb_cString, "bytes", rb_str_bytes, -1); + rb_define_method(rb_cString, "sum", rb_str_sum, -1); rb_define_global_function("sub", rb_f_sub, -1); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml