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"
32 #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
34 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
36 Note: This function may, in multibyte locales, return success even if
37 strlen (haystack) < strlen (needle) ! */
39 strcasestr (const char *haystack, const char *needle)
41 /* Be careful not to look at the entire extent of haystack or needle
42 until needed. This is useful because of these two cases:
43 - haystack may be very long, and a match of needle found early,
44 - needle may be very long, and not even a short initial segment of
45 needle may be found in haystack. */
49 mbui_iterator_t iter_needle;
51 mbui_init (iter_needle, needle);
52 if (mbui_avail (iter_needle))
55 mbui_iterator_t iter_haystack;
57 mb_copy (&b, &mbui_cur (iter_needle));
59 b.wc = towlower (b.wc);
61 mbui_init (iter_haystack, haystack);
62 for (;; mbui_advance (iter_haystack))
66 if (!mbui_avail (iter_haystack))
70 mb_copy (&c, &mbui_cur (iter_haystack));
72 c.wc = towlower (c.wc);
74 /* The first character matches. */
76 mbui_iterator_t rhaystack;
77 mbui_iterator_t rneedle;
79 memcpy (&rhaystack, &iter_haystack, sizeof (mbui_iterator_t));
80 mbui_advance (rhaystack);
82 mbui_init (rneedle, needle);
83 if (!mbui_avail (rneedle))
85 mbui_advance (rneedle);
87 for (;; mbui_advance (rhaystack), mbui_advance (rneedle))
89 if (!mbui_avail (rneedle))
91 return (char *) mbui_cur_ptr (iter_haystack);
92 if (!mbui_avail (rhaystack))
95 if (!mb_caseequal (mbui_cur (rhaystack),
97 /* Nothing in this round. */
104 return (char *) haystack;
111 /* Speed up the following searches of needle by caching its first
113 unsigned char b = TOLOWER ((unsigned char) *needle);
118 if (*haystack == '\0')
121 if (TOLOWER ((unsigned char) *haystack) == b)
122 /* The first character matches. */
124 const char *rhaystack = haystack + 1;
125 const char *rneedle = needle;
127 for (;; rhaystack++, rneedle++)
129 if (*rneedle == '\0')
131 return (char *) haystack;
132 if (*rhaystack == '\0')
135 if (TOLOWER ((unsigned char) *rhaystack)
136 != TOLOWER ((unsigned char) *rneedle))
137 /* Nothing in this round. */
144 return (char *) haystack;