New module 'memmem', from Simon Josefsson.
authorBruno Haible <bruno@clisp.org>
Mon, 4 Oct 2004 11:25:57 +0000 (11:25 +0000)
committerBruno Haible <bruno@clisp.org>
Mon, 4 Oct 2004 11:25:57 +0000 (11:25 +0000)
ChangeLog
lib/ChangeLog
lib/memmem.c [new file with mode: 0644]
lib/memmem.h [new file with mode: 0644]
m4/ChangeLog
m4/memmem.m4 [new file with mode: 0644]
modules/memmem [new file with mode: 0644]
tests/test-memmem.c [new file with mode: 0644]

index cd8bad00eeb6f27a108e28c0c6c4e0473a32a6c8..220e1d0f8233adeb7eb46a711e414903cea50337 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2004-10-01  Simon Josefsson  <jas@extundo.com>
+
+       * modules/memmem: New file.
+       * tests/test-memmem.c: New file.
+       * MODULES.html.sh (Extra functions based on ANSI C 89): Add memmem.
+
 2004-10-01  Bruno Haible  <bruno@clisp.org>
 
        * MODULES.html.sh: Add strsep.
index aaa22944df3210c5cfb431b393fe6e85fb086a96..a41db01d21d9310fac7e304e9665c3deec852b46 100644 (file)
@@ -1,3 +1,8 @@
+2004-10-01  Simon Josefsson  <jas@extundo.com>
+
+       * memmem.h: New file.
+       * memmem.c: New file, taken from glibc.
+
 2004-10-03  Paul Eggert  <eggert@cs.ucla.edu>
 
        Sync from coreutils.
diff --git a/lib/memmem.c b/lib/memmem.c
new file mode 100644 (file)
index 0000000..7a6e116
--- /dev/null
@@ -0,0 +1,58 @@
+/* Copyright (C) 1991,92,93,94,96,97,98,2000,2004 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library 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 for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, write to the Free
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
+
+#include <stddef.h>
+#include <string.h>
+
+#ifndef _LIBC
+# define __builtin_expect(expr, val)   (expr)
+#endif
+
+#undef memmem
+
+/* Return the first occurrence of NEEDLE in HAYSTACK.  */
+void *
+memmem (haystack, haystack_len, needle, needle_len)
+     const void *haystack;
+     size_t haystack_len;
+     const void *needle;
+     size_t needle_len;
+{
+  const char *begin;
+  const char *const last_possible
+    = (const char *) haystack + haystack_len - needle_len;
+
+  if (needle_len == 0)
+    /* The first occurrence of the empty string is deemed to occur at
+       the beginning of the string.  */
+    return (void *) haystack;
+
+  /* Sanity check, otherwise the loop might search through the whole
+     memory.  */
+  if (__builtin_expect (haystack_len < needle_len, 0))
+    return NULL;
+
+  for (begin = (const char *) haystack; begin <= last_possible; ++begin)
+    if (begin[0] == ((const char *) needle)[0] &&
+       !memcmp ((const void *) &begin[1],
+                (const void *) ((const char *) needle + 1),
+                needle_len - 1))
+      return (void *) begin;
+
+  return NULL;
+}
diff --git a/lib/memmem.h b/lib/memmem.h
new file mode 100644 (file)
index 0000000..47dcbd8
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2004 Free Software Foundation
+ * Written by Simon Josefsson
+ *
+ * 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., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.  */
+
+#ifndef MEMMEM_H
+# define MEMMEM_H
+
+/* Get memmem, if available. */
+# include <string.h>
+
+# if defined HAVE_DECL_MEMMEM && !HAVE_DECL_MEMMEM
+void *
+memmem (const void *haystack, size_t haystack_len,
+       const void *needle, size_t needle_len);
+# endif
+
+#endif /* MEMMEM_H */
index 7e58d06dbe256fb51f53f44a6bcb1bb1e7279d50..bad15533df3fe82eec31b8260f3ec352f725b50b 100644 (file)
@@ -1,3 +1,7 @@
+2004-10-01  Simon Josefsson  <jas@extundo.com>
+
+       * memmem.m4: New file.
+
 2004-10-01  Yoann Vandoorselaere <yoann@prelude-ids.org>
 
        * strsep.m4: New file.
diff --git a/m4/memmem.m4 b/m4/memmem.m4
new file mode 100644 (file)
index 0000000..2e4a20b
--- /dev/null
@@ -0,0 +1,20 @@
+# memmem.m4 serial 1
+dnl Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
+dnl This file is free software, distributed under the terms of the GNU
+dnl General Public License.  As a special exception to the GNU General
+dnl Public License, this file may be distributed as part of a program
+dnl that contains a configuration script generated by Autoconf, under
+dnl the same distribution terms as the rest of that program.
+
+AC_DEFUN([gl_FUNC_MEMMEM],
+[
+  dnl Persuade glibc <string.h> to declare memmem().
+  AC_REQUIRE([AC_GNU_SOURCE])
+
+  AC_REPLACE_FUNCS(memmem)
+  AC_CHECK_DECLS_ONCE(memmem)
+  gl_PREREQ_MEMMEM
+])
+
+# Prerequisites of lib/memmem.c.
+AC_DEFUN([gl_PREREQ_MEMMEM], [:])
diff --git a/modules/memmem b/modules/memmem
new file mode 100644 (file)
index 0000000..72ebd21
--- /dev/null
@@ -0,0 +1,24 @@
+Description:
+memmem() function: locate first substring in a buffer.
+
+Files:
+lib/memmem.h
+lib/memmem.c
+m4/memmem.m4
+
+Depends-on:
+
+configure.ac:
+gl_FUNC_MEMMEM
+
+Makefile.am:
+lib_SOURCES += memmem.h
+
+Include:
+"memmem.h"
+
+License:
+LGPL
+
+Maintainer:
+libc, Simon Josefsson
diff --git a/tests/test-memmem.c b/tests/test-memmem.c
new file mode 100644 (file)
index 0000000..24ef8cc
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2004 Free Software Foundation
+ * Written by Simon Josefsson
+ *
+ * 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., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.  */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdio.h>
+#include <string.h>
+#include "memmem.h"
+
+int
+main (int argc, char *argv[])
+{
+  const char *big = "foo-bar-baz";
+  char *p;
+
+  p = memmem (big, "foo", strlen (big));
+  if (p != big)
+    {
+      fprintf (stderr, "memmem FAILURE 1\n");
+      return 1;
+    }
+
+  p = memmem (big, "baz", strlen (big));
+  if (p != big + strlen (big) - 3)
+    {
+      fprintf (stderr, "memmem FAILURE 2\n");
+      return 1;
+
+  p = memmem (big, "-", strlen (big));
+  if (p != big + 3)
+    {
+      fprintf (stderr, "memmem FAILURE 3\n");
+      return 1;
+    }
+
+  p = memmem (big, "baz", strlen (big) - 1);
+  if (p != NULL)
+    {
+      fprintf (stderr, "memmem FAILURE 4\n");
+      return 1;
+    }
+
+  return 0;
+}