Mercurial > hg > octave-shane > gnulib-hg
annotate lib/strstr.c @ 17616:cae23a655793
autoupdate
author | Karl Berry <karl@freefriends.org> |
---|---|
date | Sat, 25 Jan 2014 06:40:43 -0800 |
parents | 344018b6e5d7 |
children |
rev | line source |
---|---|
17587 | 1 /* Copyright (C) 1991-1994, 1996-1998, 2000, 2004, 2007-2014 Free Software |
14079
97fc9a21a8fb
maint: update almost all copyright ranges to include 2011
Jim Meyering <meyering@redhat.com>
parents:
12559
diff
changeset
|
2 Foundation, Inc. |
9600
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 |
16366
bb182ee4a09d
maint: replace FSF snail-mail addresses with URLs
Paul Eggert <eggert@cs.ucla.edu>
parents:
16201
diff
changeset
|
16 with this program; if not, see <http://www.gnu.org/licenses/>. */ |
9600
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
17 |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
18 /* This particular implementation was written by Eric Blake, 2008. */ |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
19 |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
20 #ifndef _LIBC |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
21 # include <config.h> |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
22 #endif |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
23 |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
24 /* Specification of strstr. */ |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
25 #include <string.h> |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
26 |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
27 #include <stdbool.h> |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
28 |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
29 #ifndef _LIBC |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
30 # define __builtin_expect(expr, val) (expr) |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
31 #endif |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
32 |
9601 | 33 #define RETURN_TYPE char * |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9601
diff
changeset
|
34 #define AVAILABLE(h, h_l, j, n_l) \ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9601
diff
changeset
|
35 (!memchr ((h) + (h_l), '\0', (j) + (n_l) - (h_l)) \ |
9601 | 36 && ((h_l) = (j) + (n_l))) |
37 #include "str-two-way.h" | |
9600
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
38 |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
39 /* Return the first occurrence of NEEDLE in HAYSTACK. Return HAYSTACK |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
40 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
|
41 HAYSTACK. */ |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
42 char * |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
43 strstr (const char *haystack_start, const char *needle_start) |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
44 { |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
45 const char *haystack = haystack_start; |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
46 const char *needle = needle_start; |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
47 size_t needle_len; /* Length of NEEDLE. */ |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
48 size_t haystack_len; /* Known minimum length of HAYSTACK. */ |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
49 bool ok = true; /* True if NEEDLE is prefix of HAYSTACK. */ |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
50 |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
51 /* Determine length of NEEDLE, and in the process, make sure |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
52 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
|
53 NEEDLE if HAYSTACK is too short). */ |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
54 while (*haystack && *needle) |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
55 ok &= *haystack++ == *needle++; |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
56 if (*needle) |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
57 return NULL; |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
58 if (ok) |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
59 return (char *) haystack_start; |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
60 |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
61 /* 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
|
62 linear coefficient than the Two-Way algorithm. */ |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
63 needle_len = needle - needle_start; |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
64 haystack = strchr (haystack_start + 1, *needle_start); |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
65 if (!haystack || __builtin_expect (needle_len == 1, 0)) |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
66 return (char *) haystack; |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
67 needle -= needle_len; |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
68 haystack_len = (haystack > haystack_start + needle_len ? 1 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9601
diff
changeset
|
69 : needle_len + haystack_start - haystack); |
9600
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
70 |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
71 /* Perform the search. Abstract memory is considered to be an array |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
72 of 'unsigned char' values, not an array of 'char' values. See |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
73 ISO C 99 section 6.2.6.1. */ |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
74 if (needle_len < LONG_NEEDLE_THRESHOLD) |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
75 return two_way_short_needle ((const unsigned char *) haystack, |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9601
diff
changeset
|
76 haystack_len, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9601
diff
changeset
|
77 (const unsigned char *) needle, needle_len); |
9600
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
78 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:
9601
diff
changeset
|
79 (const unsigned char *) needle, needle_len); |
9600
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
80 } |
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 #undef LONG_NEEDLE_THRESHOLD |