Coverage Report

Created: 2025-07-18 06:09

/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.05M
{
11
1.05M
    *s = u2;
12
128M
    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
127M
        if (*s != u1) {
22
5.33M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
16.6M
            while (1) {
25
16.6M
                if (++s == end)
26
593k
                    return;
27
16.0M
                if (*s == u1)
28
4.31M
                    break;
29
11.7M
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
#ifdef STRINGLIB_FAST_MEMCHR
33
                    s++;
34
68.9k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
68.9k
                    if (s == NULL)
36
23.1k
                        return;
37
#else
38
                    Py_ssize_t i;
39
360k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
                    s++;
41
360k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
360k
                    if (i < 0)
43
195k
                        return;
44
164k
                    s += i;
45
164k
#endif
46
                    /* restart the dummy loop */
47
45.7k
                    break;
48
429k
                }
49
11.7M
            }
50
5.33M
        }
51
126M
        *s = u2;
52
126M
    }
53
1.05M
}
unicodeobject.c:ucs1lib_replace_1char_inplace
Line
Count
Source
10
384k
{
11
384k
    *s = u2;
12
38.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
38.5M
        if (*s != u1) {
22
1.00M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
2.86M
            while (1) {
25
2.86M
                if (++s == end)
26
195k
                    return;
27
2.67M
                if (*s == u1)
28
761k
                    break;
29
1.91M
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
46.5k
#ifdef STRINGLIB_FAST_MEMCHR
33
46.5k
                    s++;
34
46.5k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
46.5k
                    if (s == NULL)
36
18.0k
                        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
28.4k
                    break;
48
46.5k
                }
49
1.91M
            }
50
1.00M
        }
51
38.3M
        *s = u2;
52
38.3M
    }
53
384k
}
unicodeobject.c:ucs2lib_replace_1char_inplace
Line
Count
Source
10
659k
{
11
659k
    *s = u2;
12
64.3M
    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
64.2M
        if (*s != u1) {
22
3.12M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
12.2M
            while (1) {
25
12.2M
                if (++s == end)
26
392k
                    return;
27
11.8M
                if (*s == u1)
28
2.37M
                    break;
29
9.50M
                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
360k
                    Py_ssize_t i;
39
360k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
360k
                    s++;
41
360k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
360k
                    if (i < 0)
43
195k
                        return;
44
164k
                    s += i;
45
164k
#endif
46
                    /* restart the dummy loop */
47
164k
                    break;
48
360k
                }
49
9.50M
            }
50
3.12M
        }
51
63.6M
        *s = u2;
52
63.6M
    }
53
659k
}
unicodeobject.c:ucs4lib_replace_1char_inplace
Line
Count
Source
10
13.4k
{
11
13.4k
    *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.9M
        if (*s != u1) {
22
1.20M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
1.54M
            while (1) {
25
1.54M
                if (++s == end)
26
4.78k
                    return;
27
1.53M
                if (*s == u1)
28
1.17M
                    break;
29
359k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
22.4k
#ifdef STRINGLIB_FAST_MEMCHR
33
22.4k
                    s++;
34
22.4k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
22.4k
                    if (s == NULL)
36
5.09k
                        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
17.3k
                    break;
48
22.4k
                }
49
359k
            }
50
1.20M
        }
51
24.9M
        *s = u2;
52
24.9M
    }
53
13.4k
}