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

ruby-changes:3726

From: ko1@a...
Date: Thu, 24 Jan 2008 19:29:42 +0900 (JST)
Subject: [ruby-changes:3726] nobu - Ruby:r15215 (trunk): * sprintf.c (remove_sign_bits): returns pointer to the first char to

nobu	2008-01-24 19:29:22 +0900 (Thu, 24 Jan 2008)

  New Revision: 15215

  Modified files:
    trunk/ChangeLog
    trunk/sprintf.c
    trunk/test/ruby/test_sprintf.rb

  Log:
    * sprintf.c (remove_sign_bits): returns pointer to the first char to
      be used, instead of copying.
    
    * sprintf.c (rb_str_format): negative indicator dots should come
      before sign digits always.  [ruby-dev:33224]


  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=15215&r2=15214&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/test/ruby/test_sprintf.rb?r1=15215&r2=15214&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/sprintf.c?r1=15215&r2=15214&diff_format=u

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 15214)
+++ ChangeLog	(revision 15215)
@@ -1,3 +1,11 @@
+Thu Jan 24 19:29:20 2008  Nobuyoshi Nakada  <nobu@r...>
+
+	* sprintf.c (remove_sign_bits): returns pointer to the first char to
+	  be used, instead of copying.
+
+	* sprintf.c (rb_str_format): negative indicator dots should come
+	  before sign digits always.  [ruby-dev:33224]
+
 Thu Jan 24 18:19:42 2008  Nobuyoshi Nakada  <nobu@r...>
 
 	* include/ruby/encoding.h (rb_enc_is_newline): parenthesized arguments.
Index: sprintf.c
===================================================================
--- sprintf.c	(revision 15214)
+++ sprintf.c	(revision 15215)
@@ -46,12 +46,8 @@
 	    t++;
 	}
     }
-    if (t > s) {
-	while (*t) *s++ = *t++;
-	*s = '\0';
-    }
 
-    return str;
+    return t;
 }
 
 static char
@@ -503,10 +499,11 @@
 	  case 'B':
 	  case 'u':
 	    {
+		volatile VALUE tmp1;
 		volatile VALUE val = GETARG();
-		char fbuf[32], nbuf[64], *s, *t;
+		char fbuf[32], nbuf[64], *s;
 		const char *prefix = 0;
-		int sign = 0;
+		int sign = 0, dots = 0;
 		char sc = 0;
 		long v = 0;
 		int base, bignum = 0;
@@ -607,19 +604,19 @@
 			}
 			sprintf(fbuf, "%%l%c", c);
 			sprintf(nbuf, fbuf, v);
+			s = nbuf;
 		    }
 		    else {
 			s = nbuf;
 			if (v < 0) {
-			    strcpy(s, "..");
-			    s += 2;
+			    dots = 1;
 			}
 			sprintf(fbuf, "%%l%c", *p == 'X' ? 'x' : *p);
-			sprintf(s, fbuf, v);
+			sprintf(++s, fbuf, v);
 			if (v < 0) {
 			    char d = 0;
 
-			    remove_sign_bits(s, base);
+			    s = remove_sign_bits(s, base);
 			    switch (base) {
 			      case 16:
 				d = 'f'; break;
@@ -627,12 +624,10 @@
 				d = '7'; break;
 			    }
 			    if (d && *s != d) {
-				memmove(s+1, s, strlen(s)+1);
-				*s = d;
+				*--s = d;
 			    }
 			}
 		    }
-		    s = nbuf;
 		}
 		else {
 		    if (sign) {
@@ -653,7 +648,6 @@
 			}
 		    }
 		    else {
-                        volatile VALUE tmp1;
 			if (!RBIGNUM_SIGN(val)) {
 			    val = rb_big_clone(val);
 			    rb_big_2comp(val);
@@ -661,32 +655,29 @@
 			tmp1 = tmp = rb_big2str0(val, base, RBIGNUM_SIGN(val));
 			s = RSTRING_PTR(tmp);
 			if (*s == '-') {
+			    dots = 1;
 			    if (base == 10) {
 				rb_warning("negative number for %%u specifier");
 			    }
-			    remove_sign_bits(++s, base);
-			    tmp = rb_str_new(0, 3+strlen(s));
-			    t = RSTRING_PTR(tmp);
-			    if (!(flags&(FPREC|FZERO))) {
-				strcpy(t, "..");
-				t += 2;
-			    }
+			    s = remove_sign_bits(++s, base);
 			    switch (base) {
 			      case 16:
-				if (s[0] != 'f') strcpy(t++, "f"); break;
+				if (s[0] != 'f') *--s = 'f'; break;
 			      case 8:
-				if (s[0] != '7') strcpy(t++, "7"); break;
+				if (s[0] != '7') *--s = '7'; break;
 			      case 2:
-				if (s[0] != '1') strcpy(t++, "1"); break;
+				if (s[0] != '1') *--s = '1'; break;
 			    }
-			    strcpy(t, s);
-			    s  = RSTRING_PTR(tmp);
 			}
 		    }
 		}
 
 		pos = -1;
 		len = strlen(s);
+		if (dots) {
+		    prec -= 2;
+		    width -= 2;
+		}
 
 		if (*p == 'X') {
 		    char *pp = s;
@@ -716,6 +707,10 @@
 		    PUSH(prefix, plen);
 		}
 		CHECK(prec - len);
+		if (dots) {
+		    memcpy(&buf[blen], "..", 2);
+		    blen += 2;
+		}
 		if (!bignum && v < 0) {
 		    char c = sign_bits(base, p);
 		    while (len < prec--) {
Index: test/ruby/test_sprintf.rb
===================================================================
--- test/ruby/test_sprintf.rb	(revision 15214)
+++ test/ruby/test_sprintf.rb	(revision 15215)
@@ -19,17 +19,17 @@
     assert_equal("0000", sprintf("%04b", 0))
     assert_equal("0001", sprintf("%04b", 1))
     assert_equal("0010", sprintf("%04b", 2))
-    assert_equal("1111", sprintf("%04b", -1))
+    assert_equal("..11", sprintf("%04b", -1))
 
     assert_equal("0000", sprintf("%.4b", 0))
     assert_equal("0001", sprintf("%.4b", 1))
     assert_equal("0010", sprintf("%.4b", 2))
-    assert_equal("1111", sprintf("%.4b", -1))
+    assert_equal("..11", sprintf("%.4b", -1))
 
     assert_equal("  0000", sprintf("%6.4b", 0))
     assert_equal("  0001", sprintf("%6.4b", 1))
     assert_equal("  0010", sprintf("%6.4b", 2))
-    assert_equal("  1111", sprintf("%6.4b", -1))
+    assert_equal("  ..11", sprintf("%6.4b", -1))
 
     assert_equal(" 0b0", sprintf("%#4b", 0))
     assert_equal(" 0b1", sprintf("%#4b", 1))
@@ -39,17 +39,17 @@
     assert_equal("0b00", sprintf("%#04b", 0))
     assert_equal("0b01", sprintf("%#04b", 1))
     assert_equal("0b10", sprintf("%#04b", 2))
-    assert_equal("0b11", sprintf("%#04b", -1))
+    assert_equal("0b..1", sprintf("%#04b", -1))
 
     assert_equal("0b0000", sprintf("%#.4b", 0))
     assert_equal("0b0001", sprintf("%#.4b", 1))
     assert_equal("0b0010", sprintf("%#.4b", 2))
-    assert_equal("0b1111", sprintf("%#.4b", -1))
+    assert_equal("0b..11", sprintf("%#.4b", -1))
 
     assert_equal("0b0000", sprintf("%#6.4b", 0))
     assert_equal("0b0001", sprintf("%#6.4b", 1))
     assert_equal("0b0010", sprintf("%#6.4b", 2))
-    assert_equal("0b1111", sprintf("%#6.4b", -1))
+    assert_equal("0b..11", sprintf("%#6.4b", -1))
 
     assert_equal("+0", sprintf("%+b", 0))
     assert_equal("+1", sprintf("%+b", 1))

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

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