]> Savannah Git Hosting - gnulib.git/commitdiff
unistr/u8-strstr: Simplify code.
authorBruno Haible <bruno@clisp.org>
Sun, 2 Apr 2023 14:10:05 +0000 (16:10 +0200)
committerBruno Haible <bruno@clisp.org>
Sun, 2 Apr 2023 14:10:05 +0000 (16:10 +0200)
* lib/unistr/u8-strstr.c: Inline the contents of lib/unistr/u-strstr.h.
* lib/unistr/u-strstr.h: Remove file.
* modules/unistr/u8-strstr (Files): Remove it.

ChangeLog
lib/unistr/u-strstr.h [deleted file]
lib/unistr/u8-strstr.c
modules/unistr/u8-strstr

index f933d1e94db3fd4c32df5d8dbbd74a8873acda53..463274abee7ebe1b962ce153bf74dd61c9e9d412 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2023-04-02  Bruno Haible  <bruno@clisp.org>
 
+       unistr/u8-strstr: Simplify code.
+       * lib/unistr/u8-strstr.c: Inline the contents of lib/unistr/u-strstr.h.
+       * lib/unistr/u-strstr.h: Remove file.
+       * modules/unistr/u8-strstr (Files): Remove it.
+
        unistr/u{16,32}-strstr: Use two-way algorithm (no memory allocation).
        * lib/wcs-two-way.h: Use UNIT instead of wchar_t. Don't undefine
        RETURN_TYPE.
diff --git a/lib/unistr/u-strstr.h b/lib/unistr/u-strstr.h
deleted file mode 100644 (file)
index c0a1435..0000000
+++ /dev/null
@@ -1,139 +0,0 @@
-/* Substring test for UTF-8/UTF-16/UTF-32 strings.  -*- coding: utf-8 -*-
-   Copyright (C) 1999, 2002, 2006, 2010-2023 Free Software Foundation, Inc.
-   Written by Bruno Haible <bruno@clisp.org>, 2002, 2005.
-
-   This file is free software.
-   It is dual-licensed under "the GNU LGPLv3+ or the GNU GPLv2+".
-   You can redistribute it and/or modify it under either
-     - the terms of the GNU Lesser General Public License as published
-       by the Free Software Foundation, either version 3, or (at your
-       option) any later version, or
-     - the terms of the GNU General Public License as published by the
-       Free Software Foundation; either version 2, or (at your option)
-       any later version, or
-     - the same dual license "the GNU LGPLv3+ or the GNU GPLv2+".
-
-   This file is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Lesser General Public License and the GNU General Public License
-   for more details.
-
-   You should have received a copy of the GNU Lesser General Public
-   License and of the GNU General Public License along with this
-   program.  If not, see <https://www.gnu.org/licenses/>.  */
-
-UNIT *
-FUNC (const UNIT *haystack, const UNIT *needle)
-{
-  UNIT first = needle[0];
-
-  /* Is needle empty?  */
-  if (first == 0)
-    return (UNIT *) haystack;
-
-  /* Is needle nearly empty (only one unit)?  */
-  if (needle[1] == 0)
-    return U_STRCHR (haystack, first);
-
-#ifdef U_STRMBTOUC
-  /* Is needle nearly empty (only one character)?  */
-  {
-    ucs4_t first_uc;
-    int count = U_STRMBTOUC (&first_uc, needle);
-    if (count > 0 && needle[count] == 0)
-      return U_STRCHR (haystack, first_uc);
-  }
-#endif
-
-#if UNIT_IS_UINT8_T
-  return (uint8_t *) strstr ((const char *) haystack, (const char *) needle);
-#else
-  {
-    /* Minimizing the worst-case complexity:
-       Let n = U_STRLEN(haystack), m = U_STRLEN(needle).
-       The naïve algorithm is O(n*m) worst-case.
-       The Knuth-Morris-Pratt algorithm is O(n) worst-case but it needs a
-       memory allocation.
-       To achieve linear complexity and yet amortize the cost of the
-       memory allocation, we activate the Knuth-Morris-Pratt algorithm
-       only once the naïve algorithm has already run for some time; more
-       precisely, when
-         - the outer loop count is >= 10,
-         - the average number of comparisons per outer loop is >= 5,
-         - the total number of comparisons is >= m.
-       But we try it only once.  If the memory allocation attempt failed,
-       we don't retry it.  */
-    bool try_kmp = true;
-    size_t outer_loop_count = 0;
-    size_t comparison_count = 0;
-    size_t last_ccount = 0;                  /* last comparison count */
-    const UNIT *needle_last_ccount = needle; /* = needle + last_ccount */
-
-    /* Speed up the following searches of needle by caching its first
-       character.  */
-    UNIT b = *needle++;
-
-    for (;; haystack++)
-      {
-        if (*haystack == 0)
-          /* No match.  */
-          return NULL;
-
-        /* See whether it's advisable to use an asymptotically faster
-           algorithm.  */
-        if (try_kmp
-            && outer_loop_count >= 10
-            && comparison_count >= 5 * outer_loop_count)
-          {
-            /* See if needle + comparison_count now reaches the end of
-               needle.  */
-            if (needle_last_ccount != NULL)
-              {
-                needle_last_ccount +=
-                  U_STRNLEN (needle_last_ccount,
-                             comparison_count - last_ccount);
-                if (*needle_last_ccount == 0)
-                  needle_last_ccount = NULL;
-                last_ccount = comparison_count;
-              }
-            if (needle_last_ccount == NULL)
-              {
-                /* Try the Knuth-Morris-Pratt algorithm.  */
-                const UNIT *result;
-                bool success =
-                  knuth_morris_pratt (haystack,
-                                      needle - 1, U_STRLEN (needle - 1),
-                                      &result);
-                if (success)
-                  return (UNIT *) result;
-                try_kmp = false;
-              }
-          }
-
-        outer_loop_count++;
-        comparison_count++;
-        if (*haystack == b)
-          /* The first character matches.  */
-          {
-            const UNIT *rhaystack = haystack + 1;
-            const UNIT *rneedle = needle;
-
-            for (;; rhaystack++, rneedle++)
-              {
-                if (*rneedle == 0)
-                  /* Found a match.  */
-                  return (UNIT *) haystack;
-                if (*rhaystack == 0)
-                  /* No match.  */
-                  return NULL;
-                comparison_count++;
-                if (*rhaystack != *rneedle)
-                  /* Nothing in this round.  */
-                  break;
-              }
-          }
-      }
-  }
-#endif
-}
index 13dceca57e7cf3867147aed8df940b36609ab598..fbb483ac24ed76cd4a34c1fbec745e98950b0f40 100644 (file)
 
 #include <string.h>
 
-/* FIXME: Maybe walking the string via u8_mblen is a win?  */
-
-#define FUNC u8_strstr
-#define UNIT uint8_t
-#define U_STRCHR u8_strchr
-#define U_STRMBTOUC u8_strmbtouc
-#define UNIT_IS_UINT8_T 1
-#include "u-strstr.h"
+uint8_t *
+u8_strstr (const uint8_t *haystack, const uint8_t *needle)
+{
+  uint8_t first = needle[0];
+
+  /* Is needle empty?  */
+  if (first == 0)
+    return (uint8_t *) haystack;
+
+  /* Is needle nearly empty (only one unit)?  */
+  if (needle[1] == 0)
+    return u8_strchr (haystack, first);
+
+  /* Is needle nearly empty (only one character)?  */
+  {
+    ucs4_t first_uc;
+    int count = u8_strmbtouc (&first_uc, needle);
+    if (count > 0 && needle[count] == 0)
+      return u8_strchr (haystack, first_uc);
+  }
+
+  return (uint8_t *) strstr ((const char *) haystack, (const char *) needle);
+}
index 8cd99690e7698423f0e343802a1cf69d3a24393f..87fc9d6685518214b7458643a9e3039ebc870e71 100644 (file)
@@ -3,7 +3,6 @@ Substring test for UTF-8 strings.
 
 Files:
 lib/unistr/u8-strstr.c
-lib/unistr/u-strstr.h
 
 Depends-on:
 unistr/base