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

ruby-changes:45950

From: nobu <ko1@a...>
Date: Sun, 19 Mar 2017 21:46:36 +0900 (JST)
Subject: [ruby-changes:45950] nobu:r58021 (trunk): sprintf.c: use FILL

nobu	2017-03-19 21:46:31 +0900 (Sun, 19 Mar 2017)

  New Revision: 58021

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

  Log:
    sprintf.c: use FILL
    
    * sprintf.c (rb_str_format): use FILL and FILL_ macros instead of
      while loop.

  Modified files:
    trunk/sprintf.c
Index: sprintf.c
===================================================================
--- sprintf.c	(revision 58020)
+++ sprintf.c	(revision 58021)
@@ -74,6 +74,7 @@ sign_bits(int base, const char *p) https://github.com/ruby/ruby/blob/trunk/sprintf.c#L74
 } while (0)
 
 #define FILL(c, l) do { \
+    if ((l) <= 0) break;\
     CHECK(l);\
     FILL_(c, l);\
 } while (0)
@@ -758,20 +759,15 @@ rb_str_format(int argc, const VALUE *arg https://github.com/ruby/ruby/blob/trunk/sprintf.c#L759
 		    if ((flags&FWIDTH) && (width > slen)) {
 			width -= (int)slen;
 			if (!(flags&FMINUS)) {
-			    CHECK(width);
-			    while (width--) {
-				buf[blen++] = ' ';
-			    }
+			    FILL(' ', width);
+			    width = 0;
 			}
 			CHECK(len);
 			memcpy(&buf[blen], RSTRING_PTR(str), len);
 			RB_GC_GUARD(str);
 			blen += len;
 			if (flags&FMINUS) {
-			    CHECK(width);
-			    while (width--) {
-				buf[blen++] = ' ';
-			    }
+			    FILL(' ', width);
 			}
 			rb_enc_associate(result, enc);
 			break;
@@ -1009,10 +1005,8 @@ rb_str_format(int argc, const VALUE *arg https://github.com/ruby/ruby/blob/trunk/sprintf.c#L1005
 		    width -= prec;
 		}
 		if (!(flags&FMINUS)) {
-		    CHECK(width);
-		    while (width-- > 0) {
-			buf[blen++] = ' ';
-		    }
+		    FILL(' ', width);
+		    width = 0;
 		}
 		if (sc) PUSH(&sc, 1);
 		if (prefix) {
@@ -1021,23 +1015,18 @@ rb_str_format(int argc, const VALUE *arg https://github.com/ruby/ruby/blob/trunk/sprintf.c#L1015
 		}
 		CHECK(prec - len);
 		if (dots) PUSH("..", 2);
-		if (!sign && valsign < 0) {
-		    char c = sign_bits(base, p);
-		    while (len < prec--) {
-			buf[blen++] = c;
+		if (prec > len) {
+		    if (!sign && valsign < 0) {
+			char c = sign_bits(base, p);
+			FILL_(c, prec - len);
 		    }
-		}
-		else if ((flags & (FMINUS|FPREC)) != FMINUS) {
-		    while (len < prec--) {
-			buf[blen++] = '0';
+		    else if ((flags & (FMINUS|FPREC)) != FMINUS) {
+			FILL_('0', prec - len);
 		    }
 		}
 		PUSH(s, len);
 		RB_GC_GUARD(tmp);
-		CHECK(width);
-		while (width-- > 0) {
-		    buf[blen++] = ' ';
-		}
+		FILL(' ', width);
 	    }
 	    break;
 

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

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