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