Coverage Report

Created: 2025-07-11 06:53

/src/cpython3/Objects/stringlib/partition.h
Line
Count
Source (jump to first uncovered line)
1
/* stringlib: partition implementation */
2
3
#ifndef STRINGLIB_FASTSEARCH_H
4
#  error must include "stringlib/fastsearch.h" before including this module
5
#endif
6
7
#if !STRINGLIB_MUTABLE && !defined(STRINGLIB_GET_EMPTY)
8
#  error "STRINGLIB_GET_EMPTY must be defined if STRINGLIB_MUTABLE is zero"
9
#endif
10
11
12
Py_LOCAL_INLINE(PyObject*)
13
STRINGLIB(partition)(PyObject* str_obj,
14
                    const STRINGLIB_CHAR* str, Py_ssize_t str_len,
15
                    PyObject* sep_obj,
16
                    const STRINGLIB_CHAR* sep, Py_ssize_t sep_len)
17
0
{
18
0
    PyObject* out;
19
0
    Py_ssize_t pos;
20
21
0
    if (sep_len == 0) {
22
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
23
0
        return NULL;
24
0
    }
25
26
0
    out = PyTuple_New(3);
27
0
    if (!out)
28
0
        return NULL;
29
30
0
    pos = FASTSEARCH(str, str_len, sep, sep_len, -1, FAST_SEARCH);
31
32
0
    if (pos < 0) {
33
#if STRINGLIB_MUTABLE
34
0
        PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(str, str_len));
35
0
        PyTuple_SET_ITEM(out, 1, STRINGLIB_NEW(NULL, 0));
36
0
        PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(NULL, 0));
37
38
0
        if (PyErr_Occurred()) {
39
0
            Py_DECREF(out);
40
0
            return NULL;
41
0
        }
42
#else
43
0
        Py_INCREF(str_obj);
44
0
        PyTuple_SET_ITEM(out, 0, (PyObject*) str_obj);
45
0
        PyObject *empty = (PyObject*)STRINGLIB_GET_EMPTY();
46
        assert(empty != NULL);
47
0
        Py_INCREF(empty);
48
0
        PyTuple_SET_ITEM(out, 1, empty);
49
0
        Py_INCREF(empty);
50
0
        PyTuple_SET_ITEM(out, 2, empty);
51
#endif
52
0
        return out;
53
0
    }
54
55
0
    PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(str, pos));
56
0
    Py_INCREF(sep_obj);
57
0
    PyTuple_SET_ITEM(out, 1, sep_obj);
58
0
    pos += sep_len;
59
0
    PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str + pos, str_len - pos));
60
61
0
    if (PyErr_Occurred()) {
62
0
        Py_DECREF(out);
63
0
        return NULL;
64
0
    }
65
66
0
    return out;
67
0
}
Unexecuted instantiation: unicodeobject.c:asciilib_partition
Unexecuted instantiation: unicodeobject.c:ucs1lib_partition
Unexecuted instantiation: unicodeobject.c:ucs2lib_partition
Unexecuted instantiation: unicodeobject.c:ucs4lib_partition
Unexecuted instantiation: bytearrayobject.c:stringlib_partition
Unexecuted instantiation: bytesobject.c:stringlib_partition
68
69
Py_LOCAL_INLINE(PyObject*)
70
STRINGLIB(rpartition)(PyObject* str_obj,
71
                     const STRINGLIB_CHAR* str, Py_ssize_t str_len,
72
                     PyObject* sep_obj,
73
                     const STRINGLIB_CHAR* sep, Py_ssize_t sep_len)
74
4.59k
{
75
4.59k
    PyObject* out;
76
4.59k
    Py_ssize_t pos;
77
78
4.59k
    if (sep_len == 0) {
79
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
80
0
        return NULL;
81
0
    }
82
83
4.59k
    out = PyTuple_New(3);
84
4.59k
    if (!out)
85
0
        return NULL;
86
87
4.59k
    pos = FASTSEARCH(str, str_len, sep, sep_len, -1, FAST_RSEARCH);
88
89
4.59k
    if (pos < 0) {
90
#if STRINGLIB_MUTABLE
91
0
        PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(NULL, 0));
92
0
        PyTuple_SET_ITEM(out, 1, STRINGLIB_NEW(NULL, 0));
93
0
        PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str, str_len));
94
95
0
        if (PyErr_Occurred()) {
96
0
            Py_DECREF(out);
97
0
            return NULL;
98
0
        }
99
#else
100
1.78k
        PyObject *empty = (PyObject*)STRINGLIB_GET_EMPTY();
101
        assert(empty != NULL);
102
1.78k
        Py_INCREF(empty);
103
1.78k
        PyTuple_SET_ITEM(out, 0, empty);
104
1.78k
        Py_INCREF(empty);
105
1.78k
        PyTuple_SET_ITEM(out, 1, empty);
106
1.78k
        Py_INCREF(str_obj);
107
1.78k
        PyTuple_SET_ITEM(out, 2, (PyObject*) str_obj);
108
#endif
109
0
        return out;
110
1.78k
    }
111
112
2.81k
    PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(str, pos));
113
2.81k
    Py_INCREF(sep_obj);
114
2.81k
    PyTuple_SET_ITEM(out, 1, sep_obj);
115
2.81k
    pos += sep_len;
116
2.81k
    PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str + pos, str_len - pos));
117
118
2.81k
    if (PyErr_Occurred()) {
119
0
        Py_DECREF(out);
120
0
        return NULL;
121
0
    }
122
123
2.81k
    return out;
124
2.81k
}
unicodeobject.c:asciilib_rpartition
Line
Count
Source
74
4.59k
{
75
4.59k
    PyObject* out;
76
4.59k
    Py_ssize_t pos;
77
78
4.59k
    if (sep_len == 0) {
79
0
        PyErr_SetString(PyExc_ValueError, "empty separator");
80
0
        return NULL;
81
0
    }
82
83
4.59k
    out = PyTuple_New(3);
84
4.59k
    if (!out)
85
0
        return NULL;
86
87
4.59k
    pos = FASTSEARCH(str, str_len, sep, sep_len, -1, FAST_RSEARCH);
88
89
4.59k
    if (pos < 0) {
90
#if STRINGLIB_MUTABLE
91
        PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(NULL, 0));
92
        PyTuple_SET_ITEM(out, 1, STRINGLIB_NEW(NULL, 0));
93
        PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str, str_len));
94
95
        if (PyErr_Occurred()) {
96
            Py_DECREF(out);
97
            return NULL;
98
        }
99
#else
100
1.78k
        PyObject *empty = (PyObject*)STRINGLIB_GET_EMPTY();
101
1.78k
        assert(empty != NULL);
102
1.78k
        Py_INCREF(empty);
103
1.78k
        PyTuple_SET_ITEM(out, 0, empty);
104
1.78k
        Py_INCREF(empty);
105
1.78k
        PyTuple_SET_ITEM(out, 1, empty);
106
1.78k
        Py_INCREF(str_obj);
107
1.78k
        PyTuple_SET_ITEM(out, 2, (PyObject*) str_obj);
108
1.78k
#endif
109
1.78k
        return out;
110
1.78k
    }
111
112
2.81k
    PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(str, pos));
113
2.81k
    Py_INCREF(sep_obj);
114
2.81k
    PyTuple_SET_ITEM(out, 1, sep_obj);
115
2.81k
    pos += sep_len;
116
2.81k
    PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str + pos, str_len - pos));
117
118
2.81k
    if (PyErr_Occurred()) {
119
0
        Py_DECREF(out);
120
0
        return NULL;
121
0
    }
122
123
2.81k
    return out;
124
2.81k
}
Unexecuted instantiation: unicodeobject.c:ucs1lib_rpartition
Unexecuted instantiation: unicodeobject.c:ucs2lib_rpartition
Unexecuted instantiation: unicodeobject.c:ucs4lib_rpartition
Unexecuted instantiation: bytearrayobject.c:stringlib_rpartition
Unexecuted instantiation: bytesobject.c:stringlib_rpartition
125