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.
This commit is contained in:
Guido van Rossum 2000-04-11 15:38:46 +00:00
parent 3afba7644b
commit 86662914be
2 changed files with 14 additions and 14 deletions

View file

@ -96,7 +96,7 @@ class UserString:
def rstrip(self): return self.__class__(self.data.rstrip()) def rstrip(self): return self.__class__(self.data.rstrip())
def split(self, sep=None, maxsplit=-1): def split(self, sep=None, maxsplit=-1):
return self.data.split(sep, maxsplit) return self.data.split(sep, maxsplit)
def splitlines(self, maxsplit=-1): return self.data.splitlines(maxsplit) def splitlines(self, keepends=0): return self.data.splitlines(keepends)
def startswith(self, prefix, start=0, end=sys.maxint): def startswith(self, prefix, start=0, end=sys.maxint):
return self.data.startswith(prefix, start, end) return self.data.startswith(prefix, start, end)
def strip(self): return self.__class__(self.data.strip()) def strip(self): return self.__class__(self.data.strip())

View file

@ -2516,7 +2516,7 @@ PyObject *split_whitespace(PyUnicodeObject *self,
} }
PyObject *PyUnicode_Splitlines(PyObject *string, PyObject *PyUnicode_Splitlines(PyObject *string,
int maxcount) int keepends)
{ {
register int i; register int i;
register int j; register int j;
@ -2531,29 +2531,29 @@ PyObject *PyUnicode_Splitlines(PyObject *string,
data = PyUnicode_AS_UNICODE(string); data = PyUnicode_AS_UNICODE(string);
len = PyUnicode_GET_SIZE(string); len = PyUnicode_GET_SIZE(string);
if (maxcount < 0)
maxcount = INT_MAX;
list = PyList_New(0); list = PyList_New(0);
if (!list) if (!list)
goto onError; goto onError;
for (i = j = 0; i < len; ) { for (i = j = 0; i < len; ) {
int eol;
/* Find a line and append it */ /* Find a line and append it */
while (i < len && !Py_UNICODE_ISLINEBREAK(data[i])) while (i < len && !Py_UNICODE_ISLINEBREAK(data[i]))
i++; i++;
if (maxcount-- <= 0)
break;
SPLIT_APPEND(data, j, i);
/* Skip the line break reading CRLF as one line break */ /* Skip the line break reading CRLF as one line break */
eol = i;
if (i < len) { if (i < len) {
if (data[i] == '\r' && i + 1 < len && if (data[i] == '\r' && i + 1 < len &&
data[i+1] == '\n') data[i+1] == '\n')
i += 2; i += 2;
else else
i++; i++;
if (keepends)
eol = i;
} }
SPLIT_APPEND(data, j, eol);
j = i; j = i;
} }
if (j < len) { if (j < len) {
@ -3785,21 +3785,21 @@ unicode_split(PyUnicodeObject *self, PyObject *args)
} }
static char splitlines__doc__[] = static char splitlines__doc__[] =
"S.splitlines([maxsplit]]) -> list of strings\n\ "S.splitlines([keepends]]) -> list of strings\n\
\n\ \n\
Return a list of the lines in S, breaking at line boundaries.\n\ Return a list of the lines in S, breaking at line boundaries.\n\
If maxsplit is given, at most maxsplit are done. Line breaks are not\n\ Line breaks are not included in the resulting list unless keepends\n\
included in the resulting list."; is given and true.";
static PyObject* static PyObject*
unicode_splitlines(PyUnicodeObject *self, PyObject *args) unicode_splitlines(PyUnicodeObject *self, PyObject *args)
{ {
int maxcount = -1; int keepends = 0;
if (!PyArg_ParseTuple(args, "|i:splitlines", &maxcount)) if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends))
return NULL; return NULL;
return PyUnicode_Splitlines((PyObject *)self, maxcount); return PyUnicode_Splitlines((PyObject *)self, keepends);
} }
static static