Blame lib/str-kmp.h

Packit 709fb3
/* Substring search in a NUL terminated string of UNIT elements,
Packit 709fb3
   using the Knuth-Morris-Pratt algorithm.
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, or (at your option)
Packit 709fb3
   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
/* Before including this file, you need to define:
Packit 709fb3
     UNIT                    The element type of the needle and haystack.
Packit 709fb3
     CANON_ELEMENT(c)        A macro that canonicalizes an element right after
Packit 709fb3
                             it has been fetched from needle or haystack.
Packit 709fb3
                             The argument is of type UNIT; the result must be
Packit 709fb3
                             of type UNIT as well.  */
Packit 709fb3
Packit 709fb3
/* Knuth-Morris-Pratt algorithm.
Packit 709fb3
   See http://en.wikipedia.org/wiki/Knuth-Morris-Pratt_algorithm
Packit 709fb3
   HAYSTACK is the NUL terminated string in which to search for.
Packit 709fb3
   NEEDLE is the string to search for in HAYSTACK, consisting of NEEDLE_LEN
Packit 709fb3
   units.
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 (const UNIT *haystack,
Packit 709fb3
                    const UNIT *needle, size_t needle_len,
Packit 709fb3
                    const UNIT **resultp)
Packit 709fb3
{
Packit 709fb3
  size_t m = needle_len;
Packit 709fb3
Packit 709fb3
  /* Allocate the table.  */
Packit 709fb3
  size_t *table = (size_t *) nmalloca (m, sizeof (size_t));
Packit 709fb3
  if (table == NULL)
Packit 709fb3
    return false;
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
        UNIT b = CANON_ELEMENT (needle[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 (b == CANON_ELEMENT (needle[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
    const UNIT *rhaystack;
Packit 709fb3
    const UNIT *phaystack;
Packit 709fb3
Packit 709fb3
    *resultp = NULL;
Packit 709fb3
    j = 0;
Packit 709fb3
    rhaystack = haystack;
Packit 709fb3
    phaystack = haystack;
Packit 709fb3
    /* Invariant: phaystack = rhaystack + j.  */
Packit 709fb3
    while (*phaystack != 0)
Packit 709fb3
      if (CANON_ELEMENT (needle[j]) == CANON_ELEMENT (*phaystack))
Packit 709fb3
        {
Packit 709fb3
          j++;
Packit 709fb3
          phaystack++;
Packit 709fb3
          if (j == m)
Packit 709fb3
            {
Packit 709fb3
              /* The entire needle has been found.  */
Packit 709fb3
              *resultp = 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
          rhaystack += table[j];
Packit 709fb3
          j -= table[j];
Packit 709fb3
        }
Packit 709fb3
      else
Packit 709fb3
        {
Packit 709fb3
          /* Found a mismatch at needle[0] already.  */
Packit 709fb3
          rhaystack++;
Packit 709fb3
          phaystack++;
Packit 709fb3
        }
Packit 709fb3
  }
Packit 709fb3
Packit 709fb3
  freea (table);
Packit 709fb3
  return true;
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
#undef CANON_ELEMENT