Coverage Report

Created: 2026-01-17 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
2.04M
{
11
2.04M
    *s = u2;
12
113M
    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
112M
        if (*s != u1) {
22
6.80M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
26.4M
            while (1) {
25
26.4M
                if (++s == end)
26
1.12M
                    return;
27
25.3M
                if (*s == u1)
28
4.81M
                    break;
29
20.5M
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
#ifdef STRINGLIB_FAST_MEMCHR
33
                    s++;
34
149k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
149k
                    if (s == NULL)
36
46.1k
                        return;
37
#else
38
                    Py_ssize_t i;
39
704k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
                    s++;
41
704k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
704k
                    if (i < 0)
43
339k
                        return;
44
365k
                    s += i;
45
365k
#endif
46
                    /* restart the dummy loop */
47
102k
                    break;
48
853k
                }
49
20.5M
            }
50
6.80M
        }
51
111M
        *s = u2;
52
111M
    }
53
2.04M
}
unicodeobject.c:ucs1lib_replace_1char_inplace
Line
Count
Source
10
1.06M
{
11
1.06M
    *s = u2;
12
38.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
38.2M
        if (*s != u1) {
22
1.80M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
6.29M
            while (1) {
25
6.29M
                if (++s == end)
26
603k
                    return;
27
5.69M
                if (*s == u1)
28
1.09M
                    break;
29
4.59M
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
104k
#ifdef STRINGLIB_FAST_MEMCHR
33
104k
                    s++;
34
104k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
104k
                    if (s == NULL)
36
39.5k
                        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
65.3k
                    break;
48
104k
                }
49
4.59M
            }
50
1.80M
        }
51
37.5M
        *s = u2;
52
37.5M
    }
53
1.06M
}
unicodeobject.c:ucs2lib_replace_1char_inplace
Line
Count
Source
10
967k
{
11
967k
    *s = u2;
12
58.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
58.6M
        if (*s != u1) {
22
4.64M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
19.1M
            while (1) {
25
19.1M
                if (++s == end)
26
520k
                    return;
27
18.6M
                if (*s == u1)
28
3.41M
                    break;
29
15.2M
                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
704k
                    Py_ssize_t i;
39
704k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
704k
                    s++;
41
704k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
704k
                    if (i < 0)
43
339k
                        return;
44
365k
                    s += i;
45
365k
#endif
46
                    /* restart the dummy loop */
47
365k
                    break;
48
704k
                }
49
15.2M
            }
50
4.64M
        }
51
57.7M
        *s = u2;
52
57.7M
    }
53
967k
}
unicodeobject.c:ucs4lib_replace_1char_inplace
Line
Count
Source
10
16.1k
{
11
16.1k
    *s = u2;
12
15.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
15.7M
        if (*s != u1) {
22
352k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
989k
            while (1) {
25
989k
                if (++s == end)
26
4.58k
                    return;
27
985k
                if (*s == u1)
28
304k
                    break;
29
681k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
44.1k
#ifdef STRINGLIB_FAST_MEMCHR
33
44.1k
                    s++;
34
44.1k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
44.1k
                    if (s == NULL)
36
6.64k
                        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
37.5k
                    break;
48
44.1k
                }
49
681k
            }
50
352k
        }
51
15.7M
        *s = u2;
52
15.7M
    }
53
16.1k
}