| /- | |
| Copyright (c) 2025 Lean FRO, LLC. All rights reserved. | |
| Released under Apache 2.0 license as described in the file LICENSE. | |
| Authors: Markus Himmel | |
| -/ | |
| module | |
| prelude | |
| public import Init.Data.String.Slice.Pattern | |
| public import Init.Data.String.Slice.Numeric | |
| import Init.Data.Iterators.Consumers.Collect -- shake: keep (used in verso) | |
| set_option doc.verso true | |
| /-! | |
| # String operations based on slice operations | |
| This file contains {name}`String` operations that are implemented by passing to | |
| {name}`String.Slice`. | |
| -/ | |
| public section | |
| namespace String | |
| section | |
| open String.Slice Pattern | |
| variable {ρ : Type} {σ : Slice → Type} | |
| variable [∀ s, Std.Iterator (σ s) Id (SearchStep s)] | |
| variable [∀ s, Std.Iterators.Finite (σ s) Id] | |
| variable [∀ s, Std.IteratorLoop (σ s) Id Id] | |
| /-- | |
| Constructs a new string obtained by replacing all occurrences of {name}`pattern` with | |
| {name}`replacement` in {name}`s`. | |
| This function is generic over all currently supported patterns. The replacement may be a | |
| {name}`String` or a {name}`String.Slice`. | |
| Examples: | |
| * {lean}`"red green blue".replace 'e' "" = "rd grn blu"` | |
| * {lean}`"red green blue".replace (fun c => c == 'u' || c == 'e') "" = "rd grn bl"` | |
| * {lean}`"red green blue".replace "e" "" = "rd grn blu"` | |
| * {lean}`"red green blue".replace "ee" "E" = "red grEn blue"` | |
| * {lean}`"red green blue".replace "e" "E" = "rEd grEEn bluE"` | |
| * {lean}`"aaaaa".replace "aa" "b" = "bba"` | |
| * {lean}`"abc".replace "" "k" = "kakbkck"` | |
| -/ | |
| @[inline] | |
| def replace [ToSlice α] (s : String) (pattern : ρ) [ToForwardSearcher pattern σ] | |
| (replacement : α) : String := | |
| s.toSlice.replace pattern replacement | |
| /-- | |
| Finds the position of the first match of the pattern {name}`pattern` after the position | |
| {name}`pos`. If there is no match {name}`none` is returned. | |
| This function is generic over all currently supported patterns. | |
| Examples: | |
| * {lean}`("coffee tea water".toSlice.startPos.find? Char.isWhitespace).map (·.get!) == some ' '` | |
| * {lean}`("tea".toSlice.pos ⟨1⟩ (by decide)).find? (fun (c : Char) => c == 't') == none` | |
| -/ | |
| @[inline] | |
| def Slice.Pos.find? {s : Slice} (pos : s.Pos) (pattern : ρ) [ToForwardSearcher pattern σ] : | |
| Option s.Pos := | |
| ((s.sliceFrom pos).find? pattern).map ofSliceFrom | |
| /-- | |
| Finds the position of the first match of the pattern {name}`pattern` after the position | |
| {name}`pos`. If there is no match {lean}`s.endPos` is returned. | |
| This function is generic over all currently supported patterns. | |
| Examples: | |
| * {lean}`("coffee tea water".toSlice.startPos.find Char.isWhitespace).get! == ' '` | |
| * {lean}`("tea".toSlice.pos ⟨1⟩ (by decide)).find (fun (c : Char) => c == 't') == "tea".toSlice.endPos` | |
| -/ | |
| @[inline] | |
| def Slice.Pos.find {s : Slice} (pos : s.Pos) (pattern : ρ) [ToForwardSearcher pattern σ] : | |
| s.Pos := | |
| ofSliceFrom ((s.sliceFrom pos).find pattern) | |
| /-- | |
| Finds the position of the first match of the pattern {name}`pattern` after the position | |
| {name}`pos`. If there is no match {name}`none` is returned. | |
| This function is generic over all currently supported patterns. | |
| Examples: | |
| * {lean}`("coffee tea water".startPos.find? Char.isWhitespace).map (·.get!) == some ' '` | |
| * {lean}`("tea".pos ⟨1⟩ (by decide)).find? (fun (c : Char) => c == 't') == none` | |
| -/ | |
| @[inline] | |
| def Pos.find? {s : String} (pos : s.Pos) (pattern : ρ) | |
| [ToForwardSearcher pattern σ] : Option s.Pos := | |
| (pos.toSlice.find? pattern).map Pos.ofToSlice | |
| /-- | |
| Finds the position of the first match of the pattern {name}`pattern` after the position | |
| {name}`pos`. If there is no match {lean}`s.endPos` is returned. | |
| This function is generic over all currently supported patterns. | |
| Examples: | |
| * {lean}`("coffee tea water".startPos.find Char.isWhitespace).get! == ' '` | |
| * {lean}`("tea".pos ⟨1⟩ (by decide)).find (fun (c : Char) => c == 't') == "tea".endPos` | |
| -/ | |
| @[inline] | |
| def Pos.find {s : String} (pos : s.Pos) (pattern : ρ) [ToForwardSearcher pattern σ] : | |
| s.Pos := | |
| ofToSlice (pos.toSlice.find pattern) | |
| /-- | |
| Finds the position of the first match of the pattern {name}`pattern` in a string {name}`s`. If | |
| there is no match {name}`none` is returned. | |
| This function is generic over all currently supported patterns. | |
| Examples: | |
| * {lean}`("coffee tea water".find? Char.isWhitespace).map (·.get!) == some ' '` | |
| * {lean}`"tea".find? (fun (c : Char) => c == 'X') == none` | |
| * {lean}`("coffee tea water".find? "tea").map (·.get!) == some 't'` | |
| -/ | |
| @[inline] | |
| def find? (s : String) (pattern : ρ) [ToForwardSearcher pattern σ] : Option s.Pos := | |
| (s.toSlice.find? pattern).map Pos.ofToSlice | |
| /-- | |
| Finds the position of the first match of the pattern {name}`pattern` in a string {name}`s`. If there | |
| is no match {lean}`s.endPos` is returned. | |
| This function is generic over all currently supported patterns. | |
| Examples: | |
| * {lean}`("coffee tea water".find Char.isWhitespace).get! == ' '` | |
| * {lean}`"tea".find (fun (c : Char) => c == 'X') == "tea".endPos` | |
| * {lean}`("coffee tea water".find "tea").get! == 't'` | |
| -/ | |
| @[inline] | |
| def find (s : String) (pattern : ρ) [ToForwardSearcher pattern σ] : s.Pos := | |
| Pos.ofToSlice (s.toSlice.find pattern) | |
| /-- | |
| Finds the position of the last match of the pattern {name}`pattern` in a slice {name}`s` that is | |
| strictly before {name}`pos`. If there is no such match {lean}`none` is returned. | |
| This function is generic over all currently supported patterns except | |
| {name}`String`/{name}`String.Slice`. | |
| Examples: | |
| * {lean}`(("abc".toSlice.endPos.prev (by decide)).revFind? Char.isAlpha).map (·.get!) == some 'b'` | |
| * {lean}`"abc".toSlice.startPos.revFind? Char.isAlpha == none` | |
| -/ | |
| @[inline] | |
| def Slice.Pos.revFind? {s : Slice} (pos : s.Pos) (pattern : ρ) [ToBackwardSearcher pattern σ] : | |
| Option s.Pos := | |
| ((s.sliceTo pos).revFind? pattern).map ofSliceTo | |
| /-- | |
| Finds the position of the last match of the pattern {name}`pattern` in a string {name}`s` that is | |
| strictly before {name}`pos`. If there is no such match {lean}`none` is returned. | |
| This function is generic over all currently supported patterns except | |
| {name}`String`/{name}`String.Slice`. | |
| Examples: | |
| * {lean}`(("ab1c".endPos.prev (by decide)).revFind? Char.isAlpha).map (·.get!) == some 'b'` | |
| * {lean}`"abc".startPos.revFind? Char.isAlpha == none` | |
| -/ | |
| @[inline] | |
| def Pos.revFind? {s : String} (pos : s.Pos) (pattern : ρ) [ToBackwardSearcher pattern σ] : | |
| Option s.Pos := | |
| (pos.toSlice.revFind? pattern).map Pos.ofToSlice | |
| /-- | |
| Finds the position of the first match of the pattern {name}`pattern` in a string, starting | |
| from the end of the string and traversing towards the start. If there is no match {name}`none` is | |
| returned. | |
| This function is generic over all currently supported patterns except | |
| {name}`String`/{name}`String.Slice`. | |
| Examples: | |
| * {lean}`("coffee tea water".revFind? Char.isWhitespace).map (·.get!) == some ' '` | |
| * {lean}`"tea".revFind? (fun (c : Char) => c == 'X') == none` | |
| -/ | |
| @[inline] | |
| def revFind? (s : String) (pattern : ρ) [ToBackwardSearcher pattern σ] : Option s.Pos := | |
| (s.toSlice.revFind? pattern).map Pos.ofToSlice | |
| @[export lean_string_posof] | |
| def Internal.posOfImpl (s : String) (c : Char) : Pos.Raw := | |
| (s.find c).offset | |
| /-- | |
| Splits a string at each subslice that matches the pattern {name}`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: | |
| * {lean}`("coffee tea water".split Char.isWhitespace).toList == ["coffee".toSlice, "tea".toSlice, "water".toSlice]` | |
| * {lean}`("coffee tea water".split ' ').toList == ["coffee".toSlice, "tea".toSlice, "water".toSlice]` | |
| * {lean}`("coffee tea water".split " tea ").toList == ["coffee".toSlice, "water".toSlice]` | |
| * {lean}`("ababababa".split "aba").toList == ["".toSlice, "b".toSlice, "ba".toSlice]` | |
| * {lean}`("baaab".split "aa").toList == ["b".toSlice, "ab".toSlice]` | |
| -/ | |
| @[inline] | |
| def split (s : String) (pat : ρ) [ToForwardSearcher pat σ] := | |
| (s.toSlice.split pat : Std.Iter String.Slice) | |
| /-- | |
| Splits a string at each subslice that matches the pattern {name}`pat`. Unlike {name}`split` the | |
| matched subslices are included at the end of each subslice. | |
| This function is generic over all currently supported patterns. | |
| Examples: | |
| * {lean}`("coffee tea water".splitInclusive Char.isWhitespace).toList == ["coffee ".toSlice, "tea ".toSlice, "water".toSlice]` | |
| * {lean}`("coffee tea water".splitInclusive ' ').toList == ["coffee ".toSlice, "tea ".toSlice, "water".toSlice]` | |
| * {lean}`("coffee tea water".splitInclusive " tea ").toList == ["coffee tea ".toSlice, "water".toSlice]` | |
| * {lean}`("baaab".splitInclusive "aa").toList == ["baa".toSlice, "ab".toSlice]` | |
| -/ | |
| @[inline] | |
| def splitInclusive (s : String) (pat : ρ) [ToForwardSearcher pat σ] := | |
| (s.toSlice.splitInclusive pat : Std.Iter String.Slice) | |
| /-- | |
| Checks whether a string has a match of the pattern {name}`pat` anywhere. | |
| This function is generic over all currently supported patterns. | |
| Examples: | |
| * {lean}`"coffee tea water".contains Char.isWhitespace = true` | |
| * {lean}`"tea".contains (fun (c : Char) => c == 'X') = false` | |
| * {lean}`"coffee tea water".contains "tea" = true` | |
| -/ | |
| @[inline, suggest_for String.some] | |
| def contains (s : String) (pat : ρ) [ToForwardSearcher pat σ] : Bool := | |
| s.toSlice.contains pat | |
| @[export lean_string_contains] | |
| def Internal.containsImpl (s : String) (c : Char) : Bool := | |
| String.contains s c | |
| @[inline, inherit_doc contains] def any (s : String) (pat : ρ) [ToForwardSearcher pat σ] : Bool := | |
| s.contains pat | |
| @[export lean_string_any] | |
| def Internal.anyImpl (s : String) (p : Char → Bool) := | |
| String.any s p | |
| /-- | |
| Checks whether the string can be interpreted as the decimal representation of a natural number. | |
| A string can be interpreted as a decimal natural number if it is not empty and all the characters in | |
| it are digits. Underscores ({lit}`_`) are allowed as digit separators for readability, but cannot | |
| appear at the start, at the end, or consecutively. | |
| Use {name (scope := "Init.Data.String.Search")}`toNat?` or | |
| {name (scope := "Init.Data.String.Search")}`toNat!` to convert such a string to a natural number. | |
| Examples: | |
| * {lean}`"".isNat = false` | |
| * {lean}`"0".isNat = true` | |
| * {lean}`"5".isNat = true` | |
| * {lean}`"05".isNat = true` | |
| * {lean}`"587".isNat = true` | |
| * {lean}`"-587".isNat = false` | |
| * {lean}`" 5".isNat = false` | |
| * {lean}`"2+3".isNat = false` | |
| * {lean}`"0xff".isNat = false` | |
| -/ | |
| @[inline] def isNat (s : String) : Bool := | |
| s.toSlice.isNat | |
| /-- | |
| Interprets a string as the decimal representation of a natural number, returning it. Returns | |
| {name}`none` if the string does not contain a decimal natural number. | |
| A string can be interpreted as a decimal natural number if it is not empty and all the characters in | |
| it are digits. Underscores ({lit}`_`) are allowed as digit separators and are ignored during | |
| parsing. | |
| Use {name}`isNat` to check whether {name}`toNat?` would return {name}`some`. | |
| {name (scope := "Init.Data.String.Search")}`toNat!` is an alternative that panics instead of | |
| returning {name}`none` when the string is not a natural number. | |
| Examples: | |
| * {lean}`"".toNat? = none` | |
| * {lean}`"0".toNat? = some 0` | |
| * {lean}`"5".toNat? = some 5` | |
| * {lean}`"587".toNat? = some 587` | |
| * {lean}`"-587".toNat? = none` | |
| * {lean}`" 5".toNat? = none` | |
| * {lean}`"2+3".toNat? = none` | |
| * {lean}`"0xff".toNat? = none` | |
| -/ | |
| @[inline] def toNat? (s : String) : Option Nat := | |
| s.toSlice.toNat? | |
| /-- | |
| Interprets a string as the decimal representation of a natural number, returning it. Panics if the | |
| string does not contain a decimal natural number. | |
| A string can be interpreted as a decimal natural number if it is not empty and all the characters in | |
| it are digits. Underscores ({lit}`_`) are allowed as digit separators and are ignored during | |
| parsing. | |
| Use {name}`isNat` to check whether {name}`toNat!` would return a value. {name}`toNat?` is a safer | |
| alternative that returns {name}`none` instead of panicking when the string is not a natural number. | |
| Examples: | |
| * {lean}`"0".toNat! = 0` | |
| * {lean}`"5".toNat! = 5` | |
| * {lean}`"587".toNat! = 587` | |
| -/ | |
| @[inline] def toNat! (s : String) : Nat := | |
| s.toSlice.toNat! | |
| /-- | |
| Interprets a string as the decimal representation of an integer, returning it. Returns {lean}`none` | |
| if the string does not contain a decimal integer. | |
| A string can be interpreted as a decimal integer if it only consists of at least one decimal digit | |
| and optionally {lit}`-` in front. Leading `+` characters are not allowed. | |
| Underscores ({lit}`_`) are allowed as digit separators for readability, but cannot appear at the | |
| start, directly after the {lit}`-`, at the end, or consecutively. | |
| Use {name (scope := "Init.Data.String.Search")}`String.isInt` to check whether {name}`String.toInt?` | |
| would return {lean}`some`. {name (scope := "Init.Data.String.Search")}`String.toInt!` is an | |
| alternative that panics instead of returning {lean}`none` when the string is not an integer. | |
| Examples: | |
| * {lean}`"".toInt? = none` | |
| * {lean}`"-".toInt? = none` | |
| * {lean}`"0".toInt? = some 0` | |
| * {lean}`"5".toInt? = some 5` | |
| * {lean}`"-5".toInt? = some (-5)` | |
| * {lean}`"587".toInt? = some 587` | |
| * {lean}`"-587".toInt? = some (-587)` | |
| * {lean}`" 5".toInt? = none` | |
| * {lean}`"2-3".toInt? = none` | |
| * {lean}`"0xff".toInt? = none` | |
| -/ | |
| @[inline] def toInt? (s : String) : Option Int := | |
| s.toSlice.toInt? | |
| /-- | |
| Checks whether the string can be interpreted as the decimal representation of an integer. | |
| A string can be interpreted as a decimal integer if it only consists of at least one decimal digit | |
| and optionally {lit}`-` in front. Leading `+` characters are not allowed. | |
| Underscores ({lit}`_`) are allowed as digit separators for readability, but cannot appear at the | |
| start, directly after the {lit}`-`, at the end, or consecutively. | |
| Use {name}`String.toInt?` or {name (scope := "Init.Data.String.Search")}`String.toInt!` to convert | |
| such a string to an integer. | |
| Examples: | |
| * {lean}`"".isInt = false` | |
| * {lean}`"-".isInt = false` | |
| * {lean}`"0".isInt = true` | |
| * {lean}`"-0".isInt = true` | |
| * {lean}`"5".isInt = true` | |
| * {lean}`"587".isInt = true` | |
| * {lean}`"-587".isInt = true` | |
| * {lean}`"+587".isInt = false` | |
| * {lean}`" 5".isInt = false` | |
| * {lean}`"2-3".isInt = false` | |
| * {lean}`"0xff".isInt = false` | |
| -/ | |
| @[inline] def isInt (s : String) : Bool := | |
| s.toSlice.isInt | |
| /-- | |
| Interprets a string as the decimal representation of an integer, returning it. Panics if the string | |
| does not contain a decimal integer. | |
| A string can be interpreted as a decimal integer if it only consists of at least one decimal digit | |
| and optionally {lit}`-` in front. Leading `+` characters are not allowed. | |
| Underscores ({lit}`_`) are allowed as digit separators for readability, but cannot appear at the | |
| start, directly after the {lit}`-`, at the end, or consecutively. | |
| Use {name}`String.isInt` to check whether {name}`String.toInt!` would return a value. | |
| {name}`String.toInt?` is a safer alternative that returns {lean}`none` instead of panicking when the | |
| string is not an integer. | |
| Examples: | |
| * {lean}`"0".toInt! = 0` | |
| * {lean}`"5".toInt! = 5` | |
| * {lean}`"587".toInt! = 587` | |
| * {lean}`"-587".toInt! = -587` | |
| -/ | |
| @[inline] def toInt! (s : String) : Int := | |
| s.toSlice.toInt! | |
| /-- | |
| Returns the first character in {name}`s`. If {name}`s` is empty, returns {name}`none`. | |
| Examples: | |
| * {lean}`"abc".front? = some 'a'` | |
| * {lean}`"".front? = none` | |
| -/ | |
| @[inline] | |
| def front? (s : String) : Option Char := | |
| s.toSlice.front? | |
| /-- | |
| Returns the first character in {name}`s`. If {lean}`s = ""`, returns {lean}`(default : Char)`. | |
| Examples: | |
| * {lean}`"abc".front = 'a'` | |
| * {lean}`"".front = (default : Char)` | |
| -/ | |
| @[inline, expose] def front (s : String) : Char := | |
| s.toSlice.front | |
| @[export lean_string_front] | |
| def Internal.frontImpl (s : String) : Char := | |
| String.front s | |
| /-- | |
| Returns the last character in {name}`s`. If {name}`s` is empty, returns {name}`none`. | |
| Examples: | |
| * {lean}`"abc".back? = some 'c'` | |
| * {lean}`"".back? = none` | |
| -/ | |
| @[inline] | |
| def back? (s : String) : Option Char := | |
| s.toSlice.back? | |
| /-- | |
| Returns the last character in {name}`s`. If {lean}`s = ""`, returns {lean}`(default : Char)`. | |
| Examples: | |
| * {lean}`"abc".back = 'c'` | |
| * {lean}`"".back = (default : Char)` | |
| -/ | |
| @[inline, expose] def back (s : String) : Char := | |
| s.toSlice.back | |
| /-- | |
| Creates an iterator over all lines in {name}`s` with the line ending characters `\r\n` or `\n` being | |
| stripped. | |
| Examples: | |
| * {lean}`"foo\r\nbar\n\nbaz\n".lines.toList == ["foo".toSlice, "bar".toSlice, "".toSlice, "baz".toSlice]` | |
| * {lean}`"foo\r\nbar\n\nbaz".lines.toList == ["foo".toSlice, "bar".toSlice, "".toSlice, "baz".toSlice]` | |
| * {lean}`"foo\r\nbar\n\nbaz\r".lines.toList == ["foo".toSlice, "bar".toSlice, "".toSlice, "baz\r".toSlice]` | |
| -/ | |
| def lines (s : String) := | |
| s.toSlice.lines | |
| end | |
| end String |