diff options
author | Guido van Rossum <guido@python.org> | 2000-04-11 15:39:26 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2000-04-11 15:39:26 (GMT) |
commit | f0b7b04ae822439c33acbfb0ebbece30be663f74 (patch) | |
tree | 1a8941fbe30813ce5f0221903f42513d7a9ea163 /Objects/stringobject.c | |
parent | dc742b318480617cdb84af400c4d9dccae3a33d5 (diff) | |
download | cpython-f0b7b04ae822439c33acbfb0ebbece30be663f74.zip cpython-f0b7b04ae822439c33acbfb0ebbece30be663f74.tar.gz cpython-f0b7b04ae822439c33acbfb0ebbece30be663f74.tar.bz2 |
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.
Added support for '%r' % obj: this inserts repr(obj) rather
than str(obj).
Diffstat (limited to 'Objects/stringobject.c')
-rw-r--r-- | Objects/stringobject.c | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/Objects/stringobject.c b/Objects/stringobject.c index 1d7f61a..5b5ed9c 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -2072,11 +2072,11 @@ string_istitle(PyStringObject *self, PyObject *args) static char splitlines__doc__[] = -"S.splitlines([maxsplit]]) -> list of strings\n\ +"S.splitlines([keepends]]) -> list of strings\n\ \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\ -included in the resulting list."; +Line breaks are not included in the resulting list unless keepends\n\ +is given and true."; #define SPLIT_APPEND(data, left, right) \ str = PyString_FromStringAndSize(data + left, right - left); \ @@ -2092,43 +2092,43 @@ included in the resulting list."; static PyObject* string_splitlines(PyStringObject *self, PyObject *args) { - int maxcount = -1; register int i; register int j; int len; + int keepends = 0; PyObject *list; PyObject *str; char *data; - if (!PyArg_ParseTuple(args, "|i:splitlines", &maxcount)) + if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) return NULL; data = PyString_AS_STRING(self); len = PyString_GET_SIZE(self); - if (maxcount < 0) - maxcount = INT_MAX; - list = PyList_New(0); if (!list) goto onError; for (i = j = 0; i < len; ) { + int eol; + /* Find a line and append it */ while (i < len && data[i] != '\n' && data[i] != '\r') i++; - if (maxcount-- <= 0) - break; - SPLIT_APPEND(data, j, i); /* Skip the line break reading CRLF as one line break */ + eol = i; if (i < len) { if (data[i] == '\r' && i + 1 < len && data[i+1] == '\n') i += 2; else i++; + if (keepends) + eol = i; } + SPLIT_APPEND(data, j, eol); j = i; } if (j < len) { @@ -2591,7 +2591,10 @@ PyString_Format(format, args) fmt = fmt_start; goto unicode; } + if (c == 's') temp = PyObject_Str(v); + else + temp = PyObject_Repr(v); if (temp == NULL) goto error; if (!PyString_Check(temp)) { |