Documentation

Init.Data.String.Pattern.Basic

A step taken during the traversal of a Slice by a forward or backward searcher.

Instances For
    @[inline]

    The start position of a SearchStep.

    Equations
    Instances For
      @[inline]

      The end position of a SearchStep.

      Equations
      Instances For

        Provides simple pattern matching capabilities from the start of a Slice.

        • dropPrefix? (s : Slice) : Option s.Pos

          Checks whether the slice starts with the pattern. If it does, the slice is returned with the prefix removed; otherwise the result is none.

        • dropPrefixOfNonempty? (s : Slice) (h : s.isEmpty = false) : Option s.Pos

          Checks whether the slice starts with the pattern. If it does, the slice is returned with the prefix removed; otherwise the result is none.

        • startsWith (s : Slice) : Bool

          Checks whether the slice starts with the pattern.

        Instances

          A lawful forward pattern is one where the three functions ForwardPattern.dropPrefix?, ForwardPattern.dropPrefixOfNonempty? and ForwardPattern.startsWith agree for any given input slice.

          Note that this is a relatively weak condition. It is non-uniform in the sense that the functions can still return completely different results on different slices, even if they represent the same string.

          There is a stronger lawfulness typeclass LawfulForwardPatternModel that asserts that the ForwardPattern.dropPrefix? function behaves like a function that drops the longest prefix according to some notion of matching.

          Instances

            A strict forward pattern is one which never drops an empty prefix.

            This condition ensures that the default searcher derived from the forward pattern is a finite iterator.

            Instances
              class String.Slice.Pattern.ToForwardSearcher {ρ : Type} (pat : ρ) (σ : outParam (SliceType)) :

              Provides a conversion from a pattern to an iterator of SearchStep that searches for matches of the pattern from the start towards the end of a Slice.

              While these operations can be implemented on top of ForwardPattern, some patterns allow for more efficient implementations. For example, a searcher for String patterns derived from the ForwardPattern instance on strings would try to match the pattern at every position in the string, but more efficient string matching routines are known. Indeed, the Lean standard library uses the Knuth-Morris-Pratt algorithm. See the module Init.Data.String.Pattern.String for the implementation.

              This class can be used to provide such an efficient implementation. If there is no need to specialize in this fashion, then ToForwardSearcher.defaultImplementation can be used to automatically derive an instance.

              • toSearcher (s : Slice) : Std.Iter (SearchStep s)

                Builds an iterator of SearchStep corresponding to matches of pat along the slice s. The SearchSteps returned by this iterator must contain ranges that are adjacent, non-overlapping and cover all of s.

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

                  The default implementation of ToForwardSearcher repeatedly tries to match the pattern using the given ForwardPattern instance.

                  It is sometimes possible to give a more efficient implementation; see ToForwardSearcher for more details.

                  Equations
                  Instances For
                    @[extern lean_string_memcmp]
                    def String.Slice.Pattern.Internal.memcmpStr (lhs rhs : String) (lstart rstart len : Pos.Raw) (h1 : len.offsetBy lstart lhs.rawEndPos) (h2 : len.offsetBy rstart rhs.rawEndPos) :
                    Equations
                    Instances For
                      @[inline]
                      def String.Slice.Pattern.Internal.memcmpSlice (lhs rhs : Slice) (lstart rstart len : Pos.Raw) (h1 : len.offsetBy lstart lhs.rawEndPos) (h2 : len.offsetBy rstart rhs.rawEndPos) :
                      Equations
                      • One or more equations did not get rendered due to their size.
                      Instances For

                        Provides simple pattern matching capabilities from the end of a Slice.

                        • dropSuffix? (s : Slice) : Option s.Pos

                          Checks whether the slice ends with the pattern. If it does, the slice is returned with the suffix removed; otherwise the result is none.

                        • dropSuffixOfNonempty? (s : Slice) (h : s.isEmpty = false) : Option s.Pos

                          Checks whether the slice ends with the pattern. If it does, the slice is returned with the suffix removed; otherwise the result is none.

                        • endsWith : SliceBool

                          Checks whether the slice ends with the pattern.

                        Instances

                          A lawful backward pattern is one where the three functions BackwardPattern.dropSuffix?, BackwardPattern.dropSuffixOfNonempty? and BackwardPattern.endsWith agree for any given input slice.

                          Note that this is a relatively weak condition. It is non-uniform in the sense that the functions can still return completely different results on different slices, even if they represent the same string.

                          There is a stronger lawfulness typeclass LawfulBackwardPatternModel that asserts that the BackwardPattern.dropSuffix? function behaves like a function that drops the longest prefix according to some notion of matching.

                          Instances

                            A strict backward pattern is one which never drops an empty suffix.

                            This condition ensures that the default searcher derived from the backward pattern is a finite iterator.

                            Instances
                              class String.Slice.Pattern.ToBackwardSearcher {ρ : Type} (pat : ρ) (σ : outParam (SliceType)) :

                              Provides a conversion from a pattern to an iterator of SearchStep searching for matches of the pattern from the end towards the start of a Slice.

                              While these operations can be implemented on top of BackwardPattern, some patterns allow for more efficient implementations. For example, a searcher for String patterns derived from the BackwardPattern instance on strings would try to match the pattern at every position in the string, but more efficient string matching routines are known. Indeed, the Lean standard library uses the Knuth-Morris-Pratt algorithm. See the module Init.Data.String.Pattern.String for the implementation.

                              This class can be used to provide such an efficient implementation. If there is no need to specialize in this fashion, then ToBackwardSearcher.defaultImplementation can be used to automatically derive an instance.

                              • toSearcher (s : Slice) : Std.Iter (SearchStep s)

                                Build an iterator of SearchStep corresponding to matches of pat along the slice s. The SearchSteps returned by this iterator must contain ranges that are adjacent, non-overlapping and cover all of s.

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

                                  The default implementation of ToBackwardSearcher repeatedly tries to match the pattern using the given BackwardPattern instance.

                                  It is sometimes possible to give a more efficient implementation; see ToBackwardSearcher for more details.

                                  Equations
                                  Instances For