diff options
| author | Jörg Frings-Fürst <debian@jff.email> | 2026-03-08 17:28:33 +0100 |
|---|---|---|
| committer | Jörg Frings-Fürst <debian@jff.email> | 2026-03-08 17:28:33 +0100 |
| commit | 5f59a34ab747dde8ede7357f3431bf06bd6002fe (patch) | |
| tree | 056a4477fd870d454d5be5868cddab829a47f4d2 /lib/strstr.c | |
| parent | 27dae84ed92f1ef0300263091972338d12e78348 (diff) | |
New upstream version 1.4.2upstream/1.4.2upstream
Diffstat (limited to 'lib/strstr.c')
| -rw-r--r-- | lib/strstr.c | 32 |
1 files changed, 17 insertions, 15 deletions
diff --git a/lib/strstr.c b/lib/strstr.c index d6953f90..a5730a37 100644 --- a/lib/strstr.c +++ b/lib/strstr.c @@ -1,4 +1,4 @@ -/* Copyright (C) 1991-1994, 1996-1998, 2000, 2004, 2007-2025 Free Software +/* Copyright (C) 1991-1994, 1996-1998, 2000, 2004, 2007-2026 Free Software Foundation, Inc. This file is part of the GNU C Library. @@ -36,31 +36,33 @@ char * strstr (const char *haystack_start, const char *needle_start) { - const char *haystack = haystack_start; const char *needle = needle_start; - size_t needle_len; /* Length of NEEDLE. */ - size_t haystack_len; /* Known minimum length of HAYSTACK. */ - bool ok = true; /* True if NEEDLE is prefix of HAYSTACK. */ /* Determine length of NEEDLE, and in the process, make sure HAYSTACK is at least as long (no point processing all of a long NEEDLE if HAYSTACK is too short). */ - while (*haystack && *needle) - ok &= *haystack++ == *needle++; - if (*needle) - return NULL; - if (ok) - return (char *) haystack_start; + { + const char *haystack = haystack_start; + bool ok = true; /* True if NEEDLE is prefix of HAYSTACK. */ + while (*haystack && *needle) + ok &= *haystack++ == *needle++; + if (*needle) + return NULL; + if (ok) + return (char *) haystack_start; + } /* Reduce the size of haystack using strchr, since it has a smaller linear coefficient than the Two-Way algorithm. */ - needle_len = needle - needle_start; - haystack = strchr (haystack_start + 1, *needle_start); + size_t needle_len = /* Length of NEEDLE. */ + needle - needle_start; + const char *haystack = strchr (haystack_start + 1, *needle_start); if (!haystack || __builtin_expect (needle_len == 1, 0)) return (char *) haystack; needle -= needle_len; - haystack_len = (haystack > haystack_start + needle_len ? 1 - : needle_len + haystack_start - haystack); + size_t haystack_len = /* Known minimum length of HAYSTACK. */ + (haystack > haystack_start + needle_len ? 1 + : needle_len + haystack_start - haystack); /* Perform the search. Abstract memory is considered to be an array of 'unsigned char' values, not an array of 'char' values. See |
