Optimize x86-64 pthread_cond_wait.
[glibc.git] / string / strcasestr.c
blob92f2eac7c8365e08de941de43e16b0195bafc9ab
1 /* Return the offset of one string within another.
2 Copyright (C) 1994, 1996-2000, 2004, 2008 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
21 * My personal strstr() implementation that beats most other algorithms.
22 * Until someone tells me otherwise, I assume that this is the
23 * fastest implementation of strstr() in C.
24 * I deliberately chose not to comment it. You should have at least
25 * as much fun trying to understand it, as I had to write it :-).
27 * Stephen R. van den Berg, berg@pool.informatik.rwth-aachen.de */
29 #if HAVE_CONFIG_H
30 # include <config.h>
31 #endif
33 /* Specification. */
34 #include <string.h>
36 #include <ctype.h>
37 #include <stdbool.h>
38 #include <strings.h>
40 #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
42 /* Two-Way algorithm. */
43 #define RETURN_TYPE char *
44 #define AVAILABLE(h, h_l, j, n_l) \
45 (!memchr ((h) + (h_l), '\0', (j) + (n_l) - (h_l)) \
46 && ((h_l) = (j) + (n_l)))
47 #define CANON_ELEMENT(c) TOLOWER (c)
48 #define CMP_FUNC(p1, p2, l) \
49 __strncasecmp ((const char *) (p1), (const char *) (p2), l)
50 #include "str-two-way.h"
52 #undef strcasestr
53 #undef __strcasestr
55 /* Find the first occurrence of NEEDLE in HAYSTACK, using
56 case-insensitive comparison. This function gives unspecified
57 results in multibyte locales. */
58 char *
59 __strcasestr (const char *haystack_start, const char *needle_start)
61 const char *haystack = haystack_start;
62 const char *needle = needle_start;
63 size_t needle_len; /* Length of NEEDLE. */
64 size_t haystack_len; /* Known minimum length of HAYSTACK. */
65 bool ok = true; /* True if NEEDLE is prefix of HAYSTACK. */
67 /* Determine length of NEEDLE, and in the process, make sure
68 HAYSTACK is at least as long (no point processing all of a long
69 NEEDLE if HAYSTACK is too short). */
70 while (*haystack && *needle)
72 ok &= (TOLOWER ((unsigned char) *haystack)
73 == TOLOWER ((unsigned char) *needle));
74 haystack++;
75 needle++;
77 if (*needle)
78 return NULL;
79 if (ok)
80 return (char *) haystack_start;
81 needle_len = needle - needle_start;
82 haystack = haystack_start + 1;
83 haystack_len = needle_len - 1;
85 /* Perform the search. Abstract memory is considered to be an array
86 of 'unsigned char' values, not an array of 'char' values. See
87 ISO C 99 section 6.2.6.1. */
88 if (needle_len < LONG_NEEDLE_THRESHOLD)
89 return two_way_short_needle ((const unsigned char *) haystack,
90 haystack_len,
91 (const unsigned char *) needle_start,
92 needle_len);
93 return two_way_long_needle ((const unsigned char *) haystack, haystack_len,
94 (const unsigned char *) needle_start,
95 needle_len);
98 #undef LONG_NEEDLE_THRESHOLD
100 weak_alias (__strcasestr, strcasestr)