diff options
author | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-04-14 04:44:01 +0000 |
---|---|---|
committer | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-04-14 04:44:01 +0000 |
commit | 9fb81662b52391aa7e4b44f529b6b2c717a81d15 (patch) | |
tree | d54e10b3c73e3685694c6ff79bf34330df5e38c0 /string.c | |
parent | 80361e2c47fa79880e04df2f344b48fb607e144f (diff) |
* 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.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@16000 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'string.c')
-rw-r--r-- | string.c | 125 |
1 files changed, 30 insertions, 95 deletions
@@ -3671,6 +3671,19 @@ rb_f_split(argc, argv) } /* + * 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 @@ rb_str_each_line(argc, argv, str) 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,6 +3774,18 @@ rb_str_each_line(argc, argv, str) /* + * 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 * @@ -3779,6 +3804,7 @@ rb_str_each_byte(str) { long i; + RETURN_ENUMERATOR(str, 0, 0); for (i=0; i<RSTRING(str)->len; i++) { rb_yield(INT2FIX(RSTRING(str)->ptr[i] & 0xff)); } @@ -3786,97 +3812,6 @@ rb_str_each_byte(str) } -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 @@ -5013,11 +4948,11 @@ Init_String() 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); |