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