From ae5e291d93c2dc36da0b65017590be888311d27b Mon Sep 17 00:00:00 2001 From: nobu Date: Mon, 4 Sep 2017 14:04:50 +0000 Subject: string.c: enumerator_element * string.c (enumerator_element): push or yield elements, and return 1 if needs checks. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59742 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- string.c | 66 +++++++++++++++++++++++----------------------------------------- 1 file changed, 24 insertions(+), 42 deletions(-) diff --git a/string.c b/string.c index d08db01160..e71263efef 100644 --- a/string.c +++ b/string.c @@ -7585,6 +7585,21 @@ enumerator_wantarray(const char *method) #define WANTARRAY(m, size) \ (enumerator_wantarray(m) ? rb_ary_new_capa(size) : 0) +static inline int +enumerator_element(VALUE ary, VALUE e) +{ + if (ary) { + rb_ary_push(ary, e); + return 0; + } + else { + rb_yield(e); + return 1; + } +} + +#define ENUM_ELEM(ary, e) enumerator_element(ary, e) + static const char * chomp_newline(const char *p, const char *e, rb_encoding *enc) { @@ -7619,12 +7634,10 @@ rb_str_enumerate_lines(int argc, VALUE *argv, VALUE str, VALUE ary) } if (NIL_P(rs)) { - if (ary) { - rb_ary_push(ary, str); + if (!ENUM_ELEM(ary, str)) { return ary; } else { - rb_yield(str); return orig; } } @@ -7666,11 +7679,7 @@ rb_str_enumerate_lines(int argc, VALUE *argv, VALUE str, VALUE ary) if (!subptr) break; line = rb_str_subseq(str, subptr - ptr, subend - subptr + (chomp ? 0 : rslen)); - if (ary) { - rb_ary_push(ary, line); - } - else { - rb_yield(line); + if (ENUM_ELEM(ary, line)) { str_mod_check(str, ptr, len); } subptr = eol = NULL; @@ -7711,11 +7720,7 @@ rb_str_enumerate_lines(int argc, VALUE *argv, VALUE str, VALUE ary) } } line = rb_str_subseq(str, subptr - ptr, subend - subptr); - if (ary) { - rb_ary_push(ary, line); - } - else { - rb_yield(line); + if (ENUM_ELEM(ary, line)) { str_mod_check(str, ptr, len); } subptr = hit; @@ -7726,10 +7731,7 @@ rb_str_enumerate_lines(int argc, VALUE *argv, VALUE str, VALUE ary) pend = chomp_newline(subptr, pend, enc); } line = rb_str_subseq(str, subptr - ptr, pend - subptr); - if (ary) - rb_ary_push(ary, line); - else - rb_yield(line); + ENUM_ELEM(ary, line); RB_GC_GUARD(str); } @@ -7813,10 +7815,7 @@ rb_str_enumerate_bytes(VALUE str, VALUE ary) long i; for (i=0; i