Changeset 391 for python/trunk/Objects/stringlib/transmogrify.h
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Objects/stringlib/transmogrify.h
r2 r391 1 1 /* NOTE: this API is -ONLY- for use with single byte character strings. */ 2 2 /* Do not use it with Unicode. */ 3 4 #include "bytes_methods.h"5 6 #ifndef STRINGLIB_MUTABLE7 #warning "STRINGLIB_MUTABLE not defined before #include, assuming 0"8 #define STRINGLIB_MUTABLE 09 #endif10 3 11 4 /* the more complicated methods. parts of these should be pulled out into the … … 270 263 return (PyObject*) s; 271 264 } 272 273 274 #define _STRINGLIB_SPLIT_APPEND(data, left, right) \275 str = STRINGLIB_NEW((data) + (left), \276 (right) - (left)); \277 if (str == NULL) \278 goto onError; \279 if (PyList_Append(list, str)) { \280 Py_DECREF(str); \281 goto onError; \282 } \283 else \284 Py_DECREF(str);285 286 PyDoc_STRVAR(splitlines__doc__,287 "B.splitlines([keepends]) -> list of lines\n\288 \n\289 Return a list of the lines in B, breaking at line boundaries.\n\290 Line breaks are not included in the resulting list unless keepends\n\291 is given and true.");292 293 static PyObject*294 stringlib_splitlines(PyObject *self, PyObject *args)295 {296 register Py_ssize_t i;297 register Py_ssize_t j;298 Py_ssize_t len;299 int keepends = 0;300 PyObject *list;301 PyObject *str;302 char *data;303 304 if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends))305 return NULL;306 307 data = STRINGLIB_STR(self);308 len = STRINGLIB_LEN(self);309 310 /* This does not use the preallocated list because splitlines is311 usually run with hundreds of newlines. The overhead of312 switching between PyList_SET_ITEM and append causes about a313 2-3% slowdown for that common case. A smarter implementation314 could move the if check out, so the SET_ITEMs are done first315 and the appends only done when the prealloc buffer is full.316 That's too much work for little gain.*/317 318 list = PyList_New(0);319 if (!list)320 goto onError;321 322 for (i = j = 0; i < len; ) {323 Py_ssize_t eol;324 325 /* Find a line and append it */326 while (i < len && data[i] != '\n' && data[i] != '\r')327 i++;328 329 /* Skip the line break reading CRLF as one line break */330 eol = i;331 if (i < len) {332 if (data[i] == '\r' && i + 1 < len &&333 data[i+1] == '\n')334 i += 2;335 else336 i++;337 if (keepends)338 eol = i;339 }340 _STRINGLIB_SPLIT_APPEND(data, j, eol);341 j = i;342 }343 if (j < len) {344 _STRINGLIB_SPLIT_APPEND(data, j, len);345 }346 347 return list;348 349 onError:350 Py_XDECREF(list);351 return NULL;352 }353 354 #undef _STRINGLIB_SPLIT_APPEND355
Note:
See TracChangeset
for help on using the changeset viewer.