1/* -*- buffer-read-only: t -*- vi: set ro: */ 2/* DO NOT EDIT! GENERATED AUTOMATICALLY! */ 3#line 1 4/* Substring search in a NUL terminated string of 'char' elements, 5 using the Knuth-Morris-Pratt algorithm. 6 Copyright (C) 2005-2010 Free Software Foundation, Inc. 7 Written by Bruno Haible <bruno@clisp.org>, 2005. 8 9 This program is free software; you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation; either version 3, or (at your option) 12 any later version. 13 14 This program is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with this program; if not, write to the Free Software Foundation, 21 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 22 23/* Before including this file, you need to define: 24 CANON_ELEMENT(c) A macro that canonicalizes an element right after 25 it has been fetched from one of the two strings. 26 The argument is an 'unsigned char'; the result 27 must be an 'unsigned char' as well. */ 28 29/* Knuth-Morris-Pratt algorithm. 30 See http://en.wikipedia.org/wiki/Knuth-Morris-Pratt_algorithm 31 Return a boolean indicating success: 32 Return true and set *RESULTP if the search was completed. 33 Return false if it was aborted because not enough memory was available. */ 34static bool 35knuth_morris_pratt_unibyte (const char *haystack, const char *needle, 36 const char **resultp) 37{ 38 size_t m = strlen (needle); 39 40 /* Allocate the table. */ 41 size_t *table = (size_t *) nmalloca (m, sizeof (size_t)); 42 if (table == NULL) 43 return false; 44 /* Fill the table. 45 For 0 < i < m: 46 0 < table[i] <= i is defined such that 47 forall 0 < x < table[i]: needle[x..i-1] != needle[0..i-1-x], 48 and table[i] is as large as possible with this property. 49 This implies: 50 1) For 0 < i < m: 51 If table[i] < i, 52 needle[table[i]..i-1] = needle[0..i-1-table[i]]. 53 2) For 0 < i < m: 54 rhaystack[0..i-1] == needle[0..i-1] 55 and exists h, i <= h < m: rhaystack[h] != needle[h] 56 implies 57 forall 0 <= x < table[i]: rhaystack[x..x+m-1] != needle[0..m-1]. 58 table[0] remains uninitialized. */ 59 { 60 size_t i, j; 61 62 /* i = 1: Nothing to verify for x = 0. */ 63 table[1] = 1; 64 j = 0; 65 66 for (i = 2; i < m; i++) 67 { 68 /* Here: j = i-1 - table[i-1]. 69 The inequality needle[x..i-1] != needle[0..i-1-x] is known to hold 70 for x < table[i-1], by induction. 71 Furthermore, if j>0: needle[i-1-j..i-2] = needle[0..j-1]. */ 72 unsigned char b = CANON_ELEMENT ((unsigned char) needle[i - 1]); 73 74 for (;;) 75 { 76 /* Invariants: The inequality needle[x..i-1] != needle[0..i-1-x] 77 is known to hold for x < i-1-j. 78 Furthermore, if j>0: needle[i-1-j..i-2] = needle[0..j-1]. */ 79 if (b == CANON_ELEMENT ((unsigned char) needle[j])) 80 { 81 /* Set table[i] := i-1-j. */ 82 table[i] = i - ++j; 83 break; 84 } 85 /* The inequality needle[x..i-1] != needle[0..i-1-x] also holds 86 for x = i-1-j, because 87 needle[i-1] != needle[j] = needle[i-1-x]. */ 88 if (j == 0) 89 { 90 /* The inequality holds for all possible x. */ 91 table[i] = i; 92 break; 93 } 94 /* The inequality needle[x..i-1] != needle[0..i-1-x] also holds 95 for i-1-j < x < i-1-j+table[j], because for these x: 96 needle[x..i-2] 97 = needle[x-(i-1-j)..j-1] 98 != needle[0..j-1-(x-(i-1-j))] (by definition of table[j]) 99 = needle[0..i-2-x], 100 hence needle[x..i-1] != needle[0..i-1-x]. 101 Furthermore 102 needle[i-1-j+table[j]..i-2] 103 = needle[table[j]..j-1] 104 = needle[0..j-1-table[j]] (by definition of table[j]). */ 105 j = j - table[j]; 106 } 107 /* Here: j = i - table[i]. */ 108 } 109 } 110 111 /* Search, using the table to accelerate the processing. */ 112 { 113 size_t j; 114 const char *rhaystack; 115 const char *phaystack; 116 117 *resultp = NULL; 118 j = 0; 119 rhaystack = haystack; 120 phaystack = haystack; 121 /* Invariant: phaystack = rhaystack + j. */ 122 while (*phaystack != '\0') 123 if (CANON_ELEMENT ((unsigned char) needle[j]) 124 == CANON_ELEMENT ((unsigned char) *phaystack)) 125 { 126 j++; 127 phaystack++; 128 if (j == m) 129 { 130 /* The entire needle has been found. */ 131 *resultp = rhaystack; 132 break; 133 } 134 } 135 else if (j > 0) 136 { 137 /* Found a match of needle[0..j-1], mismatch at needle[j]. */ 138 rhaystack += table[j]; 139 j -= table[j]; 140 } 141 else 142 { 143 /* Found a mismatch at needle[0] already. */ 144 rhaystack++; 145 phaystack++; 146 } 147 } 148 149 freea (table); 150 return true; 151} 152 153#undef CANON_ELEMENT 154