Coverage Report

Created: 2026-06-09 06:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Objects/stringlib/replace.h
Line
Count
Source
1
/* stringlib: replace 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(void)
8
STRINGLIB(replace_1char_inplace)(STRINGLIB_CHAR* s, STRINGLIB_CHAR* end,
9
                                 Py_UCS4 u1, Py_UCS4 u2, Py_ssize_t maxcount)
10
58.0k
{
11
58.0k
    *s = u2;
12
3.34M
    while (--maxcount && ++s != end) {
13
        /* Find the next character to be replaced.
14
15
           If it occurs often, it is faster to scan for it using an inline
16
           loop.  If it occurs seldom, it is faster to scan for it using a
17
           function call; the overhead of the function call is amortized
18
           across the many characters that call covers.  We start with an
19
           inline loop and use a heuristic to determine whether to fall back
20
           to a function call. */
21
3.31M
        if (*s != u1) {
22
252k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
496k
            while (1) {
25
496k
                if (++s == end)
26
20.4k
                    return;
27
475k
                if (*s == u1)
28
217k
                    break;
29
258k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
#ifdef STRINGLIB_FAST_MEMCHR
33
                    s++;
34
4.81k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
4.81k
                    if (s == NULL)
36
2.13k
                        return;
37
#else
38
                    Py_ssize_t i;
39
9.77k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
                    s++;
41
9.77k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
9.77k
                    if (i < 0)
43
3.60k
                        return;
44
6.16k
                    s += i;
45
6.16k
#endif
46
                    /* restart the dummy loop */
47
2.67k
                    break;
48
14.5k
                }
49
258k
            }
50
252k
        }
51
3.28M
        *s = u2;
52
3.28M
    }
53
58.0k
}
unicodeobject.c:ucs1lib_replace_1char_inplace
Line
Count
Source
10
31.9k
{
11
31.9k
    *s = u2;
12
958k
    while (--maxcount && ++s != end) {
13
        /* Find the next character to be replaced.
14
15
           If it occurs often, it is faster to scan for it using an inline
16
           loop.  If it occurs seldom, it is faster to scan for it using a
17
           function call; the overhead of the function call is amortized
18
           across the many characters that call covers.  We start with an
19
           inline loop and use a heuristic to determine whether to fall back
20
           to a function call. */
21
937k
        if (*s != u1) {
22
36.1k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
80.7k
            while (1) {
25
80.7k
                if (++s == end)
26
9.71k
                    return;
27
71.0k
                if (*s == u1)
28
23.5k
                    break;
29
47.5k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
2.90k
#ifdef STRINGLIB_FAST_MEMCHR
33
2.90k
                    s++;
34
2.90k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
2.90k
                    if (s == NULL)
36
1.36k
                        return;
37
#else
38
                    Py_ssize_t i;
39
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
                    s++;
41
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
                    if (i < 0)
43
                        return;
44
                    s += i;
45
#endif
46
                    /* restart the dummy loop */
47
1.54k
                    break;
48
2.90k
                }
49
47.5k
            }
50
36.1k
        }
51
926k
        *s = u2;
52
926k
    }
53
31.9k
}
unicodeobject.c:ucs2lib_replace_1char_inplace
Line
Count
Source
10
21.1k
{
11
21.1k
    *s = u2;
12
1.83M
    while (--maxcount && ++s != end) {
13
        /* Find the next character to be replaced.
14
15
           If it occurs often, it is faster to scan for it using an inline
16
           loop.  If it occurs seldom, it is faster to scan for it using a
17
           function call; the overhead of the function call is amortized
18
           across the many characters that call covers.  We start with an
19
           inline loop and use a heuristic to determine whether to fall back
20
           to a function call. */
21
1.83M
        if (*s != u1) {
22
176k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
344k
            while (1) {
25
344k
                if (++s == end)
26
8.55k
                    return;
27
336k
                if (*s == u1)
28
158k
                    break;
29
177k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
#ifdef STRINGLIB_FAST_MEMCHR
33
                    s++;
34
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
                    if (s == NULL)
36
                        return;
37
#else
38
9.77k
                    Py_ssize_t i;
39
9.77k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
9.77k
                    s++;
41
9.77k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
9.77k
                    if (i < 0)
43
3.60k
                        return;
44
6.16k
                    s += i;
45
6.16k
#endif
46
                    /* restart the dummy loop */
47
6.16k
                    break;
48
9.77k
                }
49
177k
            }
50
176k
        }
51
1.81M
        *s = u2;
52
1.81M
    }
53
21.1k
}
unicodeobject.c:ucs4lib_replace_1char_inplace
Line
Count
Source
10
4.94k
{
11
4.94k
    *s = u2;
12
545k
    while (--maxcount && ++s != end) {
13
        /* Find the next character to be replaced.
14
15
           If it occurs often, it is faster to scan for it using an inline
16
           loop.  If it occurs seldom, it is faster to scan for it using a
17
           function call; the overhead of the function call is amortized
18
           across the many characters that call covers.  We start with an
19
           inline loop and use a heuristic to determine whether to fall back
20
           to a function call. */
21
543k
        if (*s != u1) {
22
39.6k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
70.8k
            while (1) {
25
70.8k
                if (++s == end)
26
2.15k
                    return;
27
68.6k
                if (*s == u1)
28
35.5k
                    break;
29
33.1k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
1.91k
#ifdef STRINGLIB_FAST_MEMCHR
33
1.91k
                    s++;
34
1.91k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
1.91k
                    if (s == NULL)
36
776
                        return;
37
#else
38
                    Py_ssize_t i;
39
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
                    s++;
41
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
                    if (i < 0)
43
                        return;
44
                    s += i;
45
#endif
46
                    /* restart the dummy loop */
47
1.13k
                    break;
48
1.91k
                }
49
33.1k
            }
50
39.6k
        }
51
540k
        *s = u2;
52
540k
    }
53
4.94k
}