Coverage Report

Created: 2026-07-14 06:16

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
63.9k
{
11
63.9k
    *s = u2;
12
3.36M
    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.33M
        if (*s != u1) {
22
250k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
483k
            while (1) {
25
483k
                if (++s == end)
26
24.4k
                    return;
27
458k
                if (*s == u1)
28
212k
                    break;
29
246k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
#ifdef STRINGLIB_FAST_MEMCHR
33
                    s++;
34
5.27k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
5.27k
                    if (s == NULL)
36
2.43k
                        return;
37
#else
38
                    Py_ssize_t i;
39
8.88k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
                    s++;
41
8.88k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
8.88k
                    if (i < 0)
43
3.54k
                        return;
44
5.34k
                    s += i;
45
5.34k
#endif
46
                    /* restart the dummy loop */
47
2.83k
                    break;
48
14.1k
                }
49
246k
            }
50
250k
        }
51
3.30M
        *s = u2;
52
3.30M
    }
53
63.9k
}
unicodeobject.c:ucs1lib_replace_1char_inplace
Line
Count
Source
10
38.8k
{
11
38.8k
    *s = u2;
12
1.05M
    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.03M
        if (*s != u1) {
22
49.8k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
101k
            while (1) {
25
101k
                if (++s == end)
26
12.2k
                    return;
27
89.0k
                if (*s == u1)
28
34.2k
                    break;
29
54.8k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
3.32k
#ifdef STRINGLIB_FAST_MEMCHR
33
3.32k
                    s++;
34
3.32k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
3.32k
                    if (s == NULL)
36
1.55k
                        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.76k
                    break;
48
3.32k
                }
49
54.8k
            }
50
49.8k
        }
51
1.01M
        *s = u2;
52
1.01M
    }
53
38.8k
}
unicodeobject.c:ucs2lib_replace_1char_inplace
Line
Count
Source
10
19.9k
{
11
19.9k
    *s = u2;
12
1.69M
    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.68M
        if (*s != u1) {
22
172k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
324k
            while (1) {
25
324k
                if (++s == end)
26
8.93k
                    return;
27
315k
                if (*s == u1)
28
154k
                    break;
29
161k
                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
8.88k
                    Py_ssize_t i;
39
8.88k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
8.88k
                    s++;
41
8.88k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
8.88k
                    if (i < 0)
43
3.54k
                        return;
44
5.34k
                    s += i;
45
5.34k
#endif
46
                    /* restart the dummy loop */
47
5.34k
                    break;
48
8.88k
                }
49
161k
            }
50
172k
        }
51
1.67M
        *s = u2;
52
1.67M
    }
53
19.9k
}
unicodeobject.c:ucs4lib_replace_1char_inplace
Line
Count
Source
10
5.15k
{
11
5.15k
    *s = u2;
12
621k
    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
620k
        if (*s != u1) {
22
28.7k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
57.1k
            while (1) {
25
57.1k
                if (++s == end)
26
3.25k
                    return;
27
53.9k
                if (*s == u1)
28
23.5k
                    break;
29
30.3k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
1.95k
#ifdef STRINGLIB_FAST_MEMCHR
33
1.95k
                    s++;
34
1.95k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
1.95k
                    if (s == NULL)
36
884
                        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.07k
                    break;
48
1.95k
                }
49
30.3k
            }
50
28.7k
        }
51
616k
        *s = u2;
52
616k
    }
53
5.15k
}