| 72 |
theseven |
1 |
/* Return the offset of one string within another.
|
|
|
2 |
Copyright (C) 1994,1996,1997,1998,1999,2000 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 Lesser General Public
|
|
|
7 |
License as published by the Free Software Foundation; either
|
|
|
8 |
version 2.1 of the 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 |
Lesser General Public License for more details.
|
|
|
14 |
|
|
|
15 |
You should have received a copy of the GNU Lesser General Public
|
|
|
16 |
License along with the GNU C Library; if not, write to the Free
|
|
|
17 |
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
|
18 |
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 |
/* Faster looping by precalculating bl, bu, cl, cu before looping.
|
|
|
30 |
* 2004 Apr 08 Jose Da Silva, digital@joescat@com */
|
|
|
31 |
|
|
|
32 |
#include "global.h"
|
| 111 |
theseven |
33 |
#include "string.h"
|
|
|
34 |
#include "libc/include/ctype.h"
|
| 72 |
theseven |
35 |
|
|
|
36 |
typedef unsigned chartype;
|
|
|
37 |
|
|
|
38 |
char* strcasestr (const char* phaystack, const char* pneedle)
|
|
|
39 |
{
|
|
|
40 |
const unsigned char *haystack, *needle;
|
|
|
41 |
chartype bl, bu, cl, cu;
|
|
|
42 |
|
|
|
43 |
haystack = (const unsigned char *) phaystack;
|
|
|
44 |
needle = (const unsigned char *) pneedle;
|
|
|
45 |
|
|
|
46 |
bl = tolower (*needle);
|
|
|
47 |
if (bl != '\0')
|
|
|
48 |
{
|
|
|
49 |
bu = toupper (bl);
|
|
|
50 |
haystack--;/* possible ANSI violation */
|
|
|
51 |
do
|
|
|
52 |
{
|
|
|
53 |
cl = *++haystack;
|
|
|
54 |
if (cl == '\0')
|
|
|
55 |
goto ret0;
|
|
|
56 |
}
|
|
|
57 |
while ((cl != bl) && (cl != bu));
|
|
|
58 |
|
|
|
59 |
cl = tolower (*++needle);
|
|
|
60 |
if (cl == '\0')
|
|
|
61 |
goto foundneedle;
|
|
|
62 |
cu = toupper (cl);
|
|
|
63 |
++needle;
|
|
|
64 |
goto jin;
|
|
|
65 |
|
|
|
66 |
for (;;)
|
|
|
67 |
{
|
|
|
68 |
chartype a;
|
|
|
69 |
const unsigned char *rhaystack, *rneedle;
|
|
|
70 |
|
|
|
71 |
do
|
|
|
72 |
{
|
|
|
73 |
a = *++haystack;
|
|
|
74 |
if (a == '\0')
|
|
|
75 |
goto ret0;
|
|
|
76 |
if ((a == bl) || (a == bu))
|
|
|
77 |
break;
|
|
|
78 |
a = *++haystack;
|
|
|
79 |
if (a == '\0')
|
|
|
80 |
goto ret0;
|
|
|
81 |
shloop:
|
|
|
82 |
;
|
|
|
83 |
}
|
|
|
84 |
while ((a != bl) && (a != bu));
|
|
|
85 |
|
|
|
86 |
jin: a = *++haystack;
|
|
|
87 |
if (a == '\0')
|
|
|
88 |
goto ret0;
|
|
|
89 |
|
|
|
90 |
if ((a != cl) && (a != cu))
|
|
|
91 |
goto shloop;
|
|
|
92 |
|
|
|
93 |
rhaystack = haystack-- + 1;
|
|
|
94 |
rneedle = needle;
|
|
|
95 |
a = tolower (*rneedle);
|
|
|
96 |
|
|
|
97 |
if (tolower (*rhaystack) == (int) a)
|
|
|
98 |
do
|
|
|
99 |
{
|
|
|
100 |
if (a == '\0')
|
|
|
101 |
goto foundneedle;
|
|
|
102 |
++rhaystack;
|
|
|
103 |
a = tolower (*++needle);
|
|
|
104 |
if (tolower (*rhaystack) != (int) a)
|
|
|
105 |
break;
|
|
|
106 |
if (a == '\0')
|
|
|
107 |
goto foundneedle;
|
|
|
108 |
++rhaystack;
|
|
|
109 |
a = tolower (*++needle);
|
|
|
110 |
}
|
|
|
111 |
while (tolower (*rhaystack) == (int) a);
|
|
|
112 |
|
|
|
113 |
needle = rneedle;/* took the register-poor approach */
|
|
|
114 |
|
|
|
115 |
if (a == '\0')
|
|
|
116 |
break;
|
|
|
117 |
}
|
|
|
118 |
}
|
|
|
119 |
foundneedle:
|
|
|
120 |
return (char*) haystack;
|
|
|
121 |
ret0:
|
|
|
122 |
return 0;
|
|
|
123 |
}
|