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

ruby-changes:34575

From: nobu <ko1@a...>
Date: Wed, 2 Jul 2014 17:03:54 +0900 (JST)
Subject: [ruby-changes:34575] nobu:r46656 (trunk): sprintf.c: get_num

nobu	2014-07-02 17:03:48 +0900 (Wed, 02 Jul 2014)

  New Revision: 46656

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

  Log:
    sprintf.c: get_num
    
    * sprintf.c (get_num): utility function for GETNUM().

  Modified files:
    trunk/sprintf.c
Index: sprintf.c
===================================================================
--- sprintf.c	(revision 46655)
+++ sprintf.c	(revision 46656)
@@ -106,19 +106,8 @@ sign_bits(int base, const char *p) https://github.com/ruby/ruby/blob/trunk/sprintf.c#L106
     (posarg = -2, rb_hash_lookup2(get_hash(&hash, argc, argv), (id), Qundef)))
 
 #define GETNUM(n, val) \
-    for (; p < end && rb_enc_isdigit(*p, enc); p++) {	\
-	int next_n = (n); \
-        if (MUL_OVERFLOW_INT_P(10, next_n)) \
-	    rb_raise(rb_eArgError, #val " too big"); \
-	next_n *= 10; \
-        if (INT_MAX - (*p - '0') < next_n) \
-	    rb_raise(rb_eArgError, #val " too big"); \
-	next_n += *p - '0'; \
-	(n) = next_n; \
-    } \
-    if (p >= end) { \
-	rb_raise(rb_eArgError, "malformed format string - %%*[0-9]"); \
-    }
+    (!(p = get_num(p, end, enc, &(n))) ? \
+     rb_raise(rb_eArgError, #val " too big") : (void)0)
 
 #define GETASTER(val) do { \
     t = p++; \
@@ -134,6 +123,25 @@ sign_bits(int base, const char *p) https://github.com/ruby/ruby/blob/trunk/sprintf.c#L123
     (val) = NUM2INT(tmp); \
 } while (0)
 
+static const char *
+get_num(const char *p, const char *end, rb_encoding *enc, int *valp)
+{
+    int next_n = *valp;
+    for (; p < end && rb_enc_isdigit(*p, enc); p++) {
+	if (MUL_OVERFLOW_INT_P(10, next_n))
+	    return NULL;
+	next_n *= 10;
+	if (INT_MAX - (*p - '0') < next_n)
+	    return NULL;
+	next_n += *p - '0';
+    }
+    if (p >= end) {
+	rb_raise(rb_eArgError, "malformed format string - %%*[0-9]");
+    }
+    *valp = next_n;
+    return p;
+}
+
 static VALUE
 get_hash(volatile VALUE *hash, int argc, const VALUE *argv)
 {

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

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