blob: 3731df56987fd50c592336ebea86972e1fce8863 [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));
Victor Stinnercc64eb52013-10-29 03:15:37 +010032
33 if (PyErr_Occurred()) {
34 Py_DECREF(out);
35 return NULL;
36 }
Antoine Pitrouf2c54842010-01-13 08:07:53 +000037#else
38 Py_INCREF(str_obj);
39 PyTuple_SET_ITEM(out, 0, (PyObject*) str_obj);
Victor Stinnerc41eed12020-06-23 15:54:35 +020040 PyObject *empty = (PyObject*)STRINGLIB_GET_EMPTY();
41 assert(empty != NULL);
42 Py_INCREF(empty);
43 PyTuple_SET_ITEM(out, 1, empty);
44 Py_INCREF(empty);
45 PyTuple_SET_ITEM(out, 2, empty);
Antoine Pitrouf2c54842010-01-13 08:07:53 +000046#endif
47 return out;
Thomas Wouters477c8d52006-05-27 19:21:47 +000048 }
49
50 PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(str, pos));
51 Py_INCREF(sep_obj);
52 PyTuple_SET_ITEM(out, 1, sep_obj);
53 pos += sep_len;
54 PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str + pos, str_len - pos));
55
56 if (PyErr_Occurred()) {
Antoine Pitrouf2c54842010-01-13 08:07:53 +000057 Py_DECREF(out);
58 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +000059 }
60
61 return out;
62}
63
64Py_LOCAL_INLINE(PyObject*)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020065STRINGLIB(rpartition)(PyObject* str_obj,
Antoine Pitrouf2c54842010-01-13 08:07:53 +000066 const STRINGLIB_CHAR* str, Py_ssize_t str_len,
67 PyObject* sep_obj,
68 const STRINGLIB_CHAR* sep, Py_ssize_t sep_len)
Thomas Wouters477c8d52006-05-27 19:21:47 +000069{
70 PyObject* out;
Antoine Pitrouda2ecaf2010-01-02 21:40:36 +000071 Py_ssize_t pos;
Thomas Wouters477c8d52006-05-27 19:21:47 +000072
73 if (sep_len == 0) {
74 PyErr_SetString(PyExc_ValueError, "empty separator");
Antoine Pitrouf2c54842010-01-13 08:07:53 +000075 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +000076 }
77
78 out = PyTuple_New(3);
79 if (!out)
Antoine Pitrouf2c54842010-01-13 08:07:53 +000080 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +000081
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020082 pos = FASTSEARCH(str, str_len, sep, sep_len, -1, FAST_RSEARCH);
Thomas Wouters477c8d52006-05-27 19:21:47 +000083
84 if (pos < 0) {
Antoine Pitrouf2c54842010-01-13 08:07:53 +000085#if STRINGLIB_MUTABLE
86 PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(NULL, 0));
87 PyTuple_SET_ITEM(out, 1, STRINGLIB_NEW(NULL, 0));
88 PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str, str_len));
Victor Stinnercc64eb52013-10-29 03:15:37 +010089
90 if (PyErr_Occurred()) {
91 Py_DECREF(out);
92 return NULL;
93 }
Antoine Pitrouf2c54842010-01-13 08:07:53 +000094#else
Victor Stinnerc41eed12020-06-23 15:54:35 +020095 PyObject *empty = (PyObject*)STRINGLIB_GET_EMPTY();
96 assert(empty != NULL);
97 Py_INCREF(empty);
98 PyTuple_SET_ITEM(out, 0, empty);
99 Py_INCREF(empty);
100 PyTuple_SET_ITEM(out, 1, empty);
Antoine Pitrouf2c54842010-01-13 08:07:53 +0000101 Py_INCREF(str_obj);
102 PyTuple_SET_ITEM(out, 2, (PyObject*) str_obj);
103#endif
104 return out;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000105 }
106
107 PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(str, pos));
108 Py_INCREF(sep_obj);
109 PyTuple_SET_ITEM(out, 1, sep_obj);
110 pos += sep_len;
111 PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str + pos, str_len - pos));
112
113 if (PyErr_Occurred()) {
Antoine Pitrouf2c54842010-01-13 08:07:53 +0000114 Py_DECREF(out);
115 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000116 }
117
118 return out;
119}
120