]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/generic/strcasestr.c
Update.
[thirdparty/glibc.git] / sysdeps / generic / strcasestr.c
1 /* Return the offset of one string within another.
2 Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
19
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 #if HAVE_CONFIG_H
30 # include <config.h>
31 #endif
32
33 #include <ctype.h>
34
35 #if defined _LIBC || defined HAVE_STRING_H
36 # include <string.h>
37 #endif
38
39 typedef unsigned chartype;
40
41 #undef strstr
42
43 char *
44 __strcasestr (phaystack, pneedle)
45 const char *phaystack;
46 const char *pneedle;
47 {
48 register const unsigned char *haystack, *needle;
49 register chartype b, c;
50
51 haystack = (const unsigned char *) phaystack;
52 needle = (const unsigned char *) pneedle;
53
54 b = tolower (*needle);
55 if (b != '\0')
56 {
57 haystack--; /* possible ANSI violation */
58 do
59 {
60 c = *++haystack;
61 if (c == '\0')
62 goto ret0;
63 }
64 while (tolower (c) != b);
65
66 c = tolower (*++needle);
67 if (c == '\0')
68 goto foundneedle;
69 ++needle;
70 goto jin;
71
72 for (;;)
73 {
74 register chartype a;
75 register const unsigned char *rhaystack, *rneedle;
76
77 do
78 {
79 a = *++haystack;
80 if (a == '\0')
81 goto ret0;
82 if (tolower (a) == b)
83 break;
84 a = *++haystack;
85 if (a == '\0')
86 goto ret0;
87 shloop: }
88 while (tolower (a) != b);
89
90 jin: a = *++haystack;
91 if (a == '\0')
92 goto ret0;
93
94 if (tolower (a) != c)
95 goto shloop;
96
97 rhaystack = haystack-- + 1;
98 rneedle = needle;
99 a = tolower (*rneedle);
100
101 if (tolower (*rhaystack) == a)
102 do
103 {
104 if (a == '\0')
105 goto foundneedle;
106 ++rhaystack;
107 a = tolower (*++needle);
108 if (tolower (*rhaystack) != a)
109 break;
110 if (a == '\0')
111 goto foundneedle;
112 ++rhaystack;
113 a = tolower (*++needle);
114 }
115 while (tolower (*rhaystack) == a);
116
117 needle = rneedle; /* took the register-poor approach */
118
119 if (a == '\0')
120 break;
121 }
122 }
123 foundneedle:
124 return (char*) haystack;
125 ret0:
126 return 0;
127 }
128
129 weak_alias (__strcasestr, strcasestr)