From ec03ca352064740b271b27e0be2b562b687b4554 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Mon, 4 Oct 2004 11:25:57 +0000 Subject: [PATCH] New module 'memmem', from Simon Josefsson. --- ChangeLog | 6 ++++++ lib/ChangeLog | 5 +++++ lib/memmem.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/memmem.h | 32 +++++++++++++++++++++++++++ m4/ChangeLog | 4 ++++ m4/memmem.m4 | 20 +++++++++++++++++ modules/memmem | 24 +++++++++++++++++++++ tests/test-memmem.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 211 insertions(+) create mode 100644 lib/memmem.c create mode 100644 lib/memmem.h create mode 100644 m4/memmem.m4 create mode 100644 modules/memmem create mode 100644 tests/test-memmem.c diff --git a/ChangeLog b/ChangeLog index cd8bad00e..220e1d0f8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2004-10-01 Simon Josefsson + + * 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 * MODULES.html.sh: Add strsep. diff --git a/lib/ChangeLog b/lib/ChangeLog index aaa22944d..a41db01d2 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,8 @@ +2004-10-01 Simon Josefsson + + * memmem.h: New file. + * memmem.c: New file, taken from glibc. + 2004-10-03 Paul Eggert Sync from coreutils. diff --git a/lib/memmem.c b/lib/memmem.c new file mode 100644 index 000000000..7a6e116fc --- /dev/null +++ b/lib/memmem.c @@ -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 +#include + +#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 index 000000000..47dcbd836 --- /dev/null +++ b/lib/memmem.h @@ -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 + +# 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 */ diff --git a/m4/ChangeLog b/m4/ChangeLog index 7e58d06db..bad15533d 100644 --- a/m4/ChangeLog +++ b/m4/ChangeLog @@ -1,3 +1,7 @@ +2004-10-01 Simon Josefsson + + * memmem.m4: New file. + 2004-10-01 Yoann Vandoorselaere * strsep.m4: New file. diff --git a/m4/memmem.m4 b/m4/memmem.m4 new file mode 100644 index 000000000..2e4a20bc3 --- /dev/null +++ b/m4/memmem.m4 @@ -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 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 index 000000000..72ebd2114 --- /dev/null +++ b/modules/memmem @@ -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 index 000000000..24ef8cc47 --- /dev/null +++ b/tests/test-memmem.c @@ -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 +#endif + +#include +#include +#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; +} -- 2.11.0