]> git.ipfire.org Git - thirdparty/glibc.git/blame - string/strcasestr.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / string / strcasestr.c
CommitLineData
f4017d20 1/* Return the offset of one string within another.
bfff8b1b 2 Copyright (C) 1994-2017 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
28#if HAVE_CONFIG_H
29# include <config.h>
30#endif
31
0caca71a
UD
32/* Specification. */
33#include <string.h>
34
f4017d20 35#include <ctype.h>
0caca71a
UD
36#include <stdbool.h>
37#include <strings.h>
f4017d20 38
99677e57 39#define TOLOWER(Ch) tolower (Ch)
f4017d20 40
0caca71a
UD
41/* Two-Way algorithm. */
42#define RETURN_TYPE char *
43#define AVAILABLE(h, h_l, j, n_l) \
44 (!memchr ((h) + (h_l), '\0', (j) + (n_l) - (h_l)) \
45 && ((h_l) = (j) + (n_l)))
57e605ba 46#define CHECK_EOL (1)
400726de 47#define RET0_IF_0(a) if (!a) goto ret0
0caca71a
UD
48#define CANON_ELEMENT(c) TOLOWER (c)
49#define CMP_FUNC(p1, p2, l) \
74e13385 50 __strncasecmp ((const char *) (p1), (const char *) (p2), l)
0caca71a 51#include "str-two-way.h"
f4017d20 52
8619129f
UD
53#undef strcasestr
54#undef __strcasestr
f4017d20 55
2b7a8664
L
56#ifndef STRCASESTR
57#define STRCASESTR __strcasestr
58#endif
59
60
0caca71a
UD
61/* Find the first occurrence of NEEDLE in HAYSTACK, using
62 case-insensitive comparison. This function gives unspecified
63 results in multibyte locales. */
f4017d20 64char *
2b7a8664 65STRCASESTR (const char *haystack_start, const char *needle_start)
f4017d20 66{
0caca71a
UD
67 const char *haystack = haystack_start;
68 const char *needle = needle_start;
69 size_t needle_len; /* Length of NEEDLE. */
70 size_t haystack_len; /* Known minimum length of HAYSTACK. */
71 bool ok = true; /* True if NEEDLE is prefix of HAYSTACK. */
72
73 /* Determine length of NEEDLE, and in the process, make sure
74 HAYSTACK is at least as long (no point processing all of a long
75 NEEDLE if HAYSTACK is too short). */
76 while (*haystack && *needle)
f4017d20 77 {
0caca71a
UD
78 ok &= (TOLOWER ((unsigned char) *haystack)
79 == TOLOWER ((unsigned char) *needle));
80 haystack++;
81 needle++;
f4017d20 82 }
0caca71a
UD
83 if (*needle)
84 return NULL;
85 if (ok)
86 return (char *) haystack_start;
87 needle_len = needle - needle_start;
88 haystack = haystack_start + 1;
89 haystack_len = needle_len - 1;
90
91 /* Perform the search. Abstract memory is considered to be an array
92 of 'unsigned char' values, not an array of 'char' values. See
93 ISO C 99 section 6.2.6.1. */
94 if (needle_len < LONG_NEEDLE_THRESHOLD)
95 return two_way_short_needle ((const unsigned char *) haystack,
96 haystack_len,
97 (const unsigned char *) needle_start,
98 needle_len);
99 return two_way_long_needle ((const unsigned char *) haystack, haystack_len,
100 (const unsigned char *) needle_start,
101 needle_len);
f4017d20
UD
102}
103
0caca71a
UD
104#undef LONG_NEEDLE_THRESHOLD
105
dbc676d4 106#ifndef NO_ALIAS
f4017d20 107weak_alias (__strcasestr, strcasestr)
dbc676d4 108#endif