Blame lib/mbsstr.c

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