Micro-optimize critical path of strstr, strcase and memmem.
[platform/upstream/glibc.git] / string / strstr.c
1 /* Return the offset of one string within another.
2    Copyright (C) 1994-2012 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library 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 GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
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 #define AVAILABLE1(h, h_l, j, n_l) (true)
39 #define AVAILABLE2(h, h_l, j, n_l) AVAILABLE (h, h_l, j, n_l)
40 #define RET0_IF_0(a) if (!a) goto ret0
41 #define AVAILABLE1_USES_J (0)
42 #include "str-two-way.h"
43
44 #undef strstr
45
46 #ifndef STRSTR
47 #define STRSTR strstr
48 #endif
49
50 /* Return the first occurrence of NEEDLE in HAYSTACK.  Return HAYSTACK
51    if NEEDLE is empty, otherwise NULL if NEEDLE is not found in
52    HAYSTACK.  */
53 char *
54 STRSTR (const char *haystack_start, const char *needle_start)
55 {
56   const char *haystack = haystack_start;
57   const char *needle = needle_start;
58   size_t needle_len; /* Length of NEEDLE.  */
59   size_t haystack_len; /* Known minimum length of HAYSTACK.  */
60   bool ok = true; /* True if NEEDLE is prefix of HAYSTACK.  */
61
62   /* Determine length of NEEDLE, and in the process, make sure
63      HAYSTACK is at least as long (no point processing all of a long
64      NEEDLE if HAYSTACK is too short).  */
65   while (*haystack && *needle)
66     ok &= *haystack++ == *needle++;
67   if (*needle)
68     return NULL;
69   if (ok)
70     return (char *) haystack_start;
71
72   /* Reduce the size of haystack using strchr, since it has a smaller
73      linear coefficient than the Two-Way algorithm.  */
74   needle_len = needle - needle_start;
75   haystack = strchr (haystack_start + 1, *needle_start);
76   if (!haystack || __builtin_expect (needle_len == 1, 0))
77     return (char *) haystack;
78   needle -= needle_len;
79   haystack_len = (haystack > haystack_start + needle_len ? 1
80                   : needle_len + haystack_start - haystack);
81
82   /* Perform the search.  Abstract memory is considered to be an array
83      of 'unsigned char' values, not an array of 'char' values.  See
84      ISO C 99 section 6.2.6.1.  */
85   if (needle_len < LONG_NEEDLE_THRESHOLD)
86     return two_way_short_needle ((const unsigned char *) haystack,
87                                  haystack_len,
88                                  (const unsigned char *) needle, needle_len);
89   return two_way_long_needle ((const unsigned char *) haystack, haystack_len,
90                               (const unsigned char *) needle, needle_len);
91 }
92 libc_hidden_builtin_def (strstr)
93
94 #undef LONG_NEEDLE_THRESHOLD