mktime: check signed shifts on long_int and time_t, too
[glibc.git] / string / strcasestr.c
blob9e1bde97aeabca00fd26e4b3ea40e291617d2f8a
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, see
18 <http://www.gnu.org/licenses/>. */
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 #ifndef STRCASESTR
56 #define STRCASESTR __strcasestr
57 #endif
60 /* Find the first occurrence of NEEDLE in HAYSTACK, using
61 case-insensitive comparison. This function gives unspecified
62 results in multibyte locales. */
63 char *
64 STRCASESTR (const char *haystack_start, const char *needle_start)
66 const char *haystack = haystack_start;
67 const char *needle = needle_start;
68 size_t needle_len; /* Length of NEEDLE. */
69 size_t haystack_len; /* Known minimum length of HAYSTACK. */
70 bool ok = true; /* True if NEEDLE is prefix of HAYSTACK. */
72 /* Determine length of NEEDLE, and in the process, make sure
73 HAYSTACK is at least as long (no point processing all of a long
74 NEEDLE if HAYSTACK is too short). */
75 while (*haystack && *needle)
77 ok &= (TOLOWER ((unsigned char) *haystack)
78 == TOLOWER ((unsigned char) *needle));
79 haystack++;
80 needle++;
82 if (*needle)
83 return NULL;
84 if (ok)
85 return (char *) haystack_start;
86 needle_len = needle - needle_start;
87 haystack = haystack_start + 1;
88 haystack_len = needle_len - 1;
90 /* Perform the search. Abstract memory is considered to be an array
91 of 'unsigned char' values, not an array of 'char' values. See
92 ISO C 99 section 6.2.6.1. */
93 if (needle_len < LONG_NEEDLE_THRESHOLD)
94 return two_way_short_needle ((const unsigned char *) haystack,
95 haystack_len,
96 (const unsigned char *) needle_start,
97 needle_len);
98 return two_way_long_needle ((const unsigned char *) haystack, haystack_len,
99 (const unsigned char *) needle_start,
100 needle_len);
103 #undef LONG_NEEDLE_THRESHOLD
105 #ifndef NO_ALIAS
106 weak_alias (__strcasestr, strcasestr)
107 #endif