Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 1 | /* stringlib: partition implementation */ |
| 2 | |
| 3 | #ifndef STRINGLIB_PARTITION_H |
| 4 | #define STRINGLIB_PARTITION_H |
| 5 | |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 6 | Py_LOCAL(PyObject*) |
| 7 | partition(PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len, |
| 8 | PyObject* sep_obj, const STRINGLIB_CHAR* sep, Py_ssize_t sep_len) |
| 9 | { |
| 10 | PyObject* out; |
| 11 | Py_ssize_t pos; |
| 12 | |
| 13 | if (sep_len == 0) { |
| 14 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 15 | return NULL; |
| 16 | } |
| 17 | |
| 18 | out = PyTuple_New(3); |
| 19 | if (!out) |
| 20 | return NULL; |
| 21 | |
| 22 | pos = fastsearch(str, str_len, sep, sep_len, FAST_SEARCH); |
| 23 | |
| 24 | if (pos < 0) { |
| 25 | Py_INCREF(str_obj); |
| 26 | PyTuple_SET_ITEM(out, 0, (PyObject*) str_obj); |
| 27 | Py_INCREF(STRINGLIB_EMPTY); |
| 28 | PyTuple_SET_ITEM(out, 1, (PyObject*) STRINGLIB_EMPTY); |
| 29 | Py_INCREF(STRINGLIB_EMPTY); |
| 30 | PyTuple_SET_ITEM(out, 2, (PyObject*) STRINGLIB_EMPTY); |
| 31 | return out; |
| 32 | } |
| 33 | |
| 34 | PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(str, pos)); |
| 35 | Py_INCREF(sep_obj); |
| 36 | PyTuple_SET_ITEM(out, 1, sep_obj); |
| 37 | pos += sep_len; |
| 38 | PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str + pos, str_len - pos)); |
| 39 | |
| 40 | if (PyErr_Occurred()) { |
| 41 | Py_DECREF(out); |
| 42 | return NULL; |
| 43 | } |
| 44 | |
| 45 | return out; |
| 46 | } |
| 47 | |
| 48 | #endif |