[Patch] {l,r}just with optional pad character parameter

Beat Bolli beat.bolli at earthling.net
Sun Jul 16 10:38:05 EDT 2000


Hi all

this is my first crack at Python patching. I was simply appalled to find
'3'.zfill(5) not working anymore in 2.0b1, so I decided to do something
about it. Now since zfill can be expressed as rjust with an optional pad
characer argument, I implemented this.

This is my 12th wedding anniversary present to the Python community.

Live long and prosper!

[Can't post an attachment?!]


Beat Bolli
--
PGP: 0x506A903A; 49D5 794A EA77 F907 764F D89E 304B 93CF 506A 903A
ICBM: 47° 02' 43.0" N, 07° 16' 17.5" E (WGS84)


--- stringobject.c.orig Sun Jul 16 15:15:15 2000
+++ stringobject.c Sun Jul 16 16:05:55 2000
@@ -1767,16 +1767,17 @@
 }

 static char ljust__doc__[] =
-"S.ljust(width) -> string\n\
+"S.ljust(width [, padchar]) -> string\n\
 \n\
 Return S left justified in a string of length width. Padding is\n\
-done using spaces.";
+done using spaces unless padchar is given.";

 static PyObject *
 string_ljust(PyStringObject *self, PyObject *args)
 {
     int width;
-    if (!PyArg_ParseTuple(args, "i:ljust", &width))
+    char padchar = ' ';
+    if (!PyArg_ParseTuple(args, "i|c:ljust", &width, &padchar))
         return NULL;

     if (PyString_GET_SIZE(self) >= width) {
@@ -1784,21 +1785,22 @@
         return (PyObject*) self;
     }

-    return pad(self, 0, width - PyString_GET_SIZE(self), ' ');
+    return pad(self, 0, width - PyString_GET_SIZE(self), padchar);
 }


 static char rjust__doc__[] =
-"S.rjust(width) -> string\n\
+"S.rjust(width [, padchar]) -> string\n\
 \n\
 Return S right justified in a string of length width. Padding is\n\
-done using spaces.";
+done using spaces unless padchar is given.";

 static PyObject *
 string_rjust(PyStringObject *self, PyObject *args)
 {
     int width;
-    if (!PyArg_ParseTuple(args, "i:rjust", &width))
+    char padchar = ' ';
+    if (!PyArg_ParseTuple(args, "i|c:rjust", &width, &padchar))
         return NULL;

     if (PyString_GET_SIZE(self) >= width) {
@@ -1806,7 +1808,7 @@
         return (PyObject*) self;
     }

-    return pad(self, width - PyString_GET_SIZE(self), 0, ' ');
+    return pad(self, width - PyString_GET_SIZE(self), 0, padchar);
 }






More information about the Python-list mailing list