Mercurial > hg > octave-jordi > gnulib-hg
annotate lib/c-strcasestr.c @ 18079:4c948fd76734 default tip master
autoupdate
author | Karl Berry <karl@freefriends.org> |
---|---|
date | Mon, 24 Aug 2015 06:09:19 -0700 |
parents | ab58d4870664 |
children |
rev | line source |
---|---|
6360 | 1 /* c-strcasestr.c -- case insensitive substring search in C locale |
17848 | 2 Copyright (C) 2005-2015 Free Software Foundation, Inc. |
6360 | 3 Written by Bruno Haible <bruno@clisp.org>, 2005. |
4 | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8948
diff
changeset
|
5 This program is free software: you can redistribute it and/or modify |
6360 | 6 it under the terms of the GNU General Public License as published by |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8948
diff
changeset
|
7 the Free Software Foundation; either version 3 of the License, or |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8948
diff
changeset
|
8 (at your option) any later version. |
6360 | 9 |
10 This program 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 | |
13 GNU General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU General Public License | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8948
diff
changeset
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
6360 | 17 |
7304
1c4ed7637c24
Include <config.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents:
6360
diff
changeset
|
18 #include <config.h> |
6360 | 19 |
20 /* Specification. */ | |
21 #include "c-strcasestr.h" | |
22 | |
8122
32bd3344417d
Ensure O(n) worst-case complexity of c_strcasestr.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
23 #include <stdbool.h> |
32bd3344417d
Ensure O(n) worst-case complexity of c_strcasestr.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
24 #include <string.h> |
6360 | 25 |
26 #include "c-ctype.h" | |
9630
729b6d4ffac1
Convert c-strcasestr to be more efficient.
Eric Blake <ebb9@byu.net>
parents:
9561
diff
changeset
|
27 #include "c-strcase.h" |
6360 | 28 |
9630
729b6d4ffac1
Convert c-strcasestr to be more efficient.
Eric Blake <ebb9@byu.net>
parents:
9561
diff
changeset
|
29 /* Two-Way algorithm. */ |
729b6d4ffac1
Convert c-strcasestr to be more efficient.
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:
9630
diff
changeset
|
31 #define AVAILABLE(h, h_l, j, n_l) \ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9630
diff
changeset
|
32 (!memchr ((h) + (h_l), '\0', (j) + (n_l) - (h_l)) \ |
9630
729b6d4ffac1
Convert c-strcasestr to be more efficient.
Eric Blake <ebb9@byu.net>
parents:
9561
diff
changeset
|
33 && ((h_l) = (j) + (n_l))) |
729b6d4ffac1
Convert c-strcasestr to be more efficient.
Eric Blake <ebb9@byu.net>
parents:
9561
diff
changeset
|
34 #define CANON_ELEMENT c_tolower |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9630
diff
changeset
|
35 #define CMP_FUNC(p1, p2, l) \ |
9630
729b6d4ffac1
Convert c-strcasestr to be more efficient.
Eric Blake <ebb9@byu.net>
parents:
9561
diff
changeset
|
36 c_strncasecmp ((const char *) (p1), (const char *) (p2), l) |
729b6d4ffac1
Convert c-strcasestr to be more efficient.
Eric Blake <ebb9@byu.net>
parents:
9561
diff
changeset
|
37 #include "str-two-way.h" |
8122
32bd3344417d
Ensure O(n) worst-case complexity of c_strcasestr.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
38 |
6360 | 39 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive |
9630
729b6d4ffac1
Convert c-strcasestr to be more efficient.
Eric Blake <ebb9@byu.net>
parents:
9561
diff
changeset
|
40 comparison from the C locale, regardless of the current locale. */ |
6360 | 41 char * |
9630
729b6d4ffac1
Convert c-strcasestr to be more efficient.
Eric Blake <ebb9@byu.net>
parents:
9561
diff
changeset
|
42 c_strcasestr (const char *haystack_start, const char *needle_start) |
6360 | 43 { |
9630
729b6d4ffac1
Convert c-strcasestr to be more efficient.
Eric Blake <ebb9@byu.net>
parents:
9561
diff
changeset
|
44 const char *haystack = haystack_start; |
729b6d4ffac1
Convert c-strcasestr to be more efficient.
Eric Blake <ebb9@byu.net>
parents:
9561
diff
changeset
|
45 const char *needle = needle_start; |
729b6d4ffac1
Convert c-strcasestr to be more efficient.
Eric Blake <ebb9@byu.net>
parents:
9561
diff
changeset
|
46 size_t needle_len; /* Length of NEEDLE. */ |
729b6d4ffac1
Convert c-strcasestr to be more efficient.
Eric Blake <ebb9@byu.net>
parents:
9561
diff
changeset
|
47 size_t haystack_len; /* Known minimum length of HAYSTACK. */ |
729b6d4ffac1
Convert c-strcasestr to be more efficient.
Eric Blake <ebb9@byu.net>
parents:
9561
diff
changeset
|
48 bool ok = true; /* True if NEEDLE is prefix of HAYSTACK. */ |
8122
32bd3344417d
Ensure O(n) worst-case complexity of c_strcasestr.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
49 |
9630
729b6d4ffac1
Convert c-strcasestr to be more efficient.
Eric Blake <ebb9@byu.net>
parents:
9561
diff
changeset
|
50 /* Determine length of NEEDLE, and in the process, make sure |
729b6d4ffac1
Convert c-strcasestr to be more efficient.
Eric Blake <ebb9@byu.net>
parents:
9561
diff
changeset
|
51 HAYSTACK is at least as long (no point processing all of a long |
729b6d4ffac1
Convert c-strcasestr to be more efficient.
Eric Blake <ebb9@byu.net>
parents:
9561
diff
changeset
|
52 NEEDLE if HAYSTACK is too short). */ |
729b6d4ffac1
Convert c-strcasestr to be more efficient.
Eric Blake <ebb9@byu.net>
parents:
9561
diff
changeset
|
53 while (*haystack && *needle) |
729b6d4ffac1
Convert c-strcasestr to be more efficient.
Eric Blake <ebb9@byu.net>
parents:
9561
diff
changeset
|
54 ok &= (c_tolower ((unsigned char) *haystack++) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9630
diff
changeset
|
55 == c_tolower ((unsigned char) *needle++)); |
9630
729b6d4ffac1
Convert c-strcasestr to be more efficient.
Eric Blake <ebb9@byu.net>
parents:
9561
diff
changeset
|
56 if (*needle) |
729b6d4ffac1
Convert c-strcasestr to be more efficient.
Eric Blake <ebb9@byu.net>
parents:
9561
diff
changeset
|
57 return NULL; |
729b6d4ffac1
Convert c-strcasestr to be more efficient.
Eric Blake <ebb9@byu.net>
parents:
9561
diff
changeset
|
58 if (ok) |
729b6d4ffac1
Convert c-strcasestr to be more efficient.
Eric Blake <ebb9@byu.net>
parents:
9561
diff
changeset
|
59 return (char *) haystack_start; |
729b6d4ffac1
Convert c-strcasestr to be more efficient.
Eric Blake <ebb9@byu.net>
parents:
9561
diff
changeset
|
60 needle_len = needle - needle_start; |
729b6d4ffac1
Convert c-strcasestr to be more efficient.
Eric Blake <ebb9@byu.net>
parents:
9561
diff
changeset
|
61 haystack = haystack_start + 1; |
729b6d4ffac1
Convert c-strcasestr to be more efficient.
Eric Blake <ebb9@byu.net>
parents:
9561
diff
changeset
|
62 haystack_len = needle_len - 1; |
8122
32bd3344417d
Ensure O(n) worst-case complexity of c_strcasestr.
Bruno Haible <bruno@clisp.org>
parents:
7304
diff
changeset
|
63 |
9630
729b6d4ffac1
Convert c-strcasestr to be more efficient.
Eric Blake <ebb9@byu.net>
parents:
9561
diff
changeset
|
64 /* Perform the search. Abstract memory is considered to be an array |
729b6d4ffac1
Convert c-strcasestr to be more efficient.
Eric Blake <ebb9@byu.net>
parents:
9561
diff
changeset
|
65 of 'unsigned char' values, not an array of 'char' values. See |
729b6d4ffac1
Convert c-strcasestr to be more efficient.
Eric Blake <ebb9@byu.net>
parents:
9561
diff
changeset
|
66 ISO C 99 section 6.2.6.1. */ |
729b6d4ffac1
Convert c-strcasestr to be more efficient.
Eric Blake <ebb9@byu.net>
parents:
9561
diff
changeset
|
67 if (needle_len < LONG_NEEDLE_THRESHOLD) |
729b6d4ffac1
Convert c-strcasestr to be more efficient.
Eric Blake <ebb9@byu.net>
parents:
9561
diff
changeset
|
68 return two_way_short_needle ((const unsigned char *) haystack, |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9630
diff
changeset
|
69 haystack_len, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9630
diff
changeset
|
70 (const unsigned char *) needle_start, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9630
diff
changeset
|
71 needle_len); |
9630
729b6d4ffac1
Convert c-strcasestr to be more efficient.
Eric Blake <ebb9@byu.net>
parents:
9561
diff
changeset
|
72 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:
9630
diff
changeset
|
73 (const unsigned char *) needle_start, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9630
diff
changeset
|
74 needle_len); |
9630
729b6d4ffac1
Convert c-strcasestr to be more efficient.
Eric Blake <ebb9@byu.net>
parents:
9561
diff
changeset
|
75 } |
6360 | 76 |
9630
729b6d4ffac1
Convert c-strcasestr to be more efficient.
Eric Blake <ebb9@byu.net>
parents:
9561
diff
changeset
|
77 #undef LONG_NEEDLE_THRESHOLD |