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