Coverage Report

Created: 2025-11-24 06:11

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.89M
{
11
1.89M
    *s = u2;
12
151M
    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
151M
        if (*s != u1) {
22
8.11M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
28.5M
            while (1) {
25
28.5M
                if (++s == end)
26
1.22M
                    return;
27
27.3M
                if (*s == u1)
28
5.97M
                    break;
29
21.3M
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
#ifdef STRINGLIB_FAST_MEMCHR
33
                    s++;
34
127k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
127k
                    if (s == NULL)
36
36.8k
                        return;
37
#else
38
                    Py_ssize_t i;
39
787k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
                    s++;
41
787k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
787k
                    if (i < 0)
43
311k
                        return;
44
475k
                    s += i;
45
475k
#endif
46
                    /* restart the dummy loop */
47
90.7k
                    break;
48
914k
                }
49
21.3M
            }
50
8.11M
        }
51
149M
        *s = u2;
52
149M
    }
53
1.89M
}
unicodeobject.c:ucs1lib_replace_1char_inplace
Line
Count
Source
10
805k
{
11
805k
    *s = u2;
12
51.4M
    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
51.2M
        if (*s != u1) {
22
1.68M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
4.43M
            while (1) {
25
4.43M
                if (++s == end)
26
573k
                    return;
27
3.86M
                if (*s == u1)
28
1.00M
                    break;
29
2.85M
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
97.9k
#ifdef STRINGLIB_FAST_MEMCHR
33
97.9k
                    s++;
34
97.9k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
97.9k
                    if (s == NULL)
36
30.0k
                        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
67.8k
                    break;
48
97.9k
                }
49
2.85M
            }
50
1.68M
        }
51
50.6M
        *s = u2;
52
50.6M
    }
53
805k
}
unicodeobject.c:ucs2lib_replace_1char_inplace
Line
Count
Source
10
1.07M
{
11
1.07M
    *s = u2;
12
78.8M
    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
78.7M
        if (*s != u1) {
22
5.49M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
22.5M
            while (1) {
25
22.5M
                if (++s == end)
26
646k
                    return;
27
21.9M
                if (*s == u1)
28
4.05M
                    break;
29
17.8M
                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
787k
                    Py_ssize_t i;
39
787k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
787k
                    s++;
41
787k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
787k
                    if (i < 0)
43
311k
                        return;
44
475k
                    s += i;
45
475k
#endif
46
                    /* restart the dummy loop */
47
475k
                    break;
48
787k
                }
49
17.8M
            }
50
5.49M
        }
51
77.8M
        *s = u2;
52
77.8M
    }
53
1.07M
}
unicodeobject.c:ucs4lib_replace_1char_inplace
Line
Count
Source
10
17.1k
{
11
17.1k
    *s = u2;
12
21.2M
    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
21.2M
        if (*s != u1) {
22
944k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
1.57M
            while (1) {
25
1.57M
                if (++s == end)
26
5.45k
                    return;
27
1.56M
                if (*s == u1)
28
909k
                    break;
29
660k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
29.6k
#ifdef STRINGLIB_FAST_MEMCHR
33
29.6k
                    s++;
34
29.6k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
29.6k
                    if (s == NULL)
36
6.71k
                        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
22.9k
                    break;
48
29.6k
                }
49
660k
            }
50
944k
        }
51
21.2M
        *s = u2;
52
21.2M
    }
53
17.1k
}