1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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
This commit is contained in:
nobu 2017-09-04 14:04:50 +00:00
parent 440dc6b7e0
commit 805d6f6f3c

View file

@ -7585,6 +7585,21 @@ enumerator_wantarray(const char *method)
#define WANTARRAY(m, size) \ #define WANTARRAY(m, size) \
(enumerator_wantarray(m) ? rb_ary_new_capa(size) : 0) (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 * static const char *
chomp_newline(const char *p, const char *e, rb_encoding *enc) 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 (NIL_P(rs)) {
if (ary) { if (!ENUM_ELEM(ary, str)) {
rb_ary_push(ary, str);
return ary; return ary;
} }
else { else {
rb_yield(str);
return orig; return orig;
} }
} }
@ -7666,11 +7679,7 @@ rb_str_enumerate_lines(int argc, VALUE *argv, VALUE str, VALUE ary)
if (!subptr) break; if (!subptr) break;
line = rb_str_subseq(str, subptr - ptr, line = rb_str_subseq(str, subptr - ptr,
subend - subptr + (chomp ? 0 : rslen)); subend - subptr + (chomp ? 0 : rslen));
if (ary) { if (ENUM_ELEM(ary, line)) {
rb_ary_push(ary, line);
}
else {
rb_yield(line);
str_mod_check(str, ptr, len); str_mod_check(str, ptr, len);
} }
subptr = eol = NULL; 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); line = rb_str_subseq(str, subptr - ptr, subend - subptr);
if (ary) { if (ENUM_ELEM(ary, line)) {
rb_ary_push(ary, line);
}
else {
rb_yield(line);
str_mod_check(str, ptr, len); str_mod_check(str, ptr, len);
} }
subptr = hit; subptr = hit;
@ -7726,10 +7731,7 @@ rb_str_enumerate_lines(int argc, VALUE *argv, VALUE str, VALUE ary)
pend = chomp_newline(subptr, pend, enc); pend = chomp_newline(subptr, pend, enc);
} }
line = rb_str_subseq(str, subptr - ptr, pend - subptr); line = rb_str_subseq(str, subptr - ptr, pend - subptr);
if (ary) ENUM_ELEM(ary, line);
rb_ary_push(ary, line);
else
rb_yield(line);
RB_GC_GUARD(str); RB_GC_GUARD(str);
} }
@ -7813,10 +7815,7 @@ rb_str_enumerate_bytes(VALUE str, VALUE ary)
long i; long i;
for (i=0; i<RSTRING_LEN(str); i++) { for (i=0; i<RSTRING_LEN(str); i++) {
if (ary) ENUM_ELEM(ary, INT2FIX(RSTRING_PTR(str)[i] & 0xff));
rb_ary_push(ary, INT2FIX(RSTRING_PTR(str)[i] & 0xff));
else
rb_yield(INT2FIX(RSTRING_PTR(str)[i] & 0xff));
} }
if (ary) if (ary)
return ary; return ary;
@ -7874,7 +7873,6 @@ static VALUE
rb_str_enumerate_chars(VALUE str, VALUE ary) rb_str_enumerate_chars(VALUE str, VALUE ary)
{ {
VALUE orig = str; VALUE orig = str;
VALUE substr;
long i, len, n; long i, len, n;
const char *ptr; const char *ptr;
rb_encoding *enc; rb_encoding *enc;
@ -7887,21 +7885,13 @@ rb_str_enumerate_chars(VALUE str, VALUE ary)
if (ENC_CODERANGE_CLEAN_P(ENC_CODERANGE(str))) { if (ENC_CODERANGE_CLEAN_P(ENC_CODERANGE(str))) {
for (i = 0; i < len; i += n) { for (i = 0; i < len; i += n) {
n = rb_enc_fast_mbclen(ptr + i, ptr + len, enc); n = rb_enc_fast_mbclen(ptr + i, ptr + len, enc);
substr = rb_str_subseq(str, i, n); ENUM_ELEM(ary, rb_str_subseq(str, i, n));
if (ary)
rb_ary_push(ary, substr);
else
rb_yield(substr);
} }
} }
else { else {
for (i = 0; i < len; i += n) { for (i = 0; i < len; i += n) {
n = rb_enc_mbclen(ptr + i, ptr + len, enc); n = rb_enc_mbclen(ptr + i, ptr + len, enc);
substr = rb_str_subseq(str, i, n); ENUM_ELEM(ary, rb_str_subseq(str, i, n));
if (ary)
rb_ary_push(ary, substr);
else
rb_yield(substr);
} }
} }
RB_GC_GUARD(str); RB_GC_GUARD(str);
@ -7970,10 +7960,7 @@ rb_str_enumerate_codepoints(VALUE str, VALUE ary)
while (ptr < end) { while (ptr < end) {
c = rb_enc_codepoint_len(ptr, end, &n, enc); c = rb_enc_codepoint_len(ptr, end, &n, enc);
if (ary) ENUM_ELEM(ary, UINT2NUM(c));
rb_ary_push(ary, UINT2NUM(c));
else
rb_yield(UINT2NUM(c));
ptr += n; ptr += n;
} }
RB_GC_GUARD(str); RB_GC_GUARD(str);
@ -8063,7 +8050,6 @@ rb_str_enumerate_grapheme_clusters(VALUE str, VALUE ary)
end = RSTRING_END(str); end = RSTRING_END(str);
while (ptr < end) { while (ptr < end) {
VALUE grapheme_cluster;
OnigPosition len = onig_match(reg_grapheme_cluster, OnigPosition len = onig_match(reg_grapheme_cluster,
(const OnigUChar *)ptr, (const OnigUChar *)end, (const OnigUChar *)ptr, (const OnigUChar *)end,
(const OnigUChar *)ptr, NULL, 0); (const OnigUChar *)ptr, NULL, 0);
@ -8071,11 +8057,7 @@ rb_str_enumerate_grapheme_clusters(VALUE str, VALUE ary)
if (len < 0) { if (len < 0) {
break; break;
} }
grapheme_cluster = rb_enc_str_new(ptr, len, enc); ENUM_ELEM(ary, rb_enc_str_new(ptr, len, enc));
if (ary)
rb_ary_push(ary, grapheme_cluster);
else
rb_yield(grapheme_cluster);
ptr += len; ptr += len;
} }
if (ary) if (ary)