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