]>
Commit | Line | Data |
---|---|---|
b168057a | 1 | /* Copyright (C) 1995-2015 Free Software Foundation, Inc. |
33a934a3 | 2 | This file is part of the GNU C Library. |
30de3b18 | 3 | |
33a934a3 | 4 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
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. | |
30de3b18 | 8 | |
33a934a3 UD |
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 | |
41bdb6e2 | 12 | Lesser General Public License for more details. |
30de3b18 | 13 | |
41bdb6e2 | 14 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
15 | License along with the GNU C Library; if not, see |
16 | <http://www.gnu.org/licenses/>. */ | |
30de3b18 RM |
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 * | |
9d46370c | 32 | wcsstr (const wchar_t *haystack, const wchar_t *needle) |
30de3b18 | 33 | { |
2e09a79a | 34 | wchar_t b, c; |
30de3b18 RM |
35 | |
36 | if ((b = *needle) != L'\0') | |
37 | { | |
38 | haystack--; /* possible ANSI violation */ | |
39 | do | |
40 | if ((c = *++haystack) == L'\0') | |
41 | goto ret0; | |
42 | while (c != b); | |
43 | ||
44 | if (!(c = *++needle)) | |
45 | goto foundneedle; | |
46 | ++needle; | |
47 | goto jin; | |
48 | ||
49 | for (;;) | |
50 | { | |
2e09a79a JM |
51 | wchar_t a; |
52 | const wchar_t *rhaystack, *rneedle; | |
30de3b18 RM |
53 | |
54 | do | |
55 | { | |
56 | if (!(a = *++haystack)) | |
57 | goto ret0; | |
58 | if (a == b) | |
59 | break; | |
60 | if ((a = *++haystack) == L'\0') | |
61 | goto ret0; | |
62 | shloop: ; | |
63 | } | |
64 | while (a != b); | |
65 | ||
66 | jin: if (!(a = *++haystack)) | |
67 | goto ret0; | |
68 | ||
69 | if (a != c) | |
70 | goto shloop; | |
71 | ||
72 | if (*(rhaystack = haystack-- + 1) == (a = *(rneedle = needle))) | |
73 | do | |
74 | { | |
75 | if (a == L'\0') | |
76 | goto foundneedle; | |
77 | if (*++rhaystack != (a = *++needle)) | |
78 | break; | |
79 | if (a == L'\0') | |
80 | goto foundneedle; | |
81 | } | |
82 | while (*++rhaystack == (a = *++needle)); | |
83 | ||
84 | needle = rneedle; /* took the register-poor approach */ | |
85 | ||
86 | if (a == L'\0') | |
87 | break; | |
88 | } | |
89 | } | |
90 | foundneedle: | |
91 | return (wchar_t*) haystack; | |
92 | ret0: | |
93 | return NULL; | |
94 | } | |
7799b7b3 UD |
95 | /* This alias is for backward compatibility with drafts of the ISO C |
96 | standard. Unfortunately the Unix(TM) standard requires this name. */ | |
97 | weak_alias (wcsstr, wcswcs) |