Blame lib/mbsstr.c

Packit 709fb3
/* Searching in a string.  -*- coding: utf-8 -*-
Packit 709fb3
   Copyright (C) 2005-2017 Free Software Foundation, Inc.
Packit 709fb3
   Written by Bruno Haible <bruno@clisp.org>, 2005.
Packit 709fb3
Packit 709fb3
   This program is free software: you can redistribute it and/or modify
Packit 709fb3
   it under the terms of the GNU General Public License as published by
Packit 709fb3
   the Free Software Foundation; either version 3 of the License, or
Packit 709fb3
   (at your option) any later version.
Packit 709fb3
Packit 709fb3
   This program is distributed in the hope that it will be useful,
Packit 709fb3
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 709fb3
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 709fb3
   GNU General Public License for more details.
Packit 709fb3
Packit 709fb3
   You should have received a copy of the GNU General Public License
Packit 709fb3
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit 709fb3
Packit 709fb3
#include <config.h>
Packit 709fb3
Packit 709fb3
/* Specification.  */
Packit 709fb3
#include <string.h>
Packit 709fb3
Packit 709fb3
#include <stdbool.h>
Packit 709fb3
#include <stddef.h>  /* for NULL, in case a nonstandard string.h lacks it */
Packit 709fb3
Packit 709fb3
#include "malloca.h"
Packit 709fb3
#include "mbuiter.h"
Packit 709fb3
Packit 709fb3
/* Knuth-Morris-Pratt algorithm.  */
Packit 709fb3
#define UNIT unsigned char
Packit 709fb3
#define CANON_ELEMENT(c) c
Packit 709fb3
#include "str-kmp.h"
Packit 709fb3
Packit 709fb3
/* Knuth-Morris-Pratt algorithm.
Packit 709fb3
   See http://en.wikipedia.org/wiki/Knuth-Morris-Pratt_algorithm
Packit 709fb3
   Return a boolean indicating success:
Packit 709fb3
   Return true and set *RESULTP if the search was completed.
Packit 709fb3
   Return false if it was aborted because not enough memory was available.  */
Packit 709fb3
static bool
Packit 709fb3
knuth_morris_pratt_multibyte (const char *haystack, const char *needle,
Packit 709fb3
                              const char **resultp)
Packit 709fb3
{
Packit 709fb3
  size_t m = mbslen (needle);
Packit 709fb3
  mbchar_t *needle_mbchars;
Packit 709fb3
  size_t *table;
Packit 709fb3
Packit 709fb3
  /* Allocate room for needle_mbchars and the table.  */
Packit 709fb3
  void *memory = nmalloca (m, sizeof (mbchar_t) + sizeof (size_t));
Packit 709fb3
  void *table_memory;
Packit 709fb3
  if (memory == NULL)
Packit 709fb3
    return false;
Packit 709fb3
  needle_mbchars = memory;
Packit 709fb3
  table_memory = needle_mbchars + m;
Packit 709fb3
  table = table_memory;
Packit 709fb3
Packit 709fb3
  /* Fill needle_mbchars.  */
Packit 709fb3
  {
Packit 709fb3
    mbui_iterator_t iter;
Packit 709fb3
    size_t j;
Packit 709fb3
Packit 709fb3
    j = 0;
Packit 709fb3
    for (mbui_init (iter, needle); mbui_avail (iter); mbui_advance (iter), j++)
Packit 709fb3
      mb_copy (&needle_mbchars[j], &mbui_cur (iter));
Packit 709fb3
  }
Packit 709fb3
Packit 709fb3
  /* Fill the table.
Packit 709fb3
     For 0 < i < m:
Packit 709fb3
       0 < table[i] <= i is defined such that
Packit 709fb3
       forall 0 < x < table[i]: needle[x..i-1] != needle[0..i-1-x],
Packit 709fb3
       and table[i] is as large as possible with this property.
Packit 709fb3
     This implies:
Packit 709fb3
     1) For 0 < i < m:
Packit 709fb3
          If table[i] < i,
Packit 709fb3
          needle[table[i]..i-1] = needle[0..i-1-table[i]].
Packit 709fb3
     2) For 0 < i < m:
Packit 709fb3
          rhaystack[0..i-1] == needle[0..i-1]
Packit 709fb3
          and exists h, i <= h < m: rhaystack[h] != needle[h]
Packit 709fb3
          implies
Packit 709fb3
          forall 0 <= x < table[i]: rhaystack[x..x+m-1] != needle[0..m-1].
Packit 709fb3
     table[0] remains uninitialized.  */
Packit 709fb3
  {
Packit 709fb3
    size_t i, j;
Packit 709fb3
Packit 709fb3
    /* i = 1: Nothing to verify for x = 0.  */
Packit 709fb3
    table[1] = 1;
Packit 709fb3
    j = 0;
Packit 709fb3
Packit 709fb3
    for (i = 2; i < m; i++)
Packit 709fb3
      {
Packit 709fb3
        /* Here: j = i-1 - table[i-1].
Packit 709fb3
           The inequality needle[x..i-1] != needle[0..i-1-x] is known to hold
Packit 709fb3
           for x < table[i-1], by induction.
Packit 709fb3
           Furthermore, if j>0: needle[i-1-j..i-2] = needle[0..j-1].  */
Packit 709fb3
        mbchar_t *b = &needle_mbchars[i - 1];
Packit 709fb3
Packit 709fb3
        for (;;)
Packit 709fb3
          {
Packit 709fb3
            /* Invariants: The inequality needle[x..i-1] != needle[0..i-1-x]
Packit 709fb3
               is known to hold for x < i-1-j.
Packit 709fb3
               Furthermore, if j>0: needle[i-1-j..i-2] = needle[0..j-1].  */
Packit 709fb3
            if (mb_equal (*b, needle_mbchars[j]))
Packit 709fb3
              {
Packit 709fb3
                /* Set table[i] := i-1-j.  */
Packit 709fb3
                table[i] = i - ++j;
Packit 709fb3
                break;
Packit 709fb3
              }
Packit 709fb3
            /* The inequality needle[x..i-1] != needle[0..i-1-x] also holds
Packit 709fb3
               for x = i-1-j, because
Packit 709fb3
                 needle[i-1] != needle[j] = needle[i-1-x].  */
Packit 709fb3
            if (j == 0)
Packit 709fb3
              {
Packit 709fb3
                /* The inequality holds for all possible x.  */
Packit 709fb3
                table[i] = i;
Packit 709fb3
                break;
Packit 709fb3
              }
Packit 709fb3
            /* The inequality needle[x..i-1] != needle[0..i-1-x] also holds
Packit 709fb3
               for i-1-j < x < i-1-j+table[j], because for these x:
Packit 709fb3
                 needle[x..i-2]
Packit 709fb3
                 = needle[x-(i-1-j)..j-1]
Packit 709fb3
                 != needle[0..j-1-(x-(i-1-j))]  (by definition of table[j])
Packit 709fb3
                    = needle[0..i-2-x],
Packit 709fb3
               hence needle[x..i-1] != needle[0..i-1-x].
Packit 709fb3
               Furthermore
Packit 709fb3
                 needle[i-1-j+table[j]..i-2]
Packit 709fb3
                 = needle[table[j]..j-1]
Packit 709fb3
                 = needle[0..j-1-table[j]]  (by definition of table[j]).  */
Packit 709fb3
            j = j - table[j];
Packit 709fb3
          }
Packit 709fb3
        /* Here: j = i - table[i].  */
Packit 709fb3
      }
Packit 709fb3
  }
Packit 709fb3
Packit 709fb3
  /* Search, using the table to accelerate the processing.  */
Packit 709fb3
  {
Packit 709fb3
    size_t j;
Packit 709fb3
    mbui_iterator_t rhaystack;
Packit 709fb3
    mbui_iterator_t phaystack;
Packit 709fb3
Packit 709fb3
    *resultp = NULL;
Packit 709fb3
    j = 0;
Packit 709fb3
    mbui_init (rhaystack, haystack);
Packit 709fb3
    mbui_init (phaystack, haystack);
Packit 709fb3
    /* Invariant: phaystack = rhaystack + j.  */
Packit 709fb3
    while (mbui_avail (phaystack))
Packit 709fb3
      if (mb_equal (needle_mbchars[j], mbui_cur (phaystack)))
Packit 709fb3
        {
Packit 709fb3
          j++;
Packit 709fb3
          mbui_advance (phaystack);
Packit 709fb3
          if (j == m)
Packit 709fb3
            {
Packit 709fb3
              /* The entire needle has been found.  */
Packit 709fb3
              *resultp = mbui_cur_ptr (rhaystack);
Packit 709fb3
              break;
Packit 709fb3
            }
Packit 709fb3
        }
Packit 709fb3
      else if (j > 0)
Packit 709fb3
        {
Packit 709fb3
          /* Found a match of needle[0..j-1], mismatch at needle[j].  */
Packit 709fb3
          size_t count = table[j];
Packit 709fb3
          j -= count;
Packit 709fb3
          for (; count > 0; count--)
Packit 709fb3
            {
Packit 709fb3
              if (!mbui_avail (rhaystack))
Packit 709fb3
                abort ();
Packit 709fb3
              mbui_advance (rhaystack);
Packit 709fb3
            }
Packit 709fb3
        }
Packit 709fb3
      else
Packit 709fb3
        {
Packit 709fb3
          /* Found a mismatch at needle[0] already.  */
Packit 709fb3
          if (!mbui_avail (rhaystack))
Packit 709fb3
            abort ();
Packit 709fb3
          mbui_advance (rhaystack);
Packit 709fb3
          mbui_advance (phaystack);
Packit 709fb3
        }
Packit 709fb3
  }
Packit 709fb3
Packit 709fb3
  freea (memory);
Packit 709fb3
  return true;
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
/* Find the first occurrence of the character string NEEDLE in the character
Packit 709fb3
   string HAYSTACK.  Return NULL if NEEDLE is not found in HAYSTACK.  */
Packit 709fb3
char *
Packit 709fb3
mbsstr (const char *haystack, const char *needle)
Packit 709fb3
{
Packit 709fb3
  /* Be careful not to look at the entire extent of haystack or needle
Packit 709fb3
     until needed.  This is useful because of these two cases:
Packit 709fb3
       - haystack may be very long, and a match of needle found early,
Packit 709fb3
       - needle may be very long, and not even a short initial segment of
Packit 709fb3
         needle may be found in haystack.  */
Packit 709fb3
  if (MB_CUR_MAX > 1)
Packit 709fb3
    {
Packit 709fb3
      mbui_iterator_t iter_needle;
Packit 709fb3
Packit 709fb3
      mbui_init (iter_needle, needle);
Packit 709fb3
      if (mbui_avail (iter_needle))
Packit 709fb3
        {
Packit 709fb3
          /* Minimizing the worst-case complexity:
Packit 709fb3
             Let n = mbslen(haystack), m = mbslen(needle).
Packit 709fb3
             The na誰ve algorithm is O(n*m) worst-case.
Packit 709fb3
             The Knuth-Morris-Pratt algorithm is O(n) worst-case but it needs a
Packit 709fb3
             memory allocation.
Packit 709fb3
             To achieve linear complexity and yet amortize the cost of the
Packit 709fb3
             memory allocation, we activate the Knuth-Morris-Pratt algorithm
Packit 709fb3
             only once the na誰ve algorithm has already run for some time; more
Packit 709fb3
             precisely, when
Packit 709fb3
               - the outer loop count is >= 10,
Packit 709fb3
               - the average number of comparisons per outer loop is >= 5,
Packit 709fb3
               - the total number of comparisons is >= m.
Packit 709fb3
             But we try it only once.  If the memory allocation attempt failed,
Packit 709fb3
             we don't retry it.  */
Packit 709fb3
          bool try_kmp = true;
Packit 709fb3
          size_t outer_loop_count = 0;
Packit 709fb3
          size_t comparison_count = 0;
Packit 709fb3
          size_t last_ccount = 0;                  /* last comparison count */
Packit 709fb3
          mbui_iterator_t iter_needle_last_ccount; /* = needle + last_ccount */
Packit 709fb3
Packit 709fb3
          mbui_iterator_t iter_haystack;
Packit 709fb3
Packit 709fb3
          mbui_init (iter_needle_last_ccount, needle);
Packit 709fb3
          mbui_init (iter_haystack, haystack);
Packit 709fb3
          for (;; mbui_advance (iter_haystack))
Packit 709fb3
            {
Packit 709fb3
              if (!mbui_avail (iter_haystack))
Packit 709fb3
                /* No match.  */
Packit 709fb3
                return NULL;
Packit 709fb3
Packit 709fb3
              /* See whether it's advisable to use an asymptotically faster
Packit 709fb3
                 algorithm.  */
Packit 709fb3
              if (try_kmp
Packit 709fb3
                  && outer_loop_count >= 10
Packit 709fb3
                  && comparison_count >= 5 * outer_loop_count)
Packit 709fb3
                {
Packit 709fb3
                  /* See if needle + comparison_count now reaches the end of
Packit 709fb3
                     needle.  */
Packit 709fb3
                  size_t count = comparison_count - last_ccount;
Packit 709fb3
                  for (;
Packit 709fb3
                       count > 0 && mbui_avail (iter_needle_last_ccount);
Packit 709fb3
                       count--)
Packit 709fb3
                    mbui_advance (iter_needle_last_ccount);
Packit 709fb3
                  last_ccount = comparison_count;
Packit 709fb3
                  if (!mbui_avail (iter_needle_last_ccount))
Packit 709fb3
                    {
Packit 709fb3
                      /* Try the Knuth-Morris-Pratt algorithm.  */
Packit 709fb3
                      const char *result;
Packit 709fb3
                      bool success =
Packit 709fb3
                        knuth_morris_pratt_multibyte (haystack, needle,
Packit 709fb3
                                                      &result);
Packit 709fb3
                      if (success)
Packit 709fb3
                        return (char *) result;
Packit 709fb3
                      try_kmp = false;
Packit 709fb3
                    }
Packit 709fb3
                }
Packit 709fb3
Packit 709fb3
              outer_loop_count++;
Packit 709fb3
              comparison_count++;
Packit 709fb3
              if (mb_equal (mbui_cur (iter_haystack), mbui_cur (iter_needle)))
Packit 709fb3
                /* The first character matches.  */
Packit 709fb3
                {
Packit 709fb3
                  mbui_iterator_t rhaystack;
Packit 709fb3
                  mbui_iterator_t rneedle;
Packit 709fb3
Packit 709fb3
                  memcpy (&rhaystack, &iter_haystack, sizeof (mbui_iterator_t));
Packit 709fb3
                  mbui_advance (rhaystack);
Packit 709fb3
Packit 709fb3
                  mbui_init (rneedle, needle);
Packit 709fb3
                  if (!mbui_avail (rneedle))
Packit 709fb3
                    abort ();
Packit 709fb3
                  mbui_advance (rneedle);
Packit 709fb3
Packit 709fb3
                  for (;; mbui_advance (rhaystack), mbui_advance (rneedle))
Packit 709fb3
                    {
Packit 709fb3
                      if (!mbui_avail (rneedle))
Packit 709fb3
                        /* Found a match.  */
Packit 709fb3
                        return (char *) mbui_cur_ptr (iter_haystack);
Packit 709fb3
                      if (!mbui_avail (rhaystack))
Packit 709fb3
                        /* No match.  */
Packit 709fb3
                        return NULL;
Packit 709fb3
                      comparison_count++;
Packit 709fb3
                      if (!mb_equal (mbui_cur (rhaystack), mbui_cur (rneedle)))
Packit 709fb3
                        /* Nothing in this round.  */
Packit 709fb3
                        break;
Packit 709fb3
                    }
Packit 709fb3
                }
Packit 709fb3
            }
Packit 709fb3
        }
Packit 709fb3
      else
Packit 709fb3
        return (char *) haystack;
Packit 709fb3
    }
Packit 709fb3
  else
Packit 709fb3
    {
Packit 709fb3
      if (*needle != '\0')
Packit 709fb3
        {
Packit 709fb3
          /* Minimizing the worst-case complexity:
Packit 709fb3
             Let n = strlen(haystack), m = strlen(needle).
Packit 709fb3
             The na誰ve algorithm is O(n*m) worst-case.
Packit 709fb3
             The Knuth-Morris-Pratt algorithm is O(n) worst-case but it needs a
Packit 709fb3
             memory allocation.
Packit 709fb3
             To achieve linear complexity and yet amortize the cost of the
Packit 709fb3
             memory allocation, we activate the Knuth-Morris-Pratt algorithm
Packit 709fb3
             only once the na誰ve algorithm has already run for some time; more
Packit 709fb3
             precisely, when
Packit 709fb3
               - the outer loop count is >= 10,
Packit 709fb3
               - the average number of comparisons per outer loop is >= 5,
Packit 709fb3
               - the total number of comparisons is >= m.
Packit 709fb3
             But we try it only once.  If the memory allocation attempt failed,
Packit 709fb3
             we don't retry it.  */
Packit 709fb3
          bool try_kmp = true;
Packit 709fb3
          size_t outer_loop_count = 0;
Packit 709fb3
          size_t comparison_count = 0;
Packit 709fb3
          size_t last_ccount = 0;                  /* last comparison count */
Packit 709fb3
          const char *needle_last_ccount = needle; /* = needle + last_ccount */
Packit 709fb3
Packit 709fb3
          /* Speed up the following searches of needle by caching its first
Packit 709fb3
             character.  */
Packit 709fb3
          char b = *needle++;
Packit 709fb3
Packit 709fb3
          for (;; haystack++)
Packit 709fb3
            {
Packit 709fb3
              if (*haystack == '\0')
Packit 709fb3
                /* No match.  */
Packit 709fb3
                return NULL;
Packit 709fb3
Packit 709fb3
              /* See whether it's advisable to use an asymptotically faster
Packit 709fb3
                 algorithm.  */
Packit 709fb3
              if (try_kmp
Packit 709fb3
                  && outer_loop_count >= 10
Packit 709fb3
                  && comparison_count >= 5 * outer_loop_count)
Packit 709fb3
                {
Packit 709fb3
                  /* See if needle + comparison_count now reaches the end of
Packit 709fb3
                     needle.  */
Packit 709fb3
                  if (needle_last_ccount != NULL)
Packit 709fb3
                    {
Packit 709fb3
                      needle_last_ccount +=
Packit 709fb3
                        strnlen (needle_last_ccount,
Packit 709fb3
                                 comparison_count - last_ccount);
Packit 709fb3
                      if (*needle_last_ccount == '\0')
Packit 709fb3
                        needle_last_ccount = NULL;
Packit 709fb3
                      last_ccount = comparison_count;
Packit 709fb3
                    }
Packit 709fb3
                  if (needle_last_ccount == NULL)
Packit 709fb3
                    {
Packit 709fb3
                      /* Try the Knuth-Morris-Pratt algorithm.  */
Packit 709fb3
                      const unsigned char *result;
Packit 709fb3
                      bool success =
Packit 709fb3
                        knuth_morris_pratt ((const unsigned char *) haystack,
Packit 709fb3
                                            (const unsigned char *) (needle - 1),
Packit 709fb3
                                            strlen (needle - 1),
Packit 709fb3
                                            &result);
Packit 709fb3
                      if (success)
Packit 709fb3
                        return (char *) result;
Packit 709fb3
                      try_kmp = false;
Packit 709fb3
                    }
Packit 709fb3
                }
Packit 709fb3
Packit 709fb3
              outer_loop_count++;
Packit 709fb3
              comparison_count++;
Packit 709fb3
              if (*haystack == b)
Packit 709fb3
                /* The first character matches.  */
Packit 709fb3
                {
Packit 709fb3
                  const char *rhaystack = haystack + 1;
Packit 709fb3
                  const char *rneedle = needle;
Packit 709fb3
Packit 709fb3
                  for (;; rhaystack++, rneedle++)
Packit 709fb3
                    {
Packit 709fb3
                      if (*rneedle == '\0')
Packit 709fb3
                        /* Found a match.  */
Packit 709fb3
                        return (char *) haystack;
Packit 709fb3
                      if (*rhaystack == '\0')
Packit 709fb3
                        /* No match.  */
Packit 709fb3
                        return NULL;
Packit 709fb3
                      comparison_count++;
Packit 709fb3
                      if (*rhaystack != *rneedle)
Packit 709fb3
                        /* Nothing in this round.  */
Packit 709fb3
                        break;
Packit 709fb3
                    }
Packit 709fb3
                }
Packit 709fb3
            }
Packit 709fb3
        }
Packit 709fb3
      else
Packit 709fb3
        return (char *) haystack;
Packit 709fb3
    }
Packit 709fb3
}