Documentation

Std.Data.String.Basic

theorem String.Pos.ne_zero_of_lt {a : String.Pos} {b : String.Pos} :
a < bb 0

Knuth-Morris-Pratt matcher type

This type is used to keep data for running the Knuth-Morris-Pratt (KMP) string matching algorithm. KMP is a linear time algorithm to locate all substrings of a string that match a given pattern. Generating the algorithm data is also linear in the length of the pattern but the data can be re-used to match the same pattern over different strings.

The KMP data for a pattern string can be generated using Matcher.ofString. Then Matcher.find? and Matcher.findAll can be used to run the algorithm on an input string.

def m := Matcher.ofString "abba"

#eval Option.isSome <| m.find? "AbbabbA" -- false
#eval Option.isSome <| m.find? "aabbaa" -- true

#eval Array.size <| m.findAll "abbabba" -- 2
#eval Array.size <| m.findAll "abbabbabba" -- 3
Instances For
    @[inline]

    Make KMP matcher from pattern substring

    Equations
    Instances For
      @[inline]

      Make KMP matcher from pattern string

      Equations
      Instances For
        @[inline, reducible]

        The byte size of the string pattern for the matcher

        Equations
        Instances For

          Find all substrings of s matching m.pattern.

          Equations
          Instances For

            Find the first substring of s matching m.pattern, or none if no such substring exists.

            Equations
            • One or more equations did not get rendered due to their size.
            Instances For

              Returns the longest common prefix of two substrings. The returned substring will use the same underlying string as s.

              Equations
              Instances For

                Returns the ending position of the common prefix, working up from spos, tpos.

                Equations
                • One or more equations did not get rendered due to their size.
                Instances For

                  Returns the longest common suffix of two substrings. The returned substring will use the same underlying string as s.

                  Equations
                  Instances For

                    Returns the starting position of the common prefix, working down from spos, tpos.

                    Equations
                    • One or more equations did not get rendered due to their size.
                    Instances For

                      If pre is a prefix of s, i.e. s = pre ++ t, returns the remainder t.

                      Equations
                      Instances For

                        If suff is a suffix of s, i.e. s = t ++ suff, returns the remainder t.

                        Equations
                        • One or more equations did not get rendered due to their size.
                        Instances For
                          @[inline]

                          Returns all the substrings of s that match pattern.

                          Equations
                          Instances For
                            @[inline]

                            Returns the first substring of s that matches pattern, or none if there is no such substring.

                            Equations
                            Instances For
                              @[inline]

                              Returns true iff pattern occurs as a substring of s.

                              Equations
                              Instances For
                                @[inline, reducible]

                                Returns all the substrings of s that match pattern.

                                Equations
                                Instances For
                                  @[inline, reducible]

                                  Returns the first substring of s that matches pattern, or none if there is no such substring.

                                  Equations
                                  Instances For
                                    @[inline, reducible]
                                    abbrev String.containsSubstr (s : String) (pattern : Substring) :

                                    Returns true iff pattern occurs as a substring of s.

                                    Equations
                                    Instances For

                                      If pre is a prefix of s, i.e. s = pre ++ t, returns the remainder t.

                                      Equations
                                      Instances For

                                        If suff is a suffix of s, i.e. s = t ++ suff, returns the remainder t.

                                        Equations
                                        Instances For

                                          s.stripPrefix pre will remove pre from the beginning of s if it occurs there, or otherwise return s.

                                          Equations
                                          Instances For

                                            s.stripSuffix suff will remove suff from the end of s if it occurs there, or otherwise return s.

                                            Equations
                                            Instances For
                                              def String.count (s : String) (c : Char) :

                                              Count the occurrences of a character in a string.

                                              Equations
                                              Instances For