1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 | Objects/stringlib/split.h
/* 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 |