Coverage Report

Created: 2026-02-26 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
1.79M
{
11
1.79M
    *s = u2;
12
126M
    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
125M
        if (*s != u1) {
22
8.42M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
30.3M
            while (1) {
25
30.3M
                if (++s == end)
26
1.03M
                    return;
27
29.2M
                if (*s == u1)
28
6.49M
                    break;
29
22.7M
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
#ifdef STRINGLIB_FAST_MEMCHR
33
                    s++;
34
151k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
151k
                    if (s == NULL)
36
26.7k
                        return;
37
#else
38
                    Py_ssize_t i;
39
738k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
                    s++;
41
738k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
738k
                    if (i < 0)
43
378k
                        return;
44
360k
                    s += i;
45
360k
#endif
46
                    /* restart the dummy loop */
47
124k
                    break;
48
890k
                }
49
22.7M
            }
50
8.42M
        }
51
124M
        *s = u2;
52
124M
    }
53
1.79M
}
unicodeobject.c:ucs1lib_replace_1char_inplace
Line
Count
Source
10
543k
{
11
543k
    *s = u2;
12
42.7M
    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
42.4M
        if (*s != u1) {
22
1.39M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
3.90M
            while (1) {
25
3.90M
                if (++s == end)
26
264k
                    return;
27
3.64M
                if (*s == u1)
28
1.04M
                    break;
29
2.59M
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
80.8k
#ifdef STRINGLIB_FAST_MEMCHR
33
80.8k
                    s++;
34
80.8k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
80.8k
                    if (s == NULL)
36
20.3k
                        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
60.5k
                    break;
48
80.8k
                }
49
2.59M
            }
50
1.39M
        }
51
42.1M
        *s = u2;
52
42.1M
    }
53
543k
}
unicodeobject.c:ucs2lib_replace_1char_inplace
Line
Count
Source
10
1.23M
{
11
1.23M
    *s = u2;
12
64.9M
    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
64.9M
        if (*s != u1) {
22
6.03M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
24.3M
            while (1) {
25
24.3M
                if (++s == end)
26
767k
                    return;
27
23.5M
                if (*s == u1)
28
4.53M
                    break;
29
19.0M
                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
738k
                    Py_ssize_t i;
39
738k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
738k
                    s++;
41
738k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
738k
                    if (i < 0)
43
378k
                        return;
44
360k
                    s += i;
45
360k
#endif
46
                    /* restart the dummy loop */
47
360k
                    break;
48
738k
                }
49
19.0M
            }
50
6.03M
        }
51
63.7M
        *s = u2;
52
63.7M
    }
53
1.23M
}
unicodeobject.c:ucs4lib_replace_1char_inplace
Line
Count
Source
10
15.2k
{
11
15.2k
    *s = u2;
12
18.6M
    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
18.5M
        if (*s != u1) {
22
993k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
2.06M
            while (1) {
25
2.06M
                if (++s == end)
26
3.98k
                    return;
27
2.05M
                if (*s == u1)
28
919k
                    break;
29
1.14M
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
70.5k
#ifdef STRINGLIB_FAST_MEMCHR
33
70.5k
                    s++;
34
70.5k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
70.5k
                    if (s == NULL)
36
6.35k
                        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
64.1k
                    break;
48
70.5k
                }
49
1.14M
            }
50
993k
        }
51
18.5M
        *s = u2;
52
18.5M
    }
53
15.2k
}