diff options
Diffstat (limited to 'gl')
-rw-r--r-- | gl/Makefile.am | 11 | ||||
-rw-r--r-- | gl/m4/gnulib-cache.m4 | 3 | ||||
-rw-r--r-- | gl/m4/gnulib-comp.m4 | 15 | ||||
-rw-r--r-- | gl/m4/strcasestr.m4 | 142 | ||||
-rw-r--r-- | gl/strcasestr.c | 82 |
5 files changed, 251 insertions, 2 deletions
diff --git a/gl/Makefile.am b/gl/Makefile.am index 54abb4c..15135c8 100644 --- a/gl/Makefile.am +++ b/gl/Makefile.am | |||
@@ -21,7 +21,7 @@ | |||
21 | # the same distribution terms as the rest of that program. | 21 | # the same distribution terms as the rest of that program. |
22 | # | 22 | # |
23 | # Generated by gnulib-tool. | 23 | # Generated by gnulib-tool. |
24 | # Reproduce by: gnulib-tool --import --dir=. --lib=libgnu --source-base=gl --m4-base=gl/m4 --doc-base=doc --tests-base=tests --aux-dir=build-aux --no-conditional-dependencies --no-libtool --macro-prefix=gl --no-vc-files base64 crypto/sha1 dirname environ floorf fsusage getaddrinfo gethostname getloadavg getopt-gnu gettext idpriv-droptemp mountlist regex setenv strcase strsep timegm unsetenv vasprintf vsnprintf | 24 | # Reproduce by: gnulib-tool --import --dir=. --lib=libgnu --source-base=gl --m4-base=gl/m4 --doc-base=doc --tests-base=tests --aux-dir=build-aux --no-conditional-dependencies --no-libtool --macro-prefix=gl --no-vc-files base64 crypto/sha1 dirname environ floorf fsusage getaddrinfo gethostname getloadavg getopt-gnu gettext idpriv-droptemp mountlist regex setenv strcase strcasestr strsep timegm unsetenv vasprintf vsnprintf |
25 | 25 | ||
26 | AUTOMAKE_OPTIONS = 1.9.6 gnits subdir-objects | 26 | AUTOMAKE_OPTIONS = 1.9.6 gnits subdir-objects |
27 | 27 | ||
@@ -1553,6 +1553,15 @@ EXTRA_libgnu_a_SOURCES += strcasecmp.c strncasecmp.c | |||
1553 | 1553 | ||
1554 | ## end gnulib module strcase | 1554 | ## end gnulib module strcase |
1555 | 1555 | ||
1556 | ## begin gnulib module strcasestr-simple | ||
1557 | |||
1558 | |||
1559 | EXTRA_DIST += str-two-way.h strcasestr.c | ||
1560 | |||
1561 | EXTRA_libgnu_a_SOURCES += strcasestr.c | ||
1562 | |||
1563 | ## end gnulib module strcasestr-simple | ||
1564 | |||
1556 | ## begin gnulib module streq | 1565 | ## begin gnulib module streq |
1557 | 1566 | ||
1558 | 1567 | ||
diff --git a/gl/m4/gnulib-cache.m4 b/gl/m4/gnulib-cache.m4 index d6fca2a..90ad4aa 100644 --- a/gl/m4/gnulib-cache.m4 +++ b/gl/m4/gnulib-cache.m4 | |||
@@ -27,7 +27,7 @@ | |||
27 | 27 | ||
28 | 28 | ||
29 | # Specification in the form of a command-line invocation: | 29 | # Specification in the form of a command-line invocation: |
30 | # gnulib-tool --import --dir=. --lib=libgnu --source-base=gl --m4-base=gl/m4 --doc-base=doc --tests-base=tests --aux-dir=build-aux --no-conditional-dependencies --no-libtool --macro-prefix=gl --no-vc-files base64 crypto/sha1 dirname environ floorf fsusage getaddrinfo gethostname getloadavg getopt-gnu gettext idpriv-droptemp mountlist regex setenv strcase strsep timegm unsetenv vasprintf vsnprintf | 30 | # gnulib-tool --import --dir=. --lib=libgnu --source-base=gl --m4-base=gl/m4 --doc-base=doc --tests-base=tests --aux-dir=build-aux --no-conditional-dependencies --no-libtool --macro-prefix=gl --no-vc-files base64 crypto/sha1 dirname environ floorf fsusage getaddrinfo gethostname getloadavg getopt-gnu gettext idpriv-droptemp mountlist regex setenv strcase strcasestr strsep timegm unsetenv vasprintf vsnprintf |
31 | 31 | ||
32 | # Specification in the form of a few gnulib-tool.m4 macro invocations: | 32 | # Specification in the form of a few gnulib-tool.m4 macro invocations: |
33 | gl_LOCAL_DIR([]) | 33 | gl_LOCAL_DIR([]) |
@@ -48,6 +48,7 @@ gl_MODULES([ | |||
48 | regex | 48 | regex |
49 | setenv | 49 | setenv |
50 | strcase | 50 | strcase |
51 | strcasestr | ||
51 | strsep | 52 | strsep |
52 | timegm | 53 | timegm |
53 | unsetenv | 54 | unsetenv |
diff --git a/gl/m4/gnulib-comp.m4 b/gl/m4/gnulib-comp.m4 index 67a8156..9a4f502 100644 --- a/gl/m4/gnulib-comp.m4 +++ b/gl/m4/gnulib-comp.m4 | |||
@@ -121,6 +121,8 @@ AC_DEFUN([gl_EARLY], | |||
121 | # Code from module stdio: | 121 | # Code from module stdio: |
122 | # Code from module stdlib: | 122 | # Code from module stdlib: |
123 | # Code from module strcase: | 123 | # Code from module strcase: |
124 | # Code from module strcasestr: | ||
125 | # Code from module strcasestr-simple: | ||
124 | # Code from module streq: | 126 | # Code from module streq: |
125 | # Code from module strerror: | 127 | # Code from module strerror: |
126 | # Code from module strerror-override: | 128 | # Code from module strerror-override: |
@@ -390,6 +392,17 @@ AC_DEFUN([gl_INIT], | |||
390 | AC_LIBOBJ([strncasecmp]) | 392 | AC_LIBOBJ([strncasecmp]) |
391 | gl_PREREQ_STRNCASECMP | 393 | gl_PREREQ_STRNCASECMP |
392 | fi | 394 | fi |
395 | gl_FUNC_STRCASESTR | ||
396 | if test $HAVE_STRCASESTR = 0 || test $REPLACE_STRCASESTR = 1; then | ||
397 | AC_LIBOBJ([strcasestr]) | ||
398 | gl_PREREQ_STRCASESTR | ||
399 | fi | ||
400 | gl_FUNC_STRCASESTR_SIMPLE | ||
401 | if test $HAVE_STRCASESTR = 0 || test $REPLACE_STRCASESTR = 1; then | ||
402 | AC_LIBOBJ([strcasestr]) | ||
403 | gl_PREREQ_STRCASESTR | ||
404 | fi | ||
405 | gl_STRING_MODULE_INDICATOR([strcasestr]) | ||
393 | gl_FUNC_STRERROR | 406 | gl_FUNC_STRERROR |
394 | if test $REPLACE_STRERROR = 1; then | 407 | if test $REPLACE_STRERROR = 1; then |
395 | AC_LIBOBJ([strerror]) | 408 | AC_LIBOBJ([strerror]) |
@@ -723,6 +736,7 @@ AC_DEFUN([gl_FILE_LIST], [ | |||
723 | lib/stdlib.in.h | 736 | lib/stdlib.in.h |
724 | lib/str-two-way.h | 737 | lib/str-two-way.h |
725 | lib/strcasecmp.c | 738 | lib/strcasecmp.c |
739 | lib/strcasestr.c | ||
726 | lib/streq.h | 740 | lib/streq.h |
727 | lib/strerror-override.c | 741 | lib/strerror-override.c |
728 | lib/strerror-override.h | 742 | lib/strerror-override.h |
@@ -866,6 +880,7 @@ AC_DEFUN([gl_FILE_LIST], [ | |||
866 | m4/stdio_h.m4 | 880 | m4/stdio_h.m4 |
867 | m4/stdlib_h.m4 | 881 | m4/stdlib_h.m4 |
868 | m4/strcase.m4 | 882 | m4/strcase.m4 |
883 | m4/strcasestr.m4 | ||
869 | m4/strerror.m4 | 884 | m4/strerror.m4 |
870 | m4/string_h.m4 | 885 | m4/string_h.m4 |
871 | m4/strings_h.m4 | 886 | m4/strings_h.m4 |
diff --git a/gl/m4/strcasestr.m4 b/gl/m4/strcasestr.m4 new file mode 100644 index 0000000..8681a6a --- /dev/null +++ b/gl/m4/strcasestr.m4 | |||
@@ -0,0 +1,142 @@ | |||
1 | # strcasestr.m4 serial 21 | ||
2 | dnl Copyright (C) 2005, 2007-2013 Free Software Foundation, Inc. | ||
3 | dnl This file is free software; the Free Software Foundation | ||
4 | dnl gives unlimited permission to copy and/or distribute it, | ||
5 | dnl with or without modifications, as long as this notice is preserved. | ||
6 | |||
7 | dnl Check that strcasestr is present and works. | ||
8 | AC_DEFUN([gl_FUNC_STRCASESTR_SIMPLE], | ||
9 | [ | ||
10 | AC_REQUIRE([gl_HEADER_STRING_H_DEFAULTS]) | ||
11 | |||
12 | dnl Persuade glibc <string.h> to declare strcasestr(). | ||
13 | AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS]) | ||
14 | |||
15 | AC_REQUIRE([gl_FUNC_MEMCHR]) | ||
16 | AC_CHECK_FUNCS([strcasestr]) | ||
17 | if test $ac_cv_func_strcasestr = no; then | ||
18 | HAVE_STRCASESTR=0 | ||
19 | else | ||
20 | if test "$gl_cv_func_memchr_works" != yes; then | ||
21 | REPLACE_STRCASESTR=1 | ||
22 | else | ||
23 | dnl Detect http://sourceware.org/bugzilla/show_bug.cgi?id=12092. | ||
24 | AC_CACHE_CHECK([whether strcasestr works], | ||
25 | [gl_cv_func_strcasestr_works_always], | ||
26 | [AC_RUN_IFELSE([AC_LANG_PROGRAM([[ | ||
27 | #include <string.h> /* for strcasestr */ | ||
28 | #define P "_EF_BF_BD" | ||
29 | #define HAYSTACK "F_BD_CE_BD" P P P P "_C3_88_20" P P P "_C3_A7_20" P | ||
30 | #define NEEDLE P P P P P | ||
31 | ]], [[return !!strcasestr (HAYSTACK, NEEDLE); | ||
32 | ]])], | ||
33 | [gl_cv_func_strcasestr_works_always=yes], | ||
34 | [gl_cv_func_strcasestr_works_always=no], | ||
35 | [dnl glibc 2.12 and cygwin 1.7.7 have a known bug. uClibc is not | ||
36 | dnl affected, since it uses different source code for strcasestr | ||
37 | dnl than glibc. | ||
38 | dnl Assume that it works on all other platforms, even if it is not | ||
39 | dnl linear. | ||
40 | AC_EGREP_CPP([Lucky user], | ||
41 | [ | ||
42 | #ifdef __GNU_LIBRARY__ | ||
43 | #include <features.h> | ||
44 | #if ((__GLIBC__ == 2 && __GLIBC_MINOR__ > 12) || (__GLIBC__ > 2)) \ | ||
45 | || defined __UCLIBC__ | ||
46 | Lucky user | ||
47 | #endif | ||
48 | #elif defined __CYGWIN__ | ||
49 | #include <cygwin/version.h> | ||
50 | #if CYGWIN_VERSION_DLL_COMBINED > CYGWIN_VERSION_DLL_MAKE_COMBINED (1007, 7) | ||
51 | Lucky user | ||
52 | #endif | ||
53 | #else | ||
54 | Lucky user | ||
55 | #endif | ||
56 | ], | ||
57 | [gl_cv_func_strcasestr_works_always="guessing yes"], | ||
58 | [gl_cv_func_strcasestr_works_always="guessing no"]) | ||
59 | ]) | ||
60 | ]) | ||
61 | case "$gl_cv_func_strcasestr_works_always" in | ||
62 | *yes) ;; | ||
63 | *) | ||
64 | REPLACE_STRCASESTR=1 | ||
65 | ;; | ||
66 | esac | ||
67 | fi | ||
68 | fi | ||
69 | ]) # gl_FUNC_STRCASESTR_SIMPLE | ||
70 | |||
71 | dnl Additionally, check that strcasestr is efficient. | ||
72 | AC_DEFUN([gl_FUNC_STRCASESTR], | ||
73 | [ | ||
74 | AC_REQUIRE([gl_FUNC_STRCASESTR_SIMPLE]) | ||
75 | if test $HAVE_STRCASESTR = 1 && test $REPLACE_STRCASESTR = 0; then | ||
76 | AC_CACHE_CHECK([whether strcasestr works in linear time], | ||
77 | [gl_cv_func_strcasestr_linear], | ||
78 | [AC_RUN_IFELSE([AC_LANG_PROGRAM([[ | ||
79 | #include <signal.h> /* for signal */ | ||
80 | #include <string.h> /* for strcasestr */ | ||
81 | #include <stdlib.h> /* for malloc */ | ||
82 | #include <unistd.h> /* for alarm */ | ||
83 | static void quit (int sig) { exit (sig + 128); } | ||
84 | ]], [[ | ||
85 | int result = 0; | ||
86 | size_t m = 1000000; | ||
87 | char *haystack = (char *) malloc (2 * m + 2); | ||
88 | char *needle = (char *) malloc (m + 2); | ||
89 | /* Failure to compile this test due to missing alarm is okay, | ||
90 | since all such platforms (mingw) also lack strcasestr. */ | ||
91 | signal (SIGALRM, quit); | ||
92 | alarm (5); | ||
93 | /* Check for quadratic performance. */ | ||
94 | if (haystack && needle) | ||
95 | { | ||
96 | memset (haystack, 'A', 2 * m); | ||
97 | haystack[2 * m] = 'B'; | ||
98 | haystack[2 * m + 1] = 0; | ||
99 | memset (needle, 'A', m); | ||
100 | needle[m] = 'B'; | ||
101 | needle[m + 1] = 0; | ||
102 | if (!strcasestr (haystack, needle)) | ||
103 | result |= 1; | ||
104 | } | ||
105 | return result; | ||
106 | ]])], | ||
107 | [gl_cv_func_strcasestr_linear=yes], [gl_cv_func_strcasestr_linear=no], | ||
108 | [dnl Only glibc > 2.12 and cygwin > 1.7.7 are known to have a | ||
109 | dnl strcasestr that works in linear time. | ||
110 | AC_EGREP_CPP([Lucky user], | ||
111 | [ | ||
112 | #include <features.h> | ||
113 | #ifdef __GNU_LIBRARY__ | ||
114 | #if ((__GLIBC__ == 2 && __GLIBC_MINOR__ > 12) || (__GLIBC__ > 2)) \ | ||
115 | && !defined __UCLIBC__ | ||
116 | Lucky user | ||
117 | #endif | ||
118 | #endif | ||
119 | #ifdef __CYGWIN__ | ||
120 | #include <cygwin/version.h> | ||
121 | #if CYGWIN_VERSION_DLL_COMBINED > CYGWIN_VERSION_DLL_MAKE_COMBINED (1007, 7) | ||
122 | Lucky user | ||
123 | #endif | ||
124 | #endif | ||
125 | ], | ||
126 | [gl_cv_func_strcasestr_linear="guessing yes"], | ||
127 | [gl_cv_func_strcasestr_linear="guessing no"]) | ||
128 | ]) | ||
129 | ]) | ||
130 | case "$gl_cv_func_strcasestr_linear" in | ||
131 | *yes) ;; | ||
132 | *) | ||
133 | REPLACE_STRCASESTR=1 | ||
134 | ;; | ||
135 | esac | ||
136 | fi | ||
137 | ]) # gl_FUNC_STRCASESTR | ||
138 | |||
139 | # Prerequisites of lib/strcasestr.c. | ||
140 | AC_DEFUN([gl_PREREQ_STRCASESTR], [ | ||
141 | : | ||
142 | ]) | ||
diff --git a/gl/strcasestr.c b/gl/strcasestr.c new file mode 100644 index 0000000..53474a4 --- /dev/null +++ b/gl/strcasestr.c | |||
@@ -0,0 +1,82 @@ | |||
1 | /* Case-insensitive searching in a string. | ||
2 | Copyright (C) 2005-2013 Free Software Foundation, Inc. | ||
3 | Written by Bruno Haible <bruno@clisp.org>, 2005. | ||
4 | |||
5 | This program is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published by | ||
7 | the Free Software Foundation; either version 3, or (at your option) | ||
8 | any later version. | ||
9 | |||
10 | This program is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | GNU General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with this program; if not, see <http://www.gnu.org/licenses/>. */ | ||
17 | |||
18 | #include <config.h> | ||
19 | |||
20 | /* Specification. */ | ||
21 | #include <string.h> | ||
22 | |||
23 | #include <ctype.h> | ||
24 | #include <stdbool.h> | ||
25 | #include <strings.h> | ||
26 | |||
27 | #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch)) | ||
28 | |||
29 | /* Two-Way algorithm. */ | ||
30 | #define RETURN_TYPE char * | ||
31 | #define AVAILABLE(h, h_l, j, n_l) \ | ||
32 | (!memchr ((h) + (h_l), '\0', (j) + (n_l) - (h_l)) \ | ||
33 | && ((h_l) = (j) + (n_l))) | ||
34 | #define CANON_ELEMENT(c) TOLOWER (c) | ||
35 | #define CMP_FUNC(p1, p2, l) \ | ||
36 | strncasecmp ((const char *) (p1), (const char *) (p2), l) | ||
37 | #include "str-two-way.h" | ||
38 | |||
39 | /* Find the first occurrence of NEEDLE in HAYSTACK, using | ||
40 | case-insensitive comparison. This function gives unspecified | ||
41 | results in multibyte locales. */ | ||
42 | char * | ||
43 | strcasestr (const char *haystack_start, const char *needle_start) | ||
44 | { | ||
45 | const char *haystack = haystack_start; | ||
46 | const char *needle = needle_start; | ||
47 | size_t needle_len; /* Length of NEEDLE. */ | ||
48 | size_t haystack_len; /* Known minimum length of HAYSTACK. */ | ||
49 | bool ok = true; /* True if NEEDLE is prefix of HAYSTACK. */ | ||
50 | |||
51 | /* Determine length of NEEDLE, and in the process, make sure | ||
52 | HAYSTACK is at least as long (no point processing all of a long | ||
53 | NEEDLE if HAYSTACK is too short). */ | ||
54 | while (*haystack && *needle) | ||
55 | { | ||
56 | ok &= (TOLOWER ((unsigned char) *haystack) | ||
57 | == TOLOWER ((unsigned char) *needle)); | ||
58 | haystack++; | ||
59 | needle++; | ||
60 | } | ||
61 | if (*needle) | ||
62 | return NULL; | ||
63 | if (ok) | ||
64 | return (char *) haystack_start; | ||
65 | needle_len = needle - needle_start; | ||
66 | haystack = haystack_start + 1; | ||
67 | haystack_len = needle_len - 1; | ||
68 | |||
69 | /* Perform the search. Abstract memory is considered to be an array | ||
70 | of 'unsigned char' values, not an array of 'char' values. See | ||
71 | ISO C 99 section 6.2.6.1. */ | ||
72 | if (needle_len < LONG_NEEDLE_THRESHOLD) | ||
73 | return two_way_short_needle ((const unsigned char *) haystack, | ||
74 | haystack_len, | ||
75 | (const unsigned char *) needle_start, | ||
76 | needle_len); | ||
77 | return two_way_long_needle ((const unsigned char *) haystack, haystack_len, | ||
78 | (const unsigned char *) needle_start, | ||
79 | needle_len); | ||
80 | } | ||
81 | |||
82 | #undef LONG_NEEDLE_THRESHOLD | ||