/* stringlib: replace implementation */12#ifndef STRINGLIB_FASTSEARCH_H3#error must include "stringlib/fastsearch.h" before including this module4#endif56Py_LOCAL_INLINE(void)7STRINGLIB(replace_1char_inplace)(STRINGLIB_CHAR* s, STRINGLIB_CHAR* end,8Py_UCS4 u1, Py_UCS4 u2, Py_ssize_t maxcount)9{10*s = u2;11while (--maxcount && ++s != end) {12/* Find the next character to be replaced.1314If it occurs often, it is faster to scan for it using an inline15loop. If it occurs seldom, it is faster to scan for it using a16function call; the overhead of the function call is amortized17across the many characters that call covers. We start with an18inline loop and use a heuristic to determine whether to fall back19to a function call. */20if (*s != u1) {21int attempts = 10;22/* search u1 in a dummy loop */23while (1) {24if (++s == end)25return;26if (*s == u1)27break;28if (!--attempts) {29/* if u1 was not found for attempts iterations,30use FASTSEARCH() or memchr() */31#ifdef STRINGLIB_FAST_MEMCHR32s++;33s = STRINGLIB_FAST_MEMCHR(s, u1, end - s);34if (s == NULL)35return;36#else37Py_ssize_t i;38STRINGLIB_CHAR ch1 = (STRINGLIB_CHAR) u1;39s++;40i = FASTSEARCH(s, end - s, &ch1, 1, 0, FAST_SEARCH);41if (i < 0)42return;43s += i;44#endif45/* restart the dummy loop */46break;47}48}49}50*s = u2;51}52}535455