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