From 37ccb10a1fdf599ceb91d18385b31ce99b17e21c Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Mon, 28 Aug 2006 13:04:33 +0000 Subject: [PATCH] New module 'c-strstr'. --- ChangeLog | 5 +++ MODULES.html.sh | 1 + lib/ChangeLog | 5 +++ lib/c-strstr.c | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/c-strstr.h | 45 +++++++++++++++++++++ modules/c-strstr | 23 +++++++++++ 6 files changed, 196 insertions(+) create mode 100644 lib/c-strstr.c create mode 100644 lib/c-strstr.h create mode 100644 modules/c-strstr diff --git a/ChangeLog b/ChangeLog index 447f6f0ed..0fd747ed1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2006-08-28 Bruno Haible + + * modules/c-strstr: New file, from GNU gettext. + * MODULES.html.sh (String handling): Add c-strstr. + 2006-08-26 Bruno Haible * modules/inttypes (Files): Add lib/inttypes_.h, remove lib/inttypes.h. diff --git a/MODULES.html.sh b/MODULES.html.sh index b6d41a9fb..116dccf58 100755 --- a/MODULES.html.sh +++ b/MODULES.html.sh @@ -1535,6 +1535,7 @@ func_all_modules () func_module strnlen1 func_module strndup func_module strsep + func_module c-strstr #func_module fstrcmp func_module xstrndup func_end_table diff --git a/lib/ChangeLog b/lib/ChangeLog index 3232dd753..c6b9d5fe9 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,8 @@ +2006-08-28 Bruno Haible + + * c-strstr.h: New file, from GNU gettext. + * c-strstr.c: New file, from GNU gettext. + 2006-08-26 Bruno Haible * inttypes_.h: New file. diff --git a/lib/c-strstr.c b/lib/c-strstr.c new file mode 100644 index 000000000..0cf97d6ff --- /dev/null +++ b/lib/c-strstr.c @@ -0,0 +1,117 @@ +/* Copyright (C) 1994, 1999, 2002-2003, 2005-2006 Free Software Foundation, Inc. +This file is part of the GNU C Library. + +This program is free software; you can redistribute it and/or modify +it under 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. + +This program 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 General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software Foundation, +Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +/* + * My personal strstr() implementation that beats most other algorithms. + * Until someone tells me otherwise, I assume that this is the + * fastest implementation of strstr() in C. + * I deliberately chose not to comment it. You should have at least + * as much fun trying to understand it, as I had to write it :-). + * + * Stephen R. van den Berg, berg@pool.informatik.rwth-aachen.de */ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include + +typedef unsigned chartype; + +char * +c_strstr (const char *phaystack, const char *pneedle) +{ + register const unsigned char *haystack, *needle; + register chartype b, c; + + haystack = (const unsigned char *) phaystack; + needle = (const unsigned char *) pneedle; + + b = *needle; + if (b != '\0') + { + haystack--; /* possible ANSI violation */ + do + { + c = *++haystack; + if (c == '\0') + goto ret0; + } + while (c != b); + + c = *++needle; + if (c == '\0') + goto foundneedle; + ++needle; + goto jin; + + for (;;) + { + register chartype a; + register const unsigned char *rhaystack, *rneedle; + + do + { + a = *++haystack; + if (a == '\0') + goto ret0; + if (a == b) + break; + a = *++haystack; + if (a == '\0') + goto ret0; +shloop:; } + while (a != b); + +jin: a = *++haystack; + if (a == '\0') + goto ret0; + + if (a != c) + goto shloop; + + rhaystack = haystack-- + 1; + rneedle = needle; + a = *rneedle; + + if (*rhaystack == a) + do + { + if (a == '\0') + goto foundneedle; + ++rhaystack; + a = *++needle; + if (*rhaystack != a) + break; + if (a == '\0') + goto foundneedle; + ++rhaystack; + a = *++needle; + } + while (*rhaystack == a); + + needle = rneedle; /* took the register-poor approach */ + + if (a == '\0') + break; + } + } +foundneedle: + return (char *) haystack; +ret0: + return 0; +} diff --git a/lib/c-strstr.h b/lib/c-strstr.h new file mode 100644 index 000000000..4ce2db252 --- /dev/null +++ b/lib/c-strstr.h @@ -0,0 +1,45 @@ +/* Searching in a string. + Copyright (C) 2001-2003, 2006 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under 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. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + + +/* The functions defined in this file assume a nearly ASCII compatible + character set. */ + + +#ifdef __cplusplus +extern "C" { +#endif + +/* Find the first occurrence of NEEDLE in HAYSTACK. + This function is safe to be called, even in a multibyte locale, if NEEDLE + 1. consists solely of printable ASCII characters excluding '\\' and '~' + [this restriction is needed because of Shift_JIS and JOHAB] + or of the control ASCII characters '\a' '\b' '\f' '\n' '\r' '\t' '\v' + [this restriction is needed because of VISCII], and + 2. has at least length 2 + [this restriction is needed because of BIG5, BIG5-HKSCS, GBK, GB18030, + Shift_JIS, JOHAB], and + 3. does not consist entirely of decimal digits, or has at least length 4 + [this restricion is needed because of GB18030]. + This function is also safe to be called, even in a multibyte locale, if + HAYSTACK and NEEDLE are known to both consist solely of printable ASCII + characters excluding '\\' and '~'. */ +extern char *c_strstr (const char *haystack, const char *needle); + +#ifdef __cplusplus +} +#endif diff --git a/modules/c-strstr b/modules/c-strstr new file mode 100644 index 000000000..bcc7b77a1 --- /dev/null +++ b/modules/c-strstr @@ -0,0 +1,23 @@ +Description: +Search for a substring in a string in C locale. + +Files: +lib/c-strstr.h +lib/c-strstr.c + +Depends-on: + +configure.ac: + +Makefile.am: +lib_SOURCES += c-strstr.h c-strstr.c + +Include: +"c-strstr.h" + +License: +LGPL + +Maintainer: +Bruno Haible + -- 2.11.0