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, |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 8 | PyObject* sep_obj, const STRINGLIB_CHAR* sep, Py_ssize_t sep_len) |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 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 | |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 48 | Py_LOCAL(PyObject*) |
| 49 | rpartition(PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len, |
| 50 | PyObject* sep_obj, const STRINGLIB_CHAR* sep, Py_ssize_t sep_len) |
| 51 | { |
| 52 | PyObject* out; |
| 53 | Py_ssize_t pos; |
| 54 | |
| 55 | if (sep_len == 0) { |
| 56 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 57 | return NULL; |
| 58 | } |
| 59 | |
| 60 | out = PyTuple_New(3); |
| 61 | if (!out) |
| 62 | return NULL; |
| 63 | |
| 64 | /* XXX - create reversefastsearch helper! */ |
| 65 | if (sep_len == 0) |
| 66 | pos = str_len; |
| 67 | else { |
| 68 | Py_ssize_t j; |
| 69 | pos = -1; |
| 70 | for (j = str_len - sep_len; j >= 0; --j) |
| 71 | if (STRINGLIB_CMP(str+j, sep, sep_len) == 0) { |
| 72 | pos = j; |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | if (pos < 0) { |
| 78 | Py_INCREF(str_obj); |
| 79 | PyTuple_SET_ITEM(out, 0, (PyObject*) str_obj); |
| 80 | Py_INCREF(STRINGLIB_EMPTY); |
| 81 | PyTuple_SET_ITEM(out, 1, (PyObject*) STRINGLIB_EMPTY); |
| 82 | Py_INCREF(STRINGLIB_EMPTY); |
| 83 | PyTuple_SET_ITEM(out, 2, (PyObject*) STRINGLIB_EMPTY); |
| 84 | return out; |
| 85 | } |
| 86 | |
| 87 | PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(str, pos)); |
| 88 | Py_INCREF(sep_obj); |
| 89 | PyTuple_SET_ITEM(out, 1, sep_obj); |
| 90 | pos += sep_len; |
| 91 | PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str + pos, str_len - pos)); |
| 92 | |
| 93 | if (PyErr_Occurred()) { |
| 94 | Py_DECREF(out); |
| 95 | return NULL; |
| 96 | } |
| 97 | |
| 98 | return out; |
| 99 | } |
| 100 | |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 101 | #endif |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 102 | |
| 103 | /* |
| 104 | Local variables: |
| 105 | c-basic-offset: 4 |
| 106 | indent-tabs-mode: nil |
| 107 | End: |
| 108 | */ |