Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Objects/stringlib/transmogrify.h

    r2 r391  
    11/* NOTE: this API is -ONLY- for use with single byte character strings. */
    22/* Do not use it with Unicode. */
    3 
    4 #include "bytes_methods.h"
    5 
    6 #ifndef STRINGLIB_MUTABLE
    7 #warning "STRINGLIB_MUTABLE not defined before #include, assuming 0"
    8 #define STRINGLIB_MUTABLE 0
    9 #endif
    103
    114/* the more complicated methods.  parts of these should be pulled out into the
     
    270263    return (PyObject*) s;
    271264}
    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 is
    311        usually run with hundreds of newlines.  The overhead of
    312        switching between PyList_SET_ITEM and append causes about a
    313        2-3% slowdown for that common case.  A smarter implementation
    314        could move the if check out, so the SET_ITEMs are done first
    315        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             else
    336                 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_APPEND
    355 
Note: See TracChangeset for help on using the changeset viewer.