bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Bug-gnulib] memmem (was: Re: memstr)


From: Simon Josefsson
Subject: [Bug-gnulib] memmem (was: Re: memstr)
Date: Fri, 01 Oct 2004 22:30:03 +0200
User-agent: Gnus/5.110003 (No Gnus v0.3) Emacs/21.3.50 (gnu/linux)

Paul Eggert <address@hidden> writes:

> Simon Josefsson <address@hidden> writes:
>
>> If so, I'm not sure the name "memstr" is appropriate any more.
>
> You're right, the name should be "memmem".  This function is already
> defined by glibc with that name, so perhaps you can borrow its
> implementation.

Ah, right.

I'll try to get the minimal change to memmem.c included in glibc, then
you could sync it from there and get a fresh license template.

Minimally tested, but I'll use it in GnuTLS now, so it will be tested
more eventually.

2004-10-01  Simon Josefsson  <address@hidden>

        * MODULES.html.sh (Extra functions based on ANSI C 89): Add
        memmem.

        * tests/test-memmem.c: New file.

        * modules/memmem: New file.

2004-10-01  Simon Josefsson  <address@hidden>

        * memmem.m4: New file.

2004-10-01  Simon Josefsson  <address@hidden>

        * memmem.h, memmem.c: New file.

Index: modules/memmem
===================================================================
RCS file: modules/memmem
diff -N modules/memmem
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ modules/memmem      1 Oct 2004 20:29:48 -0000
@@ -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
Index: m4/memmem.m4
===================================================================
RCS file: m4/memmem.m4
diff -N m4/memmem.m4
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ m4/memmem.m4        1 Oct 2004 20:29:48 -0000
@@ -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 strnlen().
+  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], [:])
Index: lib/memmem.h
===================================================================
RCS file: lib/memmem.h
diff -N lib/memmem.h
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ lib/memmem.h        1 Oct 2004 20:29:48 -0000
@@ -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: lib/memmem.c
===================================================================
RCS file: lib/memmem.c
diff -N lib/memmem.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ lib/memmem.c        1 Oct 2004 20:29:48 -0000
@@ -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;
+}
Index: tests/test-memmem.c
===================================================================
RCS file: tests/test-memmem.c
diff -N tests/test-memmem.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ tests/test-memmem.c 1 Oct 2004 20:29:48 -0000
@@ -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;
+}





reply via email to

[Prev in Thread] Current Thread [Next in Thread]