diff options
Diffstat (limited to 'gl/strstr.c')
| -rw-r--r-- | gl/strstr.c | 32 |
1 files changed, 17 insertions, 15 deletions
diff --git a/gl/strstr.c b/gl/strstr.c index d6953f90..a5730a37 100644 --- a/gl/strstr.c +++ b/gl/strstr.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* Copyright (C) 1991-1994, 1996-1998, 2000, 2004, 2007-2025 Free Software | 1 | /* Copyright (C) 1991-1994, 1996-1998, 2000, 2004, 2007-2026 Free Software |
| 2 | Foundation, Inc. | 2 | Foundation, Inc. |
| 3 | This file is part of the GNU C Library. | 3 | This file is part of the GNU C Library. |
| 4 | 4 | ||
| @@ -36,31 +36,33 @@ | |||
| 36 | char * | 36 | char * |
| 37 | strstr (const char *haystack_start, const char *needle_start) | 37 | strstr (const char *haystack_start, const char *needle_start) |
| 38 | { | 38 | { |
| 39 | const char *haystack = haystack_start; | ||
| 40 | const char *needle = needle_start; | 39 | const char *needle = needle_start; |
| 41 | size_t needle_len; /* Length of NEEDLE. */ | ||
| 42 | size_t haystack_len; /* Known minimum length of HAYSTACK. */ | ||
| 43 | bool ok = true; /* True if NEEDLE is prefix of HAYSTACK. */ | ||
| 44 | 40 | ||
| 45 | /* Determine length of NEEDLE, and in the process, make sure | 41 | /* Determine length of NEEDLE, and in the process, make sure |
| 46 | HAYSTACK is at least as long (no point processing all of a long | 42 | HAYSTACK is at least as long (no point processing all of a long |
| 47 | NEEDLE if HAYSTACK is too short). */ | 43 | NEEDLE if HAYSTACK is too short). */ |
| 48 | while (*haystack && *needle) | 44 | { |
| 49 | ok &= *haystack++ == *needle++; | 45 | const char *haystack = haystack_start; |
| 50 | if (*needle) | 46 | bool ok = true; /* True if NEEDLE is prefix of HAYSTACK. */ |
| 51 | return NULL; | 47 | while (*haystack && *needle) |
| 52 | if (ok) | 48 | ok &= *haystack++ == *needle++; |
| 53 | return (char *) haystack_start; | 49 | if (*needle) |
| 50 | return NULL; | ||
| 51 | if (ok) | ||
| 52 | return (char *) haystack_start; | ||
| 53 | } | ||
| 54 | 54 | ||
| 55 | /* Reduce the size of haystack using strchr, since it has a smaller | 55 | /* Reduce the size of haystack using strchr, since it has a smaller |
| 56 | linear coefficient than the Two-Way algorithm. */ | 56 | linear coefficient than the Two-Way algorithm. */ |
| 57 | needle_len = needle - needle_start; | 57 | size_t needle_len = /* Length of NEEDLE. */ |
| 58 | haystack = strchr (haystack_start + 1, *needle_start); | 58 | needle - needle_start; |
| 59 | const char *haystack = strchr (haystack_start + 1, *needle_start); | ||
| 59 | if (!haystack || __builtin_expect (needle_len == 1, 0)) | 60 | if (!haystack || __builtin_expect (needle_len == 1, 0)) |
| 60 | return (char *) haystack; | 61 | return (char *) haystack; |
| 61 | needle -= needle_len; | 62 | needle -= needle_len; |
| 62 | haystack_len = (haystack > haystack_start + needle_len ? 1 | 63 | size_t haystack_len = /* Known minimum length of HAYSTACK. */ |
| 63 | : needle_len + haystack_start - haystack); | 64 | (haystack > haystack_start + needle_len ? 1 |
| 65 | : needle_len + haystack_start - haystack); | ||
| 64 | 66 | ||
| 65 | /* Perform the search. Abstract memory is considered to be an array | 67 | /* Perform the search. Abstract memory is considered to be an array |
| 66 | of 'unsigned char' values, not an array of 'char' values. See | 68 | of 'unsigned char' values, not an array of 'char' values. See |
