Coverage Report

Created: 2025-09-04 06:25

/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.50M
{
11
1.50M
    *s = u2;
12
166M
    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
166M
        if (*s != u1) {
22
7.28M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
24.2M
            while (1) {
25
24.2M
                if (++s == end)
26
851k
                    return;
27
23.3M
                if (*s == u1)
28
5.69M
                    break;
29
17.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
105k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
105k
                    if (s == NULL)
36
51.2k
                        return;
37
#else
38
                    Py_ssize_t i;
39
635k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
                    s++;
41
635k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
635k
                    if (i < 0)
43
306k
                        return;
44
328k
                    s += i;
45
328k
#endif
46
                    /* restart the dummy loop */
47
54.0k
                    break;
48
740k
                }
49
17.7M
            }
50
7.28M
        }
51
165M
        *s = u2;
52
165M
    }
53
1.50M
}
unicodeobject.c:ucs1lib_replace_1char_inplace
Line
Count
Source
10
545k
{
11
545k
    *s = u2;
12
53.5M
    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
53.3M
        if (*s != u1) {
22
1.34M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
3.64M
            while (1) {
25
3.64M
                if (++s == end)
26
316k
                    return;
27
3.33M
                if (*s == u1)
28
956k
                    break;
29
2.37M
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
71.3k
#ifdef STRINGLIB_FAST_MEMCHR
33
71.3k
                    s++;
34
71.3k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
71.3k
                    if (s == NULL)
36
43.7k
                        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
27.6k
                    break;
48
71.3k
                }
49
2.37M
            }
50
1.34M
        }
51
53.0M
        *s = u2;
52
53.0M
    }
53
545k
}
unicodeobject.c:ucs2lib_replace_1char_inplace
Line
Count
Source
10
939k
{
11
939k
    *s = u2;
12
87.5M
    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
87.4M
        if (*s != u1) {
22
4.79M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
18.8M
            while (1) {
25
18.8M
                if (++s == end)
26
529k
                    return;
27
18.2M
                if (*s == u1)
28
3.62M
                    break;
29
14.6M
                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
635k
                    Py_ssize_t i;
39
635k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
635k
                    s++;
41
635k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
635k
                    if (i < 0)
43
306k
                        return;
44
328k
                    s += i;
45
328k
#endif
46
                    /* restart the dummy loop */
47
328k
                    break;
48
635k
                }
49
14.6M
            }
50
4.79M
        }
51
86.6M
        *s = u2;
52
86.6M
    }
53
939k
}
unicodeobject.c:ucs4lib_replace_1char_inplace
Line
Count
Source
10
17.4k
{
11
17.4k
    *s = u2;
12
25.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
25.7M
        if (*s != u1) {
22
1.14M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
1.80M
            while (1) {
25
1.80M
                if (++s == end)
26
5.72k
                    return;
27
1.79M
                if (*s == u1)
28
1.10M
                    break;
29
689k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
33.9k
#ifdef STRINGLIB_FAST_MEMCHR
33
33.9k
                    s++;
34
33.9k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
33.9k
                    if (s == NULL)
36
7.54k
                        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
26.3k
                    break;
48
33.9k
                }
49
689k
            }
50
1.14M
        }
51
25.7M
        *s = u2;
52
25.7M
    }
53
17.4k
}