(readline): Append trailing newline to line.
[gnulib.git] / lib / strstr.c
1 /* Copyright (C) 1994 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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.
13
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.  */
17
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 */
26
27 #include <string.h>
28 #include <sys/types.h>
29
30 typedef unsigned chartype;
31
32 char *
33 strstr (const char *phaystack, const char *pneedle)
34 {
35   register const unsigned char *haystack, *needle;
36   register chartype b, c;
37
38   haystack = (const unsigned char *) phaystack;
39   needle = (const unsigned char *) pneedle;
40
41   b = *needle;
42   if (b != '\0')
43     {
44       haystack--;                               /* possible ANSI violation */
45       do
46         {
47           c = *++haystack;
48           if (c == '\0')
49             goto ret0;
50         }
51       while (c != b);
52
53       c = *++needle;
54       if (c == '\0')
55         goto foundneedle;
56       ++needle;
57       goto jin;
58
59       for (;;)
60         {
61           register chartype a;
62           register const unsigned char *rhaystack, *rneedle;
63
64           do
65             {
66               a = *++haystack;
67               if (a == '\0')
68                 goto ret0;
69               if (a == b)
70                 break;
71               a = *++haystack;
72               if (a == '\0')
73                 goto ret0;
74 shloop:     }
75           while (a != b);
76
77 jin:      a = *++haystack;
78           if (a == '\0')
79             goto ret0;
80
81           if (a != c)
82             goto shloop;
83
84           rhaystack = haystack-- + 1;
85           rneedle = needle;
86           a = *rneedle;
87
88           if (*rhaystack == a)
89             do
90               {
91                 if (a == '\0')
92                   goto foundneedle;
93                 ++rhaystack;
94                 a = *++needle;
95                 if (*rhaystack != a)
96                   break;
97                 if (a == '\0')
98                   goto foundneedle;
99                 ++rhaystack;
100                 a = *++needle;
101               }
102             while (*rhaystack == a);
103
104           needle = rneedle;                /* took the register-poor aproach */
105
106           if (a == '\0')
107             break;
108         }
109     }
110 foundneedle:
111   return (char*) haystack;
112 ret0:
113   return 0;
114 }