Coverage Report

Created: 2025-09-05 07:10

/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.43M
{
11
1.43M
    *s = u2;
12
126M
    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
125M
        if (*s != u1) {
22
6.36M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
22.1M
            while (1) {
25
22.1M
                if (++s == end)
26
813k
                    return;
27
21.3M
                if (*s == u1)
28
4.86M
                    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
96.7k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
96.7k
                    if (s == NULL)
36
44.3k
                        return;
37
#else
38
                    Py_ssize_t i;
39
590k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
                    s++;
41
590k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
590k
                    if (i < 0)
43
288k
                        return;
44
301k
                    s += i;
45
301k
#endif
46
                    /* restart the dummy loop */
47
52.4k
                    break;
48
687k
                }
49
16.5M
            }
50
6.36M
        }
51
124M
        *s = u2;
52
124M
    }
53
1.43M
}
unicodeobject.c:ucs1lib_replace_1char_inplace
Line
Count
Source
10
539k
{
11
539k
    *s = u2;
12
40.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
40.4M
        if (*s != u1) {
22
1.32M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
3.59M
            while (1) {
25
3.59M
                if (++s == end)
26
313k
                    return;
27
3.28M
                if (*s == u1)
28
944k
                    break;
29
2.34M
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
67.1k
#ifdef STRINGLIB_FAST_MEMCHR
33
67.1k
                    s++;
34
67.1k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
67.1k
                    if (s == NULL)
36
37.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
29.4k
                    break;
48
67.1k
                }
49
2.34M
            }
50
1.32M
        }
51
40.1M
        *s = u2;
52
40.1M
    }
53
539k
}
unicodeobject.c:ucs2lib_replace_1char_inplace
Line
Count
Source
10
877k
{
11
877k
    *s = u2;
12
64.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
64.6M
        if (*s != u1) {
22
4.37M
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
17.3M
            while (1) {
25
17.3M
                if (++s == end)
26
494k
                    return;
27
16.8M
                if (*s == u1)
28
3.28M
                    break;
29
13.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
590k
                    Py_ssize_t i;
39
590k
                    STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;
40
590k
                    s++;
41
590k
                    i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);
42
590k
                    if (i < 0)
43
288k
                        return;
44
301k
                    s += i;
45
301k
#endif
46
                    /* restart the dummy loop */
47
301k
                    break;
48
590k
                }
49
13.6M
            }
50
4.37M
        }
51
63.8M
        *s = u2;
52
63.8M
    }
53
877k
}
unicodeobject.c:ucs4lib_replace_1char_inplace
Line
Count
Source
10
15.4k
{
11
15.4k
    *s = u2;
12
20.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
20.6M
        if (*s != u1) {
22
666k
            int attempts = 10;
23
            /* search u1 in a dummy loop */
24
1.21M
            while (1) {
25
1.21M
                if (++s == end)
26
4.80k
                    return;
27
1.20M
                if (*s == u1)
28
632k
                    break;
29
573k
                if (!--attempts) {
30
                    /* if u1 was not found for attempts iterations,
31
                       use FASTSEARCH() or memchr() */
32
29.5k
#ifdef STRINGLIB_FAST_MEMCHR
33
29.5k
                    s++;
34
29.5k
                    s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);
35
29.5k
                    if (s == NULL)
36
6.56k
                        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.0k
                    break;
48
29.5k
                }
49
573k
            }
50
666k
        }
51
20.6M
        *s = u2;
52
20.6M
    }
53
15.4k
}