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