1 |
207 |
jeremybenn |
/*
|
2 |
|
|
FUNCTION
|
3 |
|
|
<<strcasestr>>---case-insensitive character string search
|
4 |
|
|
|
5 |
|
|
INDEX
|
6 |
|
|
strcasestr
|
7 |
|
|
|
8 |
|
|
ANSI_SYNOPSIS
|
9 |
|
|
#include <string.h>
|
10 |
|
|
char *strcasestr(const char *<[s]>, const char *<[find]>);
|
11 |
|
|
|
12 |
|
|
TRAD_SYNOPSIS
|
13 |
|
|
#include <string.h>
|
14 |
|
|
int strcasecmp(<[s]>, <[find]>)
|
15 |
|
|
char *<[s]>;
|
16 |
|
|
char *<[find]>;
|
17 |
|
|
|
18 |
|
|
DESCRIPTION
|
19 |
|
|
<<strcasestr>> searchs the string <[s]> for
|
20 |
|
|
the first occurrence of the sequence <[find]>. <<strcasestr>>
|
21 |
|
|
is identical to <<strstr>> except the search is
|
22 |
|
|
case-insensitive.
|
23 |
|
|
|
24 |
|
|
RETURNS
|
25 |
|
|
|
26 |
|
|
A pointer to the first case-insensitive occurrence of the sequence
|
27 |
|
|
<[find]> or <<NULL>> if no match was found.
|
28 |
|
|
|
29 |
|
|
PORTABILITY
|
30 |
|
|
<<strcasestr>> is in the Berkeley Software Distribution.
|
31 |
|
|
|
32 |
|
|
<<strcasestr>> requires no supporting OS subroutines. It uses
|
33 |
|
|
tolower() from elsewhere in this library.
|
34 |
|
|
|
35 |
|
|
QUICKREF
|
36 |
|
|
strcasestr
|
37 |
|
|
*/
|
38 |
|
|
|
39 |
|
|
/*-
|
40 |
|
|
* Copyright (c) 1990, 1993
|
41 |
|
|
* The Regents of the University of California. All rights reserved.
|
42 |
|
|
*
|
43 |
|
|
* The quadratic code is derived from software contributed to Berkeley by
|
44 |
|
|
* Chris Torek.
|
45 |
|
|
*
|
46 |
|
|
* Redistribution and use in source and binary forms, with or without
|
47 |
|
|
* modification, are permitted provided that the following conditions
|
48 |
|
|
* are met:
|
49 |
|
|
* 1. Redistributions of source code must retain the above copyright
|
50 |
|
|
* notice, this list of conditions and the following disclaimer.
|
51 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
52 |
|
|
* notice, this list of conditions and the following disclaimer in the
|
53 |
|
|
* documentation and/or other materials provided with the distribution.
|
54 |
|
|
* 4. Neither the name of the University nor the names of its contributors
|
55 |
|
|
* may be used to endorse or promote products derived from this software
|
56 |
|
|
* without specific prior written permission.
|
57 |
|
|
*
|
58 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
59 |
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
60 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
61 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
62 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
63 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
64 |
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
65 |
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
66 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
67 |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
68 |
|
|
* SUCH DAMAGE.
|
69 |
|
|
*/
|
70 |
|
|
/* Linear algorithm Copyright (C) 2008 Eric Blake
|
71 |
|
|
* Permission to use, copy, modify, and distribute the linear portion of
|
72 |
|
|
* software is freely granted, provided that this notice is preserved.
|
73 |
|
|
*/
|
74 |
|
|
|
75 |
|
|
#include <sys/cdefs.h>
|
76 |
|
|
|
77 |
|
|
#include <ctype.h>
|
78 |
|
|
#include <string.h>
|
79 |
|
|
|
80 |
|
|
#if !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__)
|
81 |
|
|
# define RETURN_TYPE char *
|
82 |
|
|
# define AVAILABLE(h, h_l, j, n_l) \
|
83 |
|
|
(!memchr ((h) + (h_l), '\0', (j) + (n_l) - (h_l)) \
|
84 |
|
|
&& ((h_l) = (j) + (n_l)))
|
85 |
|
|
# define CANON_ELEMENT(c) tolower (c)
|
86 |
|
|
# define CMP_FUNC strncasecmp
|
87 |
|
|
# include "str-two-way.h"
|
88 |
|
|
#endif
|
89 |
|
|
|
90 |
|
|
/*
|
91 |
|
|
* Find the first occurrence of find in s, ignore case.
|
92 |
|
|
*/
|
93 |
|
|
char *
|
94 |
|
|
strcasestr(s, find)
|
95 |
|
|
const char *s, *find;
|
96 |
|
|
{
|
97 |
|
|
#if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
|
98 |
|
|
|
99 |
|
|
/* Less code size, but quadratic performance in the worst case. */
|
100 |
|
|
char c, sc;
|
101 |
|
|
size_t len;
|
102 |
|
|
|
103 |
|
|
if ((c = *find++) != 0) {
|
104 |
|
|
c = tolower((unsigned char)c);
|
105 |
|
|
len = strlen(find);
|
106 |
|
|
do {
|
107 |
|
|
do {
|
108 |
|
|
if ((sc = *s++) == 0)
|
109 |
|
|
return (NULL);
|
110 |
|
|
} while ((char)tolower((unsigned char)sc) != c);
|
111 |
|
|
} while (strncasecmp(s, find, len) != 0);
|
112 |
|
|
s--;
|
113 |
|
|
}
|
114 |
|
|
return ((char *)s);
|
115 |
|
|
|
116 |
|
|
#else /* compilation for speed */
|
117 |
|
|
|
118 |
|
|
/* Larger code size, but guaranteed linear performance. */
|
119 |
|
|
const char *haystack = s;
|
120 |
|
|
const char *needle = find;
|
121 |
|
|
size_t needle_len; /* Length of NEEDLE. */
|
122 |
|
|
size_t haystack_len; /* Known minimum length of HAYSTACK. */
|
123 |
|
|
int ok = 1; /* True if NEEDLE is prefix of HAYSTACK. */
|
124 |
|
|
|
125 |
|
|
/* Determine length of NEEDLE, and in the process, make sure
|
126 |
|
|
HAYSTACK is at least as long (no point processing all of a long
|
127 |
|
|
NEEDLE if HAYSTACK is too short). */
|
128 |
|
|
while (*haystack && *needle)
|
129 |
|
|
ok &= (tolower ((unsigned char) *haystack++)
|
130 |
|
|
== tolower ((unsigned char) *needle++));
|
131 |
|
|
if (*needle)
|
132 |
|
|
return NULL;
|
133 |
|
|
if (ok)
|
134 |
|
|
return (char *) s;
|
135 |
|
|
needle_len = needle - find;
|
136 |
|
|
haystack = s + 1;
|
137 |
|
|
haystack_len = needle_len - 1;
|
138 |
|
|
|
139 |
|
|
/* Perform the search. */
|
140 |
|
|
if (needle_len < LONG_NEEDLE_THRESHOLD)
|
141 |
|
|
return two_way_short_needle ((const unsigned char *) haystack,
|
142 |
|
|
haystack_len,
|
143 |
|
|
(const unsigned char *) find, needle_len);
|
144 |
|
|
return two_way_long_needle ((const unsigned char *) haystack, haystack_len,
|
145 |
|
|
(const unsigned char *) find, needle_len);
|
146 |
|
|
#endif /* compilation for speed */
|
147 |
|
|
}
|