Coverage Report

Created: 2025-11-09 06:26

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.82M
{
11
1.82M
    *s = u2;
12
152M
    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
152M
        if (*s != u1) {
22
7.65M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
27.7M
            while (1) {
25
27.7M
                if (++s == end)
26
1.07M
                    return;
27
26.6M
                if (*s == u1)
28
5.74M
                    break;
29
20.9M
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
#ifdef STRINGLIB_FAST_MEMCHR
33
                    s++;
34
178k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
178k
                    if (s == NULL)
36
64.9k
                        return;
37
#else
38
                    Py_ssize_t i;
39
651k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
                    s++;
41
651k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
651k
                    if (i < 0)
43
302k
                        return;
44
348k
                    s += i;
45
348k
#endif
46
                    /* restart the dummy loop */
47
113k
                    break;
48
829k
                }
49
20.9M
            }
50
7.65M
        }
51
151M
        *s = u2;
52
151M
    }
53
1.82M
}
unicodeobject.c:ucs1lib_replace_1char_inplace
Line
Count
Source
10
612k
{
11
612k
    *s = u2;
12
51.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
51.4M
        if (*s != u1) {
22
1.61M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
5.30M
            while (1) {
25
5.30M
                if (++s == end)
26
335k
                    return;
27
4.96M
                if (*s == u1)
28
1.13M
                    break;
29
3.83M
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
148k
#ifdef STRINGLIB_FAST_MEMCHR
33
148k
                    s++;
34
148k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
148k
                    if (s == NULL)
36
58.6k
                        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
90.2k
                    break;
48
148k
                }
49
3.83M
            }
50
1.61M
        }
51
51.0M
        *s = u2;
52
51.0M
    }
53
612k
}
unicodeobject.c:ucs2lib_replace_1char_inplace
Line
Count
Source
10
1.18M
{
11
1.18M
    *s = u2;
12
76.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
76.0M
        if (*s != u1) {
22
5.10M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
21.0M
            while (1) {
25
21.0M
                if (++s == end)
26
738k
                    return;
27
20.2M
                if (*s == u1)
28
3.71M
                    break;
29
16.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
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
                    if (s == NULL)
36
                        return;
37
#else
38
651k
                    Py_ssize_t i;
39
651k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
651k
                    s++;
41
651k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
651k
                    if (i < 0)
43
302k
                        return;
44
348k
                    s += i;
45
348k
#endif
46
                    /* restart the dummy loop */
47
348k
                    break;
48
651k
                }
49
16.5M
            }
50
5.10M
        }
51
75.0M
        *s = u2;
52
75.0M
    }
53
1.18M
}
unicodeobject.c:ucs4lib_replace_1char_inplace
Line
Count
Source
10
19.9k
{
11
19.9k
    *s = u2;
12
24.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
24.8M
        if (*s != u1) {
22
933k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
1.43M
            while (1) {
25
1.43M
                if (++s == end)
26
5.61k
                    return;
27
1.43M
                if (*s == u1)
28
897k
                    break;
29
532k
                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.29k
                        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.3k
                    break;
48
29.6k
                }
49
532k
            }
50
933k
        }
51
24.8M
        *s = u2;
52
24.8M
    }
53
19.9k
}