[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Bug-gnulib] memstr (was: Re: strnstr)
From: |
Simon Josefsson |
Subject: |
[Bug-gnulib] memstr (was: Re: strnstr) |
Date: |
Thu, 30 Sep 2004 23:28:17 +0200 |
User-agent: |
Gnus/5.110003 (No Gnus v0.3) Emacs/21.3.50 (gnu/linux) |
How about this? There are some corner cases for memstr. There is a
self test included below, that could be added to, in case someone can
trigger a bug.
The M4 macro, and memstr.h, can handle if the system define memstr,
but I'm not sure that is useful. I did found a previous use of the
function, though:
http://www.thinkage.ca/english/gcos/expl/c/lib/memstr.html
It is not clear if that is exactly the same interface as the function
below.
2004-09-29 Simon Josefsson <address@hidden>
* MODULES.html.sh (Extra functions based on ANSI C 89): Add
memstr.
* tests/test-memstr.c: New file.
* modules/memstr: New file.
2004-09-29 Simon Josefsson <address@hidden>
* memstr.h, memstr.c: New file.
2004-09-29 Simon Josefsson <address@hidden>
* memstr.m4: New file.
Index: modules/memstr
===================================================================
RCS file: modules/memstr
diff -N modules/memstr
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ modules/memstr 30 Sep 2004 21:27:21 -0000
@@ -0,0 +1,24 @@
+Description:
+memstr() function: locate first substring in a buffer.
+
+Files:
+lib/memstr.h
+lib/memstr.c
+m4/memstr.m4
+
+Depends-on:
+
+configure.ac:
+gl_FUNC_MEMSTR
+
+Makefile.am:
+lib_SOURCES += memstr.h
+
+Include:
+"memstr.h"
+
+License:
+LGPL
+
+Maintainer:
+Simon Josefsson
Index: m4/memstr.m4
===================================================================
RCS file: m4/memstr.m4
diff -N m4/memstr.m4
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ m4/memstr.m4 30 Sep 2004 21:27:21 -0000
@@ -0,0 +1,17 @@
+# memstr.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_MEMSTR],
+[
+ AC_REPLACE_FUNCS(memstr)
+ AC_CHECK_DECLS_ONCE(memstr)
+ gl_PREREQ_MEMSTR
+])
+
+# Prerequisites of lib/memstr.c.
+AC_DEFUN([gl_PREREQ_MEMSTR], [:])
Index: lib/memstr.h
===================================================================
RCS file: lib/memstr.h
diff -N lib/memstr.h
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ lib/memstr.h 30 Sep 2004 21:27:21 -0000
@@ -0,0 +1,30 @@
+/*
+ * 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 MEMSTR_H
+# define MEMSTR_H
+
+/* Get memstr, if available. */
+# include <string.h>
+
+# if defined HAVE_DECL_MEMSTR && !HAVE_DECL_MEMSTR
+char *memstr (const char *big, const char *little, size_t len);
+# endif
+
+#endif /* MEMSTR_H */
Index: lib/memstr.c
===================================================================
RCS file: lib/memstr.c
diff -N lib/memstr.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ lib/memstr.c 30 Sep 2004 21:27:21 -0000
@@ -0,0 +1,50 @@
+/*
+ * 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
+
+/* Get strlen, strncmp, size_t. */
+#include <string.h>
+
+/* Locate first occurance of zero terminated string LITTLE in the
+ first LEN characters of the string BIG. The terminating zero in
+ LITTLE is not used for comparison. Return pointer to match within
+ BIG, or NULL if no match is found. If LITTLE is the empty string,
+ BIG is returned. */
+char *
+memstr (const char *big, const char *little, size_t len)
+{
+ size_t littlelen = strlen (little);
+ char *p = (char*) big;
+ size_t i;
+
+ if (*little == '\0')
+ return p;
+
+ if (len < littlelen)
+ return NULL;
+
+ for (i = 0; i <= len - littlelen; i++)
+ if (memcmp (&p[i], little, littlelen) == 0)
+ return &p[i];
+
+ return NULL;
+}
Index: MODULES.html.sh
===================================================================
RCS file: /cvsroot/gnulib/gnulib/MODULES.html.sh,v
retrieving revision 1.61
diff -u -p -r1.61 MODULES.html.sh
--- MODULES.html.sh 29 Sep 2004 22:10:44 -0000 1.61
+++ MODULES.html.sh 30 Sep 2004 21:27:21 -0000
@@ -1512,6 +1512,7 @@ func_all_modules ()
func_module bcopy
func_module mempcpy
func_module memrchr
+ func_module memstr
func_module stpcpy
func_module stpncpy
func_module strcase
Index: tests/test-memstr.c
===================================================================
RCS file: tests/test-memstr.c
diff -N tests/test-memstr.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ tests/test-memstr.c 30 Sep 2004 21:27:21 -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 <memstr.h>
+
+int
+main (int argc, char *argv[])
+{
+ const char *big = "foo-bar-baz";
+ char *p;
+
+ p = memstr (big, "foo", strlen (big));
+ if (p != big)
+ {
+ fprintf (stderr, "memstr FAILURE 1\n");
+ return 1;
+ }
+
+ p = memstr (big, "baz", strlen (big));
+ if (p != big + strlen (big) - 3)
+ {
+ fprintf (stderr, "memstr FAILURE 2\n");
+ return 1;
+
+ p = memstr (big, "-", strlen (big));
+ if (p != big + 3)
+ {
+ fprintf (stderr, "memstr FAILURE 3\n");
+ return 1;
+ }
+
+ p = memstr (big, "baz", strlen (big) - 1);
+ if (p != NULL)
+ {
+ fprintf (stderr, "memstr FAILURE 4\n");
+ return 1;
+ }
+
+ return 0;
+}