Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1 | /* stringlib: partition implementation */ |
| 2 | |
| 3 | #ifndef STRINGLIB_PARTITION_H |
| 4 | #define STRINGLIB_PARTITION_H |
| 5 | |
| 6 | #ifndef STRINGLIB_FASTSEARCH_H |
| 7 | #error must include "stringlib/fastsearch.h" before including this module |
| 8 | #endif |
| 9 | |
| 10 | Py_LOCAL_INLINE(PyObject*) |
| 11 | stringlib_partition( |
| 12 | PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len, |
| 13 | PyObject* sep_obj, const STRINGLIB_CHAR* sep, Py_ssize_t sep_len |
| 14 | ) |
| 15 | { |
| 16 | PyObject* out; |
| 17 | Py_ssize_t pos; |
| 18 | |
| 19 | if (sep_len == 0) { |
| 20 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 21 | return NULL; |
| 22 | } |
| 23 | |
| 24 | out = PyTuple_New(3); |
| 25 | if (!out) |
| 26 | return NULL; |
| 27 | |
| 28 | pos = fastsearch(str, str_len, sep, sep_len, FAST_SEARCH); |
| 29 | |
| 30 | if (pos < 0) { |
| 31 | Py_INCREF(str_obj); |
| 32 | PyTuple_SET_ITEM(out, 0, (PyObject*) str_obj); |
| 33 | Py_INCREF(STRINGLIB_EMPTY); |
| 34 | PyTuple_SET_ITEM(out, 1, (PyObject*) STRINGLIB_EMPTY); |
| 35 | Py_INCREF(STRINGLIB_EMPTY); |
| 36 | PyTuple_SET_ITEM(out, 2, (PyObject*) STRINGLIB_EMPTY); |
| 37 | return out; |
| 38 | } |
| 39 | |
| 40 | PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(str, pos)); |
| 41 | Py_INCREF(sep_obj); |
| 42 | PyTuple_SET_ITEM(out, 1, sep_obj); |
| 43 | pos += sep_len; |
| 44 | PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str + pos, str_len - pos)); |
| 45 | |
| 46 | if (PyErr_Occurred()) { |
| 47 | Py_DECREF(out); |
| 48 | return NULL; |
| 49 | } |
| 50 | |
| 51 | return out; |
| 52 | } |
| 53 | |
| 54 | Py_LOCAL_INLINE(PyObject*) |
| 55 | stringlib_rpartition( |
| 56 | PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len, |
| 57 | PyObject* sep_obj, const STRINGLIB_CHAR* sep, Py_ssize_t sep_len |
| 58 | ) |
| 59 | { |
| 60 | PyObject* out; |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 61 | Py_ssize_t pos; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 62 | |
| 63 | if (sep_len == 0) { |
| 64 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 65 | return NULL; |
| 66 | } |
| 67 | |
| 68 | out = PyTuple_New(3); |
| 69 | if (!out) |
| 70 | return NULL; |
| 71 | |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 72 | pos = fastsearch(str, str_len, sep, sep_len, FAST_RSEARCH); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 73 | |
| 74 | if (pos < 0) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 75 | Py_INCREF(STRINGLIB_EMPTY); |
| 76 | PyTuple_SET_ITEM(out, 0, (PyObject*) STRINGLIB_EMPTY); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 77 | Py_INCREF(STRINGLIB_EMPTY); |
| 78 | PyTuple_SET_ITEM(out, 1, (PyObject*) STRINGLIB_EMPTY); |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 79 | Py_INCREF(str_obj); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 80 | PyTuple_SET_ITEM(out, 2, (PyObject*) str_obj); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 81 | return out; |
| 82 | } |
| 83 | |
| 84 | PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(str, pos)); |
| 85 | Py_INCREF(sep_obj); |
| 86 | PyTuple_SET_ITEM(out, 1, sep_obj); |
| 87 | pos += sep_len; |
| 88 | PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str + pos, str_len - pos)); |
| 89 | |
| 90 | if (PyErr_Occurred()) { |
| 91 | Py_DECREF(out); |
| 92 | return NULL; |
| 93 | } |
| 94 | |
| 95 | return out; |
| 96 | } |
| 97 | |
| 98 | #endif |
| 99 | |
| 100 | /* |
| 101 | Local variables: |
| 102 | c-basic-offset: 4 |
| 103 | indent-tabs-mode: nil |
| 104 | End: |
| 105 | */ |