Blame lib/str-kmp.h

Packit 9e4112
/* Substring search in a NUL terminated string of UNIT elements,
Packit 9e4112
   using the Knuth-Morris-Pratt algorithm.
Packit 9e4112
   Copyright (C) 2005-2018 Free Software Foundation, Inc.
Packit 9e4112
   Written by Bruno Haible <bruno@clisp.org>, 2005.
Packit 9e4112
Packit 9e4112
   This program is free software; you can redistribute it and/or modify
Packit 9e4112
   it under the terms of the GNU General Public License as published by
Packit 9e4112
   the Free Software Foundation; either version 3, or (at your option)
Packit 9e4112
   any later version.
Packit 9e4112
Packit 9e4112
   This program is distributed in the hope that it will be useful,
Packit 9e4112
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 9e4112
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 9e4112
   GNU General Public License for more details.
Packit 9e4112
Packit 9e4112
   You should have received a copy of the GNU General Public License
Packit 9e4112
   along with this program; if not, see <https://www.gnu.org/licenses/>.  */
Packit 9e4112
Packit 9e4112
/* Before including this file, you need to define:
Packit 9e4112
     UNIT                    The element type of the needle and haystack.
Packit 9e4112
     CANON_ELEMENT(c)        A macro that canonicalizes an element right after
Packit 9e4112
                             it has been fetched from needle or haystack.
Packit 9e4112
                             The argument is of type UNIT; the result must be
Packit 9e4112
                             of type UNIT as well.  */
Packit 9e4112
Packit 9e4112
/* Knuth-Morris-Pratt algorithm.
Packit 9e4112
   See https://en.wikipedia.org/wiki/Knuth-Morris-Pratt_algorithm
Packit 9e4112
   HAYSTACK is the NUL terminated string in which to search for.
Packit 9e4112
   NEEDLE is the string to search for in HAYSTACK, consisting of NEEDLE_LEN
Packit 9e4112
   units.
Packit 9e4112
   Return a boolean indicating success:
Packit 9e4112
   Return true and set *RESULTP if the search was completed.
Packit 9e4112
   Return false if it was aborted because not enough memory was available.  */
Packit 9e4112
static bool
Packit 9e4112
knuth_morris_pratt (const UNIT *haystack,
Packit 9e4112
                    const UNIT *needle, size_t needle_len,
Packit 9e4112
                    const UNIT **resultp)
Packit 9e4112
{
Packit 9e4112
  size_t m = needle_len;
Packit 9e4112
Packit 9e4112
  /* Allocate the table.  */
Packit 9e4112
  size_t *table = (size_t *) nmalloca (m, sizeof (size_t));
Packit 9e4112
  if (table == NULL)
Packit 9e4112
    return false;
Packit 9e4112
  /* Fill the table.
Packit 9e4112
     For 0 < i < m:
Packit 9e4112
       0 < table[i] <= i is defined such that
Packit 9e4112
       forall 0 < x < table[i]: needle[x..i-1] != needle[0..i-1-x],
Packit 9e4112
       and table[i] is as large as possible with this property.
Packit 9e4112
     This implies:
Packit 9e4112
     1) For 0 < i < m:
Packit 9e4112
          If table[i] < i,
Packit 9e4112
          needle[table[i]..i-1] = needle[0..i-1-table[i]].
Packit 9e4112
     2) For 0 < i < m:
Packit 9e4112
          rhaystack[0..i-1] == needle[0..i-1]
Packit 9e4112
          and exists h, i <= h < m: rhaystack[h] != needle[h]
Packit 9e4112
          implies
Packit 9e4112
          forall 0 <= x < table[i]: rhaystack[x..x+m-1] != needle[0..m-1].
Packit 9e4112
     table[0] remains uninitialized.  */
Packit 9e4112
  {
Packit 9e4112
    size_t i, j;
Packit 9e4112
Packit 9e4112
    /* i = 1: Nothing to verify for x = 0.  */
Packit 9e4112
    table[1] = 1;
Packit 9e4112
    j = 0;
Packit 9e4112
Packit 9e4112
    for (i = 2; i < m; i++)
Packit 9e4112
      {
Packit 9e4112
        /* Here: j = i-1 - table[i-1].
Packit 9e4112
           The inequality needle[x..i-1] != needle[0..i-1-x] is known to hold
Packit 9e4112
           for x < table[i-1], by induction.
Packit 9e4112
           Furthermore, if j>0: needle[i-1-j..i-2] = needle[0..j-1].  */
Packit 9e4112
        UNIT b = CANON_ELEMENT (needle[i - 1]);
Packit 9e4112
Packit 9e4112
        for (;;)
Packit 9e4112
          {
Packit 9e4112
            /* Invariants: The inequality needle[x..i-1] != needle[0..i-1-x]
Packit 9e4112
               is known to hold for x < i-1-j.
Packit 9e4112
               Furthermore, if j>0: needle[i-1-j..i-2] = needle[0..j-1].  */
Packit 9e4112
            if (b == CANON_ELEMENT (needle[j]))
Packit 9e4112
              {
Packit 9e4112
                /* Set table[i] := i-1-j.  */
Packit 9e4112
                table[i] = i - ++j;
Packit 9e4112
                break;
Packit 9e4112
              }
Packit 9e4112
            /* The inequality needle[x..i-1] != needle[0..i-1-x] also holds
Packit 9e4112
               for x = i-1-j, because
Packit 9e4112
                 needle[i-1] != needle[j] = needle[i-1-x].  */
Packit 9e4112
            if (j == 0)
Packit 9e4112
              {
Packit 9e4112
                /* The inequality holds for all possible x.  */
Packit 9e4112
                table[i] = i;
Packit 9e4112
                break;
Packit 9e4112
              }
Packit 9e4112
            /* The inequality needle[x..i-1] != needle[0..i-1-x] also holds
Packit 9e4112
               for i-1-j < x < i-1-j+table[j], because for these x:
Packit 9e4112
                 needle[x..i-2]
Packit 9e4112
                 = needle[x-(i-1-j)..j-1]
Packit 9e4112
                 != needle[0..j-1-(x-(i-1-j))]  (by definition of table[j])
Packit 9e4112
                    = needle[0..i-2-x],
Packit 9e4112
               hence needle[x..i-1] != needle[0..i-1-x].
Packit 9e4112
               Furthermore
Packit 9e4112
                 needle[i-1-j+table[j]..i-2]
Packit 9e4112
                 = needle[table[j]..j-1]
Packit 9e4112
                 = needle[0..j-1-table[j]]  (by definition of table[j]).  */
Packit 9e4112
            j = j - table[j];
Packit 9e4112
          }
Packit 9e4112
        /* Here: j = i - table[i].  */
Packit 9e4112
      }
Packit 9e4112
  }
Packit 9e4112
Packit 9e4112
  /* Search, using the table to accelerate the processing.  */
Packit 9e4112
  {
Packit 9e4112
    size_t j;
Packit 9e4112
    const UNIT *rhaystack;
Packit 9e4112
    const UNIT *phaystack;
Packit 9e4112
Packit 9e4112
    *resultp = NULL;
Packit 9e4112
    j = 0;
Packit 9e4112
    rhaystack = haystack;
Packit 9e4112
    phaystack = haystack;
Packit 9e4112
    /* Invariant: phaystack = rhaystack + j.  */
Packit 9e4112
    while (*phaystack != 0)
Packit 9e4112
      if (CANON_ELEMENT (needle[j]) == CANON_ELEMENT (*phaystack))
Packit 9e4112
        {
Packit 9e4112
          j++;
Packit 9e4112
          phaystack++;
Packit 9e4112
          if (j == m)
Packit 9e4112
            {
Packit 9e4112
              /* The entire needle has been found.  */
Packit 9e4112
              *resultp = rhaystack;
Packit 9e4112
              break;
Packit 9e4112
            }
Packit 9e4112
        }
Packit 9e4112
      else if (j > 0)
Packit 9e4112
        {
Packit 9e4112
          /* Found a match of needle[0..j-1], mismatch at needle[j].  */
Packit 9e4112
          rhaystack += table[j];
Packit 9e4112
          j -= table[j];
Packit 9e4112
        }
Packit 9e4112
      else
Packit 9e4112
        {
Packit 9e4112
          /* Found a mismatch at needle[0] already.  */
Packit 9e4112
          rhaystack++;
Packit 9e4112
          phaystack++;
Packit 9e4112
        }
Packit 9e4112
  }
Packit 9e4112
Packit 9e4112
  freea (table);
Packit 9e4112
  return true;
Packit 9e4112
}
Packit 9e4112
Packit 9e4112
#undef CANON_ELEMENT