1 /* Case-insensitive searching in a string.
2 Copyright (C) 2005-2007 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2005.
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 2, or (at your option)
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.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
25 #include <stddef.h> /* for NULL, in case a nonstandard string.h lacks it */
31 #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
33 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
35 Note: This function may, in multibyte locales, return success even if
36 strlen (haystack) < strlen (needle) ! */
38 strcasestr (const char *haystack, const char *needle)
40 /* Be careful not to look at the entire extent of haystack or needle
41 until needed. This is useful because of these two cases:
42 - haystack may be very long, and a match of needle found early,
43 - needle may be very long, and not even a short initial segment of
44 needle may be found in haystack. */
48 mbui_iterator_t iter_needle;
50 mbui_init (iter_needle, needle);
51 if (mbui_avail (iter_needle))
54 mbui_iterator_t iter_haystack;
56 mb_copy (&b, &mbui_cur (iter_needle));
58 b.wc = towlower (b.wc);
60 mbui_init (iter_haystack, haystack);
61 for (;; mbui_advance (iter_haystack))
65 if (!mbui_avail (iter_haystack))
69 mb_copy (&c, &mbui_cur (iter_haystack));
71 c.wc = towlower (c.wc);
73 /* The first character matches. */
75 mbui_iterator_t rhaystack;
76 mbui_iterator_t rneedle;
78 memcpy (&rhaystack, &iter_haystack, sizeof (mbui_iterator_t));
79 mbui_advance (rhaystack);
81 mbui_init (rneedle, needle);
82 if (!mbui_avail (rneedle))
84 mbui_advance (rneedle);
86 for (;; mbui_advance (rhaystack), mbui_advance (rneedle))
88 if (!mbui_avail (rneedle))
90 return (char *) mbui_cur_ptr (iter_haystack);
91 if (!mbui_avail (rhaystack))
94 if (!mb_caseequal (mbui_cur (rhaystack),
96 /* Nothing in this round. */
103 return (char *) haystack;
110 /* Speed up the following searches of needle by caching its first
112 unsigned char b = TOLOWER ((unsigned char) *needle);
117 if (*haystack == '\0')
120 if (TOLOWER ((unsigned char) *haystack) == b)
121 /* The first character matches. */
123 const char *rhaystack = haystack + 1;
124 const char *rneedle = needle;
126 for (;; rhaystack++, rneedle++)
128 if (*rneedle == '\0')
130 return (char *) haystack;
131 if (*rhaystack == '\0')
134 if (TOLOWER ((unsigned char) *rhaystack)
135 != TOLOWER ((unsigned char) *rneedle))
136 /* Nothing in this round. */
143 return (char *) haystack;