1 /* Return the offset of one string within another.
2 Copyright (C) 1994, 1996-2000, 2004, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 * My personal strstr() implementation that beats most other algorithms.
23 * Until someone tells me otherwise, I assume that this is the
24 * fastest implementation of strstr() in C.
25 * I deliberately chose not to comment it. You should have at least
26 * as much fun trying to understand it, as I had to write it :-).
28 * Stephen R. van den Berg, berg@pool.informatik.rwth-aachen.de */
41 #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
43 /* Two-Way algorithm. */
44 #define RETURN_TYPE char *
45 #define AVAILABLE(h, h_l, j, n_l) \
46 (!memchr ((h) + (h_l), '\0', (j) + (n_l) - (h_l)) \
47 && ((h_l) = (j) + (n_l)))
48 #define CANON_ELEMENT(c) TOLOWER (c)
49 #define CMP_FUNC(p1, p2, l) \
50 __strncasecmp ((const char *) (p1), (const char *) (p2), l)
51 #include "str-two-way.h"
57 #define STRCASESTR __strcasestr
61 /* Find the first occurrence of NEEDLE in HAYSTACK, using
62 case-insensitive comparison. This function gives unspecified
63 results in multibyte locales. */
65 STRCASESTR (const char *haystack_start
, const char *needle_start
)
67 const char *haystack
= haystack_start
;
68 const char *needle
= needle_start
;
69 size_t needle_len
; /* Length of NEEDLE. */
70 size_t haystack_len
; /* Known minimum length of HAYSTACK. */
71 bool ok
= true; /* True if NEEDLE is prefix of HAYSTACK. */
73 /* Determine length of NEEDLE, and in the process, make sure
74 HAYSTACK is at least as long (no point processing all of a long
75 NEEDLE if HAYSTACK is too short). */
76 while (*haystack
&& *needle
)
78 ok
&= (TOLOWER ((unsigned char) *haystack
)
79 == TOLOWER ((unsigned char) *needle
));
86 return (char *) haystack_start
;
87 needle_len
= needle
- needle_start
;
88 haystack
= haystack_start
+ 1;
89 haystack_len
= needle_len
- 1;
91 /* Perform the search. Abstract memory is considered to be an array
92 of 'unsigned char' values, not an array of 'char' values. See
93 ISO C 99 section 6.2.6.1. */
94 if (needle_len
< LONG_NEEDLE_THRESHOLD
)
95 return two_way_short_needle ((const unsigned char *) haystack
,
97 (const unsigned char *) needle_start
,
99 return two_way_long_needle ((const unsigned char *) haystack
, haystack_len
,
100 (const unsigned char *) needle_start
,
104 #undef LONG_NEEDLE_THRESHOLD
107 weak_alias (__strcasestr
, strcasestr
)