Skip to content

Commit 03ad72f

Browse files
author
guido
committed
Marc-Andre Lemburg:
The maxsplit functionality in .splitlines() was replaced by the keepends functionality which allows keeping the line end markers together with the string. git-svn-id: http://svn.python.org/projects/python/trunk@15125 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 4301217 commit 03ad72f

2 files changed

Lines changed: 14 additions & 14 deletions

File tree

Lib/UserString.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def rjust(self, width): return self.__class__(self.data.rjust(width))
9696
def rstrip(self): return self.__class__(self.data.rstrip())
9797
def split(self, sep=None, maxsplit=-1):
9898
return self.data.split(sep, maxsplit)
99-
def splitlines(self, maxsplit=-1): return self.data.splitlines(maxsplit)
99+
def splitlines(self, keepends=0): return self.data.splitlines(keepends)
100100
def startswith(self, prefix, start=0, end=sys.maxint):
101101
return self.data.startswith(prefix, start, end)
102102
def strip(self): return self.__class__(self.data.strip())

Objects/unicodeobject.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2516,7 +2516,7 @@ PyObject *split_whitespace(PyUnicodeObject *self,
25162516
}
25172517

25182518
PyObject *PyUnicode_Splitlines(PyObject *string,
2519-
int maxcount)
2519+
int keepends)
25202520
{
25212521
register int i;
25222522
register int j;
@@ -2531,29 +2531,29 @@ PyObject *PyUnicode_Splitlines(PyObject *string,
25312531
data = PyUnicode_AS_UNICODE(string);
25322532
len = PyUnicode_GET_SIZE(string);
25332533

2534-
if (maxcount < 0)
2535-
maxcount = INT_MAX;
2536-
25372534
list = PyList_New(0);
25382535
if (!list)
25392536
goto onError;
25402537

25412538
for (i = j = 0; i < len; ) {
2539+
int eol;
2540+
25422541
/* Find a line and append it */
25432542
while (i < len && !Py_UNICODE_ISLINEBREAK(data[i]))
25442543
i++;
2545-
if (maxcount-- <= 0)
2546-
break;
2547-
SPLIT_APPEND(data, j, i);
25482544

25492545
/* Skip the line break reading CRLF as one line break */
2546+
eol = i;
25502547
if (i < len) {
25512548
if (data[i] == '\r' && i + 1 < len &&
25522549
data[i+1] == '\n')
25532550
i += 2;
25542551
else
25552552
i++;
2553+
if (keepends)
2554+
eol = i;
25562555
}
2556+
SPLIT_APPEND(data, j, eol);
25572557
j = i;
25582558
}
25592559
if (j < len) {
@@ -3785,21 +3785,21 @@ unicode_split(PyUnicodeObject *self, PyObject *args)
37853785
}
37863786

37873787
static char splitlines__doc__[] =
3788-
"S.splitlines([maxsplit]]) -> list of strings\n\
3788+
"S.splitlines([keepends]]) -> list of strings\n\
37893789
\n\
37903790
Return a list of the lines in S, breaking at line boundaries.\n\
3791-
If maxsplit is given, at most maxsplit are done. Line breaks are not\n\
3792-
included in the resulting list.";
3791+
Line breaks are not included in the resulting list unless keepends\n\
3792+
is given and true.";
37933793

37943794
static PyObject*
37953795
unicode_splitlines(PyUnicodeObject *self, PyObject *args)
37963796
{
3797-
int maxcount = -1;
3797+
int keepends = 0;
37983798

3799-
if (!PyArg_ParseTuple(args, "|i:splitlines", &maxcount))
3799+
if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends))
38003800
return NULL;
38013801

3802-
return PyUnicode_Splitlines((PyObject *)self, maxcount);
3802+
return PyUnicode_Splitlines((PyObject *)self, keepends);
38033803
}
38043804

38053805
static

0 commit comments

Comments
 (0)