From: Bruno Haible Date: Sun, 2 Apr 2023 14:10:05 +0000 (+0200) Subject: unistr/u8-strstr: Simplify code. X-Git-Tag: v1.0~1531 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=1ca053ce4128c8a8fbd0ece8b4a26ec5ce791933;p=gnulib.git 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. --- diff --git a/ChangeLog b/ChangeLog index f933d1e94d..463274abee 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ 2023-04-02 Bruno Haible + 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 index c0a1435587..0000000000 --- a/lib/unistr/u-strstr.h +++ /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 , 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 . */ - -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 -} diff --git a/lib/unistr/u8-strstr.c b/lib/unistr/u8-strstr.c index 13dceca57e..fbb483ac24 100644 --- a/lib/unistr/u8-strstr.c +++ b/lib/unistr/u8-strstr.c @@ -30,11 +30,26 @@ #include -/* 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); +} diff --git a/modules/unistr/u8-strstr b/modules/unistr/u8-strstr index 8cd99690e7..87fc9d6685 100644 --- a/modules/unistr/u8-strstr +++ b/modules/unistr/u8-strstr @@ -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