strstr.c (d1d015864103b253b3fcb2f72a0da5b0cfeb31b6) | strstr.c (88521634e964e3da78a0ce54109cddb59bf3f099) |
---|---|
1/*- | 1/*- |
2 * Copyright (c) 1990, 1993 3 * The Regents of the University of California. All rights reserved. | 2 * Copyright (c) 2005-2014 Rich Felker, et al. |
4 * | 3 * |
5 * This code is derived from software contributed to Berkeley by 6 * Chris Torek. | 4 * Permission is hereby granted, free of charge, to any person obtaining 5 * a copy of this software and associated documentation files (the 6 * "Software"), to deal in the Software without restriction, including 7 * without limitation the rights to use, copy, modify, merge, publish, 8 * distribute, sublicense, and/or sell copies of the Software, and to 9 * permit persons to whom the Software is furnished to do so, subject to 10 * the following conditions: |
7 * | 11 * |
8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. | 12 * The above copyright notice and this permission notice shall be 13 * included in all copies or substantial portions of the Software. |
19 * | 14 * |
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. | 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
31 */ | 22 */ |
32 33#if defined(LIBC_SCCS) && !defined(lint) 34static char sccsid[] = "@(#)strstr.c 8.1 (Berkeley) 6/4/93"; 35#endif /* LIBC_SCCS and not lint */ | |
36#include <sys/cdefs.h> 37__FBSDID("$FreeBSD$"); 38 39#include <string.h> | 23#include <sys/cdefs.h> 24__FBSDID("$FreeBSD$"); 25 26#include <string.h> |
27#include <stdint.h> |
|
40 | 28 |
41/* 42 * Find the first occurrence of find in s. 43 */ 44char * 45strstr(const char *s, const char *find) | 29static char *twobyte_strstr(const unsigned char *h, const unsigned char *n) |
46{ | 30{ |
47 char c, sc; 48 size_t len; | 31 uint16_t nw = n[0]<<8 | n[1], hw = h[0]<<8 | h[1]; 32 for (h++; *h && hw != nw; hw = hw<<8 | *++h); 33 return *h ? (char *)h-1 : 0; 34} |
49 | 35 |
50 if ((c = *find++) != '\0') { 51 len = strlen(find); 52 do { 53 do { 54 if ((sc = *s++) == '\0') 55 return (NULL); 56 } while (sc != c); 57 } while (strncmp(s, find, len) != 0); 58 s--; | 36static char *threebyte_strstr(const unsigned char *h, const unsigned char *n) 37{ 38 uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8; 39 uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8; 40 for (h+=2; *h && hw != nw; hw = (hw|*++h)<<8); 41 return *h ? (char *)h-2 : 0; 42} 43 44static char *fourbyte_strstr(const unsigned char *h, const unsigned char *n) 45{ 46 uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8 | n[3]; 47 uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8 | h[3]; 48 for (h+=3; *h && hw != nw; hw = hw<<8 | *++h); 49 return *h ? (char *)h-3 : 0; 50} 51 52#define MAX(a,b) ((a)>(b)?(a):(b)) 53#define MIN(a,b) ((a)<(b)?(a):(b)) 54 55#define BITOP(a,b,op) \ 56 ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a)))) 57 58static char *twoway_strstr(const unsigned char *h, const unsigned char *n) 59{ 60 const unsigned char *z; 61 size_t l, ip, jp, k, p, ms, p0, mem, mem0; 62 size_t byteset[32 / sizeof(size_t)] = { 0 }; 63 size_t shift[256]; 64 65 /* Computing length of needle and fill shift table */ 66 for (l=0; n[l] && h[l]; l++) 67 BITOP(byteset, n[l], |=), shift[n[l]] = l+1; 68 if (n[l]) return 0; /* hit the end of h */ 69 70 /* Compute maximal suffix */ 71 ip = -1; jp = 0; k = p = 1; 72 while (jp+k<l) { 73 if (n[ip+k] == n[jp+k]) { 74 if (k == p) { 75 jp += p; 76 k = 1; 77 } else k++; 78 } else if (n[ip+k] > n[jp+k]) { 79 jp += k; 80 k = 1; 81 p = jp - ip; 82 } else { 83 ip = jp++; 84 k = p = 1; 85 } |
59 } | 86 } |
60 return ((char *)s); | 87 ms = ip; 88 p0 = p; 89 90 /* And with the opposite comparison */ 91 ip = -1; jp = 0; k = p = 1; 92 while (jp+k<l) { 93 if (n[ip+k] == n[jp+k]) { 94 if (k == p) { 95 jp += p; 96 k = 1; 97 } else k++; 98 } else if (n[ip+k] < n[jp+k]) { 99 jp += k; 100 k = 1; 101 p = jp - ip; 102 } else { 103 ip = jp++; 104 k = p = 1; 105 } 106 } 107 if (ip+1 > ms+1) ms = ip; 108 else p = p0; 109 110 /* Periodic needle? */ 111 if (memcmp(n, n+p, ms+1)) { 112 mem0 = 0; 113 p = MAX(ms, l-ms-1) + 1; 114 } else mem0 = l-p; 115 mem = 0; 116 117 /* Initialize incremental end-of-haystack pointer */ 118 z = h; 119 120 /* Search loop */ 121 for (;;) { 122 /* Update incremental end-of-haystack pointer */ 123 if (z-h < l) { 124 /* Fast estimate for MIN(l,63) */ 125 size_t grow = l | 63; 126 const unsigned char *z2 = memchr(z, 0, grow); 127 if (z2) { 128 z = z2; 129 if (z-h < l) return 0; 130 } else z += grow; 131 } 132 133 /* Check last byte first; advance by shift on mismatch */ 134 if (BITOP(byteset, h[l-1], &)) { 135 k = l-shift[h[l-1]]; 136 //printf("adv by %zu (on %c) at [%s] (%zu;l=%zu)\n", k, h[l-1], h, shift[h[l-1]], l); 137 if (k) { 138 if (mem0 && mem && k < p) k = l-p; 139 h += k; 140 mem = 0; 141 continue; 142 } 143 } else { 144 h += l; 145 mem = 0; 146 continue; 147 } 148 149 /* Compare right half */ 150 for (k=MAX(ms+1,mem); n[k] && n[k] == h[k]; k++); 151 if (n[k]) { 152 h += k-ms; 153 mem = 0; 154 continue; 155 } 156 /* Compare left half */ 157 for (k=ms+1; k>mem && n[k-1] == h[k-1]; k--); 158 if (k <= mem) return (char *)h; 159 h += p; 160 mem = mem0; 161 } |
61} | 162} |
163 164char *strstr(const char *h, const char *n) 165{ 166 /* Return immediately on empty needle */ 167 if (!n[0]) return (char *)h; 168 169 /* Use faster algorithms for short needles */ 170 h = strchr(h, *n); 171 if (!h || !n[1]) return (char *)h; 172 if (!h[1]) return 0; 173 if (!n[2]) return twobyte_strstr((void *)h, (void *)n); 174 if (!h[2]) return 0; 175 if (!n[3]) return threebyte_strstr((void *)h, (void *)n); 176 if (!h[3]) return 0; 177 if (!n[4]) return fourbyte_strstr((void *)h, (void *)n); 178 179 return twoway_strstr((void *)h, (void *)n); 180} |
|