Set GL(dl_nns) to 1 for vDSO in libc.a
[glibc.git] / string / strcasestr.c
blob9467b7a759425ca4d0af083057bc1c8ce70d8edc
1 /* Return the offset of one string within another.
2 Copyright (C) 1994-2012 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
20 * My personal strstr() implementation that beats most other algorithms.
21 * Until someone tells me otherwise, I assume that this is the
22 * fastest implementation of strstr() in C.
23 * I deliberately chose not to comment it. You should have at least
24 * as much fun trying to understand it, as I had to write it :-).
26 * Stephen R. van den Berg, berg@pool.informatik.rwth-aachen.de */
28 #if HAVE_CONFIG_H
29 # include <config.h>
30 #endif
32 /* Specification. */
33 #include <string.h>
35 #include <ctype.h>
36 #include <stdbool.h>
37 #include <strings.h>
39 #define TOLOWER(Ch) tolower (Ch)
41 /* Two-Way algorithm. */
42 #define RETURN_TYPE char *
43 #define AVAILABLE(h, h_l, j, n_l) \
44 (!memchr ((h) + (h_l), '\0', (j) + (n_l) - (h_l)) \
45 && ((h_l) = (j) + (n_l)))
46 #define AVAILABLE1(h, h_l, j, n_l) (true)
47 #define AVAILABLE2(h, h_l, j, n_l) AVAILABLE (h, h_l, j, n_l)
48 #define RET0_IF_0(a) if (!a) goto ret0
49 #define AVAILABLE1_USES_J (0)
50 #define CANON_ELEMENT(c) TOLOWER (c)
51 #define CMP_FUNC(p1, p2, l) \
52 __strncasecmp ((const char *) (p1), (const char *) (p2), l)
53 #include "str-two-way.h"
55 #undef strcasestr
56 #undef __strcasestr
58 #ifndef STRCASESTR
59 #define STRCASESTR __strcasestr
60 #endif
63 /* Find the first occurrence of NEEDLE in HAYSTACK, using
64 case-insensitive comparison. This function gives unspecified
65 results in multibyte locales. */
66 char *
67 STRCASESTR (const char *haystack_start, const char *needle_start)
69 const char *haystack = haystack_start;
70 const char *needle = needle_start;
71 size_t needle_len; /* Length of NEEDLE. */
72 size_t haystack_len; /* Known minimum length of HAYSTACK. */
73 bool ok = true; /* True if NEEDLE is prefix of HAYSTACK. */
75 /* Determine length of NEEDLE, and in the process, make sure
76 HAYSTACK is at least as long (no point processing all of a long
77 NEEDLE if HAYSTACK is too short). */
78 while (*haystack && *needle)
80 ok &= (TOLOWER ((unsigned char) *haystack)
81 == TOLOWER ((unsigned char) *needle));
82 haystack++;
83 needle++;
85 if (*needle)
86 return NULL;
87 if (ok)
88 return (char *) haystack_start;
89 needle_len = needle - needle_start;
90 haystack = haystack_start + 1;
91 haystack_len = needle_len - 1;
93 /* Perform the search. Abstract memory is considered to be an array
94 of 'unsigned char' values, not an array of 'char' values. See
95 ISO C 99 section 6.2.6.1. */
96 if (needle_len < LONG_NEEDLE_THRESHOLD)
97 return two_way_short_needle ((const unsigned char *) haystack,
98 haystack_len,
99 (const unsigned char *) needle_start,
100 needle_len);
101 return two_way_long_needle ((const unsigned char *) haystack, haystack_len,
102 (const unsigned char *) needle_start,
103 needle_len);
106 #undef LONG_NEEDLE_THRESHOLD
108 #ifndef NO_ALIAS
109 weak_alias (__strcasestr, strcasestr)
110 #endif