Fix return value.
[gnulib.git] / lib / strcasestr.c
1 /* Case-insensitive searching in a string.
2    Copyright (C) 2005 Free Software Foundation, Inc.
3    Written by Bruno Haible <bruno@clisp.org>, 2005.
4
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)
8    any later version.
9
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.
14
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.  */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 /* Specification.  */
24 #include "strcasestr.h"
25
26 #include <ctype.h>
27
28 #if HAVE_MBRTOWC
29 # include "mbuiter.h"
30 #endif
31
32 #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
33
34 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
35    comparison.
36    Note: This function may, in multibyte locales, return success even if
37    strlen (haystack) < strlen (needle) !  */
38 char *
39 strcasestr (const char *haystack, const char *needle)
40 {
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.  */
46 #if HAVE_MBRTOWC
47   if (MB_CUR_MAX > 1)
48     {
49       mbui_iterator_t iter_needle;
50
51       mbui_init (iter_needle, needle);
52       if (mbui_avail (iter_needle))
53         {
54           mbchar_t b;
55           mbui_iterator_t iter_haystack;
56
57           mb_copy (&b, &mbui_cur (iter_needle));
58           if (b.wc_valid)
59             b.wc = towlower (b.wc);
60
61           mbui_init (iter_haystack, haystack);
62           for (;; mbui_advance (iter_haystack))
63             {
64               mbchar_t c;
65
66               if (!mbui_avail (iter_haystack))
67                 /* No match.  */
68                 return NULL;
69
70               mb_copy (&c, &mbui_cur (iter_haystack));
71               if (c.wc_valid)
72                 c.wc = towlower (c.wc);
73               if (mb_equal (c, b))
74                 /* The first character matches.  */
75                 {
76                   mbui_iterator_t rhaystack;
77                   mbui_iterator_t rneedle;
78
79                   memcpy (&rhaystack, &iter_haystack, sizeof (mbui_iterator_t));
80                   mbui_advance (rhaystack);
81
82                   mbui_init (rneedle, needle);
83                   if (!mbui_avail (rneedle))
84                     abort ();
85                   mbui_advance (rneedle);
86
87                   for (;; mbui_advance (rhaystack), mbui_advance (rneedle))
88                     {
89                       if (!mbui_avail (rneedle))
90                         /* Found a match.  */
91                         return (char *) mbui_cur_ptr (iter_haystack);
92                       if (!mbui_avail (rhaystack))
93                         /* No match.  */
94                         return NULL;
95                       if (!mb_caseequal (mbui_cur (rhaystack),
96                                          mbui_cur (rneedle)))
97                         /* Nothing in this round.  */
98                         break;
99                     }
100                 }
101             }
102         }
103       else
104         return (char *) haystack;
105     }
106   else
107 #endif
108     {
109       if (*needle != '\0')
110         {
111           /* Speed up the following searches of needle by caching its first
112              character.  */
113           unsigned char b = TOLOWER ((unsigned char) *needle);
114
115           needle++;
116           for (;; haystack++)
117             {
118               if (*haystack == '\0')
119                 /* No match.  */
120                 return NULL;
121               if (TOLOWER ((unsigned char) *haystack) == b)
122                 /* The first character matches.  */
123                 {
124                   const char *rhaystack = haystack + 1;
125                   const char *rneedle = needle;
126
127                   for (;; rhaystack++, rneedle++)
128                     {
129                       if (*rneedle == '\0')
130                         /* Found a match.  */
131                         return (char *) haystack;
132                       if (*rhaystack == '\0')
133                         /* No match.  */
134                         return NULL;
135                       if (TOLOWER ((unsigned char) *rhaystack)
136                           != TOLOWER ((unsigned char) *rneedle))
137                         /* Nothing in this round.  */
138                         break;
139                     }
140                 }
141             }
142         }
143       else
144         return (char *) haystack;
145     }
146 }