Mercurial > hg > octave-lojdl > gnulib-hg
changeset 5310:26382eb5dbd2
New module 'memmem', from Simon Josefsson.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Mon, 04 Oct 2004 11:25:57 +0000 |
parents | a8c8e46043f9 |
children | 1a35ce4adf66 |
files | ChangeLog lib/ChangeLog lib/memmem.c lib/memmem.h m4/ChangeLog m4/memmem.m4 modules/memmem tests/test-memmem.c |
diffstat | 8 files changed, 211 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- 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.
--- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -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.
new file mode 100644 --- /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 <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; +}
new file mode 100644 --- /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 <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 */
--- a/m4/ChangeLog +++ b/m4/ChangeLog @@ -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.
new file mode 100644 --- /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 <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], [:])
new file mode 100644 --- /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
new file mode 100644 --- /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 <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; +}