Mercurial > hg > octave-kai > gnulib-hg
annotate lib/strstr.c @ 9601:fbf94c8a908a
Share two-way algorithm.
* lib/str-two-way.h: New file, merged from...
* lib/memmem.c: ...here...
* lib/strstr.c: ...and here.
* modules/memmem (Files): Use it.
* modules/strstr (Files): Likewise.
Signed-off-by: Eric Blake <ebb9@byu.net>
author | Eric Blake <ebb9@byu.net> |
---|---|
date | Thu, 10 Jan 2008 12:06:35 -0700 |
parents | 99268e709d31 |
children | e8d2c6fc33ad |
rev | line source |
---|---|
9600
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
1 /* Copyright (C) 1991,92,93,94,96,97,98,2000,2004,2007,2008 Free Software |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
2 Foundation, Inc. |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
3 This file is part of the GNU C Library. |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
4 |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
5 This program is free software; you can redistribute it and/or modify |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
6 it under the terms of the GNU General Public License as published by |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
7 the Free Software Foundation; either version 2, or (at your option) |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
8 any later version. |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
9 |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
10 This program is distributed in the hope that it will be useful, |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
13 GNU General Public License for more details. |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
14 |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
15 You should have received a copy of the GNU General Public License along |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
16 with this program; if not, write to the Free Software Foundation, |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
18 |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
19 /* This particular implementation was written by Eric Blake, 2008. */ |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
20 |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
21 #ifndef _LIBC |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
22 # include <config.h> |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
23 #endif |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
24 |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
25 /* Specification of strstr. */ |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
26 #include <string.h> |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
27 |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
28 #include <stdbool.h> |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
29 |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
30 #ifndef _LIBC |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
31 # define __builtin_expect(expr, val) (expr) |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
32 #endif |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
33 |
9601 | 34 #define RETURN_TYPE char * |
35 #define AVAILABLE(h, h_l, j, n_l) \ | |
36 (!memchr ((h) + (h_l), '\0', (j) + (n_l) - (h_l)) \ | |
37 && ((h_l) = (j) + (n_l))) | |
38 #include "str-two-way.h" | |
9600
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
39 |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
40 /* Return the first occurrence of NEEDLE in HAYSTACK. Return HAYSTACK |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
41 if NEEDLE is empty, otherwise NULL if NEEDLE is not found in |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
42 HAYSTACK. */ |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
43 char * |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
44 strstr (const char *haystack_start, const char *needle_start) |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
45 { |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
46 const char *haystack = haystack_start; |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
47 const char *needle = needle_start; |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
48 size_t needle_len; /* Length of NEEDLE. */ |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
49 size_t haystack_len; /* Known minimum length of HAYSTACK. */ |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
50 bool ok = true; /* True if NEEDLE is prefix of HAYSTACK. */ |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
51 |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
52 /* Determine length of NEEDLE, and in the process, make sure |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
53 HAYSTACK is at least as long (no point processing all of a long |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
54 NEEDLE if HAYSTACK is too short). */ |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
55 while (*haystack && *needle) |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
56 ok &= *haystack++ == *needle++; |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
57 if (*needle) |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
58 return NULL; |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
59 if (ok) |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
60 return (char *) haystack_start; |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
61 |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
62 /* Reduce the size of haystack using strchr, since it has a smaller |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
63 linear coefficient than the Two-Way algorithm. */ |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
64 needle_len = needle - needle_start; |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
65 haystack = strchr (haystack_start + 1, *needle_start); |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
66 if (!haystack || __builtin_expect (needle_len == 1, 0)) |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
67 return (char *) haystack; |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
68 needle -= needle_len; |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
69 haystack_len = (haystack > haystack_start + needle_len ? 1 |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
70 : needle_len + haystack_start - haystack); |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
71 |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
72 /* Perform the search. Abstract memory is considered to be an array |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
73 of 'unsigned char' values, not an array of 'char' values. See |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
74 ISO C 99 section 6.2.6.1. */ |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
75 if (needle_len < LONG_NEEDLE_THRESHOLD) |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
76 return two_way_short_needle ((const unsigned char *) haystack, |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
77 haystack_len, |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
78 (const unsigned char *) needle, needle_len); |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
79 return two_way_long_needle ((const unsigned char *) haystack, haystack_len, |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
80 (const unsigned char *) needle, needle_len); |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
81 } |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
82 |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
83 #undef LONG_NEEDLE_THRESHOLD |