lib: Remove an optimization in string_replace()
authorVolker Lendecke <vl@samba.org>
Mon, 28 Sep 2020 10:31:13 +0000 (12:31 +0200)
committerJeremy Allison <jra@samba.org>
Wed, 30 Sep 2020 15:58:38 +0000 (15:58 +0000)
Why? This simplifies the code.

Why do I believe we can do this? I don't think this is a very common
operation in critical code paths. Also, next_codepoint() already has
the same optimization. If this turns out to be a measurable
performance issue, we should turn next_codepoint() into a static
inline function doing the 7-bit optimized code path inlined the same
way we did it for tdb_oob(). This way all callers would benefit from
this optimization.

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
lib/util/util_str_common.c

index 1e93a46fbad115ad900ee1d8e1f62ca60784a19a..7bdba9a7a8ce27f5d38e12a31712abf713583867 100644 (file)
@@ -72,40 +72,16 @@ _PUBLIC_ size_t ucs2_align(const void *base_ptr, const void *p, int flags)
 **/
 void string_replace( char *s, char oldc, char newc )
 {
-       char *p;
-
-       /* this is quite a common operation, so we want it to be
-          fast. We optimise for the ascii case, knowing that all our
-          supported multi-byte character sets are ascii-compatible
-          (ie. they match for the first 128 chars) */
-
-       for (p = s; *p; p++) {
-               if (*p & 0x80) /* mb string - slow path. */
-                       break;
-               if (*p == oldc) {
-                       *p = newc;
-               }
-       }
-
-       if (!*p)
-               return;
-
-       /* Slow (mb) path. */
-#ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
-       /* With compose characters we must restart from the beginning. JRA. */
-       p = s;
-#endif
-
-       while (*p) {
+       while (*s != '\0') {
                size_t c_size;
-               next_codepoint(p, &c_size);
+               next_codepoint(s, &c_size);
 
                if (c_size == 1) {
-                       if (*p == oldc) {
-                               *p = newc;
+                       if (*s == oldc) {
+                               *s = newc;
                        }
                }
-               p += c_size;
+               s += c_size;
        }
 }