[pypy-svn] r5432 - in pypy/trunk/src/pypy/objspace/std: . test

hpk at codespeak.net hpk at codespeak.net
Mon Jul 5 11:54:32 CEST 2004


Author: hpk
Date: Mon Jul  5 11:54:31 2004
New Revision: 5432

Modified:
   pypy/trunk/src/pypy/objspace/std/stringobject.py
   pypy/trunk/src/pypy/objspace/std/test/test_stringformat.py
Log:
Ben Young's test and implementation of zero-padding for string-formatting 



Modified: pypy/trunk/src/pypy/objspace/std/stringobject.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/stringobject.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/stringobject.py	Mon Jul  5 11:54:31 2004
@@ -991,9 +991,16 @@
             else:
                 width = None
                 prec = None
+                lpadchar = ' '
+                padzeros = False
+                seennum = False
                 if c in '-0123456789':
                     j = i
                     while format[j] in '-0123456789':
+                        if format[j] in '0123456789' and not seennum:
+                            seennum = True
+                            if format[j] == '0':
+                                padzeros = True
                         j += 1
                     if format[i:j] != '-':
                         width = int(format[i:j])
@@ -1050,6 +1057,10 @@
                 else:
                     raise ValueError, "unsupported format character '%s' (%x) at index %d" % (
                             c, ord(c), i)
+
+                if c in 'xdg' and padzeros:
+                    lpadchar = '0'
+
                 if prec is not None:
                     pieces[-1] = pieces[-1][:prec]
                 if width is not None:
@@ -1059,7 +1070,7 @@
                         p = p + ' '*d
                     else:
                         d = max(width - len(p), 0)
-                        p = ' '*d + p
+                        p = lpadchar*d + p
                     pieces[-1] = p
             state = 0
             start = i+1

Modified: pypy/trunk/src/pypy/objspace/std/test/test_stringformat.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/test/test_stringformat.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/test/test_stringformat.py	Mon Jul  5 11:54:31 2004
@@ -1,6 +1,14 @@
 import autopath
 from pypy.tool import testit
 
+    def test_zero_pad(self):
+        self.assertEquals("%02d"%1,   "01")
+        self.assertEquals("%05d"%1,   "00001")
+        self.assertEquals("%-05d"%1,  "1    ")
+        self.assertEquals("%04f"%2.3, "2.300000")
+        self.assertEquals("%04g"%2.3, "02.3")
+        self.assertEquals("%-04g"%2.3,"2.3 ")
+        self.assertEquals("%04s"%2.3, " 2.3")
 
 class TestStringObjectWithDict(testit.AppTestCase):
 



More information about the Pypy-commit mailing list