blob: 40cb5129d5d9780926092f5901aba881ee600149 [file] [log] [blame]
Thomas Wouters477c8d52006-05-27 19:21:47 +00001/* stringlib: partition implementation */
2
Thomas Wouters477c8d52006-05-27 19:21:47 +00003#ifndef STRINGLIB_FASTSEARCH_H
4#error must include "stringlib/fastsearch.h" before including this module
5#endif
6
7Py_LOCAL_INLINE(PyObject*)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02008STRINGLIB(partition)(PyObject* str_obj,
Antoine Pitrouf2c54842010-01-13 08:07:53 +00009 const STRINGLIB_CHAR* str, Py_ssize_t str_len,
10 PyObject* sep_obj,
11 const STRINGLIB_CHAR* sep, Py_ssize_t sep_len)
Thomas Wouters477c8d52006-05-27 19:21:47 +000012{
13 PyObject* out;
14 Py_ssize_t pos;
15
16 if (sep_len == 0) {
17 PyErr_SetString(PyExc_ValueError, "empty separator");
Antoine Pitrouf2c54842010-01-13 08:07:53 +000018 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +000019 }
20
21 out = PyTuple_New(3);
22 if (!out)
Antoine Pitrouf2c54842010-01-13 08:07:53 +000023 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +000024
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020025 pos = FASTSEARCH(str, str_len, sep, sep_len, -1, FAST_SEARCH);
Thomas Wouters477c8d52006-05-27 19:21:47 +000026
27 if (pos < 0) {
Antoine Pitrouf2c54842010-01-13 08:07:53 +000028#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));
32#else
33 Py_INCREF(str_obj);
34 PyTuple_SET_ITEM(out, 0, (PyObject*) str_obj);
35 Py_INCREF(STRINGLIB_EMPTY);
36 PyTuple_SET_ITEM(out, 1, (PyObject*) STRINGLIB_EMPTY);
37 Py_INCREF(STRINGLIB_EMPTY);
38 PyTuple_SET_ITEM(out, 2, (PyObject*) STRINGLIB_EMPTY);
39#endif
40 return out;
Thomas Wouters477c8d52006-05-27 19:21:47 +000041 }
42
43 PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(str, pos));
44 Py_INCREF(sep_obj);
45 PyTuple_SET_ITEM(out, 1, sep_obj);
46 pos += sep_len;
47 PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str + pos, str_len - pos));
48
49 if (PyErr_Occurred()) {
Antoine Pitrouf2c54842010-01-13 08:07:53 +000050 Py_DECREF(out);
51 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +000052 }
53
54 return out;
55}
56
57Py_LOCAL_INLINE(PyObject*)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020058STRINGLIB(rpartition)(PyObject* str_obj,
Antoine Pitrouf2c54842010-01-13 08:07:53 +000059 const STRINGLIB_CHAR* str, Py_ssize_t str_len,
60 PyObject* sep_obj,
61 const STRINGLIB_CHAR* sep, Py_ssize_t sep_len)
Thomas Wouters477c8d52006-05-27 19:21:47 +000062{
63 PyObject* out;
Antoine Pitrouda2ecaf2010-01-02 21:40:36 +000064 Py_ssize_t pos;
Thomas Wouters477c8d52006-05-27 19:21:47 +000065
66 if (sep_len == 0) {
67 PyErr_SetString(PyExc_ValueError, "empty separator");
Antoine Pitrouf2c54842010-01-13 08:07:53 +000068 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +000069 }
70
71 out = PyTuple_New(3);
72 if (!out)
Antoine Pitrouf2c54842010-01-13 08:07:53 +000073 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +000074
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020075 pos = FASTSEARCH(str, str_len, sep, sep_len, -1, FAST_RSEARCH);
Thomas Wouters477c8d52006-05-27 19:21:47 +000076
77 if (pos < 0) {
Antoine Pitrouf2c54842010-01-13 08:07:53 +000078#if STRINGLIB_MUTABLE
79 PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(NULL, 0));
80 PyTuple_SET_ITEM(out, 1, STRINGLIB_NEW(NULL, 0));
81 PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str, str_len));
82#else
83 Py_INCREF(STRINGLIB_EMPTY);
84 PyTuple_SET_ITEM(out, 0, (PyObject*) STRINGLIB_EMPTY);
85 Py_INCREF(STRINGLIB_EMPTY);
86 PyTuple_SET_ITEM(out, 1, (PyObject*) STRINGLIB_EMPTY);
87 Py_INCREF(str_obj);
88 PyTuple_SET_ITEM(out, 2, (PyObject*) str_obj);
89#endif
90 return out;
Thomas Wouters477c8d52006-05-27 19:21:47 +000091 }
92
93 PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(str, pos));
94 Py_INCREF(sep_obj);
95 PyTuple_SET_ITEM(out, 1, sep_obj);
96 pos += sep_len;
97 PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str + pos, str_len - pos));
98
99 if (PyErr_Occurred()) {
Antoine Pitrouf2c54842010-01-13 08:07:53 +0000100 Py_DECREF(out);
101 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000102 }
103
104 return out;
105}
106