annotate lib/strcasestr.c @ 6164:83bee8e13391

Include a header, to define NULL.
author Bruno Haible <bruno@clisp.org>
date Wed, 31 Aug 2005 11:51:18 +0000
parents 199c20f568d7
children 1c4ed7637c24
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6058
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Case-insensitive searching in a string.
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
2 Copyright (C) 2005 Free Software Foundation, Inc.
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 Written by Bruno Haible <bruno@clisp.org>, 2005.
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 This program is free software; you can redistribute it and/or modify
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 the Free Software Foundation; either version 2, or (at your option)
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8 any later version.
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 GNU General Public License for more details.
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16 along with this program; if not, write to the Free Software Foundation,
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 #ifdef HAVE_CONFIG_H
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 # include <config.h>
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 #endif
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 /* Specification. */
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 #include "strcasestr.h"
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 #include <ctype.h>
6164
83bee8e13391 Include a header, to define NULL.
Bruno Haible <bruno@clisp.org>
parents: 6064
diff changeset
27 #include <stddef.h> /* for NULL */
6058
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 #if HAVE_MBRTOWC
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 # include "mbuiter.h"
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 #endif
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 comparison.
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 Note: This function may, in multibyte locales, return success even if
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 strlen (haystack) < strlen (needle) ! */
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 char *
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 strcasestr (const char *haystack, const char *needle)
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 {
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 /* Be careful not to look at the entire extent of haystack or needle
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 until needed. This is useful because of these two cases:
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 - haystack may be very long, and a match of needle found early,
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 - needle may be very long, and not even a short initial segment of
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 needle may be found in haystack. */
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 #if HAVE_MBRTOWC
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 if (MB_CUR_MAX > 1)
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 {
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 mbui_iterator_t iter_needle;
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 mbui_init (iter_needle, needle);
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 if (mbui_avail (iter_needle))
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 {
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 mbchar_t b;
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 mbui_iterator_t iter_haystack;
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 mb_copy (&b, &mbui_cur (iter_needle));
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 if (b.wc_valid)
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 b.wc = towlower (b.wc);
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 mbui_init (iter_haystack, haystack);
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 for (;; mbui_advance (iter_haystack))
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 {
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 mbchar_t c;
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 if (!mbui_avail (iter_haystack))
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 /* No match. */
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 return NULL;
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 mb_copy (&c, &mbui_cur (iter_haystack));
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 if (c.wc_valid)
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 c.wc = towlower (c.wc);
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 if (mb_equal (c, b))
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 /* The first character matches. */
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 {
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 mbui_iterator_t rhaystack;
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 mbui_iterator_t rneedle;
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 memcpy (&rhaystack, &iter_haystack, sizeof (mbui_iterator_t));
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 mbui_advance (rhaystack);
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 mbui_init (rneedle, needle);
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 if (!mbui_avail (rneedle))
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 abort ();
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 mbui_advance (rneedle);
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 for (;; mbui_advance (rhaystack), mbui_advance (rneedle))
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 {
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 if (!mbui_avail (rneedle))
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 /* Found a match. */
6064
199c20f568d7 Fix return value.
Bruno Haible <bruno@clisp.org>
parents: 6058
diff changeset
92 return (char *) mbui_cur_ptr (iter_haystack);
6058
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 if (!mbui_avail (rhaystack))
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 /* No match. */
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 return NULL;
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 if (!mb_caseequal (mbui_cur (rhaystack),
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 mbui_cur (rneedle)))
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 /* Nothing in this round. */
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99 break;
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 }
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101 }
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102 }
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103 }
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 else
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 return (char *) haystack;
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106 }
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 else
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108 #endif
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109 {
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110 if (*needle != '\0')
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
111 {
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112 /* Speed up the following searches of needle by caching its first
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113 character. */
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114 unsigned char b = TOLOWER ((unsigned char) *needle);
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
116 needle++;
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
117 for (;; haystack++)
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
118 {
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
119 if (*haystack == '\0')
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
120 /* No match. */
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
121 return NULL;
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
122 if (TOLOWER ((unsigned char) *haystack) == b)
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
123 /* The first character matches. */
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
124 {
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
125 const char *rhaystack = haystack + 1;
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
126 const char *rneedle = needle;
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
127
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
128 for (;; rhaystack++, rneedle++)
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
129 {
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
130 if (*rneedle == '\0')
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
131 /* Found a match. */
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
132 return (char *) haystack;
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
133 if (*rhaystack == '\0')
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
134 /* No match. */
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
135 return NULL;
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
136 if (TOLOWER ((unsigned char) *rhaystack)
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
137 != TOLOWER ((unsigned char) *rneedle))
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
138 /* Nothing in this round. */
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
139 break;
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
140 }
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
141 }
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
142 }
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
143 }
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
144 else
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
145 return (char *) haystack;
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
146 }
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
147 }