/src/Python-3.8.3/Objects/stringlib/split.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* stringlib: split implementation */ |
2 | | |
3 | | #ifndef STRINGLIB_FASTSEARCH_H |
4 | | #error must include "stringlib/fastsearch.h" before including this module |
5 | | #endif |
6 | | |
7 | | /* Overallocate the initial list to reduce the number of reallocs for small |
8 | | split sizes. Eg, "A A A A A A A A A A".split() (10 elements) has three |
9 | | resizes, to sizes 4, 8, then 16. Most observed string splits are for human |
10 | | text (roughly 11 words per line) and field delimited data (usually 1-10 |
11 | | fields). For large strings the split algorithms are bandwidth limited |
12 | | so increasing the preallocation likely will not improve things.*/ |
13 | | |
14 | 860 | #define MAX_PREALLOC 12 |
15 | | |
16 | | /* 5 splits gives 6 elements */ |
17 | | #define PREALLOC_SIZE(maxsplit) \ |
18 | 74 | (maxsplit >= MAX_PREALLOC ? MAX_PREALLOC : maxsplit+1) |
19 | | |
20 | | #define SPLIT_APPEND(data, left, right) \ |
21 | 0 | sub = STRINGLIB_NEW((data) + (left), \ |
22 | 0 | (right) - (left)); \ |
23 | 0 | if (sub == NULL) \ |
24 | 0 | goto onError; \ |
25 | 0 | if (PyList_Append(list, sub)) { \ |
26 | 0 | Py_DECREF(sub); \ |
27 | 0 | goto onError; \ |
28 | 0 | } \ |
29 | 0 | else \ |
30 | 0 | Py_DECREF(sub); |
31 | | |
32 | 638 | #define SPLIT_ADD(data, left, right) { \ |
33 | 638 | sub = STRINGLIB_NEW((data) + (left), \ |
34 | 638 | (right) - (left)); \ |
35 | 638 | if (sub == NULL) \ |
36 | 638 | goto onError; \ |
37 | 638 | if (count < MAX_PREALLOC) { \ |
38 | 601 | PyList_SET_ITEM(list, count, sub); \ |
39 | 601 | } else { \ |
40 | 37 | if (PyList_Append(list, sub)) { \ |
41 | 0 | Py_DECREF(sub); \ |
42 | 0 | goto onError; \ |
43 | 0 | } \ |
44 | 37 | else \ |
45 | 37 | Py_DECREF(sub); \ |
46 | 37 | } \ |
47 | 638 | count++; } |
48 | | |
49 | | |
50 | | /* Always force the list to the expected size. */ |
51 | 74 | #define FIX_PREALLOC_SIZE(list) Py_SIZE(list) = count |
52 | | |
53 | | Py_LOCAL_INLINE(PyObject *) |
54 | | STRINGLIB(split_whitespace)(PyObject* str_obj, |
55 | | const STRINGLIB_CHAR* str, Py_ssize_t str_len, |
56 | | Py_ssize_t maxcount) |
57 | 4 | { |
58 | 4 | Py_ssize_t i, j, count=0; |
59 | 4 | PyObject *list = PyList_New(PREALLOC_SIZE(maxcount)); |
60 | 4 | PyObject *sub; |
61 | | |
62 | 4 | if (list == NULL) |
63 | 0 | return NULL; |
64 | | |
65 | 4 | i = j = 0; |
66 | 82 | while (maxcount-- > 0) { |
67 | 357 | while (i < str_len && STRINGLIB_ISSPACE(str[i])) |
68 | 275 | i++; |
69 | 82 | if (i == str_len) break; |
70 | 78 | j = i; i++; |
71 | 969 | while (i < str_len && !STRINGLIB_ISSPACE(str[i])) |
72 | 891 | i++; |
73 | | #ifndef STRINGLIB_MUTABLE |
74 | 78 | if (j == 0 && i == str_len && STRINGLIB_CHECK_EXACT(str_obj)) { |
75 | | /* No whitespace in str_obj, so just use it as list[0] */ |
76 | 0 | Py_INCREF(str_obj); |
77 | 0 | PyList_SET_ITEM(list, 0, (PyObject *)str_obj); |
78 | 0 | count++; |
79 | 0 | break; |
80 | 0 | } |
81 | 78 | #endif |
82 | 234 | SPLIT_ADD(str, j, i); |
83 | 234 | } |
84 | | |
85 | 4 | if (i < str_len) { |
86 | | /* Only occurs when maxcount was reached */ |
87 | | /* Skip any remaining whitespace and copy to end of string */ |
88 | 0 | while (i < str_len && STRINGLIB_ISSPACE(str[i])) |
89 | 0 | i++; |
90 | 0 | if (i != str_len) |
91 | 0 | SPLIT_ADD(str, i, str_len); |
92 | 0 | } |
93 | 4 | FIX_PREALLOC_SIZE(list); |
94 | 4 | return list; |
95 | | |
96 | 0 | onError: |
97 | 0 | Py_DECREF(list); |
98 | 0 | return NULL; |
99 | 4 | } Unexecuted instantiation: bytearrayobject.c:stringlib_split_whitespace Unexecuted instantiation: bytesobject.c:stringlib_split_whitespace unicodeobject.c:asciilib_split_whitespace Line | Count | Source | 57 | 4 | { | 58 | 4 | Py_ssize_t i, j, count=0; | 59 | 4 | PyObject *list = PyList_New(PREALLOC_SIZE(maxcount)); | 60 | 4 | PyObject *sub; | 61 | | | 62 | 4 | if (list == NULL) | 63 | 0 | return NULL; | 64 | | | 65 | 4 | i = j = 0; | 66 | 82 | while (maxcount-- > 0) { | 67 | 357 | while (i < str_len && STRINGLIB_ISSPACE(str[i])) | 68 | 275 | i++; | 69 | 82 | if (i == str_len) break; | 70 | 78 | j = i; i++; | 71 | 969 | while (i < str_len && !STRINGLIB_ISSPACE(str[i])) | 72 | 891 | i++; | 73 | 78 | #ifndef STRINGLIB_MUTABLE | 74 | 78 | if (j == 0 && i == str_len && STRINGLIB_CHECK_EXACT(str_obj)) { | 75 | | /* No whitespace in str_obj, so just use it as list[0] */ | 76 | 0 | Py_INCREF(str_obj); | 77 | 0 | PyList_SET_ITEM(list, 0, (PyObject *)str_obj); | 78 | 0 | count++; | 79 | 0 | break; | 80 | 0 | } | 81 | 78 | #endif | 82 | 234 | SPLIT_ADD(str, j, i); | 83 | 234 | } | 84 | | | 85 | 4 | if (i < str_len) { | 86 | | /* Only occurs when maxcount was reached */ | 87 | | /* Skip any remaining whitespace and copy to end of string */ | 88 | 0 | while (i < str_len && STRINGLIB_ISSPACE(str[i])) | 89 | 0 | i++; | 90 | 0 | if (i != str_len) | 91 | 0 | SPLIT_ADD(str, i, str_len); | 92 | 0 | } | 93 | 4 | FIX_PREALLOC_SIZE(list); | 94 | 4 | return list; | 95 | | | 96 | 0 | onError: | 97 | 0 | Py_DECREF(list); | 98 | 0 | return NULL; | 99 | 4 | } |
Unexecuted instantiation: unicodeobject.c:ucs1lib_split_whitespace Unexecuted instantiation: unicodeobject.c:ucs2lib_split_whitespace Unexecuted instantiation: unicodeobject.c:ucs4lib_split_whitespace |
100 | | |
101 | | Py_LOCAL_INLINE(PyObject *) |
102 | | STRINGLIB(split_char)(PyObject* str_obj, |
103 | | const STRINGLIB_CHAR* str, Py_ssize_t str_len, |
104 | | const STRINGLIB_CHAR ch, |
105 | | Py_ssize_t maxcount) |
106 | 70 | { |
107 | 70 | Py_ssize_t i, j, count=0; |
108 | 70 | PyObject *list = PyList_New(PREALLOC_SIZE(maxcount)); |
109 | 70 | PyObject *sub; |
110 | | |
111 | 70 | if (list == NULL) |
112 | 0 | return NULL; |
113 | | |
114 | 70 | i = j = 0; |
115 | 630 | while ((j < str_len) && (maxcount-- > 0)) { |
116 | 4.53k | for(; j < str_len; j++) { |
117 | | /* I found that using memchr makes no difference */ |
118 | 4.46k | if (str[j] == ch) { |
119 | 490 | SPLIT_ADD(str, i, j); |
120 | 490 | i = j = j + 1; |
121 | 490 | break; |
122 | 490 | } |
123 | 4.46k | } |
124 | 560 | } |
125 | | #ifndef STRINGLIB_MUTABLE |
126 | 70 | if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) { |
127 | | /* ch not in str_obj, so just use str_obj as list[0] */ |
128 | 0 | Py_INCREF(str_obj); |
129 | 0 | PyList_SET_ITEM(list, 0, (PyObject *)str_obj); |
130 | 0 | count++; |
131 | 0 | } else |
132 | 70 | #endif |
133 | 70 | if (i <= str_len) { |
134 | 140 | SPLIT_ADD(str, i, str_len); |
135 | 140 | } |
136 | 70 | FIX_PREALLOC_SIZE(list); |
137 | 70 | return list; |
138 | | |
139 | 0 | onError: |
140 | 0 | Py_DECREF(list); |
141 | 0 | return NULL; |
142 | 0 | } Unexecuted instantiation: bytearrayobject.c:stringlib_split_char Unexecuted instantiation: bytesobject.c:stringlib_split_char unicodeobject.c:asciilib_split_char Line | Count | Source | 106 | 70 | { | 107 | 70 | Py_ssize_t i, j, count=0; | 108 | 70 | PyObject *list = PyList_New(PREALLOC_SIZE(maxcount)); | 109 | 70 | PyObject *sub; | 110 | | | 111 | 70 | if (list == NULL) | 112 | 0 | return NULL; | 113 | | | 114 | 70 | i = j = 0; | 115 | 630 | while ((j < str_len) && (maxcount-- > 0)) { | 116 | 4.53k | for(; j < str_len; j++) { | 117 | | /* I found that using memchr makes no difference */ | 118 | 4.46k | if (str[j] == ch) { | 119 | 490 | SPLIT_ADD(str, i, j); | 120 | 490 | i = j = j + 1; | 121 | 490 | break; | 122 | 490 | } | 123 | 4.46k | } | 124 | 560 | } | 125 | 70 | #ifndef STRINGLIB_MUTABLE | 126 | 70 | if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) { | 127 | | /* ch not in str_obj, so just use str_obj as list[0] */ | 128 | 0 | Py_INCREF(str_obj); | 129 | 0 | PyList_SET_ITEM(list, 0, (PyObject *)str_obj); | 130 | 0 | count++; | 131 | 0 | } else | 132 | 70 | #endif | 133 | 70 | if (i <= str_len) { | 134 | 140 | SPLIT_ADD(str, i, str_len); | 135 | 140 | } | 136 | 70 | FIX_PREALLOC_SIZE(list); | 137 | 70 | return list; | 138 | | | 139 | 0 | onError: | 140 | 0 | Py_DECREF(list); | 141 | 0 | return NULL; | 142 | 70 | } |
Unexecuted instantiation: unicodeobject.c:ucs1lib_split_char Unexecuted instantiation: unicodeobject.c:ucs2lib_split_char Unexecuted instantiation: unicodeobject.c:ucs4lib_split_char |
143 | | |
144 | | Py_LOCAL_INLINE(PyObject *) |
145 | | STRINGLIB(split)(PyObject* str_obj, |
146 | | const STRINGLIB_CHAR* str, Py_ssize_t str_len, |
147 | | const STRINGLIB_CHAR* sep, Py_ssize_t sep_len, |
148 | | Py_ssize_t maxcount) |
149 | 70 | { |
150 | 70 | Py_ssize_t i, j, pos, count=0; |
151 | 70 | PyObject *list, *sub; |
152 | | |
153 | 70 | if (sep_len == 0) { |
154 | 0 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
155 | 0 | return NULL; |
156 | 0 | } |
157 | 70 | else if (sep_len == 1) |
158 | 70 | return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount); |
159 | | |
160 | 0 | list = PyList_New(PREALLOC_SIZE(maxcount)); |
161 | 0 | if (list == NULL) |
162 | 0 | return NULL; |
163 | | |
164 | 0 | i = j = 0; |
165 | 0 | while (maxcount-- > 0) { |
166 | 0 | pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH); |
167 | 0 | if (pos < 0) |
168 | 0 | break; |
169 | 0 | j = i + pos; |
170 | 0 | SPLIT_ADD(str, i, j); |
171 | 0 | i = j + sep_len; |
172 | 0 | } |
173 | | #ifndef STRINGLIB_MUTABLE |
174 | 0 | if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) { |
175 | | /* No match in str_obj, so just use it as list[0] */ |
176 | 0 | Py_INCREF(str_obj); |
177 | 0 | PyList_SET_ITEM(list, 0, (PyObject *)str_obj); |
178 | 0 | count++; |
179 | 0 | } else |
180 | 0 | #endif |
181 | 0 | { |
182 | 0 | SPLIT_ADD(str, i, str_len); |
183 | 0 | } |
184 | 0 | FIX_PREALLOC_SIZE(list); |
185 | 0 | return list; |
186 | | |
187 | 0 | onError: |
188 | 0 | Py_DECREF(list); |
189 | 0 | return NULL; |
190 | 0 | } Unexecuted instantiation: bytearrayobject.c:stringlib_split Unexecuted instantiation: bytesobject.c:stringlib_split unicodeobject.c:asciilib_split Line | Count | Source | 149 | 70 | { | 150 | 70 | Py_ssize_t i, j, pos, count=0; | 151 | 70 | PyObject *list, *sub; | 152 | | | 153 | 70 | if (sep_len == 0) { | 154 | 0 | PyErr_SetString(PyExc_ValueError, "empty separator"); | 155 | 0 | return NULL; | 156 | 0 | } | 157 | 70 | else if (sep_len == 1) | 158 | 70 | return STRINGLIB(split_char)(str_obj, str, str_len, sep[0], maxcount); | 159 | | | 160 | 0 | list = PyList_New(PREALLOC_SIZE(maxcount)); | 161 | 0 | if (list == NULL) | 162 | 0 | return NULL; | 163 | | | 164 | 0 | i = j = 0; | 165 | 0 | while (maxcount-- > 0) { | 166 | 0 | pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH); | 167 | 0 | if (pos < 0) | 168 | 0 | break; | 169 | 0 | j = i + pos; | 170 | 0 | SPLIT_ADD(str, i, j); | 171 | 0 | i = j + sep_len; | 172 | 0 | } | 173 | 0 | #ifndef STRINGLIB_MUTABLE | 174 | 0 | if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) { | 175 | | /* No match in str_obj, so just use it as list[0] */ | 176 | 0 | Py_INCREF(str_obj); | 177 | 0 | PyList_SET_ITEM(list, 0, (PyObject *)str_obj); | 178 | 0 | count++; | 179 | 0 | } else | 180 | 0 | #endif | 181 | 0 | { | 182 | 0 | SPLIT_ADD(str, i, str_len); | 183 | 0 | } | 184 | 0 | FIX_PREALLOC_SIZE(list); | 185 | 0 | return list; | 186 | | | 187 | 0 | onError: | 188 | 0 | Py_DECREF(list); | 189 | 0 | return NULL; | 190 | 0 | } |
Unexecuted instantiation: unicodeobject.c:ucs1lib_split Unexecuted instantiation: unicodeobject.c:ucs2lib_split Unexecuted instantiation: unicodeobject.c:ucs4lib_split |
191 | | |
192 | | Py_LOCAL_INLINE(PyObject *) |
193 | | STRINGLIB(rsplit_whitespace)(PyObject* str_obj, |
194 | | const STRINGLIB_CHAR* str, Py_ssize_t str_len, |
195 | | Py_ssize_t maxcount) |
196 | 0 | { |
197 | 0 | Py_ssize_t i, j, count=0; |
198 | 0 | PyObject *list = PyList_New(PREALLOC_SIZE(maxcount)); |
199 | 0 | PyObject *sub; |
200 | |
|
201 | 0 | if (list == NULL) |
202 | 0 | return NULL; |
203 | | |
204 | 0 | i = j = str_len - 1; |
205 | 0 | while (maxcount-- > 0) { |
206 | 0 | while (i >= 0 && STRINGLIB_ISSPACE(str[i])) |
207 | 0 | i--; |
208 | 0 | if (i < 0) break; |
209 | 0 | j = i; i--; |
210 | 0 | while (i >= 0 && !STRINGLIB_ISSPACE(str[i])) |
211 | 0 | i--; |
212 | | #ifndef STRINGLIB_MUTABLE |
213 | 0 | if (j == str_len - 1 && i < 0 && STRINGLIB_CHECK_EXACT(str_obj)) { |
214 | | /* No whitespace in str_obj, so just use it as list[0] */ |
215 | 0 | Py_INCREF(str_obj); |
216 | 0 | PyList_SET_ITEM(list, 0, (PyObject *)str_obj); |
217 | 0 | count++; |
218 | 0 | break; |
219 | 0 | } |
220 | 0 | #endif |
221 | 0 | SPLIT_ADD(str, i + 1, j + 1); |
222 | 0 | } |
223 | | |
224 | 0 | if (i >= 0) { |
225 | | /* Only occurs when maxcount was reached */ |
226 | | /* Skip any remaining whitespace and copy to beginning of string */ |
227 | 0 | while (i >= 0 && STRINGLIB_ISSPACE(str[i])) |
228 | 0 | i--; |
229 | 0 | if (i >= 0) |
230 | 0 | SPLIT_ADD(str, 0, i + 1); |
231 | 0 | } |
232 | 0 | FIX_PREALLOC_SIZE(list); |
233 | 0 | if (PyList_Reverse(list) < 0) |
234 | 0 | goto onError; |
235 | 0 | return list; |
236 | | |
237 | 0 | onError: |
238 | 0 | Py_DECREF(list); |
239 | 0 | return NULL; |
240 | 0 | } Unexecuted instantiation: bytearrayobject.c:stringlib_rsplit_whitespace Unexecuted instantiation: bytesobject.c:stringlib_rsplit_whitespace Unexecuted instantiation: unicodeobject.c:asciilib_rsplit_whitespace Unexecuted instantiation: unicodeobject.c:ucs1lib_rsplit_whitespace Unexecuted instantiation: unicodeobject.c:ucs2lib_rsplit_whitespace Unexecuted instantiation: unicodeobject.c:ucs4lib_rsplit_whitespace |
241 | | |
242 | | Py_LOCAL_INLINE(PyObject *) |
243 | | STRINGLIB(rsplit_char)(PyObject* str_obj, |
244 | | const STRINGLIB_CHAR* str, Py_ssize_t str_len, |
245 | | const STRINGLIB_CHAR ch, |
246 | | Py_ssize_t maxcount) |
247 | 0 | { |
248 | 0 | Py_ssize_t i, j, count=0; |
249 | 0 | PyObject *list = PyList_New(PREALLOC_SIZE(maxcount)); |
250 | 0 | PyObject *sub; |
251 | |
|
252 | 0 | if (list == NULL) |
253 | 0 | return NULL; |
254 | | |
255 | 0 | i = j = str_len - 1; |
256 | 0 | while ((i >= 0) && (maxcount-- > 0)) { |
257 | 0 | for(; i >= 0; i--) { |
258 | 0 | if (str[i] == ch) { |
259 | 0 | SPLIT_ADD(str, i + 1, j + 1); |
260 | 0 | j = i = i - 1; |
261 | 0 | break; |
262 | 0 | } |
263 | 0 | } |
264 | 0 | } |
265 | | #ifndef STRINGLIB_MUTABLE |
266 | 0 | if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) { |
267 | | /* ch not in str_obj, so just use str_obj as list[0] */ |
268 | 0 | Py_INCREF(str_obj); |
269 | 0 | PyList_SET_ITEM(list, 0, (PyObject *)str_obj); |
270 | 0 | count++; |
271 | 0 | } else |
272 | 0 | #endif |
273 | 0 | if (j >= -1) { |
274 | 0 | SPLIT_ADD(str, 0, j + 1); |
275 | 0 | } |
276 | 0 | FIX_PREALLOC_SIZE(list); |
277 | 0 | if (PyList_Reverse(list) < 0) |
278 | 0 | goto onError; |
279 | 0 | return list; |
280 | | |
281 | 0 | onError: |
282 | 0 | Py_DECREF(list); |
283 | 0 | return NULL; |
284 | 0 | } Unexecuted instantiation: bytearrayobject.c:stringlib_rsplit_char Unexecuted instantiation: bytesobject.c:stringlib_rsplit_char Unexecuted instantiation: unicodeobject.c:asciilib_rsplit_char Unexecuted instantiation: unicodeobject.c:ucs1lib_rsplit_char Unexecuted instantiation: unicodeobject.c:ucs2lib_rsplit_char Unexecuted instantiation: unicodeobject.c:ucs4lib_rsplit_char |
285 | | |
286 | | Py_LOCAL_INLINE(PyObject *) |
287 | | STRINGLIB(rsplit)(PyObject* str_obj, |
288 | | const STRINGLIB_CHAR* str, Py_ssize_t str_len, |
289 | | const STRINGLIB_CHAR* sep, Py_ssize_t sep_len, |
290 | | Py_ssize_t maxcount) |
291 | 0 | { |
292 | 0 | Py_ssize_t j, pos, count=0; |
293 | 0 | PyObject *list, *sub; |
294 | |
|
295 | 0 | if (sep_len == 0) { |
296 | 0 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
297 | 0 | return NULL; |
298 | 0 | } |
299 | 0 | else if (sep_len == 1) |
300 | 0 | return STRINGLIB(rsplit_char)(str_obj, str, str_len, sep[0], maxcount); |
301 | | |
302 | 0 | list = PyList_New(PREALLOC_SIZE(maxcount)); |
303 | 0 | if (list == NULL) |
304 | 0 | return NULL; |
305 | | |
306 | 0 | j = str_len; |
307 | 0 | while (maxcount-- > 0) { |
308 | 0 | pos = FASTSEARCH(str, j, sep, sep_len, -1, FAST_RSEARCH); |
309 | 0 | if (pos < 0) |
310 | 0 | break; |
311 | 0 | SPLIT_ADD(str, pos + sep_len, j); |
312 | 0 | j = pos; |
313 | 0 | } |
314 | | #ifndef STRINGLIB_MUTABLE |
315 | 0 | if (count == 0 && STRINGLIB_CHECK_EXACT(str_obj)) { |
316 | | /* No match in str_obj, so just use it as list[0] */ |
317 | 0 | Py_INCREF(str_obj); |
318 | 0 | PyList_SET_ITEM(list, 0, (PyObject *)str_obj); |
319 | 0 | count++; |
320 | 0 | } else |
321 | 0 | #endif |
322 | 0 | { |
323 | 0 | SPLIT_ADD(str, 0, j); |
324 | 0 | } |
325 | 0 | FIX_PREALLOC_SIZE(list); |
326 | 0 | if (PyList_Reverse(list) < 0) |
327 | 0 | goto onError; |
328 | 0 | return list; |
329 | | |
330 | 0 | onError: |
331 | 0 | Py_DECREF(list); |
332 | 0 | return NULL; |
333 | 0 | } Unexecuted instantiation: bytearrayobject.c:stringlib_rsplit Unexecuted instantiation: bytesobject.c:stringlib_rsplit Unexecuted instantiation: unicodeobject.c:asciilib_rsplit Unexecuted instantiation: unicodeobject.c:ucs1lib_rsplit Unexecuted instantiation: unicodeobject.c:ucs2lib_rsplit Unexecuted instantiation: unicodeobject.c:ucs4lib_rsplit |
334 | | |
335 | | Py_LOCAL_INLINE(PyObject *) |
336 | | STRINGLIB(splitlines)(PyObject* str_obj, |
337 | | const STRINGLIB_CHAR* str, Py_ssize_t str_len, |
338 | | int keepends) |
339 | 0 | { |
340 | | /* This does not use the preallocated list because splitlines is |
341 | | usually run with hundreds of newlines. The overhead of |
342 | | switching between PyList_SET_ITEM and append causes about a |
343 | | 2-3% slowdown for that common case. A smarter implementation |
344 | | could move the if check out, so the SET_ITEMs are done first |
345 | | and the appends only done when the prealloc buffer is full. |
346 | | That's too much work for little gain.*/ |
347 | |
|
348 | 0 | Py_ssize_t i; |
349 | 0 | Py_ssize_t j; |
350 | 0 | PyObject *list = PyList_New(0); |
351 | 0 | PyObject *sub; |
352 | |
|
353 | 0 | if (list == NULL) |
354 | 0 | return NULL; |
355 | | |
356 | 0 | for (i = j = 0; i < str_len; ) { |
357 | 0 | Py_ssize_t eol; |
358 | | |
359 | | /* Find a line and append it */ |
360 | 0 | while (i < str_len && !STRINGLIB_ISLINEBREAK(str[i])) |
361 | 0 | i++; |
362 | | |
363 | | /* Skip the line break reading CRLF as one line break */ |
364 | 0 | eol = i; |
365 | 0 | if (i < str_len) { |
366 | 0 | if (str[i] == '\r' && i + 1 < str_len && str[i+1] == '\n') |
367 | 0 | i += 2; |
368 | 0 | else |
369 | 0 | i++; |
370 | 0 | if (keepends) |
371 | 0 | eol = i; |
372 | 0 | } |
373 | | #ifndef STRINGLIB_MUTABLE |
374 | 0 | if (j == 0 && eol == str_len && STRINGLIB_CHECK_EXACT(str_obj)) { |
375 | | /* No linebreak in str_obj, so just use it as list[0] */ |
376 | 0 | if (PyList_Append(list, str_obj)) |
377 | 0 | goto onError; |
378 | 0 | break; |
379 | 0 | } |
380 | 0 | #endif |
381 | 0 | SPLIT_APPEND(str, j, eol); |
382 | 0 | j = i; |
383 | 0 | } |
384 | 0 | return list; |
385 | | |
386 | 0 | onError: |
387 | 0 | Py_DECREF(list); |
388 | 0 | return NULL; |
389 | 0 | } Unexecuted instantiation: bytearrayobject.c:stringlib_splitlines Unexecuted instantiation: bytesobject.c:stringlib_splitlines Unexecuted instantiation: unicodeobject.c:asciilib_splitlines Unexecuted instantiation: unicodeobject.c:ucs1lib_splitlines Unexecuted instantiation: unicodeobject.c:ucs2lib_splitlines Unexecuted instantiation: unicodeobject.c:ucs4lib_splitlines |
390 | | |