Documentation

Init.Data.String.Slice.Pattern

@[inline]
def String.Slice.startsWith {ρ : Type} (s : Slice) (pat : ρ) [Pattern.ForwardPattern pat] :

Checks whether the slice (s) begins with the pattern (pat).

This function is generic over all currently supported patterns.

Examples:

Equations
Instances For
    inductive String.Slice.SplitIterator {σ : Slice → Type} {ρ : Type} (pat : ρ) (s : Slice) [Pattern.ToForwardSearcher pat σ] :
    Instances For
      @[instance_reducible]
      instance String.Slice.instInhabitedSplitIterator {a✝ : Slice → Type} {a✝¹ : Type} {a✝² : a✝¹} {a✝³ : Slice} {a✝⁴ : Pattern.ToForwardSearcher a✝² a✝} :
      Inhabited (SplitIterator a✝² a✝³)
      Equations
      @[instance_reducible]
      Equations
      • One or more equations did not get rendered due to their size.
      @[specialize #[3]]
      def String.Slice.splitToSubslice {ρ : Type} {σ : Slice → Type} (s : Slice) (pat : ρ) [Pattern.ToForwardSearcher pat σ] :

      Splits a slice at each subslice that matches the pattern pat.

      The subslices that matched the pattern are not included in any of the resulting subslices. If multiple subslices in a row match the pattern, the resulting list will contain empty subslices.

      This function is generic over all currently supported patterns.

      Examples:

      • ("coffee tea water".toSlice.splitToSubslice Char.isWhitespace).toStringList == ["coffee", "tea", "water"]

      • ("coffee tea water".toSlice.splitToSubslice ' ').toStringList == ["coffee", "tea", "water"]

      • ("coffee tea water".toSlice.splitToSubslice " tea ").toStringList == ["coffee", "water"]

      • ("ababababa".toSlice.splitToSubslice "aba").toStringList == ["", "b", "ba"]

      • ("baaab".toSlice.splitToSubslice "aa").toStringList == ["b", "ab"]

      Equations
      Instances For
        @[inline]
        def String.Slice.split {ρ : Type} {σ : Slice → Type} [(s : Slice) → Std.Iterator (σ s) Id (Pattern.SearchStep s)] (s : Slice) (pat : ρ) [Pattern.ToForwardSearcher pat σ] :

        Splits a slice at each subslice that matches the pattern pat.

        The subslices that matched the pattern are not included in any of the resulting subslices. If multiple subslices in a row match the pattern, the resulting list will contain empty slices.

        This function is generic over all currently supported patterns.

        Examples:

        • ("coffee tea water".toSlice.split Char.isWhitespace).toStringList == ["coffee", "tea", "water"]

        • ("coffee tea water".toSlice.split ' ').toStringList == ["coffee", "tea", "water"]

        • ("coffee tea water".toSlice.split " tea ").toStringList == ["coffee", "water"]

        • ("ababababa".toSlice.split "aba").toStringList == ["", "b", "ba"]

        • ("baaab".toSlice.split "aa").toStringList == ["b", "ab"]

        Equations
        Instances For
          inductive String.Slice.SplitInclusiveIterator {σ : Slice → Type} {ρ : Type} (pat : ρ) (s : Slice) [Pattern.ToForwardSearcher pat σ] :
          Instances For
            @[instance_reducible]
            Equations
            • One or more equations did not get rendered due to their size.
            @[specialize #[3]]
            def String.Slice.splitInclusive {ρ : Type} {σ : Slice → Type} (s : Slice) (pat : ρ) [Pattern.ToForwardSearcher pat σ] :

            Splits a slice at each subslice that matches the pattern pat. Unlike split the matched subslices are included at the end of each subslice.

            This function is generic over all currently supported patterns.

            Examples:

            • ("coffee tea water".toSlice.splitInclusive Char.isWhitespace).toList == ["coffee ".toSlice, "tea ".toSlice, "water".toSlice]

            • ("coffee tea water".toSlice.splitInclusive ' ').toList == ["coffee ".toSlice, "tea ".toSlice, "water".toSlice]

            • ("coffee tea water".toSlice.splitInclusive " tea ").toList == ["coffee tea ".toSlice, "water".toSlice]

            • ("baaab".toSlice.splitInclusive "aa").toList == ["baa".toSlice, "ab".toSlice]

            Equations
            Instances For
              @[inline]

              If pat matches a prefix of s, returns the position at the start of the remainder. Returns none otherwise.

              This function is generic over all currently supported patterns.

              Equations
              Instances For
                @[inline]
                def String.Slice.Pos.skip? {ρ : Type} {s : Slice} (pos : s.Pos) (pat : ρ) [Pattern.ForwardPattern pat] :

                If pat matches a prefix at pos, returns the position after the end of the match. Returns none otherwise.

                This function is generic over all currently supported patterns.

                Equations
                Instances For
                  @[inline]

                  If pat matches a prefix of s, returns the remainder. Returns none otherwise.

                  Use String.Slice.dropPrefix to return the slice unchanged when pat does not match a prefix.

                  This function is generic over all currently supported patterns.

                  Examples:

                  Equations
                  Instances For
                    @[specialize #[2]]

                    If pat matches a prefix of s, returns the remainder. Returns s unmodified otherwise.

                    Use String.Slice.dropPrefix? to return none when pat does not match a prefix.

                    This function is generic over all currently supported patterns.

                    Examples:

                    • "red green blue".toSlice.dropPrefix "red " == "green blue".toSlice

                    • "red green blue".toSlice.dropPrefix "reed " == "red green blue".toSlice

                    • "red green blue".toSlice.dropPrefix 'r' == "ed green blue".toSlice

                    • "red green blue".toSlice.dropPrefix Char.isLower == "ed green blue".toSlice

                    Equations
                    Instances For
                      def String.Slice.replace {ρ : Type} {σ : Slice → Type} [(s : Slice) → Std.Iterator (σ s) Id (Pattern.SearchStep s)] [(s : Slice) → Std.IteratorLoop (σ s) Id Id] {α : Type u_1} [ToSlice α] (s : Slice) (pattern : ρ) [Pattern.ToForwardSearcher pattern σ] (replacement : α) :

                      Constructs a new string obtained by replacing all occurrences of pattern with replacement in s.

                      This function is generic over all currently supported patterns. The replacement may be a String or a String.Slice.

                      Examples:

                      • "red green blue".toSlice.replace 'e' "" = "rd grn blu"

                      • "red green blue".toSlice.replace (fun c => c == 'u' || c == 'e') "" = "rd grn bl"

                      • "red green blue".toSlice.replace "e" "" = "rd grn blu"

                      • "red green blue".toSlice.replace "ee" "E" = "red grEn blue"

                      • "red green blue".toSlice.replace "e" "E" = "rEd grEEn bluE"

                      • "aaaaa".toSlice.replace "aa" "b" = "bba"

                      • "abc".toSlice.replace "" "k" = "kakbkck"

                      Equations
                      • One or more equations did not get rendered due to their size.
                      Instances For
                        @[irreducible, specialize #[3]]
                        def String.Slice.Pos.skipWhile {ρ : Type} {s : Slice} (pos : s.Pos) (pat : ρ) [Pattern.ForwardPattern pat] :
                        s.Pos

                        Advances pos as long as pat matches.

                        Equations
                        Instances For
                          @[inline]

                          Returns the position after the longest prefix of s for which pat matches (potentially repeatedly).

                          Equations
                          Instances For
                            @[inline]
                            def String.Slice.dropWhile {ρ : Type} (s : Slice) (pat : ρ) [Pattern.ForwardPattern pat] :

                            Creates a new slice by removing the longest prefix from s for which pat matched (potentially repeatedly).

                            Examples:

                            • "red green blue".toSlice.dropWhile Char.isLower == " green blue".toSlice

                            • "red green blue".toSlice.dropWhile 'r' == "ed green blue".toSlice

                            • "red red green blue".toSlice.dropWhile "red " == "green blue".toSlice

                            • "red green blue".toSlice.dropWhile (fun (_ : Char) => true) == "".toSlice

                            Equations
                            Instances For
                              @[inline]

                              Removes leading whitespace from a slice by moving its start position to the first non-whitespace character, or to its end position if there is no non-whitespace character.

                              “Whitespace” is defined as characters for which Char.isWhitespace returns true.

                              Examples:

                              Equations
                              Instances For
                                @[inline]
                                def String.Slice.takeWhile {ρ : Type} (s : Slice) (pat : ρ) [Pattern.ForwardPattern pat] :

                                Creates a new slice that contains the longest prefix of s for which pat matched (potentially repeatedly).

                                This function is generic over all currently supported patterns.

                                Examples:

                                • "red green blue".toSlice.takeWhile Char.isLower == "red".toSlice

                                • "red green blue".toSlice.takeWhile 'r' == "r".toSlice

                                • "red red green blue".toSlice.takeWhile "red " == "red red ".toSlice

                                • "red green blue".toSlice.takeWhile (fun (_ : Char) => true) == "red green blue".toSlice

                                Equations
                                Instances For
                                  @[inline]
                                  def String.Slice.find? {ρ : Type} {σ : Slice → Type} [(s : Slice) → Std.Iterator (σ s) Id (Pattern.SearchStep s)] [(s : Slice) → Std.IteratorLoop (σ s) Id Id] (s : Slice) (pat : ρ) [Pattern.ToForwardSearcher pat σ] :

                                  Finds the position of the first match of the pattern pat in a slice s. If there is no match none is returned.

                                  This function is generic over all currently supported patterns.

                                  Examples:

                                  • ("coffee tea water".toSlice.find? Char.isWhitespace).map (·.get!) == some ' '

                                  • "tea".toSlice.find? (fun (c : Char) => c == 'X') == none

                                  • ("coffee tea water".toSlice.find? "tea").map (·.get!) == some 't'

                                  Equations
                                  • One or more equations did not get rendered due to their size.
                                  Instances For
                                    @[inline]
                                    def String.Slice.find {ρ : Type} {σ : Slice → Type} [(s : Slice) → Std.Iterator (σ s) Id (Pattern.SearchStep s)] [(s : Slice) → Std.IteratorLoop (σ s) Id Id] (s : Slice) (pat : ρ) [Pattern.ToForwardSearcher pat σ] :
                                    s.Pos

                                    Finds the position of the first match of the pattern pat in a slice s. If there is no match s.endPos is returned.

                                    This function is generic over all currently supported patterns.

                                    Examples:

                                    • ("coffee tea water".toSlice.find Char.isWhitespace).get! == ' '

                                    • "tea".toSlice.find (fun (c : Char) => c == 'X') == "tea".toSlice.endPos

                                    • ("coffee tea water".toSlice.find "tea").get! == 't'

                                    Equations
                                    Instances For
                                      @[specialize #[5]]
                                      def String.Slice.contains {ρ : Type} {σ : Slice → Type} [(s : Slice) → Std.Iterator (σ s) Id (Pattern.SearchStep s)] [(s : Slice) → Std.IteratorLoop (σ s) Id Id] (s : Slice) (pat : ρ) [Pattern.ToForwardSearcher pat σ] :

                                      Checks whether a slice has a match of the pattern pat anywhere.

                                      This function is generic over all currently supported patterns.

                                      Examples:

                                      Equations
                                      • One or more equations did not get rendered due to their size.
                                      Instances For
                                        @[inline]
                                        def String.Slice.any {ρ : Type} {σ : Slice → Type} [(s : Slice) → Std.Iterator (σ s) Id (Pattern.SearchStep s)] [(s : Slice) → Std.IteratorLoop (σ s) Id Id] (s : Slice) (pat : ρ) [Pattern.ToForwardSearcher pat σ] :

                                        Checks whether a slice has a match of the pattern pat anywhere.

                                        This function is generic over all currently supported patterns.

                                        Examples:

                                        Equations
                                        Instances For
                                          @[inline]
                                          def String.Slice.all {ρ : Type} (s : Slice) (pat : ρ) [Pattern.ForwardPattern pat] :

                                          Checks whether a slice only consists of matches of the pattern pat.

                                          Short-circuits at the first pattern mis-match.

                                          This function is generic over all currently supported patterns.

                                          Examples:

                                          Equations
                                          Instances For
                                            @[inline]
                                            def String.Slice.endsWith {ρ : Type} (s : Slice) (pat : ρ) [Pattern.BackwardPattern pat] :

                                            Checks whether the slice (s) ends with the pattern (pat).

                                            This function is generic over all currently supported patterns.

                                            Examples:

                                            Equations
                                            Instances For
                                              inductive String.Slice.RevSplitIterator {σ : Slice → Type} {ρ : Type} (pat : ρ) (s : Slice) [Pattern.ToBackwardSearcher pat σ] :
                                              Instances For
                                                @[instance_reducible]
                                                instance String.Slice.instInhabitedRevSplitIterator {a✝ : Slice → Type} {a✝¹ : Type} {a✝² : a✝¹} {a✝³ : Slice} {a✝⁴ : Pattern.ToBackwardSearcher a✝² a✝} :
                                                Inhabited (RevSplitIterator a✝² a✝³)
                                                Equations
                                                @[instance_reducible]
                                                instance String.Slice.RevSplitIterator.instIteratorOfPure {ρ✝ : Type} {ρ : ρ✝} {σ : Slice → Type} [(s : Slice) → Std.Iterator (σ s) Id (Pattern.SearchStep s)] [Pattern.ToBackwardSearcher ρ σ] {m : Type → Type u_1} {s : Slice} [Pure m] :
                                                Equations
                                                • One or more equations did not get rendered due to their size.
                                                @[specialize #[3]]
                                                def String.Slice.revSplit {σ : Slice → Type} {ρ : Type} (s : Slice) (pat : ρ) [Pattern.ToBackwardSearcher pat σ] :

                                                Splits a slice at each subslice that matches the pattern pat, starting from the end of the slice and traversing towards the start.

                                                The subslices that matched the pattern are not included in any of the resulting subslices. If multiple subslices in a row match the pattern, the resulting list will contain empty slices.

                                                This function is generic over all currently supported patterns except String/String.Slice.

                                                Examples:

                                                • ("coffee tea water".toSlice.revSplit Char.isWhitespace).toList == ["water".toSlice, "tea".toSlice, "coffee".toSlice]

                                                • ("coffee tea water".toSlice.revSplit ' ').toList == ["water".toSlice, "tea".toSlice, "coffee".toSlice]

                                                Equations
                                                Instances For
                                                  @[inline]

                                                  If pat matches a suffix of s, returns the position at the beginning of the suffix. Returns none otherwise.

                                                  This function is generic over all currently supported patterns.

                                                  Equations
                                                  Instances For
                                                    @[inline]
                                                    def String.Slice.Pos.revSkip? {ρ : Type} {s : Slice} (pos : s.Pos) (pat : ρ) [Pattern.BackwardPattern pat] :

                                                    If pat matches a suffix at pos, returns the position at the beginning of the match. Returns none otherwise.

                                                    This function is generic over all currently supported patterns.

                                                    Equations
                                                    Instances For
                                                      @[inline]

                                                      If pat matches a suffix of s, returns the remainder. Returns none otherwise.

                                                      Use String.Slice.dropSuffix to return the slice unchanged when pat does not match a suffix.

                                                      This function is generic over all currently supported patterns.

                                                      Examples:

                                                      Equations
                                                      Instances For
                                                        @[specialize #[2]]

                                                        If pat matches a suffix of s, returns the remainder. Returns s unmodified otherwise.

                                                        Use String.Slice.dropSuffix? to return none when pat does not match a suffix.

                                                        This function is generic over all currently supported patterns.

                                                        Examples:

                                                        • "red green blue".toSlice.dropSuffix " blue" == "red green".toSlice

                                                        • "red green blue".toSlice.dropSuffix "bluu " == "red green blue".toSlice

                                                        • "red green blue".toSlice.dropSuffix 'e' == "red green blu".toSlice

                                                        • "red green blue".toSlice.dropSuffix Char.isLower == "red green blu".toSlice

                                                        Equations
                                                        Instances For
                                                          @[irreducible, specialize #[3]]
                                                          def String.Slice.Pos.revSkipWhile {ρ : Type} {s : Slice} (pos : s.Pos) (pat : ρ) [Pattern.BackwardPattern pat] :
                                                          s.Pos

                                                          Rewinds pos as long as pat matches.

                                                          Equations
                                                          Instances For
                                                            @[inline]

                                                            Returns the position at the start of the longest suffix of s for which pat matches (potentially repeatedly).

                                                            Equations
                                                            Instances For
                                                              @[inline]
                                                              def String.Slice.revAll {ρ : Type} (s : Slice) (pat : ρ) [Pattern.BackwardPattern pat] :

                                                              Checks whether a slice only consists of matches of the pattern pat, starting from the back of the string.

                                                              Short-circuits at the first pattern mis-match.

                                                              This function is generic over all currently supported patterns.

                                                              For many types of patterns, this function can be expected to return the same result as Slice.all. If mismatches are expected to occur close to the end of the string, this function might be more efficient.

                                                              For some types of patterns, this function will return a different result than Slice.all. Consider, for example, a pattern that matches the longest string at the given position that matches the regular expression "a|aa|ab". Then, given the input string "aab", performing Slice.all will greedily match the prefix "aa" and then get stuck on the remainder "b", causing it to return false. On the other hand, Slice.revAll will match the suffix "ab" and then match the remainder "a", so it will return true.

                                                              Examples:

                                                              Equations
                                                              Instances For
                                                                @[inline]

                                                                Creates a new slice by removing the longest suffix from s for which pat matched (potentially repeatedly).

                                                                Examples:

                                                                Equations
                                                                Instances For
                                                                  @[inline]

                                                                  Removes trailing whitespace from a slice by moving its end position to just after the last non-whitespace character, or to its start position if there is no non-whitespace character.

                                                                  “Whitespace” is defined as characters for which Char.isWhitespace returns true.

                                                                  Examples:

                                                                  Equations
                                                                  Instances For
                                                                    @[inline]

                                                                    Creates a new slice that contains the longest suffix of s for which pat matched (potentially repeatedly).

                                                                    This function is generic over all currently supported patterns.

                                                                    Examples:

                                                                    Equations
                                                                    Instances For
                                                                      @[specialize #[5]]
                                                                      def String.Slice.revFind? {σ : Slice → Type} [(s : Slice) → Std.Iterator (σ s) Id (Pattern.SearchStep s)] [(s : Slice) → Std.IteratorLoop (σ s) Id Id] {ρ : Type} (s : Slice) (pat : ρ) [Pattern.ToBackwardSearcher pat σ] :

                                                                      Finds the start position of the last match of the pattern pat in a slice, searching from the end of the slice towards the start. If there is no match none is returned.

                                                                      This function is generic over all currently supported patterns except String/String.Slice.

                                                                      Examples:

                                                                      • ("coffee tea water".toSlice.revFind? Char.isWhitespace).map (·.get!) == some ' '

                                                                      • "tea".toSlice.revFind? (fun (c : Char) => c == 'X') == none

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

                                                                        Removes leading and trailing whitespace from a slice.

                                                                        “Whitespace” is defined as characters for which Char.isWhitespace returns true.

                                                                        Examples:

                                                                        Equations
                                                                        Instances For
                                                                          Equations
                                                                          Instances For

                                                                            Creates an iterator over all lines in s with the line ending characters \r\n or \n being stripped.

                                                                            Examples:

                                                                            • "foo\r\nbar\n\nbaz\n".toSlice.lines.toList == ["foo".toSlice, "bar".toSlice, "".toSlice, "baz".toSlice]

                                                                            • "foo\r\nbar\n\nbaz".toSlice.lines.toList == ["foo".toSlice, "bar".toSlice, "".toSlice, "baz".toSlice]

                                                                            • "foo\r\nbar\n\nbaz\r".toSlice.lines.toList == ["foo".toSlice, "bar".toSlice, "".toSlice, "baz\r".toSlice]

                                                                            Equations
                                                                            Instances For