lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Copyright (C) 1995-2015 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | |
| 4 | The GNU C Library is free software; you can redistribute it and/or |
| 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. |
| 8 | |
| 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 |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with the GNU C Library; if not, see |
| 16 | <http://www.gnu.org/licenses/>. */ |
| 17 | |
| 18 | /* |
| 19 | * The original strstr() file contains the following comment: |
| 20 | * |
| 21 | * My personal strstr() implementation that beats most other algorithms. |
| 22 | * Until someone tells me otherwise, I assume that this is the |
| 23 | * fastest implementation of strstr() in C. |
| 24 | * I deliberately chose not to comment it. You should have at least |
| 25 | * as much fun trying to understand it, as I had to write it :-). |
| 26 | * |
| 27 | * Stephen R. van den Berg, berg@pool.informatik.rwth-aachen.de */ |
| 28 | |
| 29 | #include <wchar.h> |
| 30 | |
| 31 | wchar_t * |
| 32 | wcsstr (haystack, needle) |
| 33 | const wchar_t *haystack; |
| 34 | const wchar_t *needle; |
| 35 | { |
| 36 | wchar_t b, c; |
| 37 | |
| 38 | if ((b = *needle) != L'\0') |
| 39 | { |
| 40 | haystack--; /* possible ANSI violation */ |
| 41 | do |
| 42 | if ((c = *++haystack) == L'\0') |
| 43 | goto ret0; |
| 44 | while (c != b); |
| 45 | |
| 46 | if (!(c = *++needle)) |
| 47 | goto foundneedle; |
| 48 | ++needle; |
| 49 | goto jin; |
| 50 | |
| 51 | for (;;) |
| 52 | { |
| 53 | wchar_t a; |
| 54 | const wchar_t *rhaystack, *rneedle; |
| 55 | |
| 56 | do |
| 57 | { |
| 58 | if (!(a = *++haystack)) |
| 59 | goto ret0; |
| 60 | if (a == b) |
| 61 | break; |
| 62 | if ((a = *++haystack) == L'\0') |
| 63 | goto ret0; |
| 64 | shloop: ; |
| 65 | } |
| 66 | while (a != b); |
| 67 | |
| 68 | jin: if (!(a = *++haystack)) |
| 69 | goto ret0; |
| 70 | |
| 71 | if (a != c) |
| 72 | goto shloop; |
| 73 | |
| 74 | if (*(rhaystack = haystack-- + 1) == (a = *(rneedle = needle))) |
| 75 | do |
| 76 | { |
| 77 | if (a == L'\0') |
| 78 | goto foundneedle; |
| 79 | if (*++rhaystack != (a = *++needle)) |
| 80 | break; |
| 81 | if (a == L'\0') |
| 82 | goto foundneedle; |
| 83 | } |
| 84 | while (*++rhaystack == (a = *++needle)); |
| 85 | |
| 86 | needle = rneedle; /* took the register-poor approach */ |
| 87 | |
| 88 | if (a == L'\0') |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | foundneedle: |
| 93 | return (wchar_t*) haystack; |
| 94 | ret0: |
| 95 | return NULL; |
| 96 | } |
| 97 | /* This alias is for backward compatibility with drafts of the ISO C |
| 98 | standard. Unfortunately the Unix(TM) standard requires this name. */ |
| 99 | weak_alias (wcsstr, wcswcs) |