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