1 /* Case-insensitive searching in a string.
2 Copyright (C) 2005 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. */
24 #include "strcasestr.h"
27 #include <stddef.h> /* for NULL */
33 #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
35 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
37 Note: This function may, in multibyte locales, return success even if
38 strlen (haystack) < strlen (needle) ! */
40 strcasestr (const char *haystack, const char *needle)
42 /* Be careful not to look at the entire extent of haystack or needle
43 until needed. This is useful because of these two cases:
44 - haystack may be very long, and a match of needle found early,
45 - needle may be very long, and not even a short initial segment of
46 needle may be found in haystack. */
50 mbui_iterator_t iter_needle;
52 mbui_init (iter_needle, needle);
53 if (mbui_avail (iter_needle))
56 mbui_iterator_t iter_haystack;
58 mb_copy (&b, &mbui_cur (iter_needle));
60 b.wc = towlower (b.wc);
62 mbui_init (iter_haystack, haystack);
63 for (;; mbui_advance (iter_haystack))
67 if (!mbui_avail (iter_haystack))
71 mb_copy (&c, &mbui_cur (iter_haystack));
73 c.wc = towlower (c.wc);
75 /* The first character matches. */
77 mbui_iterator_t rhaystack;
78 mbui_iterator_t rneedle;
80 memcpy (&rhaystack, &iter_haystack, sizeof (mbui_iterator_t));
81 mbui_advance (rhaystack);
83 mbui_init (rneedle, needle);
84 if (!mbui_avail (rneedle))
86 mbui_advance (rneedle);
88 for (;; mbui_advance (rhaystack), mbui_advance (rneedle))
90 if (!mbui_avail (rneedle))
92 return (char *) mbui_cur_ptr (iter_haystack);
93 if (!mbui_avail (rhaystack))
96 if (!mb_caseequal (mbui_cur (rhaystack),
98 /* Nothing in this round. */
105 return (char *) haystack;
112 /* Speed up the following searches of needle by caching its first
114 unsigned char b = TOLOWER ((unsigned char) *needle);
119 if (*haystack == '\0')
122 if (TOLOWER ((unsigned char) *haystack) == b)
123 /* The first character matches. */
125 const char *rhaystack = haystack + 1;
126 const char *rneedle = needle;
128 for (;; rhaystack++, rneedle++)
130 if (*rneedle == '\0')
132 return (char *) haystack;
133 if (*rhaystack == '\0')
136 if (TOLOWER ((unsigned char) *rhaystack)
137 != TOLOWER ((unsigned char) *rneedle))
138 /* Nothing in this round. */
145 return (char *) haystack;