Coverage Report

Created: 2026-03-23 06:45

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
81.3k
{
11
81.3k
    *s = u2;
12
2.94M
    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
2.90M
        if (*s != u1) {
22
444k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
1.31M
            while (1) {
25
1.31M
                if (++s == end)
26
38.9k
                    return;
27
1.27M
                if (*s == u1)
28
367k
                    break;
29
910k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
#ifdef STRINGLIB_FAST_MEMCHR
33
                    s++;
34
27.2k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
27.2k
                    if (s == NULL)
36
1.89k
                        return;
37
#else
38
                    Py_ssize_t i;
39
11.1k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
                    s++;
41
11.1k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
11.1k
                    if (i < 0)
43
3.32k
                        return;
44
7.79k
                    s += i;
45
7.79k
#endif
46
                    /* restart the dummy loop */
47
25.3k
                    break;
48
38.3k
                }
49
910k
            }
50
444k
        }
51
2.85M
        *s = u2;
52
2.85M
    }
53
81.3k
}
unicodeobject.c:ucs1lib_replace_1char_inplace
Line
Count
Source
10
47.8k
{
11
47.8k
    *s = u2;
12
848k
    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
825k
        if (*s != u1) {
22
246k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
861k
            while (1) {
25
861k
                if (++s == end)
26
23.9k
                    return;
27
837k
                if (*s == u1)
28
197k
                    break;
29
640k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
24.8k
#ifdef STRINGLIB_FAST_MEMCHR
33
24.8k
                    s++;
34
24.8k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
24.8k
                    if (s == NULL)
36
1.19k
                        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
23.6k
                    break;
48
24.8k
                }
49
640k
            }
50
246k
        }
51
800k
        *s = u2;
52
800k
    }
53
47.8k
}
unicodeobject.c:ucs2lib_replace_1char_inplace
Line
Count
Source
10
25.3k
{
11
25.3k
    *s = u2;
12
1.43M
    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.42M
        if (*s != u1) {
22
148k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
360k
            while (1) {
25
360k
                if (++s == end)
26
12.4k
                    return;
27
347k
                if (*s == u1)
28
124k
                    break;
29
222k
                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
11.1k
                    Py_ssize_t i;
39
11.1k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
11.1k
                    s++;
41
11.1k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
11.1k
                    if (i < 0)
43
3.32k
                        return;
44
7.79k
                    s += i;
45
7.79k
#endif
46
                    /* restart the dummy loop */
47
7.79k
                    break;
48
11.1k
                }
49
222k
            }
50
148k
        }
51
1.40M
        *s = u2;
52
1.40M
    }
53
25.3k
}
unicodeobject.c:ucs4lib_replace_1char_inplace
Line
Count
Source
10
8.05k
{
11
8.05k
    *s = u2;
12
660k
    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
655k
        if (*s != u1) {
22
49.4k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
94.7k
            while (1) {
25
94.7k
                if (++s == end)
26
2.44k
                    return;
27
92.3k
                if (*s == u1)
28
44.5k
                    break;
29
47.7k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
2.41k
#ifdef STRINGLIB_FAST_MEMCHR
33
2.41k
                    s++;
34
2.41k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
2.41k
                    if (s == NULL)
36
708
                        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.70k
                    break;
48
2.41k
                }
49
47.7k
            }
50
49.4k
        }
51
652k
        *s = u2;
52
652k
    }
53
8.05k
}