diff options
author | Thomas Guyot-Sionnest <dermoth@aei.ca> | 2010-04-07 21:11:46 -0400 |
---|---|---|
committer | Thomas Guyot-Sionnest <dermoth@aei.ca> | 2010-04-12 21:26:35 -0400 |
commit | 74da141e618ef99959d509cb2e7be35a348a39db (patch) | |
tree | 88ebc38b381a1021fc2d74864a71e230ae591c3d /gl/strstr.c | |
parent | c63a4f726a0b6ad8cf6040f947754a81fd4683bb (diff) | |
download | monitoring-plugins-74da141e618ef99959d509cb2e7be35a348a39db.tar.gz |
Sync with the latest Gnulib code (177f525)
Signed-off-by: Thomas Guyot-Sionnest <dermoth@aei.ca>
Diffstat (limited to 'gl/strstr.c')
-rw-r--r-- | gl/strstr.c | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/gl/strstr.c b/gl/strstr.c new file mode 100644 index 00000000..ae184ff7 --- /dev/null +++ b/gl/strstr.c | |||
@@ -0,0 +1,83 @@ | |||
1 | /* Copyright (C) 1991, 1992, 1993, 1994, 1996, 1997, 1998, 2000, 2004, 2007, | ||
2 | 2008, 2009, 2010 Free Software Foundation, Inc. | ||
3 | This file is part of the GNU C Library. | ||
4 | |||
5 | This program is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published by | ||
7 | the Free Software Foundation; either version 3, or (at your option) | ||
8 | any later version. | ||
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 along | ||
16 | with this program; if not, write to the Free Software Foundation, | ||
17 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ | ||
18 | |||
19 | /* This particular implementation was written by Eric Blake, 2008. */ | ||
20 | |||
21 | #ifndef _LIBC | ||
22 | # include <config.h> | ||
23 | #endif | ||
24 | |||
25 | /* Specification of strstr. */ | ||
26 | #include <string.h> | ||
27 | |||
28 | #include <stdbool.h> | ||
29 | |||
30 | #ifndef _LIBC | ||
31 | # define __builtin_expect(expr, val) (expr) | ||
32 | #endif | ||
33 | |||
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" | ||
39 | |||
40 | /* Return the first occurrence of NEEDLE in HAYSTACK. Return HAYSTACK | ||
41 | if NEEDLE is empty, otherwise NULL if NEEDLE is not found in | ||
42 | HAYSTACK. */ | ||
43 | char * | ||
44 | strstr (const char *haystack_start, const char *needle_start) | ||
45 | { | ||
46 | const char *haystack = haystack_start; | ||
47 | const char *needle = needle_start; | ||
48 | size_t needle_len; /* Length of NEEDLE. */ | ||
49 | size_t haystack_len; /* Known minimum length of HAYSTACK. */ | ||
50 | bool ok = true; /* True if NEEDLE is prefix of HAYSTACK. */ | ||
51 | |||
52 | /* Determine length of NEEDLE, and in the process, make sure | ||
53 | HAYSTACK is at least as long (no point processing all of a long | ||
54 | NEEDLE if HAYSTACK is too short). */ | ||
55 | while (*haystack && *needle) | ||
56 | ok &= *haystack++ == *needle++; | ||
57 | if (*needle) | ||
58 | return NULL; | ||
59 | if (ok) | ||
60 | return (char *) haystack_start; | ||
61 | |||
62 | /* Reduce the size of haystack using strchr, since it has a smaller | ||
63 | linear coefficient than the Two-Way algorithm. */ | ||
64 | needle_len = needle - needle_start; | ||
65 | haystack = strchr (haystack_start + 1, *needle_start); | ||
66 | if (!haystack || __builtin_expect (needle_len == 1, 0)) | ||
67 | return (char *) haystack; | ||
68 | needle -= needle_len; | ||
69 | haystack_len = (haystack > haystack_start + needle_len ? 1 | ||
70 | : needle_len + haystack_start - haystack); | ||
71 | |||
72 | /* Perform the search. Abstract memory is considered to be an array | ||
73 | of 'unsigned char' values, not an array of 'char' values. See | ||
74 | ISO C 99 section 6.2.6.1. */ | ||
75 | if (needle_len < LONG_NEEDLE_THRESHOLD) | ||
76 | return two_way_short_needle ((const unsigned char *) haystack, | ||
77 | haystack_len, | ||
78 | (const unsigned char *) needle, needle_len); | ||
79 | return two_way_long_needle ((const unsigned char *) haystack, haystack_len, | ||
80 | (const unsigned char *) needle, needle_len); | ||
81 | } | ||
82 | |||
83 | #undef LONG_NEEDLE_THRESHOLD | ||