partition.h 2.84 KB
Newer Older
1 2 3 4 5 6 7
/* stringlib: partition implementation */

#ifndef STRINGLIB_FASTSEARCH_H
#error must include "stringlib/fastsearch.h" before including this module
#endif

Py_LOCAL_INLINE(PyObject*)
Martin v. Löwis's avatar
Martin v. Löwis committed
8
STRINGLIB(partition)(PyObject* str_obj,
9 10 11
                    const STRINGLIB_CHAR* str, Py_ssize_t str_len,
                    PyObject* sep_obj,
                    const STRINGLIB_CHAR* sep, Py_ssize_t sep_len)
12 13 14 15 16 17
{
    PyObject* out;
    Py_ssize_t pos;

    if (sep_len == 0) {
        PyErr_SetString(PyExc_ValueError, "empty separator");
18
        return NULL;
19 20 21 22
    }

    out = PyTuple_New(3);
    if (!out)
23
        return NULL;
24

Martin v. Löwis's avatar
Martin v. Löwis committed
25
    pos = FASTSEARCH(str, str_len, sep, sep_len, -1, FAST_SEARCH);
26 27

    if (pos < 0) {
28 29 30 31 32 33 34 35 36 37 38 39 40
#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;
41 42 43 44 45 46 47 48 49
    }

    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()) {
50 51
        Py_DECREF(out);
        return NULL;
52 53 54 55 56 57
    }

    return out;
}

Py_LOCAL_INLINE(PyObject*)
Martin v. Löwis's avatar
Martin v. Löwis committed
58
STRINGLIB(rpartition)(PyObject* str_obj,
59 60 61
                     const STRINGLIB_CHAR* str, Py_ssize_t str_len,
                     PyObject* sep_obj,
                     const STRINGLIB_CHAR* sep, Py_ssize_t sep_len)
62 63
{
    PyObject* out;
64
    Py_ssize_t pos;
65 66 67

    if (sep_len == 0) {
        PyErr_SetString(PyExc_ValueError, "empty separator");
68
        return NULL;
69 70 71 72
    }

    out = PyTuple_New(3);
    if (!out)
73
        return NULL;
74

Martin v. Löwis's avatar
Martin v. Löwis committed
75
    pos = FASTSEARCH(str, str_len, sep, sep_len, -1, FAST_RSEARCH);
76 77

    if (pos < 0) {
78 79 80 81 82 83 84 85 86 87 88 89 90
#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;
91 92 93 94 95 96 97 98 99
    }

    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()) {
100 101
        Py_DECREF(out);
        return NULL;
102 103 104 105 106
    }

    return out;
}