Showing the changes to sys/src/cmd/python/Objects/stringlib/partition.h (1 of 3706 files). Show all
Unified · Split
| @@ -0,0 +1,111 @@ | ||
| 1 | +/* stringlib: partition implementation */ | |
| 2 | + | |
| 3 | +#ifndef STRINGLIB_PARTITION_H | |
| 4 | +#define STRINGLIB_PARTITION_H | |
| 5 | + | |
| 6 | +#ifndef STRINGLIB_FASTSEARCH_H | |
| 7 | +#error must include "stringlib/fastsearch.h" before including this module | |
| 8 | +#endif | |
| 9 | + | |
| 10 | +Py_LOCAL_INLINE(PyObject*) | |
| 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 | + ) | |
| 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 | + | |
| 54 | +Py_LOCAL_INLINE(PyObject*) | |
| 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 | + ) | |
| 59 | +{ | |
| 60 | + PyObject* out; | |
| 61 | + Py_ssize_t pos, j; | |
| 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! */ | |
| 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 | + } | |
| 79 | + | |
| 80 | + if (pos < 0) { | |
| 81 | + Py_INCREF(STRINGLIB_EMPTY); | |
| 82 | + PyTuple_SET_ITEM(out, 0, (PyObject*) STRINGLIB_EMPTY); | |
| 83 | + Py_INCREF(STRINGLIB_EMPTY); | |
| 84 | + PyTuple_SET_ITEM(out, 1, (PyObject*) STRINGLIB_EMPTY); | |
| 85 | + Py_INCREF(str_obj); | |
| 86 | + PyTuple_SET_ITEM(out, 2, (PyObject*) str_obj); | |
| 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 | + | |
| 104 | +#endif | |
| 105 | + | |
| 106 | +/* | |
| 107 | +Local variables: | |
| 108 | +c-basic-offset: 4 | |
| 109 | +indent-tabs-mode: nil | |
| 110 | +End: | |
| 111 | +*/ | |