add memmem()
memmem() is a nice GNU extension for searching a length limited string in another one. This compat version is based on the version found in glibc 2.2 (GPL 2); I only removed the optimization of checking the first char by hand, and generally tried to keep the code simple. We can add it back if memcmp shows up high in a profile, but for now I prefer to keep it (almost trivially) simple. Since I don't really know which platforms beside those with a glibc have their own memmem(), I used a heuristic: if NO_STRCASESTR is set, then NO_MEMMEM is set, too. Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Sep 7, 2007 at 00:32 UTC
b21b9f1de313acb5550c070911ae58c735cdb451
3 files changed
+46
Makefile
+11
@@ -28,6 +28,8 @@ all::
28
#
29
# Define NO_STRCASESTR if you don't have strcasestr.
30
#
31
+# Define NO_MEMMEM if you don't have memmem.
32
+#
33
# Define NO_STRLCPY if you don't have strlcpy.
34
#
35
# Define NO_STRTOUMAX if you don't have strtoumax in the C library.
@@ -402,6 +404,7 @@ ifeq ($(uname_S),SunOS)
404
NEEDS_NSL = YesPlease
405
SHELL_PATH = /bin/bash
406
NO_STRCASESTR = YesPlease
407
+ NO_MEMMEM = YesPlease
408
NO_HSTRERROR = YesPlease
409
ifeq ($(uname_R),5.8)
410
NEEDS_LIBICONV = YesPlease
@@ -424,6 +427,7 @@ ifeq ($(uname_O),Cygwin)
427
NO_D_TYPE_IN_DIRENT = YesPlease
428
NO_D_INO_IN_DIRENT = YesPlease
429
NO_STRCASESTR = YesPlease
430
+ NO_MEMMEM = YesPlease
431
NO_SYMLINK_HEAD = YesPlease
432
NEEDS_LIBICONV = YesPlease
433
NO_FAST_WORKING_DIRECTORY = UnfortunatelyYes
@@ -442,6 +446,7 @@ ifeq ($(uname_S),FreeBSD)
446
endif
447
ifeq ($(uname_S),OpenBSD)
448
NO_STRCASESTR = YesPlease
449
+ NO_MEMMEM = YesPlease
450
NEEDS_LIBICONV = YesPlease
451
BASIC_CFLAGS += -I/usr/local/include
452
BASIC_LDFLAGS += -L/usr/local/lib
@@ -456,6 +461,7 @@ ifeq ($(uname_S),NetBSD)
461
endif
462
ifeq ($(uname_S),AIX)
463
NO_STRCASESTR=YesPlease
464
+ NO_MEMMEM = YesPlease
465
NO_STRLCPY = YesPlease
466
NEEDS_LIBICONV=YesPlease
467
endif
@@ -467,6 +473,7 @@ ifeq ($(uname_S),IRIX64)
473
NO_IPV6=YesPlease
474
NO_SETENV=YesPlease
475
NO_STRCASESTR=YesPlease
476
+ NO_MEMMEM = YesPlease
477
NO_STRLCPY = YesPlease
478
NO_SOCKADDR_STORAGE=YesPlease
479
SHELL_PATH=/usr/gnu/bin/bash
@@ -661,6 +668,10 @@ ifdef NO_HSTRERROR
668
COMPAT_CFLAGS += -DNO_HSTRERROR
669
COMPAT_OBJS += compat/hstrerror.o
670
endif
671
+ifdef NO_MEMMEM
672
+ COMPAT_CFLAGS += -DNO_MEMMEM
673
+ COMPAT_OBJS += compat/memmem.o
674
+endif
675
676
ifeq ($(TCLTK_PATH),)
677
NO_TCLTK=NoThanks
compat/memmem.c
new
+29
@@ -0,0 +1,29 @@
1
+#include "../git-compat-util.h"
2
+
3
+void *gitmemmem(const void *haystack, size_t haystack_len,
4
+ const void *needle, size_t needle_len)
5
+{
6
+ const char *begin = haystack;
7
+ const char *last_possible = begin + haystack_len - needle_len;
8
+
9
+ /*
10
+ * The first occurrence of the empty string is deemed to occur at
11
+ * the beginning of the string.
12
+ */
13
+ if (needle_len == 0)
14
+ return (void *)begin;
15
+
16
+ /*
17
+ * Sanity check, otherwise the loop might search through the whole
18
+ * memory.
19
+ */
20
+ if (haystack_len < needle_len)
21
+ return NULL;
22
+
23
+ for (; begin <= last_possible; begin++) {
24
+ if (!memcmp(begin, needle, needle_len))
25
+ return (void *)begin;
26
+ }
27
+
28
+ return NULL;
29
+}
git-compat-util.h
+6
@@ -172,6 +172,12 @@ extern uintmax_t gitstrtoumax(const char *, char **, int);
172
extern const char *githstrerror(int herror);
173
#endif
174
175
+#ifdef NO_MEMMEM
176
+#define memmem gitmemmem
177
+void *gitmemmem(const void *haystack, size_t haystacklen,
178
+ const void *needle, size_t needlelen);
179
+#endif
180
+
181
extern void release_pack_memory(size_t, int);
182
183
static inline char* xstrdup(const char *str)