+++ /dev/null
-/* stringlib: partition implementation */\r
-\r
-#ifndef STRINGLIB_PARTITION_H\r
-#define STRINGLIB_PARTITION_H\r
-\r
-#ifndef STRINGLIB_FASTSEARCH_H\r
-#error must include "stringlib/fastsearch.h" before including this module\r
-#endif\r
-\r
-Py_LOCAL_INLINE(PyObject*)\r
-stringlib_partition(PyObject* str_obj,\r
- const STRINGLIB_CHAR* str, Py_ssize_t str_len,\r
- PyObject* sep_obj,\r
- const STRINGLIB_CHAR* sep, Py_ssize_t sep_len)\r
-{\r
- PyObject* out;\r
- Py_ssize_t pos;\r
-\r
- if (sep_len == 0) {\r
- PyErr_SetString(PyExc_ValueError, "empty separator");\r
- return NULL;\r
- }\r
-\r
- out = PyTuple_New(3);\r
- if (!out)\r
- return NULL;\r
-\r
- pos = fastsearch(str, str_len, sep, sep_len, -1, FAST_SEARCH);\r
-\r
- if (pos < 0) {\r
-#if STRINGLIB_MUTABLE\r
- PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(str, str_len));\r
- PyTuple_SET_ITEM(out, 1, STRINGLIB_NEW(NULL, 0));\r
- PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(NULL, 0));\r
-#else\r
- Py_INCREF(str_obj);\r
- PyTuple_SET_ITEM(out, 0, (PyObject*) str_obj);\r
- Py_INCREF(STRINGLIB_EMPTY);\r
- PyTuple_SET_ITEM(out, 1, (PyObject*) STRINGLIB_EMPTY);\r
- Py_INCREF(STRINGLIB_EMPTY);\r
- PyTuple_SET_ITEM(out, 2, (PyObject*) STRINGLIB_EMPTY);\r
-#endif\r
- return out;\r
- }\r
-\r
- PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(str, pos));\r
- Py_INCREF(sep_obj);\r
- PyTuple_SET_ITEM(out, 1, sep_obj);\r
- pos += sep_len;\r
- PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str + pos, str_len - pos));\r
-\r
- if (PyErr_Occurred()) {\r
- Py_DECREF(out);\r
- return NULL;\r
- }\r
-\r
- return out;\r
-}\r
-\r
-Py_LOCAL_INLINE(PyObject*)\r
-stringlib_rpartition(PyObject* str_obj,\r
- const STRINGLIB_CHAR* str, Py_ssize_t str_len,\r
- PyObject* sep_obj,\r
- const STRINGLIB_CHAR* sep, Py_ssize_t sep_len)\r
-{\r
- PyObject* out;\r
- Py_ssize_t pos;\r
-\r
- if (sep_len == 0) {\r
- PyErr_SetString(PyExc_ValueError, "empty separator");\r
- return NULL;\r
- }\r
-\r
- out = PyTuple_New(3);\r
- if (!out)\r
- return NULL;\r
-\r
- pos = fastsearch(str, str_len, sep, sep_len, -1, FAST_RSEARCH);\r
-\r
- if (pos < 0) {\r
-#if STRINGLIB_MUTABLE\r
- PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(NULL, 0));\r
- PyTuple_SET_ITEM(out, 1, STRINGLIB_NEW(NULL, 0));\r
- PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str, str_len));\r
-#else\r
- Py_INCREF(STRINGLIB_EMPTY);\r
- PyTuple_SET_ITEM(out, 0, (PyObject*) STRINGLIB_EMPTY);\r
- Py_INCREF(STRINGLIB_EMPTY);\r
- PyTuple_SET_ITEM(out, 1, (PyObject*) STRINGLIB_EMPTY);\r
- Py_INCREF(str_obj);\r
- PyTuple_SET_ITEM(out, 2, (PyObject*) str_obj);\r
-#endif\r
- return out;\r
- }\r
-\r
- PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(str, pos));\r
- Py_INCREF(sep_obj);\r
- PyTuple_SET_ITEM(out, 1, sep_obj);\r
- pos += sep_len;\r
- PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str + pos, str_len - pos));\r
-\r
- if (PyErr_Occurred()) {\r
- Py_DECREF(out);\r
- return NULL;\r
- }\r
-\r
- return out;\r
-}\r
-\r
-#endif\r