diff options
Diffstat (limited to 'gl/strcasestr.c')
| -rw-r--r-- | gl/strcasestr.c | 41 |
1 files changed, 22 insertions, 19 deletions
diff --git a/gl/strcasestr.c b/gl/strcasestr.c index fd0e2c3e..fcfbcf3b 100644 --- a/gl/strcasestr.c +++ b/gl/strcasestr.c | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* Case-insensitive searching in a string. | 1 | /* Case-insensitive searching in a string. |
| 2 | Copyright (C) 2005-2025 Free Software Foundation, Inc. | 2 | Copyright (C) 2005-2026 Free Software Foundation, Inc. |
| 3 | Written by Bruno Haible <bruno@clisp.org>, 2005. | 3 | Written by Bruno Haible <bruno@clisp.org>, 2005. |
| 4 | 4 | ||
| 5 | This file is free software: you can redistribute it and/or modify | 5 | This file is free software: you can redistribute it and/or modify |
| @@ -39,29 +39,32 @@ | |||
| 39 | char * | 39 | char * |
| 40 | strcasestr (const char *haystack_start, const char *needle_start) | 40 | strcasestr (const char *haystack_start, const char *needle_start) |
| 41 | { | 41 | { |
| 42 | const char *haystack = haystack_start; | ||
| 43 | const char *needle = needle_start; | 42 | const char *needle = needle_start; |
| 44 | size_t needle_len; /* Length of NEEDLE. */ | ||
| 45 | size_t haystack_len; /* Known minimum length of HAYSTACK. */ | ||
| 46 | bool ok = true; /* True if NEEDLE is prefix of HAYSTACK. */ | ||
| 47 | 43 | ||
| 48 | /* Determine length of NEEDLE, and in the process, make sure | 44 | /* Determine length of NEEDLE, and in the process, make sure |
| 49 | HAYSTACK is at least as long (no point processing all of a long | 45 | HAYSTACK is at least as long (no point processing all of a long |
| 50 | NEEDLE if HAYSTACK is too short). */ | 46 | NEEDLE if HAYSTACK is too short). */ |
| 51 | while (*haystack && *needle) | 47 | { |
| 52 | { | 48 | const char *haystack = haystack_start; |
| 53 | ok &= (tolower ((unsigned char) *haystack) | 49 | bool ok = true; /* True if NEEDLE is prefix of HAYSTACK. */ |
| 54 | == tolower ((unsigned char) *needle)); | 50 | while (*haystack && *needle) |
| 55 | haystack++; | 51 | { |
| 56 | needle++; | 52 | ok &= (tolower ((unsigned char) *haystack) |
| 57 | } | 53 | == tolower ((unsigned char) *needle)); |
| 58 | if (*needle) | 54 | haystack++; |
| 59 | return NULL; | 55 | needle++; |
| 60 | if (ok) | 56 | } |
| 61 | return (char *) haystack_start; | 57 | if (*needle) |
| 62 | needle_len = needle - needle_start; | 58 | return NULL; |
| 63 | haystack = haystack_start + 1; | 59 | if (ok) |
| 64 | haystack_len = needle_len - 1; | 60 | return (char *) haystack_start; |
| 61 | } | ||
| 62 | |||
| 63 | size_t needle_len = /* Length of NEEDLE. */ | ||
| 64 | needle - needle_start; | ||
| 65 | const char *haystack = haystack_start + 1; | ||
| 66 | size_t haystack_len = /* Known minimum length of HAYSTACK. */ | ||
| 67 | needle_len - 1; | ||
| 65 | 68 | ||
| 66 | /* Perform the search. Abstract memory is considered to be an array | 69 | /* Perform the search. Abstract memory is considered to be an array |
| 67 | of 'unsigned char' values, not an array of 'char' values. See | 70 | of 'unsigned char' values, not an array of 'char' values. See |
