]>
Commit | Line | Data |
---|---|---|
b168057a | 1 | /* Copyright (C) 1991-2015 Free Software Foundation, Inc. |
63551311 | 2 | This file is part of the GNU C Library. |
28f540f4 | 3 | |
63551311 | 4 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
5 | modify it under the terms of the GNU Lesser General Public |
6 | License as published by the Free Software Foundation; either | |
7 | version 2.1 of the License, or (at your option) any later version. | |
28f540f4 | 8 | |
63551311 UD |
9 | The GNU C Library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 12 | Lesser General Public License for more details. |
28f540f4 | 13 | |
41bdb6e2 | 14 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
15 | License along with the GNU C Library; if not, see |
16 | <http://www.gnu.org/licenses/>. */ | |
28f540f4 | 17 | |
0caca71a UD |
18 | /* This particular implementation was written by Eric Blake, 2008. */ |
19 | ||
20 | #ifndef _LIBC | |
21 | # include <config.h> | |
22 | #endif | |
23 | ||
24 | /* Specification of memmem. */ | |
28f540f4 RM |
25 | #include <string.h> |
26 | ||
290855f3 RM |
27 | #ifndef _LIBC |
28 | # define __builtin_expect(expr, val) (expr) | |
9975e3d3 | 29 | # define __memmem memmem |
290855f3 RM |
30 | #endif |
31 | ||
0caca71a UD |
32 | #define RETURN_TYPE void * |
33 | #define AVAILABLE(h, h_l, j, n_l) ((j) <= (h_l) - (n_l)) | |
34 | #include "str-two-way.h" | |
35 | ||
9a0a462c | 36 | #undef memmem |
28f540f4 | 37 | |
0caca71a UD |
38 | /* Return the first occurrence of NEEDLE in HAYSTACK. Return HAYSTACK |
39 | if NEEDLE_LEN is 0, otherwise NULL if NEEDLE is not found in | |
40 | HAYSTACK. */ | |
63551311 | 41 | void * |
9975e3d3 JM |
42 | __memmem (const void *haystack_start, size_t haystack_len, |
43 | const void *needle_start, size_t needle_len) | |
28f540f4 | 44 | { |
0caca71a UD |
45 | /* Abstract memory is considered to be an array of 'unsigned char' values, |
46 | not an array of 'char' values. See ISO C 99 section 6.2.6.1. */ | |
47 | const unsigned char *haystack = (const unsigned char *) haystack_start; | |
48 | const unsigned char *needle = (const unsigned char *) needle_start; | |
28f540f4 RM |
49 | |
50 | if (needle_len == 0) | |
dc825f85 | 51 | /* The first occurrence of the empty string is deemed to occur at |
4775243a | 52 | the beginning of the string. */ |
9aae565a | 53 | return (void *) haystack; |
28f540f4 | 54 | |
1261b97d AJ |
55 | /* Sanity check, otherwise the loop might search through the whole |
56 | memory. */ | |
a1ffb40e | 57 | if (__glibc_unlikely (haystack_len < needle_len)) |
1261b97d AJ |
58 | return NULL; |
59 | ||
0caca71a UD |
60 | /* Use optimizations in memchr when possible, to reduce the search |
61 | size of haystack using a linear algorithm with a smaller | |
62 | coefficient. However, avoid memchr for long needles, since we | |
63 | can often achieve sublinear performance. */ | |
64 | if (needle_len < LONG_NEEDLE_THRESHOLD) | |
65 | { | |
66 | haystack = memchr (haystack, *needle, haystack_len); | |
67 | if (!haystack || __builtin_expect (needle_len == 1, 0)) | |
68 | return (void *) haystack; | |
69 | haystack_len -= haystack - (const unsigned char *) haystack_start; | |
70 | if (haystack_len < needle_len) | |
71 | return NULL; | |
72 | return two_way_short_needle (haystack, haystack_len, needle, needle_len); | |
73 | } | |
74 | else | |
75 | return two_way_long_needle (haystack, haystack_len, needle, needle_len); | |
28f540f4 | 76 | } |
9975e3d3 JM |
77 | libc_hidden_def (__memmem) |
78 | weak_alias (__memmem, memmem) | |
79 | libc_hidden_weak (memmem) | |
0caca71a UD |
80 | |
81 | #undef LONG_NEEDLE_THRESHOLD |