Mercurial > cpython-lru_cache-weakref
view Objects/stringlib/split.h @ 58664:993f703a61ba
Merged revisions 77484,77487,77561,77570,77593,77603,77608,77667,77702-77703,77739,77858,77887,77889 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r77484 | skip.montanaro | 2010-01-13 19:12:34 -0600 (Wed, 13 Jan 2010) | 4 lines
Update PyEval_EvalFrame to PyEval_EvalFrameEx. This looks to have been done
partially before. Also add a comment describing how this might have to work
with different versions of the interpreter.
........
r77487 | ezio.melotti | 2010-01-14 05:34:10 -0600 (Thu, 14 Jan 2010) | 1 line
Fixed typo
........
r77561 | georg.brandl | 2010-01-17 02:42:30 -0600 (Sun, 17 Jan 2010) | 1 line
#7699: improve datetime docs: straightforward linking to strftime/strptime section, mark classmethods as such.
........
r77570 | georg.brandl | 2010-01-17 06:14:42 -0600 (Sun, 17 Jan 2010) | 1 line
Add note about usage of STRINGLIB_EMPTY.
........
r77593 | georg.brandl | 2010-01-17 17:33:53 -0600 (Sun, 17 Jan 2010) | 1 line
Fix internal reference.
........
r77603 | benjamin.peterson | 2010-01-18 17:07:56 -0600 (Mon, 18 Jan 2010) | 8 lines
data descriptors do not override the class dictionary if __get__ is not defined
Adjust documentation and add a test to verify this behavior.
See http://mail.python.org/pipermail/python-dev/2010-January/095637.html for
discussion.
........
r77608 | gregory.p.smith | 2010-01-19 02:19:03 -0600 (Tue, 19 Jan 2010) | 6 lines
Do not compile stubs for the sha2 series hashes in the openssl hashlib
module when the openssl version is too old to support them. That
leads both compiled code bloat and to unittests attempting to test
implementations that don't exist for comparison purposes on such
platforms.
........
r77667 | mark.dickinson | 2010-01-21 12:32:27 -0600 (Thu, 21 Jan 2010) | 1 line
Add two more test_strtod test values.
........
r77702 | georg.brandl | 2010-01-23 02:43:31 -0600 (Sat, 23 Jan 2010) | 1 line
#7762: fix refcount annotation of PyUnicode_Tailmatch().
........
r77703 | georg.brandl | 2010-01-23 02:47:54 -0600 (Sat, 23 Jan 2010) | 1 line
#7725: fix referencing issue.
........
r77739 | benjamin.peterson | 2010-01-24 21:52:52 -0600 (Sun, 24 Jan 2010) | 1 line
mention from_float() in error message
........
r77858 | georg.brandl | 2010-01-30 11:57:48 -0600 (Sat, 30 Jan 2010) | 1 line
#7802: fix invalid example (heh).
........
r77887 | georg.brandl | 2010-01-31 12:51:49 -0600 (Sun, 31 Jan 2010) | 5 lines
Fix-up ftplib documentation:
move exception descriptions to toplevel, not inside a class
remove attribution in "versionadded"
spell and grammar check docstring of FTP_TLS
........
r77889 | michael.foord | 2010-01-31 13:59:26 -0600 (Sun, 31 Jan 2010) | 1 line
Minor modification to unittest documentation.
........
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Wed, 03 Feb 2010 02:35:45 +0000 |
| parents | d1fd0a34bb36 |
| children | 8beaa9a37387 |
line wrap: on
line source
/* stringlib: split implementation */ #ifndef STRINGLIB_SPLIT_H #define STRINGLIB_SPLIT_H #ifndef STRINGLIB_FASTSEARCH_H #error must include "stringlib/fastsearch.h" before including this module #endif /* Overallocate the initial list to reduce the number of reallocs for small split sizes. Eg, "A A A A A A A A A A".split() (10 elements) has three resizes, to sizes 4, 8, then 16. Most observed string splits are for human text (roughly 11 words per line) and field delimited data (usually 1-10 fields). For large strings the split algorithms are bandwidth limited so increasing the preallocation likely will not improve things.*/ #define MAX_PREALLOC 12 /* 5 splits gives 6 elements */ #define PREALLOC_SIZE(maxsplit) \ (maxsplit >= MAX_PREALLOC ? MAX_PREALLOC : maxsplit+1) #define SPLIT_APPEND(data, left, right) \ sub = STRINGLIB_NEW((data) + (left), \ (right) - (left)); \ if (sub == NULL) \ goto onError; \ if (PyList_Append(list, sub)) { \ Py_DECREF(sub); \ goto onError; \ } \ else \ Py_DECREF(sub); #define SPLIT_ADD(data, left, right) { \ sub = STRINGLIB_NEW((data) + (left), \ (right) - (left)); \ if (sub == NULL) \ goto onError; \ if (count < MAX_PREALLOC) { \ PyList_SET_ITEM(list, count, sub); \ } else { \ if (PyList_Append(list, sub)) { \ Py_DECREF(sub); \ goto onError; \ } \ else \ Py_DECREF(sub); \ } \ count++; } /* Always force the list to the expected size. */ #define FIX_PREALLOC_SIZE(list) Py_SIZE(list) = count Py_LOCAL_INLINE(PyObject *) stringlib_split_whitespace(PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len, Py_ssize_t maxcount) { Py_ssize_t i, j, count=0; PyObject *list = PyList_New(PREALLOC_SIZE(maxcount)); PyObject *sub; if (list == NULL) return NULL; i = j = 0; while (maxcount-- > 0) { while (i < str_len && STRINGLIB_ISSPACE(str[i])) i++; if (i == str_len) break; j = i; i++; while (i < str_len && !STRINGLIB_ISSPACE(str[i])) i++; #ifndef STRINGLIB_MUTABLE if (j == 0 && i == str_len && STRINGLIB_CHECK_EXACT(str_obj)) { /* No whitespace in str_obj, so just use it as list[0] */ Py_INCREF(str_obj); PyList_SET_ITEM(list, 0, (PyObject *)str_obj); count++; break; } #endif SPLIT_ADD(str, j, i); } if (i < str_len) { /* Only occurs when maxcount was reached */ /* Skip any remaining whitespace and copy to end of string */ while (i < str_len && STRINGLIB_ISSPACE(str[i])) i++; if (i != str_len) SPLIT_ADD(str, i, str_len); } FIX_PREALLOC_SIZE(list); return list; onError: Py_DECREF(list); return NULL; } Py_LOCAL_INLINE(PyObject *) stringlib_split_char(PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len, const STRINGLIB_CHAR ch, Py_ssize_t maxcount) { Py_ssize_t i, j, count=0; PyObject *list = PyList_New(PREALLOC_SIZE(maxcount)); PyObject *sub; if (list == NULL) return NULL; i = j = 0; while ((j < str_len) && (maxcount-- > 0)) { for(; j < str_len; j++) { /* I found that using memchr makes no difference */ if (str[j] == ch) { SPLIT_ADD(str, i, j); i = j = j + 1; break; } } } #ifndef STRINGLIB_MUTABLE if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) { /* ch not in str_obj, so just use str_obj as list[0] */ Py_INCREF(str_obj); PyList_SET_ITEM(list, 0, (PyObject *)str_obj); count++; } else #endif if (i <= str_len) { SPLIT_ADD(str, i, str_len); } FIX_PREALLOC_SIZE(list); return list; onError: Py_DECREF(list); return NULL; } Py_LOCAL_INLINE(PyObject *) stringlib_split(PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len, const STRINGLIB_CHAR* sep, Py_ssize_t sep_len, Py_ssize_t maxcount) { Py_ssize_t i, j, pos, count=0; PyObject *list, *sub; if (sep_len == 0) { PyErr_SetString(PyExc_ValueError, "empty separator"); return NULL; } else if (sep_len == 1) return stringlib_split_char(str_obj, str, str_len, sep[0], maxcount); list = PyList_New(PREALLOC_SIZE(maxcount)); if (list == NULL) return NULL; i = j = 0; while (maxcount-- > 0) { pos = fastsearch(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH); if (pos < 0) break; j = i + pos; SPLIT_ADD(str, i, j); i = j + sep_len; } #ifndef STRINGLIB_MUTABLE if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) { /* No match in str_obj, so just use it as list[0] */ Py_INCREF(str_obj); PyList_SET_ITEM(list, 0, (PyObject *)str_obj); count++; } else #endif { SPLIT_ADD(str, i, str_len); } FIX_PREALLOC_SIZE(list); return list; onError: Py_DECREF(list); return NULL; } Py_LOCAL_INLINE(PyObject *) stringlib_rsplit_whitespace(PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len, Py_ssize_t maxcount) { Py_ssize_t i, j, count=0; PyObject *list = PyList_New(PREALLOC_SIZE(maxcount)); PyObject *sub; if (list == NULL) return NULL; i = j = str_len - 1; while (maxcount-- > 0) { while (i >= 0 && STRINGLIB_ISSPACE(str[i])) i--; if (i < 0) break; j = i; i--; while (i >= 0 && !STRINGLIB_ISSPACE(str[i])) i--; #ifndef STRINGLIB_MUTABLE if (j == str_len - 1 && i < 0 && STRINGLIB_CHECK_EXACT(str_obj)) { /* No whitespace in str_obj, so just use it as list[0] */ Py_INCREF(str_obj); PyList_SET_ITEM(list, 0, (PyObject *)str_obj); count++; break; } #endif SPLIT_ADD(str, i + 1, j + 1); } if (i >= 0) { /* Only occurs when maxcount was reached */ /* Skip any remaining whitespace and copy to beginning of string */ while (i >= 0 && STRINGLIB_ISSPACE(str[i])) i--; if (i >= 0) SPLIT_ADD(str, 0, i + 1); } FIX_PREALLOC_SIZE(list); if (PyList_Reverse(list) < 0) goto onError; return list; onError: Py_DECREF(list); return NULL; } Py_LOCAL_INLINE(PyObject *) stringlib_rsplit_char(PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len, const STRINGLIB_CHAR ch, Py_ssize_t maxcount) { Py_ssize_t i, j, count=0; PyObject *list = PyList_New(PREALLOC_SIZE(maxcount)); PyObject *sub; if (list == NULL) return NULL; i = j = str_len - 1; while ((i >= 0) && (maxcount-- > 0)) { for(; i >= 0; i--) { if (str[i] == ch) { SPLIT_ADD(str, i + 1, j + 1); j = i = i - 1; break; } } } #ifndef STRINGLIB_MUTABLE if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) { /* ch not in str_obj, so just use str_obj as list[0] */ Py_INCREF(str_obj); PyList_SET_ITEM(list, 0, (PyObject *)str_obj); count++; } else #endif if (j >= -1) { SPLIT_ADD(str, 0, j + 1); } FIX_PREALLOC_SIZE(list); if (PyList_Reverse(list) < 0) goto onError; return list; onError: Py_DECREF(list); return NULL; } Py_LOCAL_INLINE(PyObject *) stringlib_rsplit(PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len, const STRINGLIB_CHAR* sep, Py_ssize_t sep_len, Py_ssize_t maxcount) { Py_ssize_t j, pos, count=0; PyObject *list, *sub; if (sep_len == 0) { PyErr_SetString(PyExc_ValueError, "empty separator"); return NULL; } else if (sep_len == 1) return stringlib_rsplit_char(str_obj, str, str_len, sep[0], maxcount); list = PyList_New(PREALLOC_SIZE(maxcount)); if (list == NULL) return NULL; j = str_len; while (maxcount-- > 0) { pos = fastsearch(str, j, sep, sep_len, -1, FAST_RSEARCH); if (pos < 0) break; SPLIT_ADD(str, pos + sep_len, j); j = pos; } #ifndef STRINGLIB_MUTABLE if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) { /* No match in str_obj, so just use it as list[0] */ Py_INCREF(str_obj); PyList_SET_ITEM(list, 0, (PyObject *)str_obj); count++; } else #endif { SPLIT_ADD(str, 0, j); } FIX_PREALLOC_SIZE(list); if (PyList_Reverse(list) < 0) goto onError; return list; onError: Py_DECREF(list); return NULL; } Py_LOCAL_INLINE(PyObject *) stringlib_splitlines(PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len, int keepends) { /* This does not use the preallocated list because splitlines is usually run with hundreds of newlines. The overhead of switching between PyList_SET_ITEM and append causes about a 2-3% slowdown for that common case. A smarter implementation could move the if check out, so the SET_ITEMs are done first and the appends only done when the prealloc buffer is full. That's too much work for little gain.*/ register Py_ssize_t i; register Py_ssize_t j; PyObject *list = PyList_New(0); PyObject *sub; if (list == NULL) return NULL; for (i = j = 0; i < str_len; ) { Py_ssize_t eol; /* Find a line and append it */ while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i])) i++; /* Skip the line break reading CRLF as one line break */ eol = i; if (i < str_len) { if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n') i += 2; else i++; if (keepends) eol = i; } #ifndef STRINGLIB_MUTABLE if (j == 0 && eol == str_len && STRINGLIB_CHECK_EXACT(str_obj)) { /* No linebreak in str_obj, so just use it as list[0] */ if (PyList_Append(list, str_obj)) goto onError; break; } #endif SPLIT_APPEND(str, j, eol); j = i; } return list; onError: Py_DECREF(list); return NULL; } #endif
