# New Ticket Created by  Jürgen Bömmels 
# Please include the string:  [perl #18098]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt2/Ticket/Display.html?id=18098 >


Some of the flags Parrot_sprintf functions don't work in the same way
as the corresponding C flags.

Namely hexadecimal values are unsigned, the width is off by one and
zeropadded signed numbers have the sign before the 0s.



-- attachment  1 ------------------------------------------------------
url: http://rt.perl.org/rt2/attach/40586/32722/bfc0c2/sprintf.diff

Index: spf_render.c
===================================================================
RCS file: /cvs/public/parrot/spf_render.c,v
retrieving revision 1.3
diff -u -r1.3 spf_render.c
--- spf_render.c	15 Oct 2002 18:39:19 -0000	1.3
+++ spf_render.c	26 Oct 2002 23:45:10 -0000
@@ -131,15 +131,26 @@
             fill = cstr2pstr(" ");
         }
 
-        if (info->width > len - 1)
-            string_repeat(interpreter, fill, info->width - (len - 1), &fill);
+        if (info->width > len)
+            string_repeat(interpreter, fill, info->width - len, &fill);
 
         if (info->flags & FLAG_MINUS) { /* left-align */
             string_append(interpreter, str, fill, 0);
         }
         else {                  /* right-align */
-            string_set(interpreter, str, string_concat(interpreter,
-                                                       fill, str, 0));
+            /* signed and zero padded */
+            if (info->flags & FLAG_ZERO 
+                && (string_ord(str,0) == '-' || string_ord(str,0) == '+')) {
+                STRING *temp;
+                string_substr(interpreter, str, 1, len-1, &temp);
+                string_chopn(str, -1);
+                string_append(interpreter, str, fill, 0);
+                string_append(interpreter, str, temp, 0);
+            }
+            else {
+                string_set(interpreter, str, 
+                           string_concat(interpreter, fill, str, 0));
+            }
         }
     }
 }
@@ -496,8 +507,8 @@
                             break;
 
                         case 'x':
-                            theint = obj->getint(interpreter, info.type, obj);
-                            int_to_str(interpreter, ts, tc, theint, 16);
+                            theuint = obj->getuint(interpreter, info.type, obj);
+                            uint_to_str(interpreter, ts, tc, theuint, 16);
 
                             handle_flags(interpreter, &info, ts, 1, "0x");
 
@@ -505,8 +516,9 @@
                             break;
 
                         case 'X':
-                            theint = obj->getint(interpreter, info.type, obj);
-                            int_to_str(interpreter, ts, tc, theint, 16);
+                            theuint = 
+                                obj->getuint(interpreter, info.type, obj);
+                            uint_to_str(interpreter, ts, tc, theuint, 16);
 
                             handle_flags(interpreter, &info, ts, 1, "0X");
 
@@ -514,8 +526,9 @@
                             break;
 
                         case 'b':
-                            theint = obj->getint(interpreter, info.type, obj);
-                            int_to_str(interpreter, ts, tc, theint, 2);
+                            theuint = 
+                                obj->getuint(interpreter, info.type, obj);
+                            uint_to_str(interpreter, ts, tc, theuint, 2);
 
                             handle_flags(interpreter, &info, ts, 1, "0b");
 
@@ -543,7 +556,7 @@
 
                         case 'p':
                             ptr = obj->getptr(interpreter, info.type, obj);
-                            int_to_str(interpreter, ts, tc,
+                            uint_to_str(interpreter, ts, tc,
                                        (HUGEINTVAL) (size_t) ptr, 16);
 
                             handle_flags(interpreter, &info, ts, 1, "0x");
Index: t/src/sprintf.t
===================================================================
RCS file: /cvs/public/parrot/t/src/sprintf.t,v
retrieving revision 1.6
diff -u -r1.6 sprintf.t
--- t/src/sprintf.t	15 Oct 2002 18:41:28 -0000	1.6
+++ t/src/sprintf.t	26 Oct 2002 23:45:25 -0000
@@ -84,9 +84,30 @@
 	        printf("0x%x %s", (int) ival,
             string_to_cstring(interpreter, S));
 
+                /* test several flags */
+                ival = 25;
+                S = Parrot_sprintf_c(interpreter, "== % 5d\n", ival);
+                printf("% 5d %s", (int) ival,
+                       string_to_cstring(interpreter, S));
+                S = Parrot_sprintf_c(interpreter, "== %-5d|\n", ival);
+                printf("%-5d %s", (int) ival,
+                       string_to_cstring(interpreter, S));
+                S = Parrot_sprintf_c(interpreter, "== %05d\n", ival);
+                printf("%05d %s", (int) ival,
+                       string_to_cstring(interpreter, S));
+
+                ival = -1;
+                S = Parrot_sprintf_c(interpreter, "== %#x\n", ival);
+                printf("0x%x %s", (int) ival, 
+                       string_to_cstring(interpreter, S));
+                S = Parrot_sprintf_c(interpreter, "== %08d\n", ival);
+                printf("%08d %s", (int) ival,
+                       string_to_cstring(interpreter, S));
+
 		    /* Test we've not left junk behind on the stack */
 	        S = Parrot_sprintf_c(interpreter, "That's all, %s\n", "folks!");
 	        printf(string_to_cstring(interpreter, S));
+
 	        return 0;
        }
 CODE
@@ -105,6 +126,11 @@
 1e+06 == 1e+06
 0.5 == 0.5
 0x20 == 0x20
+   25 ==    25
+25    == 25   |
+00025 == 00025
+0xffffffff == 0xffffffff
+-0000001 == -0000001
 That's all, folks!
 OUTPUT
 }

Reply via email to