Coverage Report

Created: 2022-07-08 09:39

/home/mdboom/Work/builds/cpython/Objects/stringlib/find.h
Line
Count
Source (jump to first uncovered line)
1
/* stringlib: find/index implementation */
2
3
#ifndef STRINGLIB_FASTSEARCH_H
4
#error must include "stringlib/fastsearch.h" before including this module
5
#endif
6
7
Py_LOCAL_INLINE(Py_ssize_t)
8
STRINGLIB(find)(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
9
               const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
10
               Py_ssize_t offset)
11
{
12
    Py_ssize_t pos;
13
14
    assert(str_len >= 0);
15
    if (sub_len == 0)
  Branch (15:9): [True: 2.42k, False: 825k]
  Branch (15:9): [True: 0, False: 3.80k]
  Branch (15:9): [True: 17, False: 4.42k]
  Branch (15:9): [True: 958, False: 832k]
  Branch (15:9): [True: 5.15k, False: 1.40M]
  Branch (15:9): [True: 0, False: 390]
  Branch (15:9): [True: 0, False: 46]
16
        return offset;
17
18
    pos = FASTSEARCH(str, str_len, sub, sub_len, -1, FAST_SEARCH);
19
20
    if (pos >= 0)
  Branch (20:9): [True: 29.5k, False: 795k]
  Branch (20:9): [True: 3.79k, False: 8]
  Branch (20:9): [True: 4.24k, False: 181]
  Branch (20:9): [True: 616k, False: 215k]
  Branch (20:9): [True: 53.6k, False: 1.35M]
  Branch (20:9): [True: 354, False: 36]
  Branch (20:9): [True: 31, False: 15]
21
        pos += offset;
22
23
    return pos;
24
}
bytes_methods.c:stringlib_find
Line
Count
Source
11
{
12
    Py_ssize_t pos;
13
14
    assert(str_len >= 0);
15
    if (sub_len == 0)
  Branch (15:9): [True: 2.42k, False: 825k]
16
        return offset;
17
18
    pos = FASTSEARCH(str, str_len, sub, sub_len, -1, FAST_SEARCH);
19
20
    if (pos >= 0)
  Branch (20:9): [True: 29.5k, False: 795k]
21
        pos += offset;
22
23
    return pos;
24
}
bytearrayobject.c:stringlib_find
Line
Count
Source
11
{
12
    Py_ssize_t pos;
13
14
    assert(str_len >= 0);
15
    if (sub_len == 0)
  Branch (15:9): [True: 0, False: 3.80k]
16
        return offset;
17
18
    pos = FASTSEARCH(str, str_len, sub, sub_len, -1, FAST_SEARCH);
19
20
    if (pos >= 0)
  Branch (20:9): [True: 3.79k, False: 8]
21
        pos += offset;
22
23
    return pos;
24
}
bytesobject.c:stringlib_find
Line
Count
Source
11
{
12
    Py_ssize_t pos;
13
14
    assert(str_len >= 0);
15
    if (sub_len == 0)
  Branch (15:9): [True: 17, False: 4.42k]
16
        return offset;
17
18
    pos = FASTSEARCH(str, str_len, sub, sub_len, -1, FAST_SEARCH);
19
20
    if (pos >= 0)
  Branch (20:9): [True: 4.24k, False: 181]
21
        pos += offset;
22
23
    return pos;
24
}
unicodeobject.c:asciilib_find
Line
Count
Source
11
{
12
    Py_ssize_t pos;
13
14
    assert(str_len >= 0);
15
    if (sub_len == 0)
  Branch (15:9): [True: 958, False: 832k]
16
        return offset;
17
18
    pos = FASTSEARCH(str, str_len, sub, sub_len, -1, FAST_SEARCH);
19
20
    if (pos >= 0)
  Branch (20:9): [True: 616k, False: 215k]
21
        pos += offset;
22
23
    return pos;
24
}
unicodeobject.c:ucs1lib_find
Line
Count
Source
11
{
12
    Py_ssize_t pos;
13
14
    assert(str_len >= 0);
15
    if (sub_len == 0)
  Branch (15:9): [True: 5.15k, False: 1.40M]
16
        return offset;
17
18
    pos = FASTSEARCH(str, str_len, sub, sub_len, -1, FAST_SEARCH);
19
20
    if (pos >= 0)
  Branch (20:9): [True: 53.6k, False: 1.35M]
21
        pos += offset;
22
23
    return pos;
24
}
unicodeobject.c:ucs2lib_find
Line
Count
Source
11
{
12
    Py_ssize_t pos;
13
14
    assert(str_len >= 0);
15
    if (sub_len == 0)
  Branch (15:9): [True: 0, False: 390]
16
        return offset;
17
18
    pos = FASTSEARCH(str, str_len, sub, sub_len, -1, FAST_SEARCH);
19
20
    if (pos >= 0)
  Branch (20:9): [True: 354, False: 36]
21
        pos += offset;
22
23
    return pos;
24
}
unicodeobject.c:ucs4lib_find
Line
Count
Source
11
{
12
    Py_ssize_t pos;
13
14
    assert(str_len >= 0);
15
    if (sub_len == 0)
  Branch (15:9): [True: 0, False: 46]
16
        return offset;
17
18
    pos = FASTSEARCH(str, str_len, sub, sub_len, -1, FAST_SEARCH);
19
20
    if (pos >= 0)
  Branch (20:9): [True: 31, False: 15]
21
        pos += offset;
22
23
    return pos;
24
}
25
26
Py_LOCAL_INLINE(Py_ssize_t)
27
STRINGLIB(rfind)(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
28
                const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
29
                Py_ssize_t offset)
30
{
31
    Py_ssize_t pos;
32
33
    assert(str_len >= 0);
34
    if (sub_len == 0)
  Branch (34:9): [True: 736, False: 196k]
  Branch (34:9): [True: 0, False: 7]
  Branch (34:9): [True: 740, False: 196k]
  Branch (34:9): [True: 0, False: 0]
  Branch (34:9): [True: 0, False: 2]
  Branch (34:9): [True: 0, False: 4]
35
        return str_len + offset;
36
37
    pos = FASTSEARCH(str, str_len, sub, sub_len, -1, FAST_RSEARCH);
38
39
    if (pos >= 0)
  Branch (39:9): [True: 5.68k, False: 190k]
  Branch (39:9): [True: 6, False: 1]
  Branch (39:9): [True: 5.68k, False: 190k]
  Branch (39:9): [True: 0, False: 0]
  Branch (39:9): [True: 2, False: 0]
  Branch (39:9): [True: 4, False: 0]
40
        pos += offset;
41
42
    return pos;
43
}
bytes_methods.c:stringlib_rfind
Line
Count
Source
30
{
31
    Py_ssize_t pos;
32
33
    assert(str_len >= 0);
34
    if (sub_len == 0)
  Branch (34:9): [True: 736, False: 196k]
35
        return str_len + offset;
36
37
    pos = FASTSEARCH(str, str_len, sub, sub_len, -1, FAST_RSEARCH);
38
39
    if (pos >= 0)
  Branch (39:9): [True: 5.68k, False: 190k]
40
        pos += offset;
41
42
    return pos;
43
}
Unexecuted instantiation: bytearrayobject.c:stringlib_rfind
bytesobject.c:stringlib_rfind
Line
Count
Source
30
{
31
    Py_ssize_t pos;
32
33
    assert(str_len >= 0);
34
    if (sub_len == 0)
  Branch (34:9): [True: 0, False: 7]
35
        return str_len + offset;
36
37
    pos = FASTSEARCH(str, str_len, sub, sub_len, -1, FAST_RSEARCH);
38
39
    if (pos >= 0)
  Branch (39:9): [True: 6, False: 1]
40
        pos += offset;
41
42
    return pos;
43
}
unicodeobject.c:asciilib_rfind
Line
Count
Source
30
{
31
    Py_ssize_t pos;
32
33
    assert(str_len >= 0);
34
    if (sub_len == 0)
  Branch (34:9): [True: 740, False: 196k]
35
        return str_len + offset;
36
37
    pos = FASTSEARCH(str, str_len, sub, sub_len, -1, FAST_RSEARCH);
38
39
    if (pos >= 0)
  Branch (39:9): [True: 5.68k, False: 190k]
40
        pos += offset;
41
42
    return pos;
43
}
Unexecuted instantiation: unicodeobject.c:ucs1lib_rfind
unicodeobject.c:ucs2lib_rfind
Line
Count
Source
30
{
31
    Py_ssize_t pos;
32
33
    assert(str_len >= 0);
34
    if (sub_len == 0)
  Branch (34:9): [True: 0, False: 2]
35
        return str_len + offset;
36
37
    pos = FASTSEARCH(str, str_len, sub, sub_len, -1, FAST_RSEARCH);
38
39
    if (pos >= 0)
  Branch (39:9): [True: 2, False: 0]
40
        pos += offset;
41
42
    return pos;
43
}
unicodeobject.c:ucs4lib_rfind
Line
Count
Source
30
{
31
    Py_ssize_t pos;
32
33
    assert(str_len >= 0);
34
    if (sub_len == 0)
  Branch (34:9): [True: 0, False: 4]
35
        return str_len + offset;
36
37
    pos = FASTSEARCH(str, str_len, sub, sub_len, -1, FAST_RSEARCH);
38
39
    if (pos >= 0)
  Branch (39:9): [True: 4, False: 0]
40
        pos += offset;
41
42
    return pos;
43
}
44
45
Py_LOCAL_INLINE(Py_ssize_t)
46
STRINGLIB(find_slice)(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
47
                     const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
48
                     Py_ssize_t start, Py_ssize_t end)
49
{
50
    return STRINGLIB(find)(str + start, end - start, sub, sub_len, start);
51
}
bytes_methods.c:stringlib_find_slice
Line
Count
Source
49
{
50
    return STRINGLIB(find)(str + start, end - start, sub, sub_len, start);
51
}
Unexecuted instantiation: bytearrayobject.c:stringlib_find_slice
Unexecuted instantiation: bytesobject.c:stringlib_find_slice
unicodeobject.c:asciilib_find_slice
Line
Count
Source
49
{
50
    return STRINGLIB(find)(str + start, end - start, sub, sub_len, start);
51
}
Unexecuted instantiation: unicodeobject.c:ucs1lib_find_slice
unicodeobject.c:ucs2lib_find_slice
Line
Count
Source
49
{
50
    return STRINGLIB(find)(str + start, end - start, sub, sub_len, start);
51
}
unicodeobject.c:ucs4lib_find_slice
Line
Count
Source
49
{
50
    return STRINGLIB(find)(str + start, end - start, sub, sub_len, start);
51
}
52
53
Py_LOCAL_INLINE(Py_ssize_t)
54
STRINGLIB(rfind_slice)(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
55
                      const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
56
                      Py_ssize_t start, Py_ssize_t end)
57
{
58
    return STRINGLIB(rfind)(str + start, end - start, sub, sub_len, start);
59
}
bytes_methods.c:stringlib_rfind_slice
Line
Count
Source
57
{
58
    return STRINGLIB(rfind)(str + start, end - start, sub, sub_len, start);
59
}
Unexecuted instantiation: bytearrayobject.c:stringlib_rfind_slice
Unexecuted instantiation: bytesobject.c:stringlib_rfind_slice
unicodeobject.c:asciilib_rfind_slice
Line
Count
Source
57
{
58
    return STRINGLIB(rfind)(str + start, end - start, sub, sub_len, start);
59
}
Unexecuted instantiation: unicodeobject.c:ucs1lib_rfind_slice
unicodeobject.c:ucs2lib_rfind_slice
Line
Count
Source
57
{
58
    return STRINGLIB(rfind)(str + start, end - start, sub, sub_len, start);
59
}
unicodeobject.c:ucs4lib_rfind_slice
Line
Count
Source
57
{
58
    return STRINGLIB(rfind)(str + start, end - start, sub, sub_len, start);
59
}
60
61
#ifdef STRINGLIB_WANT_CONTAINS_OBJ
62
63
Py_LOCAL_INLINE(int)
64
STRINGLIB(contains_obj)(PyObject* str, PyObject* sub)
65
{
66
    return STRINGLIB(find)(
67
        STRINGLIB_STR(str), STRINGLIB_LEN(str),
68
        STRINGLIB_STR(sub), STRINGLIB_LEN(sub), 0
69
        ) != -1;
70
}
71
72
#endif /* STRINGLIB_WANT_CONTAINS_OBJ */
73
74
/*
75
This function is a helper for the "find" family (find, rfind, index,
76
rindex) and for count, startswith and endswith, because they all have
77
the same behaviour for the arguments.
78
79
It does not touch the variables received until it knows everything
80
is ok.
81
*/
82
83
#define FORMAT_BUFFER_SIZE 50
84
85
Py_LOCAL_INLINE(int)
86
STRINGLIB(parse_args_finds)(const char * function_name, PyObject *args,
87
                           PyObject **subobj,
88
                           Py_ssize_t *start, Py_ssize_t *end)
89
{
90
    PyObject *tmp_subobj;
91
    Py_ssize_t tmp_start = 0;
92
    Py_ssize_t tmp_end = PY_SSIZE_T_MAX;
93
    PyObject *obj_start=Py_None, *obj_end=Py_None;
94
    char format[FORMAT_BUFFER_SIZE] = "O|OO:";
95
    size_t len = strlen(format);
96
97
    strncpy(format + len, function_name, FORMAT_BUFFER_SIZE - len - 1);
98
    format[FORMAT_BUFFER_SIZE - 1] = '\0';
99
100
    if (!PyArg_ParseTuple(args, format, &tmp_subobj, &obj_start, &obj_end))
  Branch (100:9): [True: 24, False: 1.17M]
  Branch (100:9): [True: 22, False: 8.90M]
101
        return 0;
102
103
    /* To support None in "start" and "end" arguments, meaning
104
       the same as if they were not passed.
105
    */
106
    if (obj_start != Py_None)
  Branch (106:9): [True: 58.2k, False: 1.11M]
  Branch (106:9): [True: 4.01M, False: 4.89M]
107
        if (!_PyEval_SliceIndex(obj_start, &tmp_start))
  Branch (107:13): [True: 0, False: 58.2k]
  Branch (107:13): [True: 0, False: 4.01M]
108
            return 0;
109
    if (obj_end != Py_None)
  Branch (109:9): [True: 166, False: 1.17M]
  Branch (109:9): [True: 354k, False: 8.55M]
110
        if (!_PyEval_SliceIndex(obj_end, &tmp_end))
  Branch (110:13): [True: 0, False: 166]
  Branch (110:13): [True: 0, False: 354k]
111
            return 0;
112
113
    *start = tmp_start;
114
    *end = tmp_end;
115
    *subobj = tmp_subobj;
116
    return 1;
117
}
bytes_methods.c:stringlib_parse_args_finds
Line
Count
Source
89
{
90
    PyObject *tmp_subobj;
91
    Py_ssize_t tmp_start = 0;
92
    Py_ssize_t tmp_end = PY_SSIZE_T_MAX;
93
    PyObject *obj_start=Py_None, *obj_end=Py_None;
94
    char format[FORMAT_BUFFER_SIZE] = "O|OO:";
95
    size_t len = strlen(format);
96
97
    strncpy(format + len, function_name, FORMAT_BUFFER_SIZE - len - 1);
98
    format[FORMAT_BUFFER_SIZE - 1] = '\0';
99
100
    if (!PyArg_ParseTuple(args, format, &tmp_subobj, &obj_start, &obj_end))
  Branch (100:9): [True: 24, False: 1.17M]
101
        return 0;
102
103
    /* To support None in "start" and "end" arguments, meaning
104
       the same as if they were not passed.
105
    */
106
    if (obj_start != Py_None)
  Branch (106:9): [True: 58.2k, False: 1.11M]
107
        if (!_PyEval_SliceIndex(obj_start, &tmp_start))
  Branch (107:13): [True: 0, False: 58.2k]
108
            return 0;
109
    if (obj_end != Py_None)
  Branch (109:9): [True: 166, False: 1.17M]
110
        if (!_PyEval_SliceIndex(obj_end, &tmp_end))
  Branch (110:13): [True: 0, False: 166]
111
            return 0;
112
113
    *start = tmp_start;
114
    *end = tmp_end;
115
    *subobj = tmp_subobj;
116
    return 1;
117
}
Unexecuted instantiation: bytearrayobject.c:stringlib_parse_args_finds
Unexecuted instantiation: bytesobject.c:stringlib_parse_args_finds
unicodeobject.c:asciilib_parse_args_finds
Line
Count
Source
89
{
90
    PyObject *tmp_subobj;
91
    Py_ssize_t tmp_start = 0;
92
    Py_ssize_t tmp_end = PY_SSIZE_T_MAX;
93
    PyObject *obj_start=Py_None, *obj_end=Py_None;
94
    char format[FORMAT_BUFFER_SIZE] = "O|OO:";
95
    size_t len = strlen(format);
96
97
    strncpy(format + len, function_name, FORMAT_BUFFER_SIZE - len - 1);
98
    format[FORMAT_BUFFER_SIZE - 1] = '\0';
99
100
    if (!PyArg_ParseTuple(args, format, &tmp_subobj, &obj_start, &obj_end))
  Branch (100:9): [True: 22, False: 8.90M]
101
        return 0;
102
103
    /* To support None in "start" and "end" arguments, meaning
104
       the same as if they were not passed.
105
    */
106
    if (obj_start != Py_None)
  Branch (106:9): [True: 4.01M, False: 4.89M]
107
        if (!_PyEval_SliceIndex(obj_start, &tmp_start))
  Branch (107:13): [True: 0, False: 4.01M]
108
            return 0;
109
    if (obj_end != Py_None)
  Branch (109:9): [True: 354k, False: 8.55M]
110
        if (!_PyEval_SliceIndex(obj_end, &tmp_end))
  Branch (110:13): [True: 0, False: 354k]
111
            return 0;
112
113
    *start = tmp_start;
114
    *end = tmp_end;
115
    *subobj = tmp_subobj;
116
    return 1;
117
}
Unexecuted instantiation: unicodeobject.c:ucs1lib_parse_args_finds
Unexecuted instantiation: unicodeobject.c:ucs2lib_parse_args_finds
Unexecuted instantiation: unicodeobject.c:ucs4lib_parse_args_finds
118
119
#undef FORMAT_BUFFER_SIZE