Kaydet (Commit) f0b7b04a authored tarafından Guido van Rossum's avatar Guido van Rossum

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).
üst dc742b31
...@@ -2072,11 +2072,11 @@ string_istitle(PyStringObject *self, PyObject *args) ...@@ -2072,11 +2072,11 @@ string_istitle(PyStringObject *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.";
#define SPLIT_APPEND(data, left, right) \ #define SPLIT_APPEND(data, left, right) \
str = PyString_FromStringAndSize(data + left, right - left); \ str = PyString_FromStringAndSize(data + left, right - left); \
...@@ -2092,43 +2092,43 @@ included in the resulting list."; ...@@ -2092,43 +2092,43 @@ included in the resulting list.";
static PyObject* static PyObject*
string_splitlines(PyStringObject *self, PyObject *args) string_splitlines(PyStringObject *self, PyObject *args)
{ {
int maxcount = -1;
register int i; register int i;
register int j; register int j;
int len; int len;
int keepends = 0;
PyObject *list; PyObject *list;
PyObject *str; PyObject *str;
char *data; char *data;
if (!PyArg_ParseTuple(args, "|i:splitlines", &maxcount)) if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends))
return NULL; return NULL;
data = PyString_AS_STRING(self); data = PyString_AS_STRING(self);
len = PyString_GET_SIZE(self); len = PyString_GET_SIZE(self);
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 && data[i] != '\n' && data[i] != '\r') while (i < len && data[i] != '\n' && data[i] != '\r')
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) {
...@@ -2591,7 +2591,10 @@ PyString_Format(format, args) ...@@ -2591,7 +2591,10 @@ PyString_Format(format, args)
fmt = fmt_start; fmt = fmt_start;
goto unicode; goto unicode;
} }
if (c == 's')
temp = PyObject_Str(v); temp = PyObject_Str(v);
else
temp = PyObject_Repr(v);
if (temp == NULL) if (temp == NULL)
goto error; goto error;
if (!PyString_Check(temp)) { if (!PyString_Check(temp)) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment