lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Return the offset of one string within another. |
| 2 | Copyright (C) 1994,1996,1997,2000,2001,2003 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 | #include <string.h> |
| 30 | |
| 31 | |
| 32 | typedef unsigned chartype; |
| 33 | |
| 34 | char *strstr (const char *phaystack, const char *pneedle) |
| 35 | { |
| 36 | const unsigned char *haystack, *needle; |
| 37 | chartype b; |
| 38 | const unsigned char *rneedle; |
| 39 | |
| 40 | haystack = (const unsigned char *) phaystack; |
| 41 | |
| 42 | if ((b = *(needle = (const unsigned char *) pneedle))) |
| 43 | { |
| 44 | chartype c; |
| 45 | haystack--; /* possible ANSI violation */ |
| 46 | |
| 47 | { |
| 48 | chartype a; |
| 49 | do |
| 50 | if (!(a = *++haystack)) |
| 51 | goto ret0; |
| 52 | while (a != b); |
| 53 | } |
| 54 | |
| 55 | if (!(c = *++needle)) |
| 56 | goto foundneedle; |
| 57 | ++needle; |
| 58 | goto jin; |
| 59 | |
| 60 | for (;;) |
| 61 | { |
| 62 | { |
| 63 | chartype a; |
| 64 | if (0) |
| 65 | jin:{ |
| 66 | if ((a = *++haystack) == c) |
| 67 | goto crest; |
| 68 | } |
| 69 | else |
| 70 | a = *++haystack; |
| 71 | do |
| 72 | { |
| 73 | for (; a != b; a = *++haystack) |
| 74 | { |
| 75 | if (!a) |
| 76 | goto ret0; |
| 77 | if ((a = *++haystack) == b) |
| 78 | break; |
| 79 | if (!a) |
| 80 | goto ret0; |
| 81 | } |
| 82 | } |
| 83 | while ((a = *++haystack) != c); |
| 84 | } |
| 85 | crest: |
| 86 | { |
| 87 | chartype a; |
| 88 | { |
| 89 | const unsigned char *rhaystack; |
| 90 | if (*(rhaystack = haystack-- + 1) == (a = *(rneedle = needle))) |
| 91 | do |
| 92 | { |
| 93 | if (!a) |
| 94 | goto foundneedle; |
| 95 | if (*++rhaystack != (a = *++needle)) |
| 96 | break; |
| 97 | if (!a) |
| 98 | goto foundneedle; |
| 99 | } |
| 100 | while (*++rhaystack == (a = *++needle)); |
| 101 | needle = rneedle; /* took the register-poor aproach */ |
| 102 | } |
| 103 | if (!a) |
| 104 | break; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | foundneedle: |
| 109 | return (char *) haystack; |
| 110 | ret0: |
| 111 | return 0; |
| 112 | } |
| 113 | libc_hidden_def(strstr) |