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 | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 6 | #ifndef STRINGLIB_FASTSEARCH_H |
| 7 | #error must include "stringlib/fastsearch.h" before including this module |
| 8 | #endif |
| 9 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 10 | Py_LOCAL_INLINE(PyObject*) |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 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 | ) |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 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 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 54 | Py_LOCAL_INLINE(PyObject*) |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 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 | ) |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 59 | { |
| 60 | PyObject* out; |
Neal Norwitz | d1b6cd7 | 2006-05-27 05:21:30 +0000 | [diff] [blame] | 61 | Py_ssize_t pos, j; |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +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 | |
| 72 | /* XXX - create reversefastsearch helper! */ |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 73 | pos = -1; |
| 74 | for (j = str_len - sep_len; j >= 0; --j) |
| 75 | if (STRINGLIB_CMP(str+j, sep, sep_len) == 0) { |
| 76 | pos = j; |
| 77 | break; |
| 78 | } |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 79 | |
| 80 | if (pos < 0) { |
Raymond Hettinger | a0c95fa | 2006-09-04 15:32:48 +0000 | [diff] [blame] | 81 | Py_INCREF(STRINGLIB_EMPTY); |
| 82 | PyTuple_SET_ITEM(out, 0, (PyObject*) STRINGLIB_EMPTY); |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 83 | Py_INCREF(STRINGLIB_EMPTY); |
| 84 | PyTuple_SET_ITEM(out, 1, (PyObject*) STRINGLIB_EMPTY); |
Raymond Hettinger | a0c95fa | 2006-09-04 15:32:48 +0000 | [diff] [blame] | 85 | Py_INCREF(str_obj); |
| 86 | PyTuple_SET_ITEM(out, 2, (PyObject*) str_obj); |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 87 | return out; |
| 88 | } |
| 89 | |
| 90 | PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(str, pos)); |
| 91 | Py_INCREF(sep_obj); |
| 92 | PyTuple_SET_ITEM(out, 1, sep_obj); |
| 93 | pos += sep_len; |
| 94 | PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str + pos, str_len - pos)); |
| 95 | |
| 96 | if (PyErr_Occurred()) { |
| 97 | Py_DECREF(out); |
| 98 | return NULL; |
| 99 | } |
| 100 | |
| 101 | return out; |
| 102 | } |
| 103 | |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 104 | #endif |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 105 | |
| 106 | /* |
| 107 | Local variables: |
| 108 | c-basic-offset: 4 |
| 109 | indent-tabs-mode: nil |
| 110 | End: |
| 111 | */ |