ruby-changes:4507
From: ko1@a...
Date: Mon, 14 Apr 2008 13:44:12 +0900 (JST)
Subject: [ruby-changes:4507] knu - Ruby:r16000 (ruby_1_8): * string.c (rb_str_each_line, rb_str_each_byte): Reflect
knu 2008-04-14 13:44:01 +0900 (Mon, 14 Apr 2008) New Revision: 16000 Modified files: branches/ruby_1_8/ChangeLog branches/ruby_1_8/string.c Log: * string.c (rb_str_each_line, rb_str_each_byte): Reflect enumerator integration. #lines and #bytes are now aliases to #each_line and #each_byte, respectively. http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/ChangeLog?r1=16000&r2=15999&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/string.c?r1=16000&r2=15999&diff_format=u Index: ruby_1_8/ChangeLog =================================================================== --- ruby_1_8/ChangeLog (revision 15999) +++ ruby_1_8/ChangeLog (revision 16000) @@ -1,3 +1,9 @@ +Mon Apr 14 13:42:20 2008 Akinori MUSHA <knu@i...> + + * string.c (rb_str_each_line, rb_str_each_byte): Reflect + enumerator integration. #lines and #bytes are now aliases to + #each_line and #each_byte, respectively. + Mon Apr 14 13:19:36 2008 Akinori MUSHA <knu@i...> * range.c (range_each, range_step): Return an enumerator if no Index: ruby_1_8/string.c =================================================================== --- ruby_1_8/string.c (revision 15999) +++ ruby_1_8/string.c (revision 16000) @@ -3671,7 +3671,20 @@ } /* + * 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"] + */ + +/* + * call-seq: * str.each(separator=$/) {|substr| block } => str * str.each_line(separator=$/) {|substr| block } => str * @@ -3719,7 +3732,7 @@ if (rb_scan_args(argc, argv, "01", &rs) == 0) { rs = rb_rs; } - + RETURN_ENUMERATOR(str, argc, argv); if (NIL_P(rs)) { rb_yield(str); return str; @@ -3761,7 +3774,19 @@ /* + * 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] + */ + +/* + * call-seq: * str.each_byte {|fixnum| block } => str * * Passes each byte in <i>str</i> to the given block. @@ -3779,6 +3804,7 @@ { long i; + RETURN_ENUMERATOR(str, 0, 0); for (i=0; i<RSTRING(str)->len; i++) { rb_yield(INT2FIX(RSTRING(str)->ptr[i] & 0xff)); } @@ -3786,99 +3812,8 @@ } -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>, @@ -5013,11 +4948,11 @@ rb_define_method(rb_cString, "squeeze!", rb_str_squeeze_bang, -1); rb_define_method(rb_cString, "each_line", rb_str_each_line, -1); - rb_define_method(rb_cString, "each", rb_str_each_line, -1); + 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, "lines", rb_str_each_line, -1); + rb_define_method(rb_cString, "bytes", rb_str_each_byte, 0); rb_define_method(rb_cString, "sum", rb_str_sum, -1); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/