]> git.ipfire.org Git - thirdparty/glibc.git/blob - string/strcasestr.c
Simplify and speedup strstr/strcasestr first match
[thirdparty/glibc.git] / string / strcasestr.c
1 /* Return the offset of one string within another.
2 Copyright (C) 1994-2018 Free Software Foundation, Inc.
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
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.
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
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
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
28 /* Specification. */
29 #include <string.h>
30
31 #include <ctype.h>
32 #include <stdbool.h>
33 #include <strings.h>
34
35 #define TOLOWER(Ch) tolower (Ch)
36
37 /* Two-Way algorithm. */
38 #define RETURN_TYPE char *
39 #define AVAILABLE(h, h_l, j, n_l) \
40 (((j) + (n_l) <= (h_l)) || ((h_l) += __strnlen ((void*)((h) + (h_l)), 512), \
41 (j) + (n_l) <= (h_l)))
42 #define CHECK_EOL (1)
43 #define RET0_IF_0(a) if (!a) goto ret0
44 #define CANON_ELEMENT(c) TOLOWER (c)
45 #define CMP_FUNC(p1, p2, l) \
46 __strncasecmp ((const char *) (p1), (const char *) (p2), l)
47 #include "str-two-way.h"
48
49 #undef strcasestr
50 #undef __strcasestr
51
52 #ifndef STRCASESTR
53 #define STRCASESTR __strcasestr
54 #endif
55
56
57 /* Find the first occurrence of NEEDLE in HAYSTACK, using
58 case-insensitive comparison. This function gives unspecified
59 results in multibyte locales. */
60 char *
61 STRCASESTR (const char *haystack, const char *needle)
62 {
63 size_t needle_len; /* Length of NEEDLE. */
64 size_t haystack_len; /* Known minimum length of HAYSTACK. */
65
66 /* Handle empty NEEDLE special case. */
67 if (needle[0] == '\0')
68 return (char *) haystack;
69
70 /* Ensure HAYSTACK length is at least as long as NEEDLE length.
71 Since a match may occur early on in a huge HAYSTACK, use strnlen
72 and read ahead a few cachelines for improved performance. */
73 needle_len = strlen (needle);
74 haystack_len = __strnlen (haystack, needle_len + 256);
75 if (haystack_len < needle_len)
76 return NULL;
77
78 /* Perform the search. Abstract memory is considered to be an array
79 of 'unsigned char' values, not an array of 'char' values. See
80 ISO C 99 section 6.2.6.1. */
81 if (needle_len < LONG_NEEDLE_THRESHOLD)
82 return two_way_short_needle ((const unsigned char *) haystack,
83 haystack_len,
84 (const unsigned char *) needle,
85 needle_len);
86 return two_way_long_needle ((const unsigned char *) haystack, haystack_len,
87 (const unsigned char *) needle,
88 needle_len);
89 }
90
91 #undef LONG_NEEDLE_THRESHOLD
92
93 #ifndef NO_ALIAS
94 weak_alias (__strcasestr, strcasestr)
95 #endif