Mercurial > hg > octave-kai > gnulib-hg
annotate lib/strstr.c @ 4682:90abbdf08583
Assume ANSI C.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Wed, 10 Sep 2003 14:07:55 +0000 (2003-09-10) |
parents | e34e6bd35c66 |
children | a48fb0e98c8c |
rev | line source |
---|---|
4682 | 1 /* Copyright (C) 1994, 1999, 2002-2003 Free Software Foundation, Inc. |
656 | 2 This file is part of the GNU C Library. |
5 | 3 |
656 | 4 This program is free software; you can redistribute it and/or modify |
5 it under the terms of the GNU General Public License as published by | |
6 the Free Software Foundation; either version 2, or (at your option) | |
7 any later version. | |
8 | |
9 This program 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 | |
12 GNU General Public License for more details. | |
5 | 13 |
656 | 14 You should have received a copy of the GNU General Public License |
15 along with this program; if not, write to the Free Software | |
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
5 | 17 |
656 | 18 /* |
19 * My personal strstr() implementation that beats most other algorithms. | |
20 * Until someone tells me otherwise, I assume that this is the | |
21 * fastest implementation of strstr() in C. | |
22 * I deliberately chose not to comment it. You should have at least | |
23 * as much fun trying to understand it, as I had to write it :-). | |
24 * | |
25 * Stephen R. van den Berg, berg@pool.informatik.rwth-aachen.de */ | |
5 | 26 |
2020
989f1a5c1c0c
(strstr): Include config.h.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
27 #if HAVE_CONFIG_H |
989f1a5c1c0c
(strstr): Include config.h.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
28 # include <config.h> |
989f1a5c1c0c
(strstr): Include config.h.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
29 #endif |
989f1a5c1c0c
(strstr): Include config.h.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
30 |
4682 | 31 #include <string.h> |
5 | 32 |
656 | 33 typedef unsigned chartype; |
5 | 34 |
2988 | 35 #undef strstr |
36 | |
5 | 37 char * |
1557 | 38 strstr (const char *phaystack, const char *pneedle) |
5 | 39 { |
656 | 40 register const unsigned char *haystack, *needle; |
41 register chartype b, c; | |
42 | |
43 haystack = (const unsigned char *) phaystack; | |
44 needle = (const unsigned char *) pneedle; | |
45 | |
46 b = *needle; | |
47 if (b != '\0') | |
48 { | |
49 haystack--; /* possible ANSI violation */ | |
50 do | |
51 { | |
52 c = *++haystack; | |
53 if (c == '\0') | |
54 goto ret0; | |
55 } | |
56 while (c != b); | |
57 | |
58 c = *++needle; | |
59 if (c == '\0') | |
60 goto foundneedle; | |
61 ++needle; | |
62 goto jin; | |
63 | |
64 for (;;) | |
65 { | |
66 register chartype a; | |
67 register const unsigned char *rhaystack, *rneedle; | |
5 | 68 |
656 | 69 do |
70 { | |
71 a = *++haystack; | |
72 if (a == '\0') | |
73 goto ret0; | |
74 if (a == b) | |
75 break; | |
76 a = *++haystack; | |
77 if (a == '\0') | |
78 goto ret0; | |
2020
989f1a5c1c0c
(strstr): Include config.h.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
79 shloop:; } |
656 | 80 while (a != b); |
81 | |
82 jin: a = *++haystack; | |
83 if (a == '\0') | |
84 goto ret0; | |
85 | |
86 if (a != c) | |
87 goto shloop; | |
88 | |
89 rhaystack = haystack-- + 1; | |
90 rneedle = needle; | |
91 a = *rneedle; | |
92 | |
93 if (*rhaystack == a) | |
94 do | |
95 { | |
96 if (a == '\0') | |
97 goto foundneedle; | |
98 ++rhaystack; | |
99 a = *++needle; | |
100 if (*rhaystack != a) | |
101 break; | |
102 if (a == '\0') | |
103 goto foundneedle; | |
104 ++rhaystack; | |
105 a = *++needle; | |
106 } | |
107 while (*rhaystack == a); | |
108 | |
2988 | 109 needle = rneedle; /* took the register-poor approach */ |
656 | 110 |
111 if (a == '\0') | |
112 break; | |
113 } | |
5 | 114 } |
656 | 115 foundneedle: |
116 return (char*) haystack; | |
117 ret0: | |
5 | 118 return 0; |
119 } |