blob: ed32a6f2b382ec14bb4b46f03037001fe6ed2fec [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);
40 Py_INCREF(STRINGLIB_EMPTY);
41 PyTuple_SET_ITEM(out, 1, (PyObject*) STRINGLIB_EMPTY);
42 Py_INCREF(STRINGLIB_EMPTY);
43 PyTuple_SET_ITEM(out, 2, (PyObject*) STRINGLIB_EMPTY);
44#endif
45 return out;
Thomas Wouters477c8d52006-05-27 19:21:47 +000046 }
47
48 PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(str, pos));
49 Py_INCREF(sep_obj);
50 PyTuple_SET_ITEM(out, 1, sep_obj);
51 pos += sep_len;
52 PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str + pos, str_len - pos));
53
54 if (PyErr_Occurred()) {
Antoine Pitrouf2c54842010-01-13 08:07:53 +000055 Py_DECREF(out);
56 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +000057 }
58
59 return out;
60}
61
62Py_LOCAL_INLINE(PyObject*)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020063STRINGLIB(rpartition)(PyObject* str_obj,
Antoine Pitrouf2c54842010-01-13 08:07:53 +000064 const STRINGLIB_CHAR* str, Py_ssize_t str_len,
65 PyObject* sep_obj,
66 const STRINGLIB_CHAR* sep, Py_ssize_t sep_len)
Thomas Wouters477c8d52006-05-27 19:21:47 +000067{
68 PyObject* out;
Antoine Pitrouda2ecaf2010-01-02 21:40:36 +000069 Py_ssize_t pos;
Thomas Wouters477c8d52006-05-27 19:21:47 +000070
71 if (sep_len == 0) {
72 PyErr_SetString(PyExc_ValueError, "empty separator");
Antoine Pitrouf2c54842010-01-13 08:07:53 +000073 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +000074 }
75
76 out = PyTuple_New(3);
77 if (!out)
Antoine Pitrouf2c54842010-01-13 08:07:53 +000078 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +000079
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020080 pos = FASTSEARCH(str, str_len, sep, sep_len, -1, FAST_RSEARCH);
Thomas Wouters477c8d52006-05-27 19:21:47 +000081
82 if (pos < 0) {
Antoine Pitrouf2c54842010-01-13 08:07:53 +000083#if STRINGLIB_MUTABLE
84 PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(NULL, 0));
85 PyTuple_SET_ITEM(out, 1, STRINGLIB_NEW(NULL, 0));
86 PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str, str_len));
Victor Stinnercc64eb52013-10-29 03:15:37 +010087
88 if (PyErr_Occurred()) {
89 Py_DECREF(out);
90 return NULL;
91 }
Antoine Pitrouf2c54842010-01-13 08:07:53 +000092#else
93 Py_INCREF(STRINGLIB_EMPTY);
94 PyTuple_SET_ITEM(out, 0, (PyObject*) STRINGLIB_EMPTY);
95 Py_INCREF(STRINGLIB_EMPTY);
96 PyTuple_SET_ITEM(out, 1, (PyObject*) STRINGLIB_EMPTY);
97 Py_INCREF(str_obj);
98 PyTuple_SET_ITEM(out, 2, (PyObject*) str_obj);
99#endif
100 return out;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000101 }
102
103 PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(str, pos));
104 Py_INCREF(sep_obj);
105 PyTuple_SET_ITEM(out, 1, sep_obj);
106 pos += sep_len;
107 PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str + pos, str_len - pos));
108
109 if (PyErr_Occurred()) {
Antoine Pitrouf2c54842010-01-13 08:07:53 +0000110 Py_DECREF(out);
111 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000112 }
113
114 return out;
115}
116