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

ruby-changes:38419

From: nobu <ko1@a...>
Date: Fri, 15 May 2015 18:06:39 +0900 (JST)
Subject: [ruby-changes:38419] nobu:r50500 (trunk): string.c: all_digits_p

nobu	2015-05-15 18:06:25 +0900 (Fri, 15 May 2015)

  New Revision: 50500

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

  Log:
    string.c: all_digits_p
    
    * string.c (all_digits_p): extract duplicate code from
      rb_str_upto.

  Modified files:
    trunk/string.c
Index: string.c
===================================================================
--- string.c	(revision 50499)
+++ string.c	(revision 50500)
@@ -3449,6 +3449,15 @@ rb_str_succ_bang(VALUE str) https://github.com/ruby/ruby/blob/trunk/string.c#L3449
     return str;
 }
 
+static int
+all_digits_p(const char *s, long len)
+{
+    while (len-- > 0) {
+	if (!ISDIGIT(*s)) return 0;
+	s++;
+    }
+    return 1;
+}
 
 /*
  *  call-seq:
@@ -3513,22 +3522,13 @@ rb_str_upto(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/string.c#L3522
 	return beg;
     }
     /* both edges are all digits */
-    if (ascii && ISDIGIT(RSTRING_PTR(beg)[0]) && ISDIGIT(RSTRING_PTR(end)[0])) {
-	char *s, *send;
+    if (ascii && ISDIGIT(RSTRING_PTR(beg)[0]) && ISDIGIT(RSTRING_PTR(end)[0]) &&
+	all_digits_p(RSTRING_PTR(beg), RSTRING_LEN(beg)) &&
+	all_digits_p(RSTRING_PTR(end), RSTRING_LEN(end))) {
 	VALUE b, e;
 	int width;
 
-	s = RSTRING_PTR(beg); send = RSTRING_END(beg);
-	width = rb_long2int(send - s);
-	while (s < send) {
-	    if (!ISDIGIT(*s)) goto no_digits;
-	    s++;
-	}
-	s = RSTRING_PTR(end); send = RSTRING_END(end);
-	while (s < send) {
-	    if (!ISDIGIT(*s)) goto no_digits;
-	    s++;
-	}
+	width = RSTRING_LENINT(beg);
 	b = rb_str_to_inum(beg, 10, FALSE);
 	e = rb_str_to_inum(end, 10, FALSE);
 	if (FIXNUM_P(b) && FIXNUM_P(e)) {
@@ -3556,7 +3556,6 @@ rb_str_upto(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/string.c#L3556
 	return beg;
     }
     /* normal case */
-  no_digits:
     n = rb_str_cmp(beg, end);
     if (n > 0 || (excl && n == 0)) return beg;
 

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

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