changeset 7209:b528c3e6739e

New module 'c-strstr'.
author Bruno Haible <bruno@clisp.org>
date Mon, 28 Aug 2006 13:04:33 +0000
parents 783f359a4ea2
children d75c60e05cde
files ChangeLog MODULES.html.sh lib/ChangeLog lib/c-strstr.c lib/c-strstr.h modules/c-strstr
diffstat 6 files changed, 196 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2006-08-28  Bruno Haible  <bruno@clisp.org>
+
+	* modules/c-strstr: New file, from GNU gettext.
+	* MODULES.html.sh (String handling): Add c-strstr.
+
 2006-08-26  Bruno Haible  <bruno@clisp.org>
 
 	* modules/inttypes (Files): Add lib/inttypes_.h, remove lib/inttypes.h.
--- a/MODULES.html.sh
+++ b/MODULES.html.sh
@@ -1535,6 +1535,7 @@
   func_module strnlen1
   func_module strndup
   func_module strsep
+  func_module c-strstr
   #func_module fstrcmp
   func_module xstrndup
   func_end_table
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -1,3 +1,8 @@
+2006-08-28  Bruno Haible  <bruno@clisp.org>
+
+	* c-strstr.h: New file, from GNU gettext.
+	* c-strstr.c: New file, from GNU gettext.
+
 2006-08-26  Bruno Haible  <bruno@clisp.org>
 
 	* inttypes_.h: New file.
new file mode 100644
--- /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 <config.h>
+#endif
+
+#include <string.h>
+
+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;
+}
new file mode 100644
--- /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
new file mode 100644
--- /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
+