Skip to content

Commit 74f9a76

Browse files
author
rhettinger
committed
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
git-svn-id: http://svn.python.org/projects/python/trunk@34740 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 36f53f2 commit 74f9a76

7 files changed

Lines changed: 96 additions & 50 deletions

File tree

Doc/lib/libstdtypes.tex

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -546,9 +546,9 @@ \subsubsection{String Methods \label{string-methods}}
546546
Return a copy of the string with only its first character capitalized.
547547
\end{methoddesc}
548548

549-
\begin{methoddesc}[string]{center}{width}
549+
\begin{methoddesc}[string]{center}{width\optional{, fillchar}}
550550
Return centered in a string of length \var{width}. Padding is done
551-
using spaces.
551+
using the specified \var{fillchar} (default is a space).
552552
\end{methoddesc}
553553

554554
\begin{methoddesc}[string]{count}{sub\optional{, start\optional{, end}}}
@@ -645,9 +645,10 @@ \subsubsection{String Methods \label{string-methods}}
645645
providing this method.
646646
\end{methoddesc}
647647

648-
\begin{methoddesc}[string]{ljust}{width}
648+
\begin{methoddesc}[string]{ljust}{width\optional{, fillchar}}
649649
Return the string left justified in a string of length \var{width}.
650-
Padding is done using spaces. The original string is returned if
650+
Padding is done using the specified \var{fillchar} (default is a
651+
space). The original string is returned if
651652
\var{width} is less than \code{len(\var{s})}.
652653
\end{methoddesc}
653654

@@ -683,9 +684,10 @@ \subsubsection{String Methods \label{string-methods}}
683684
substring \var{sub} is not found.
684685
\end{methoddesc}
685686

686-
\begin{methoddesc}[string]{rjust}{width}
687+
\begin{methoddesc}[string]{rjust}{width\optional{, fillchar}}
687688
Return the string right justified in a string of length \var{width}.
688-
Padding is done using spaces. The original string is returned if
689+
Padding is done using the specified \var{fillchar} (default is a space).
690+
The original string is returned if
689691
\var{width} is less than \code{len(\var{s})}.
690692
\end{methoddesc}
691693

Lib/UserString.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ def __mod__(self, args):
6060

6161
# the following methods are defined in alphabetical order:
6262
def capitalize(self): return self.__class__(self.data.capitalize())
63-
def center(self, width): return self.__class__(self.data.center(width))
63+
def center(self, width, *args):
64+
return self.__class__(self.data.center(width, *args))
6465
def count(self, sub, start=0, end=sys.maxint):
6566
return self.data.count(sub, start, end)
6667
def decode(self, encoding=None, errors=None): # XXX improve this?
@@ -97,7 +98,8 @@ def isspace(self): return self.data.isspace()
9798
def istitle(self): return self.data.istitle()
9899
def isupper(self): return self.data.isupper()
99100
def join(self, seq): return self.data.join(seq)
100-
def ljust(self, width): return self.__class__(self.data.ljust(width))
101+
def ljust(self, width, *args):
102+
return self.__class__(self.data.ljust(width, *args))
101103
def lower(self): return self.__class__(self.data.lower())
102104
def lstrip(self, chars=None): return self.__class__(self.data.lstrip(chars))
103105
def replace(self, old, new, maxsplit=-1):
@@ -106,7 +108,8 @@ def rfind(self, sub, start=0, end=sys.maxint):
106108
return self.data.rfind(sub, start, end)
107109
def rindex(self, sub, start=0, end=sys.maxint):
108110
return self.data.rindex(sub, start, end)
109-
def rjust(self, width): return self.__class__(self.data.rjust(width))
111+
def rjust(self, width, *args):
112+
return self.__class__(self.data.rjust(width, *args))
110113
def rstrip(self, chars=None): return self.__class__(self.data.rstrip(chars))
111114
def split(self, sep=None, maxsplit=-1):
112115
return self.data.split(sep, maxsplit)

Lib/string.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -237,37 +237,37 @@ def atol(s, base=10):
237237

238238

239239
# Left-justify a string
240-
def ljust(s, width):
241-
"""ljust(s, width) -> string
240+
def ljust(s, width, *args):
241+
"""ljust(s, width[, fillchar]) -> string
242242
243243
Return a left-justified version of s, in a field of the
244244
specified width, padded with spaces as needed. The string is
245-
never truncated.
245+
never truncated. If specified the fillchar is used instead of spaces.
246246
247247
"""
248-
return s.ljust(width)
248+
return s.ljust(width, *args)
249249

250250
# Right-justify a string
251-
def rjust(s, width):
252-
"""rjust(s, width) -> string
251+
def rjust(s, width, *args):
252+
"""rjust(s, width[, fillchar]) -> string
253253
254254
Return a right-justified version of s, in a field of the
255255
specified width, padded with spaces as needed. The string is
256-
never truncated.
256+
never truncated. If specified the fillchar is used instead of spaces.
257257
258258
"""
259-
return s.rjust(width)
259+
return s.rjust(width, *args)
260260

261261
# Center a string
262-
def center(s, width):
263-
"""center(s, width) -> string
262+
def center(s, width, *args):
263+
"""center(s, width[, fillchar]) -> string
264264
265265
Return a center version of s, in a field of the specified
266266
width. padded with spaces as needed. The string is never
267-
truncated.
267+
truncated. If specified the fillchar is used instead of spaces.
268268
269269
"""
270-
return s.center(width)
270+
return s.center(width, *args)
271271

272272
# Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03'
273273
# Decadent feature: the argument may be a string or a number

Lib/test/string_tests.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,23 +227,23 @@ def test_ljust(self):
227227
self.checkequal('abc ', 'abc', 'ljust', 6)
228228
self.checkequal('abc', 'abc', 'ljust', 3)
229229
self.checkequal('abc', 'abc', 'ljust', 2)
230-
230+
self.checkequal('abc*******', 'abc', 'ljust', 10, '*')
231231
self.checkraises(TypeError, 'abc', 'ljust')
232232

233233
def test_rjust(self):
234234
self.checkequal(' abc', 'abc', 'rjust', 10)
235235
self.checkequal(' abc', 'abc', 'rjust', 6)
236236
self.checkequal('abc', 'abc', 'rjust', 3)
237237
self.checkequal('abc', 'abc', 'rjust', 2)
238-
238+
self.checkequal('*******abc', 'abc', 'rjust', 10, '*')
239239
self.checkraises(TypeError, 'abc', 'rjust')
240240

241241
def test_center(self):
242242
self.checkequal(' abc ', 'abc', 'center', 10)
243243
self.checkequal(' abc ', 'abc', 'center', 6)
244244
self.checkequal('abc', 'abc', 'center', 3)
245245
self.checkequal('abc', 'abc', 'center', 2)
246-
246+
self.checkequal('***abc****', 'abc', 'center', 10, '*')
247247
self.checkraises(TypeError, 'abc', 'center')
248248

249249
def test_swapcase(self):

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ What's New in Python 2.4 alpha 1?
1212
Core and builtins
1313
-----------------
1414

15+
- For str and unicode objects, the ljust(), center(), and rjust()
16+
methods now accept an optional argument specifying a fill
17+
character other than a space.
18+
1519
- When method objects have an attribute that can be satisfied either
1620
by the function object or by the method object, the function
1721
object's attribute usually wins. Christian Tismer pointed out that

Objects/stringobject.c

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2617,62 +2617,67 @@ pad(PyStringObject *self, int left, int right, char fill)
26172617
}
26182618

26192619
PyDoc_STRVAR(ljust__doc__,
2620-
"S.ljust(width) -> string\n"
2620+
"S.ljust(width[, fillchar]) -> string\n"
26212621
"\n"
26222622
"Return S left justified in a string of length width. Padding is\n"
2623-
"done using spaces.");
2623+
"done using the specified fill character (default is a space).");
26242624

26252625
static PyObject *
26262626
string_ljust(PyStringObject *self, PyObject *args)
26272627
{
26282628
int width;
2629-
if (!PyArg_ParseTuple(args, "i:ljust", &width))
2629+
char fillchar = ' ';
2630+
2631+
if (!PyArg_ParseTuple(args, "i|c:ljust", &width, &fillchar))
26302632
return NULL;
26312633

26322634
if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) {
26332635
Py_INCREF(self);
26342636
return (PyObject*) self;
26352637
}
26362638

2637-
return pad(self, 0, width - PyString_GET_SIZE(self), ' ');
2639+
return pad(self, 0, width - PyString_GET_SIZE(self), fillchar);
26382640
}
26392641

26402642

26412643
PyDoc_STRVAR(rjust__doc__,
2642-
"S.rjust(width) -> string\n"
2644+
"S.rjust(width[, fillchar]) -> string\n"
26432645
"\n"
26442646
"Return S right justified in a string of length width. Padding is\n"
2645-
"done using spaces.");
2647+
"done using the specified fill character (default is a space)");
26462648

26472649
static PyObject *
26482650
string_rjust(PyStringObject *self, PyObject *args)
26492651
{
26502652
int width;
2651-
if (!PyArg_ParseTuple(args, "i:rjust", &width))
2653+
char fillchar = ' ';
2654+
2655+
if (!PyArg_ParseTuple(args, "i|c:rjust", &width, &fillchar))
26522656
return NULL;
26532657

26542658
if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) {
26552659
Py_INCREF(self);
26562660
return (PyObject*) self;
26572661
}
26582662

2659-
return pad(self, width - PyString_GET_SIZE(self), 0, ' ');
2663+
return pad(self, width - PyString_GET_SIZE(self), 0, fillchar);
26602664
}
26612665

26622666

26632667
PyDoc_STRVAR(center__doc__,
2664-
"S.center(width) -> string\n"
2668+
"S.center(width[, fillchar]) -> string\n"
26652669
"\n"
2666-
"Return S centered in a string of length width. Padding is done\n"
2667-
"using spaces.");
2670+
"Return S centered in a string of length width. Padding is\n"
2671+
"done using the specified fill character (default is a space)");
26682672

26692673
static PyObject *
26702674
string_center(PyStringObject *self, PyObject *args)
26712675
{
26722676
int marg, left;
26732677
int width;
2678+
char fillchar = ' ';
26742679

2675-
if (!PyArg_ParseTuple(args, "i:center", &width))
2680+
if (!PyArg_ParseTuple(args, "i|c:center", &width, &fillchar))
26762681
return NULL;
26772682

26782683
if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) {
@@ -2683,7 +2688,7 @@ string_center(PyStringObject *self, PyObject *args)
26832688
marg = width - PyString_GET_SIZE(self);
26842689
left = marg / 2 + (marg & width & 1);
26852690

2686-
return pad(self, left, marg - left, ' ');
2691+
return pad(self, left, marg - left, fillchar);
26872692
}
26882693

26892694
PyDoc_STRVAR(zfill__doc__,

Objects/unicodeobject.c

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4404,19 +4404,47 @@ unicode_capwords(PyUnicodeObject *self)
44044404
}
44054405
#endif
44064406

4407+
/* Argument converter. Coerces to a single unicode character */
4408+
4409+
static int
4410+
convert_uc(PyObject *obj, void *addr)
4411+
{
4412+
Py_UNICODE *fillcharloc = (Py_UNICODE *)addr;
4413+
PyObject *uniobj;
4414+
Py_UNICODE *unistr;
4415+
4416+
uniobj = PyUnicode_FromObject(obj);
4417+
if (uniobj == NULL) {
4418+
PyErr_SetString(PyExc_TypeError,
4419+
"The fill character cannot be converted to Unicode");
4420+
return 0;
4421+
}
4422+
if (PyUnicode_GET_SIZE(uniobj) != 1) {
4423+
PyErr_SetString(PyExc_TypeError,
4424+
"The fill character must be exactly one character long");
4425+
Py_DECREF(uniobj);
4426+
return 0;
4427+
}
4428+
unistr = PyUnicode_AS_UNICODE(uniobj);
4429+
*fillcharloc = unistr[0];
4430+
Py_DECREF(uniobj);
4431+
return 1;
4432+
}
4433+
44074434
PyDoc_STRVAR(center__doc__,
4408-
"S.center(width) -> unicode\n\
4435+
"S.center(width[, fillchar]) -> unicode\n\
44094436
\n\
4410-
Return S centered in a Unicode string of length width. Padding is done\n\
4411-
using spaces.");
4437+
Return S centered in a Unicode string of length width. Padding is\n\
4438+
done using the specified fill character (default is a space)");
44124439

44134440
static PyObject *
44144441
unicode_center(PyUnicodeObject *self, PyObject *args)
44154442
{
44164443
int marg, left;
44174444
int width;
4445+
Py_UNICODE fillchar = ' ';
44184446

4419-
if (!PyArg_ParseTuple(args, "i:center", &width))
4447+
if (!PyArg_ParseTuple(args, "i|O&:center", &width, convert_uc, &fillchar))
44204448
return NULL;
44214449

44224450
if (self->length >= width && PyUnicode_CheckExact(self)) {
@@ -4427,7 +4455,7 @@ unicode_center(PyUnicodeObject *self, PyObject *args)
44274455
marg = width - self->length;
44284456
left = marg / 2 + (marg & width & 1);
44294457

4430-
return (PyObject*) pad(self, left, marg - left, ' ');
4458+
return (PyObject*) pad(self, left, marg - left, fillchar);
44314459
}
44324460

44334461
#if 0
@@ -5170,24 +5198,26 @@ unicode_length(PyUnicodeObject *self)
51705198
}
51715199

51725200
PyDoc_STRVAR(ljust__doc__,
5173-
"S.ljust(width) -> unicode\n\
5201+
"S.ljust(width[, fillchar]) -> unicode\n\
51745202
\n\
51755203
Return S left justified in a Unicode string of length width. Padding is\n\
5176-
done using spaces.");
5204+
done using the specified fill character (default is a space).");
51775205

51785206
static PyObject *
51795207
unicode_ljust(PyUnicodeObject *self, PyObject *args)
51805208
{
51815209
int width;
5182-
if (!PyArg_ParseTuple(args, "i:ljust", &width))
5210+
Py_UNICODE fillchar = ' ';
5211+
5212+
if (!PyArg_ParseTuple(args, "i|O&:ljust", &width, convert_uc, &fillchar))
51835213
return NULL;
51845214

51855215
if (self->length >= width && PyUnicode_CheckExact(self)) {
51865216
Py_INCREF(self);
51875217
return (PyObject*) self;
51885218
}
51895219

5190-
return (PyObject*) pad(self, 0, width - self->length, ' ');
5220+
return (PyObject*) pad(self, 0, width - self->length, fillchar);
51915221
}
51925222

51935223
PyDoc_STRVAR(lower__doc__,
@@ -5552,24 +5582,26 @@ unicode_rindex(PyUnicodeObject *self, PyObject *args)
55525582
}
55535583

55545584
PyDoc_STRVAR(rjust__doc__,
5555-
"S.rjust(width) -> unicode\n\
5585+
"S.rjust(width[, fillchar]) -> unicode\n\
55565586
\n\
55575587
Return S right justified in a Unicode string of length width. Padding is\n\
5558-
done using spaces.");
5588+
done using the specified fill character (default is a space).");
55595589

55605590
static PyObject *
55615591
unicode_rjust(PyUnicodeObject *self, PyObject *args)
55625592
{
55635593
int width;
5564-
if (!PyArg_ParseTuple(args, "i:rjust", &width))
5594+
Py_UNICODE fillchar = ' ';
5595+
5596+
if (!PyArg_ParseTuple(args, "i|O&:rjust", &width, convert_uc, &fillchar))
55655597
return NULL;
55665598

55675599
if (self->length >= width && PyUnicode_CheckExact(self)) {
55685600
Py_INCREF(self);
55695601
return (PyObject*) self;
55705602
}
55715603

5572-
return (PyObject*) pad(self, width - self->length, 0, ' ');
5604+
return (PyObject*) pad(self, width - self->length, 0, fillchar);
55735605
}
55745606

55755607
static PyObject*

0 commit comments

Comments
 (0)