2006-05-26 14:22:38 -03:00
|
|
|
/* stringlib: partition implementation */
|
|
|
|
|
|
|
|
#ifndef STRINGLIB_PARTITION_H
|
|
|
|
#define STRINGLIB_PARTITION_H
|
|
|
|
|
2006-05-26 16:24:53 -03:00
|
|
|
#ifndef STRINGLIB_FASTSEARCH_H
|
|
|
|
#error must include "stringlib/fastsearch.h" before including this module
|
|
|
|
#endif
|
|
|
|
|
2006-05-27 11:58:20 -03:00
|
|
|
Py_LOCAL_INLINE(PyObject*)
|
2010-01-13 03:55:48 -04:00
|
|
|
stringlib_partition(PyObject* str_obj,
|
|
|
|
const STRINGLIB_CHAR* str, Py_ssize_t str_len,
|
|
|
|
PyObject* sep_obj,
|
|
|
|
const STRINGLIB_CHAR* sep, Py_ssize_t sep_len)
|
2006-05-26 14:22:38 -03:00
|
|
|
{
|
|
|
|
PyObject* out;
|
|
|
|
Py_ssize_t pos;
|
|
|
|
|
|
|
|
if (sep_len == 0) {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "empty separator");
|
2010-01-13 03:55:48 -04:00
|
|
|
return NULL;
|
2006-05-26 14:22:38 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
out = PyTuple_New(3);
|
|
|
|
if (!out)
|
2010-01-13 03:55:48 -04:00
|
|
|
return NULL;
|
2006-05-26 14:22:38 -03:00
|
|
|
|
2010-01-13 03:55:48 -04:00
|
|
|
pos = fastsearch(str, str_len, sep, sep_len, -1, FAST_SEARCH);
|
2006-05-26 14:22:38 -03:00
|
|
|
|
|
|
|
if (pos < 0) {
|
2010-01-13 03:55:48 -04:00
|
|
|
#if STRINGLIB_MUTABLE
|
|
|
|
PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(str, str_len));
|
|
|
|
PyTuple_SET_ITEM(out, 1, STRINGLIB_NEW(NULL, 0));
|
|
|
|
PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(NULL, 0));
|
|
|
|
#else
|
|
|
|
Py_INCREF(str_obj);
|
|
|
|
PyTuple_SET_ITEM(out, 0, (PyObject*) str_obj);
|
|
|
|
Py_INCREF(STRINGLIB_EMPTY);
|
|
|
|
PyTuple_SET_ITEM(out, 1, (PyObject*) STRINGLIB_EMPTY);
|
|
|
|
Py_INCREF(STRINGLIB_EMPTY);
|
|
|
|
PyTuple_SET_ITEM(out, 2, (PyObject*) STRINGLIB_EMPTY);
|
|
|
|
#endif
|
|
|
|
return out;
|
2006-05-26 14:22:38 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(str, pos));
|
|
|
|
Py_INCREF(sep_obj);
|
|
|
|
PyTuple_SET_ITEM(out, 1, sep_obj);
|
|
|
|
pos += sep_len;
|
|
|
|
PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str + pos, str_len - pos));
|
|
|
|
|
|
|
|
if (PyErr_Occurred()) {
|
2010-01-13 03:55:48 -04:00
|
|
|
Py_DECREF(out);
|
|
|
|
return NULL;
|
2006-05-26 14:22:38 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2006-05-27 11:58:20 -03:00
|
|
|
Py_LOCAL_INLINE(PyObject*)
|
2010-01-13 03:55:48 -04:00
|
|
|
stringlib_rpartition(PyObject* str_obj,
|
|
|
|
const STRINGLIB_CHAR* str, Py_ssize_t str_len,
|
|
|
|
PyObject* sep_obj,
|
|
|
|
const STRINGLIB_CHAR* sep, Py_ssize_t sep_len)
|
2006-05-26 15:15:38 -03:00
|
|
|
{
|
|
|
|
PyObject* out;
|
2010-01-02 17:12:58 -04:00
|
|
|
Py_ssize_t pos;
|
2006-05-26 15:15:38 -03:00
|
|
|
|
|
|
|
if (sep_len == 0) {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "empty separator");
|
2010-01-13 03:55:48 -04:00
|
|
|
return NULL;
|
2006-05-26 15:15:38 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
out = PyTuple_New(3);
|
|
|
|
if (!out)
|
2010-01-13 03:55:48 -04:00
|
|
|
return NULL;
|
2006-05-26 15:15:38 -03:00
|
|
|
|
2010-01-13 03:55:48 -04:00
|
|
|
pos = fastsearch(str, str_len, sep, sep_len, -1, FAST_RSEARCH);
|
2006-05-26 15:15:38 -03:00
|
|
|
|
|
|
|
if (pos < 0) {
|
2010-01-13 03:55:48 -04:00
|
|
|
#if STRINGLIB_MUTABLE
|
|
|
|
PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(NULL, 0));
|
|
|
|
PyTuple_SET_ITEM(out, 1, STRINGLIB_NEW(NULL, 0));
|
|
|
|
PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str, str_len));
|
|
|
|
#else
|
|
|
|
Py_INCREF(STRINGLIB_EMPTY);
|
|
|
|
PyTuple_SET_ITEM(out, 0, (PyObject*) STRINGLIB_EMPTY);
|
|
|
|
Py_INCREF(STRINGLIB_EMPTY);
|
|
|
|
PyTuple_SET_ITEM(out, 1, (PyObject*) STRINGLIB_EMPTY);
|
|
|
|
Py_INCREF(str_obj);
|
|
|
|
PyTuple_SET_ITEM(out, 2, (PyObject*) str_obj);
|
|
|
|
#endif
|
|
|
|
return out;
|
2006-05-26 15:15:38 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(str, pos));
|
|
|
|
Py_INCREF(sep_obj);
|
|
|
|
PyTuple_SET_ITEM(out, 1, sep_obj);
|
|
|
|
pos += sep_len;
|
|
|
|
PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str + pos, str_len - pos));
|
|
|
|
|
|
|
|
if (PyErr_Occurred()) {
|
2010-01-13 03:55:48 -04:00
|
|
|
Py_DECREF(out);
|
|
|
|
return NULL;
|
2006-05-26 15:15:38 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2006-05-26 14:22:38 -03:00
|
|
|
#endif
|