annotate lib/strcasestr.c @ 40186:8964917f9574

autoupdate
author Karl Berry <karl@freefriends.org>
date Mon, 18 Feb 2019 08:02:49 -0800
parents b06060465f09
children
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.
40057
b06060465f09 maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents: 19484
diff changeset
2 Copyright (C) 2005-2019 Free Software Foundation, Inc.
6058
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
19190
9759915b2aca all: prefer https: URLs
Paul Eggert <eggert@cs.ucla.edu>
parents: 18626
diff changeset
16 along with this program; if not, see <https://www.gnu.org/licenses/>. */
6058
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
7304
1c4ed7637c24 Include <config.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents: 6164
diff changeset
18 #include <config.h>
6058
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 /* Specification. */
7980
89b6dfd07076 Declare strcasestr() in the <string.h> replacement, rather than in
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
21 #include <string.h>
6058
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 #include <ctype.h>
8125
0545abcdfb09 Ensure O(n) worst-case complexity of strcasestr substitute.
Bruno Haible <bruno@clisp.org>
parents: 8098
diff changeset
24 #include <stdbool.h>
9623
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
25 #include <strings.h>
8145
e8d8167b9164 Optimize memory allocation to use alloca when possible.
Bruno Haible <bruno@clisp.org>
parents: 8125
diff changeset
26
6058
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28
9623
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
29 /* Two-Way algorithm. */
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
30 #define RETURN_TYPE char *
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9623
diff changeset
31 #define AVAILABLE(h, h_l, j, n_l) \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9623
diff changeset
32 (!memchr ((h) + (h_l), '\0', (j) + (n_l) - (h_l)) \
9623
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
33 && ((h_l) = (j) + (n_l)))
9561
d722bd5e44bd Unify 5 copies of the KMP code.
Bruno Haible <bruno@clisp.org>
parents: 9558
diff changeset
34 #define CANON_ELEMENT(c) TOLOWER (c)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9623
diff changeset
35 #define CMP_FUNC(p1, p2, l) \
9623
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
36 strncasecmp ((const char *) (p1), (const char *) (p2), l)
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
37 #include "str-two-way.h"
8125
0545abcdfb09 Ensure O(n) worst-case complexity of strcasestr substitute.
Bruno Haible <bruno@clisp.org>
parents: 8098
diff changeset
38
9623
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
39 /* Find the first occurrence of NEEDLE in HAYSTACK, using
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
40 case-insensitive comparison. This function gives unspecified
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
41 results in multibyte locales. */
6058
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 char *
9623
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
43 strcasestr (const char *haystack_start, const char *needle_start)
6058
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 {
9623
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
45 const char *haystack = haystack_start;
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
46 const char *needle = needle_start;
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
47 size_t needle_len; /* Length of NEEDLE. */
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
48 size_t haystack_len; /* Known minimum length of HAYSTACK. */
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
49 bool ok = true; /* True if NEEDLE is prefix of HAYSTACK. */
8125
0545abcdfb09 Ensure O(n) worst-case complexity of strcasestr substitute.
Bruno Haible <bruno@clisp.org>
parents: 8098
diff changeset
50
9623
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
51 /* Determine length of NEEDLE, and in the process, make sure
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
52 HAYSTACK is at least as long (no point processing all of a long
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
53 NEEDLE if HAYSTACK is too short). */
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
54 while (*haystack && *needle)
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
55 {
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
56 ok &= (TOLOWER ((unsigned char) *haystack)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9623
diff changeset
57 == TOLOWER ((unsigned char) *needle));
9623
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
58 haystack++;
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
59 needle++;
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
60 }
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
61 if (*needle)
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
62 return NULL;
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
63 if (ok)
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
64 return (char *) haystack_start;
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
65 needle_len = needle - needle_start;
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
66 haystack = haystack_start + 1;
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
67 haystack_len = needle_len - 1;
8125
0545abcdfb09 Ensure O(n) worst-case complexity of strcasestr substitute.
Bruno Haible <bruno@clisp.org>
parents: 8098
diff changeset
68
9623
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
69 /* Perform the search. Abstract memory is considered to be an array
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
70 of 'unsigned char' values, not an array of 'char' values. See
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
71 ISO C 99 section 6.2.6.1. */
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
72 if (needle_len < LONG_NEEDLE_THRESHOLD)
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
73 return two_way_short_needle ((const unsigned char *) haystack,
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9623
diff changeset
74 haystack_len,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9623
diff changeset
75 (const unsigned char *) needle_start,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9623
diff changeset
76 needle_len);
9623
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
77 return two_way_long_needle ((const unsigned char *) haystack, haystack_len,
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9623
diff changeset
78 (const unsigned char *) needle_start,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9623
diff changeset
79 needle_len);
9623
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
80 }
6058
a7440145d6a9 New module 'strcasestr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81
9623
69d9307c0aa0 Convert strcasestr module to use Two-Way algorithm.
Eric Blake <ebb9@byu.net>
parents: 9561
diff changeset
82 #undef LONG_NEEDLE_THRESHOLD