]>
Commit | Line | Data |
---|---|---|
f4017d20 | 1 | /* Return the offset of one string within another. |
04277e02 | 2 | Copyright (C) 1994-2019 Free Software Foundation, Inc. |
f4017d20 UD |
3 | This file is part of the GNU C Library. |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
f4017d20 UD |
9 | |
10 | The GNU C Library 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 GNU | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
f4017d20 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
f4017d20 UD |
18 | |
19 | /* | |
20 | * My personal strstr() implementation that beats most other algorithms. | |
21 | * Until someone tells me otherwise, I assume that this is the | |
22 | * fastest implementation of strstr() in C. | |
23 | * I deliberately chose not to comment it. You should have at least | |
24 | * as much fun trying to understand it, as I had to write it :-). | |
25 | * | |
26 | * Stephen R. van den Berg, berg@pool.informatik.rwth-aachen.de */ | |
27 | ||
0caca71a UD |
28 | /* Specification. */ |
29 | #include <string.h> | |
30 | ||
f4017d20 | 31 | #include <ctype.h> |
0caca71a UD |
32 | #include <stdbool.h> |
33 | #include <strings.h> | |
f4017d20 | 34 | |
99677e57 | 35 | #define TOLOWER(Ch) tolower (Ch) |
f4017d20 | 36 | |
0caca71a UD |
37 | /* Two-Way algorithm. */ |
38 | #define RETURN_TYPE char * | |
39 | #define AVAILABLE(h, h_l, j, n_l) \ | |
83a552b0 WD |
40 | (((j) + (n_l) <= (h_l)) \ |
41 | || ((h_l) += __strnlen ((void*)((h) + (h_l)), (n_l) + 512), \ | |
42 | (j) + (n_l) <= (h_l))) | |
57e605ba | 43 | #define CHECK_EOL (1) |
400726de | 44 | #define RET0_IF_0(a) if (!a) goto ret0 |
0caca71a UD |
45 | #define CANON_ELEMENT(c) TOLOWER (c) |
46 | #define CMP_FUNC(p1, p2, l) \ | |
74e13385 | 47 | __strncasecmp ((const char *) (p1), (const char *) (p2), l) |
0caca71a | 48 | #include "str-two-way.h" |
f4017d20 | 49 | |
8619129f UD |
50 | #undef strcasestr |
51 | #undef __strcasestr | |
f4017d20 | 52 | |
2b7a8664 L |
53 | #ifndef STRCASESTR |
54 | #define STRCASESTR __strcasestr | |
55 | #endif | |
56 | ||
57 | ||
0caca71a UD |
58 | /* Find the first occurrence of NEEDLE in HAYSTACK, using |
59 | case-insensitive comparison. This function gives unspecified | |
60 | results in multibyte locales. */ | |
f4017d20 | 61 | char * |
284f42bc | 62 | STRCASESTR (const char *haystack, const char *needle) |
f4017d20 | 63 | { |
0caca71a UD |
64 | size_t needle_len; /* Length of NEEDLE. */ |
65 | size_t haystack_len; /* Known minimum length of HAYSTACK. */ | |
284f42bc WD |
66 | |
67 | /* Handle empty NEEDLE special case. */ | |
68 | if (needle[0] == '\0') | |
69 | return (char *) haystack; | |
70 | ||
71 | /* Ensure HAYSTACK length is at least as long as NEEDLE length. | |
72 | Since a match may occur early on in a huge HAYSTACK, use strnlen | |
73 | and read ahead a few cachelines for improved performance. */ | |
74 | needle_len = strlen (needle); | |
75 | haystack_len = __strnlen (haystack, needle_len + 256); | |
76 | if (haystack_len < needle_len) | |
0caca71a | 77 | return NULL; |
0caca71a UD |
78 | |
79 | /* Perform the search. Abstract memory is considered to be an array | |
80 | of 'unsigned char' values, not an array of 'char' values. See | |
81 | ISO C 99 section 6.2.6.1. */ | |
82 | if (needle_len < LONG_NEEDLE_THRESHOLD) | |
83 | return two_way_short_needle ((const unsigned char *) haystack, | |
84 | haystack_len, | |
284f42bc | 85 | (const unsigned char *) needle, |
0caca71a UD |
86 | needle_len); |
87 | return two_way_long_needle ((const unsigned char *) haystack, haystack_len, | |
284f42bc | 88 | (const unsigned char *) needle, |
0caca71a | 89 | needle_len); |
f4017d20 UD |
90 | } |
91 | ||
0caca71a UD |
92 | #undef LONG_NEEDLE_THRESHOLD |
93 | ||
dbc676d4 | 94 | #ifndef NO_ALIAS |
f4017d20 | 95 | weak_alias (__strcasestr, strcasestr) |
dbc676d4 | 96 | #endif |