maint: update copyright
[gnulib.git] / lib / mbsstr.c
1 /* Searching in a string.
2    Copyright (C) 2005-2014 Free Software Foundation, Inc.
3    Written by Bruno Haible <bruno@clisp.org>, 2005.
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9
10    This program 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
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 #include <config.h>
19
20 /* Specification.  */
21 #include <string.h>
22
23 #include <stdbool.h>
24 #include <stddef.h>  /* for NULL, in case a nonstandard string.h lacks it */
25
26 #include "malloca.h"
27 #include "mbuiter.h"
28
29 /* Knuth-Morris-Pratt algorithm.  */
30 #define UNIT unsigned char
31 #define CANON_ELEMENT(c) c
32 #include "str-kmp.h"
33
34 /* Knuth-Morris-Pratt algorithm.
35    See http://en.wikipedia.org/wiki/Knuth-Morris-Pratt_algorithm
36    Return a boolean indicating success:
37    Return true and set *RESULTP if the search was completed.
38    Return false if it was aborted because not enough memory was available.  */
39 static bool
40 knuth_morris_pratt_multibyte (const char *haystack, const char *needle,
41                               const char **resultp)
42 {
43   size_t m = mbslen (needle);
44   mbchar_t *needle_mbchars;
45   size_t *table;
46
47   /* Allocate room for needle_mbchars and the table.  */
48   void *memory = nmalloca (m, sizeof (mbchar_t) + sizeof (size_t));
49   void *table_memory;
50   if (memory == NULL)
51     return false;
52   needle_mbchars = memory;
53   table = table_memory = needle_mbchars + m;
54
55   /* Fill needle_mbchars.  */
56   {
57     mbui_iterator_t iter;
58     size_t j;
59
60     j = 0;
61     for (mbui_init (iter, needle); mbui_avail (iter); mbui_advance (iter), j++)
62       mb_copy (&needle_mbchars[j], &mbui_cur (iter));
63   }
64
65   /* Fill the table.
66      For 0 < i < m:
67        0 < table[i] <= i is defined such that
68        forall 0 < x < table[i]: needle[x..i-1] != needle[0..i-1-x],
69        and table[i] is as large as possible with this property.
70      This implies:
71      1) For 0 < i < m:
72           If table[i] < i,
73           needle[table[i]..i-1] = needle[0..i-1-table[i]].
74      2) For 0 < i < m:
75           rhaystack[0..i-1] == needle[0..i-1]
76           and exists h, i <= h < m: rhaystack[h] != needle[h]
77           implies
78           forall 0 <= x < table[i]: rhaystack[x..x+m-1] != needle[0..m-1].
79      table[0] remains uninitialized.  */
80   {
81     size_t i, j;
82
83     /* i = 1: Nothing to verify for x = 0.  */
84     table[1] = 1;
85     j = 0;
86
87     for (i = 2; i < m; i++)
88       {
89         /* Here: j = i-1 - table[i-1].
90            The inequality needle[x..i-1] != needle[0..i-1-x] is known to hold
91            for x < table[i-1], by induction.
92            Furthermore, if j>0: needle[i-1-j..i-2] = needle[0..j-1].  */
93         mbchar_t *b = &needle_mbchars[i - 1];
94
95         for (;;)
96           {
97             /* Invariants: The inequality needle[x..i-1] != needle[0..i-1-x]
98                is known to hold for x < i-1-j.
99                Furthermore, if j>0: needle[i-1-j..i-2] = needle[0..j-1].  */
100             if (mb_equal (*b, needle_mbchars[j]))
101               {
102                 /* Set table[i] := i-1-j.  */
103                 table[i] = i - ++j;
104                 break;
105               }
106             /* The inequality needle[x..i-1] != needle[0..i-1-x] also holds
107                for x = i-1-j, because
108                  needle[i-1] != needle[j] = needle[i-1-x].  */
109             if (j == 0)
110               {
111                 /* The inequality holds for all possible x.  */
112                 table[i] = i;
113                 break;
114               }
115             /* The inequality needle[x..i-1] != needle[0..i-1-x] also holds
116                for i-1-j < x < i-1-j+table[j], because for these x:
117                  needle[x..i-2]
118                  = needle[x-(i-1-j)..j-1]
119                  != needle[0..j-1-(x-(i-1-j))]  (by definition of table[j])
120                     = needle[0..i-2-x],
121                hence needle[x..i-1] != needle[0..i-1-x].
122                Furthermore
123                  needle[i-1-j+table[j]..i-2]
124                  = needle[table[j]..j-1]
125                  = needle[0..j-1-table[j]]  (by definition of table[j]).  */
126             j = j - table[j];
127           }
128         /* Here: j = i - table[i].  */
129       }
130   }
131
132   /* Search, using the table to accelerate the processing.  */
133   {
134     size_t j;
135     mbui_iterator_t rhaystack;
136     mbui_iterator_t phaystack;
137
138     *resultp = NULL;
139     j = 0;
140     mbui_init (rhaystack, haystack);
141     mbui_init (phaystack, haystack);
142     /* Invariant: phaystack = rhaystack + j.  */
143     while (mbui_avail (phaystack))
144       if (mb_equal (needle_mbchars[j], mbui_cur (phaystack)))
145         {
146           j++;
147           mbui_advance (phaystack);
148           if (j == m)
149             {
150               /* The entire needle has been found.  */
151               *resultp = mbui_cur_ptr (rhaystack);
152               break;
153             }
154         }
155       else if (j > 0)
156         {
157           /* Found a match of needle[0..j-1], mismatch at needle[j].  */
158           size_t count = table[j];
159           j -= count;
160           for (; count > 0; count--)
161             {
162               if (!mbui_avail (rhaystack))
163                 abort ();
164               mbui_advance (rhaystack);
165             }
166         }
167       else
168         {
169           /* Found a mismatch at needle[0] already.  */
170           if (!mbui_avail (rhaystack))
171             abort ();
172           mbui_advance (rhaystack);
173           mbui_advance (phaystack);
174         }
175   }
176
177   freea (memory);
178   return true;
179 }
180
181 /* Find the first occurrence of the character string NEEDLE in the character
182    string HAYSTACK.  Return NULL if NEEDLE is not found in HAYSTACK.  */
183 char *
184 mbsstr (const char *haystack, const char *needle)
185 {
186   /* Be careful not to look at the entire extent of haystack or needle
187      until needed.  This is useful because of these two cases:
188        - haystack may be very long, and a match of needle found early,
189        - needle may be very long, and not even a short initial segment of
190          needle may be found in haystack.  */
191   if (MB_CUR_MAX > 1)
192     {
193       mbui_iterator_t iter_needle;
194
195       mbui_init (iter_needle, needle);
196       if (mbui_avail (iter_needle))
197         {
198           /* Minimizing the worst-case complexity:
199              Let n = mbslen(haystack), m = mbslen(needle).
200              The naïve algorithm is O(n*m) worst-case.
201              The Knuth-Morris-Pratt algorithm is O(n) worst-case but it needs a
202              memory allocation.
203              To achieve linear complexity and yet amortize the cost of the
204              memory allocation, we activate the Knuth-Morris-Pratt algorithm
205              only once the naïve algorithm has already run for some time; more
206              precisely, when
207                - the outer loop count is >= 10,
208                - the average number of comparisons per outer loop is >= 5,
209                - the total number of comparisons is >= m.
210              But we try it only once.  If the memory allocation attempt failed,
211              we don't retry it.  */
212           bool try_kmp = true;
213           size_t outer_loop_count = 0;
214           size_t comparison_count = 0;
215           size_t last_ccount = 0;                  /* last comparison count */
216           mbui_iterator_t iter_needle_last_ccount; /* = needle + last_ccount */
217
218           mbui_iterator_t iter_haystack;
219
220           mbui_init (iter_needle_last_ccount, needle);
221           mbui_init (iter_haystack, haystack);
222           for (;; mbui_advance (iter_haystack))
223             {
224               if (!mbui_avail (iter_haystack))
225                 /* No match.  */
226                 return NULL;
227
228               /* See whether it's advisable to use an asymptotically faster
229                  algorithm.  */
230               if (try_kmp
231                   && outer_loop_count >= 10
232                   && comparison_count >= 5 * outer_loop_count)
233                 {
234                   /* See if needle + comparison_count now reaches the end of
235                      needle.  */
236                   size_t count = comparison_count - last_ccount;
237                   for (;
238                        count > 0 && mbui_avail (iter_needle_last_ccount);
239                        count--)
240                     mbui_advance (iter_needle_last_ccount);
241                   last_ccount = comparison_count;
242                   if (!mbui_avail (iter_needle_last_ccount))
243                     {
244                       /* Try the Knuth-Morris-Pratt algorithm.  */
245                       const char *result;
246                       bool success =
247                         knuth_morris_pratt_multibyte (haystack, needle,
248                                                       &result);
249                       if (success)
250                         return (char *) result;
251                       try_kmp = false;
252                     }
253                 }
254
255               outer_loop_count++;
256               comparison_count++;
257               if (mb_equal (mbui_cur (iter_haystack), mbui_cur (iter_needle)))
258                 /* The first character matches.  */
259                 {
260                   mbui_iterator_t rhaystack;
261                   mbui_iterator_t rneedle;
262
263                   memcpy (&rhaystack, &iter_haystack, sizeof (mbui_iterator_t));
264                   mbui_advance (rhaystack);
265
266                   mbui_init (rneedle, needle);
267                   if (!mbui_avail (rneedle))
268                     abort ();
269                   mbui_advance (rneedle);
270
271                   for (;; mbui_advance (rhaystack), mbui_advance (rneedle))
272                     {
273                       if (!mbui_avail (rneedle))
274                         /* Found a match.  */
275                         return (char *) mbui_cur_ptr (iter_haystack);
276                       if (!mbui_avail (rhaystack))
277                         /* No match.  */
278                         return NULL;
279                       comparison_count++;
280                       if (!mb_equal (mbui_cur (rhaystack), mbui_cur (rneedle)))
281                         /* Nothing in this round.  */
282                         break;
283                     }
284                 }
285             }
286         }
287       else
288         return (char *) haystack;
289     }
290   else
291     {
292       if (*needle != '\0')
293         {
294           /* Minimizing the worst-case complexity:
295              Let n = strlen(haystack), m = strlen(needle).
296              The naïve algorithm is O(n*m) worst-case.
297              The Knuth-Morris-Pratt algorithm is O(n) worst-case but it needs a
298              memory allocation.
299              To achieve linear complexity and yet amortize the cost of the
300              memory allocation, we activate the Knuth-Morris-Pratt algorithm
301              only once the naïve algorithm has already run for some time; more
302              precisely, when
303                - the outer loop count is >= 10,
304                - the average number of comparisons per outer loop is >= 5,
305                - the total number of comparisons is >= m.
306              But we try it only once.  If the memory allocation attempt failed,
307              we don't retry it.  */
308           bool try_kmp = true;
309           size_t outer_loop_count = 0;
310           size_t comparison_count = 0;
311           size_t last_ccount = 0;                  /* last comparison count */
312           const char *needle_last_ccount = needle; /* = needle + last_ccount */
313
314           /* Speed up the following searches of needle by caching its first
315              character.  */
316           char b = *needle++;
317
318           for (;; haystack++)
319             {
320               if (*haystack == '\0')
321                 /* No match.  */
322                 return NULL;
323
324               /* See whether it's advisable to use an asymptotically faster
325                  algorithm.  */
326               if (try_kmp
327                   && outer_loop_count >= 10
328                   && comparison_count >= 5 * outer_loop_count)
329                 {
330                   /* See if needle + comparison_count now reaches the end of
331                      needle.  */
332                   if (needle_last_ccount != NULL)
333                     {
334                       needle_last_ccount +=
335                         strnlen (needle_last_ccount,
336                                  comparison_count - last_ccount);
337                       if (*needle_last_ccount == '\0')
338                         needle_last_ccount = NULL;
339                       last_ccount = comparison_count;
340                     }
341                   if (needle_last_ccount == NULL)
342                     {
343                       /* Try the Knuth-Morris-Pratt algorithm.  */
344                       const unsigned char *result;
345                       bool success =
346                         knuth_morris_pratt ((const unsigned char *) haystack,
347                                             (const unsigned char *) (needle - 1),
348                                             strlen (needle - 1),
349                                             &result);
350                       if (success)
351                         return (char *) result;
352                       try_kmp = false;
353                     }
354                 }
355
356               outer_loop_count++;
357               comparison_count++;
358               if (*haystack == b)
359                 /* The first character matches.  */
360                 {
361                   const char *rhaystack = haystack + 1;
362                   const char *rneedle = needle;
363
364                   for (;; rhaystack++, rneedle++)
365                     {
366                       if (*rneedle == '\0')
367                         /* Found a match.  */
368                         return (char *) haystack;
369                       if (*rhaystack == '\0')
370                         /* No match.  */
371                         return NULL;
372                       comparison_count++;
373                       if (*rhaystack != *rneedle)
374                         /* Nothing in this round.  */
375                         break;
376                     }
377                 }
378             }
379         }
380       else
381         return (char *) haystack;
382     }
383 }