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)); |
Victor Stinner | cc64eb5 | 2013-10-29 03:15:37 +0100 | [diff] [blame] | 32 | |
| 33 | if (PyErr_Occurred()) { |
| 34 | Py_DECREF(out); |
| 35 | return NULL; |
| 36 | } |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 37 | #else |
| 38 | Py_INCREF(str_obj); |
| 39 | PyTuple_SET_ITEM(out, 0, (PyObject*) str_obj); |
Victor Stinner | c41eed1 | 2020-06-23 15:54:35 +0200 | [diff] [blame^] | 40 | PyObject *empty = (PyObject*)STRINGLIB_GET_EMPTY(); |
| 41 | assert(empty != NULL); |
| 42 | Py_INCREF(empty); |
| 43 | PyTuple_SET_ITEM(out, 1, empty); |
| 44 | Py_INCREF(empty); |
| 45 | PyTuple_SET_ITEM(out, 2, empty); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 46 | #endif |
| 47 | return out; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(str, pos)); |
| 51 | Py_INCREF(sep_obj); |
| 52 | PyTuple_SET_ITEM(out, 1, sep_obj); |
| 53 | pos += sep_len; |
| 54 | PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str + pos, str_len - pos)); |
| 55 | |
| 56 | if (PyErr_Occurred()) { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 57 | Py_DECREF(out); |
| 58 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | return out; |
| 62 | } |
| 63 | |
| 64 | Py_LOCAL_INLINE(PyObject*) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 65 | STRINGLIB(rpartition)(PyObject* str_obj, |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 66 | const STRINGLIB_CHAR* str, Py_ssize_t str_len, |
| 67 | PyObject* sep_obj, |
| 68 | const STRINGLIB_CHAR* sep, Py_ssize_t sep_len) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 69 | { |
| 70 | PyObject* out; |
Antoine Pitrou | da2ecaf | 2010-01-02 21:40:36 +0000 | [diff] [blame] | 71 | Py_ssize_t pos; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 72 | |
| 73 | if (sep_len == 0) { |
| 74 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 75 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | out = PyTuple_New(3); |
| 79 | if (!out) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 80 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 81 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 82 | pos = FASTSEARCH(str, str_len, sep, sep_len, -1, FAST_RSEARCH); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 83 | |
| 84 | if (pos < 0) { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 85 | #if STRINGLIB_MUTABLE |
| 86 | PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(NULL, 0)); |
| 87 | PyTuple_SET_ITEM(out, 1, STRINGLIB_NEW(NULL, 0)); |
| 88 | PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str, str_len)); |
Victor Stinner | cc64eb5 | 2013-10-29 03:15:37 +0100 | [diff] [blame] | 89 | |
| 90 | if (PyErr_Occurred()) { |
| 91 | Py_DECREF(out); |
| 92 | return NULL; |
| 93 | } |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 94 | #else |
Victor Stinner | c41eed1 | 2020-06-23 15:54:35 +0200 | [diff] [blame^] | 95 | PyObject *empty = (PyObject*)STRINGLIB_GET_EMPTY(); |
| 96 | assert(empty != NULL); |
| 97 | Py_INCREF(empty); |
| 98 | PyTuple_SET_ITEM(out, 0, empty); |
| 99 | Py_INCREF(empty); |
| 100 | PyTuple_SET_ITEM(out, 1, empty); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 101 | Py_INCREF(str_obj); |
| 102 | PyTuple_SET_ITEM(out, 2, (PyObject*) str_obj); |
| 103 | #endif |
| 104 | return out; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(str, pos)); |
| 108 | Py_INCREF(sep_obj); |
| 109 | PyTuple_SET_ITEM(out, 1, sep_obj); |
| 110 | pos += sep_len; |
| 111 | PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str + pos, str_len - pos)); |
| 112 | |
| 113 | if (PyErr_Occurred()) { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 114 | Py_DECREF(out); |
| 115 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | return out; |
| 119 | } |
| 120 | |