[前][次][番号順一覧][スレッド一覧]

ruby-changes:47626

From: nobu <ko1@a...>
Date: Mon, 4 Sep 2017 23:04:54 +0900 (JST)
Subject: [ruby-changes:47626] nobu:r59742 (trunk): string.c: enumerator_element

nobu	2017-09-04 23:04:50 +0900 (Mon, 04 Sep 2017)

  New Revision: 59742

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=59742

  Log:
    string.c: enumerator_element
    
    * string.c (enumerator_element): push or yield elements, and
      return 1 if needs checks.

  Modified files:
    trunk/string.c
Index: string.c
===================================================================
--- string.c	(revision 59741)
+++ string.c	(revision 59742)
@@ -7585,6 +7585,21 @@ enumerator_wantarray(const char *method) https://github.com/ruby/ruby/blob/trunk/string.c#L7585
 #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 * https://github.com/ruby/ruby/blob/trunk/string.c#L7634
     }
 
     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 * https://github.com/ruby/ruby/blob/trunk/string.c#L7679
 	    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 * https://github.com/ruby/ruby/blob/trunk/string.c#L7720
 	    }
 	}
 	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 * https://github.com/ruby/ruby/blob/trunk/string.c#L7731
 	    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 https://github.com/ruby/ruby/blob/trunk/string.c#L7815
     long i;
 
     for (i=0; i<RSTRING_LEN(str); i++) {
-	if (ary)
-	    rb_ary_push(ary, INT2FIX(RSTRING_PTR(str)[i] & 0xff));
-	else
-	    rb_yield(INT2FIX(RSTRING_PTR(str)[i] & 0xff));
+	ENUM_ELEM(ary, INT2FIX(RSTRING_PTR(str)[i] & 0xff));
     }
     if (ary)
 	return ary;
@@ -7874,7 +7873,6 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/string.c#L7873
 rb_str_enumerate_chars(VALUE str, VALUE ary)
 {
     VALUE orig = str;
-    VALUE substr;
     long i, len, n;
     const char *ptr;
     rb_encoding *enc;
@@ -7887,21 +7885,13 @@ rb_str_enumerate_chars(VALUE str, VALUE https://github.com/ruby/ruby/blob/trunk/string.c#L7885
     if (ENC_CODERANGE_CLEAN_P(ENC_CODERANGE(str))) {
 	for (i = 0; i < len; i += n) {
 	    n = rb_enc_fast_mbclen(ptr + i, ptr + len, enc);
-	    substr = rb_str_subseq(str, i, n);
-	    if (ary)
-		rb_ary_push(ary, substr);
-	    else
-		rb_yield(substr);
+	    ENUM_ELEM(ary, rb_str_subseq(str, i, n));
 	}
     }
     else {
 	for (i = 0; i < len; i += n) {
 	    n = rb_enc_mbclen(ptr + i, ptr + len, enc);
-	    substr = rb_str_subseq(str, i, n);
-	    if (ary)
-		rb_ary_push(ary, substr);
-	    else
-		rb_yield(substr);
+	    ENUM_ELEM(ary, rb_str_subseq(str, i, n));
 	}
     }
     RB_GC_GUARD(str);
@@ -7970,10 +7960,7 @@ rb_str_enumerate_codepoints(VALUE str, V https://github.com/ruby/ruby/blob/trunk/string.c#L7960
 
     while (ptr < end) {
 	c = rb_enc_codepoint_len(ptr, end, &n, enc);
-	if (ary)
-	    rb_ary_push(ary, UINT2NUM(c));
-	else
-	    rb_yield(UINT2NUM(c));
+	ENUM_ELEM(ary, UINT2NUM(c));
 	ptr += n;
     }
     RB_GC_GUARD(str);
@@ -8063,7 +8050,6 @@ rb_str_enumerate_grapheme_clusters(VALUE https://github.com/ruby/ruby/blob/trunk/string.c#L8050
     end = RSTRING_END(str);
 
     while (ptr < end) {
-	VALUE grapheme_cluster;
 	OnigPosition len = onig_match(reg_grapheme_cluster,
 				      (const OnigUChar *)ptr, (const OnigUChar *)end,
 				      (const OnigUChar *)ptr, NULL, 0);
@@ -8071,11 +8057,7 @@ rb_str_enumerate_grapheme_clusters(VALUE https://github.com/ruby/ruby/blob/trunk/string.c#L8057
 	if (len < 0) {
 	    break;
 	}
-	grapheme_cluster = rb_enc_str_new(ptr, len, enc);
-	if (ary)
-	    rb_ary_push(ary, grapheme_cluster);
-	else
-	    rb_yield(grapheme_cluster);
+	ENUM_ELEM(ary, rb_enc_str_new(ptr, len, enc));
 	ptr += len;
     }
     if (ary)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]