From ded7e1c2a10a044911efc02aef654bd647aec4c4 Mon Sep 17 00:00:00 2001 From: nobu Date: Sun, 3 Sep 2017 13:21:07 +0000 Subject: string.c: make array in WANTARRAY * string.c (WANTARRAY): make array for the result in method functions and pass it to enumerator functions. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59736 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- string.c | 80 ++++++++++++++++++++++++++-------------------------------------- 1 file changed, 33 insertions(+), 47 deletions(-) (limited to 'string.c') diff --git a/string.c b/string.c index b11ffe8244..d08db01160 100644 --- a/string.c +++ b/string.c @@ -7582,7 +7582,8 @@ enumerator_wantarray(const char *method) return 1; } -#define WANTARRAY(m) enumerator_wantarray(m) +#define WANTARRAY(m, size) \ + (enumerator_wantarray(m) ? rb_ary_new_capa(size) : 0) static const char * chomp_newline(const char *p, const char *e, rb_encoding *enc) @@ -7598,14 +7599,13 @@ chomp_newline(const char *p, const char *e, rb_encoding *enc) } static VALUE -rb_str_enumerate_lines(int argc, VALUE *argv, VALUE str, int wantarray) +rb_str_enumerate_lines(int argc, VALUE *argv, VALUE str, VALUE ary) { rb_encoding *enc; VALUE line, rs, orig = str, opts = Qnil, chomp = Qfalse; const char *ptr, *pend, *subptr, *subend, *rsptr, *hit, *adjusted; long pos, len, rslen; int rsnewline = 0; - VALUE ary = 0; if (rb_scan_args(argc, argv, "01:", &rs, &opts) == 0) rs = rb_rs; @@ -7618,11 +7618,8 @@ rb_str_enumerate_lines(int argc, VALUE *argv, VALUE str, int wantarray) chomp = (chomp != Qundef && RTEST(chomp)); } - if (wantarray) - ary = rb_ary_new(); - if (NIL_P(rs)) { - if (wantarray) { + if (ary) { rb_ary_push(ary, str); return ary; } @@ -7669,7 +7666,7 @@ rb_str_enumerate_lines(int argc, VALUE *argv, VALUE str, int wantarray) if (!subptr) break; line = rb_str_subseq(str, subptr - ptr, subend - subptr + (chomp ? 0 : rslen)); - if (wantarray) { + if (ary) { rb_ary_push(ary, line); } else { @@ -7714,7 +7711,7 @@ rb_str_enumerate_lines(int argc, VALUE *argv, VALUE str, int wantarray) } } line = rb_str_subseq(str, subptr - ptr, subend - subptr); - if (wantarray) { + if (ary) { rb_ary_push(ary, line); } else { @@ -7729,7 +7726,7 @@ rb_str_enumerate_lines(int argc, VALUE *argv, VALUE str, int wantarray) pend = chomp_newline(subptr, pend, enc); } line = rb_str_subseq(str, subptr - ptr, pend - subptr); - if (wantarray) + if (ary) rb_ary_push(ary, line); else rb_yield(line); @@ -7737,7 +7734,7 @@ rb_str_enumerate_lines(int argc, VALUE *argv, VALUE str, int wantarray) } end: - if (wantarray) + if (ary) return ary; else return orig; @@ -7800,7 +7797,8 @@ rb_str_each_line(int argc, VALUE *argv, VALUE str) static VALUE rb_str_lines(int argc, VALUE *argv, VALUE str) { - return rb_str_enumerate_lines(argc, argv, str, WANTARRAY("lines")); + VALUE ary = WANTARRAY("lines", 0); + return rb_str_enumerate_lines(argc, argv, str, ary); } static VALUE @@ -7810,21 +7808,17 @@ rb_str_each_byte_size(VALUE str, VALUE args, VALUE eobj) } static VALUE -rb_str_enumerate_bytes(VALUE str, int wantarray) +rb_str_enumerate_bytes(VALUE str, VALUE ary) { long i; - VALUE ary; - - if (wantarray) - ary = rb_ary_new2(RSTRING_LEN(str)); for (i=0; i