Coverage Report

Created: 2025-12-14 07:06

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.66M
{
11
1.66M
    *s = u2;
12
125M
    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
124M
        if (*s != u1) {
22
6.69M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
24.3M
            while (1) {
25
24.3M
                if (++s == end)
26
889k
                    return;
27
23.4M
                if (*s == u1)
28
5.00M
                    break;
29
18.4M
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
#ifdef STRINGLIB_FAST_MEMCHR
33
                    s++;
34
115k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
115k
                    if (s == NULL)
36
19.2k
                        return;
37
#else
38
                    Py_ssize_t i;
39
686k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
                    s++;
41
686k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
686k
                    if (i < 0)
43
346k
                        return;
44
339k
                    s += i;
45
339k
#endif
46
                    /* restart the dummy loop */
47
95.9k
                    break;
48
801k
                }
49
18.4M
            }
50
6.69M
        }
51
123M
        *s = u2;
52
123M
    }
53
1.66M
}
unicodeobject.c:ucs1lib_replace_1char_inplace
Line
Count
Source
10
591k
{
11
591k
    *s = u2;
12
42.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
42.7M
        if (*s != u1) {
22
1.28M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
3.67M
            while (1) {
25
3.67M
                if (++s == end)
26
287k
                    return;
27
3.38M
                if (*s == u1)
28
918k
                    break;
29
2.47M
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
79.0k
#ifdef STRINGLIB_FAST_MEMCHR
33
79.0k
                    s++;
34
79.0k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
79.0k
                    if (s == NULL)
36
12.2k
                        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
66.7k
                    break;
48
79.0k
                }
49
2.47M
            }
50
1.28M
        }
51
42.4M
        *s = u2;
52
42.4M
    }
53
591k
}
unicodeobject.c:ucs2lib_replace_1char_inplace
Line
Count
Source
10
1.06M
{
11
1.06M
    *s = u2;
12
62.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
62.1M
        if (*s != u1) {
22
4.58M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
19.4M
            while (1) {
25
19.4M
                if (++s == end)
26
597k
                    return;
27
18.8M
                if (*s == u1)
28
3.30M
                    break;
29
15.4M
                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
686k
                    Py_ssize_t i;
39
686k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
686k
                    s++;
41
686k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
686k
                    if (i < 0)
43
346k
                        return;
44
339k
                    s += i;
45
339k
#endif
46
                    /* restart the dummy loop */
47
339k
                    break;
48
686k
                }
49
15.4M
            }
50
4.58M
        }
51
61.1M
        *s = u2;
52
61.1M
    }
53
1.06M
}
unicodeobject.c:ucs4lib_replace_1char_inplace
Line
Count
Source
10
16.7k
{
11
16.7k
    *s = u2;
12
19.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
19.9M
        if (*s != u1) {
22
817k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
1.27M
            while (1) {
25
1.27M
                if (++s == end)
26
4.32k
                    return;
27
1.27M
                if (*s == u1)
28
776k
                    break;
29
495k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
36.2k
#ifdef STRINGLIB_FAST_MEMCHR
33
36.2k
                    s++;
34
36.2k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
36.2k
                    if (s == NULL)
36
7.06k
                        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
29.1k
                    break;
48
36.2k
                }
49
495k
            }
50
817k
        }
51
19.9M
        *s = u2;
52
19.9M
    }
53
16.7k
}