lean4-htt/tests/lean/run/string_replace.lean
Markus Himmel 167429501b
refactor: redefine String.replace (#10986)
This PR defines `String.Slice.replace` and redefines `String.replace` to
use the `Slice` version.

The new implementation is generic in the pattern, so it supports things
like `"education".replace isVowel "☃!" = "☃!d☃!c☃!t☃!☃!n"`. Since it
uses the `ForwardSearcher` infrastructure, `String` patterns are
searched using KMP, unlike the previous implementation which had
quadratic runtime. As a side effect, the behavior when replacing an
empty string now matches that of most other programming languages,
namely `"abc".replace "" "k" = "kakbkck"`.
2025-10-29 07:48:33 +00:00

21 lines
1 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

module
def isVowel (c : Char) : Bool :=
c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'
#guard "education".replace isVowel "☃!" = "☃!d☃!c☃!t☃!☃!n"
#guard "red green blue".replace "e" "" = "rd grn blu"
#guard "red green blue".replace 'e' "" = "rd grn blu"
#guard "red green blue".replace "ee" "E" = "red grEn blue"
#guard "red green blue".replace "e" "E" = "rEd grEEn bluE"
#guard "abc".replace "" "k" = "kakbkck"
#guard "".replace "" "z" = "zz"
#guard "𝔸".replace "" "z" = "z𝔸z"
#guard "v̂".replace "" "z" = "zvẑz"
#guard "aaaaa".replace "aa" "b" = "bba"
#guard "v̂fo".replace ("fo".toSlice.drop 1 |>.dropEnd 1) ("☃🔭🌌".toSlice.dropEnd 1 |>.drop 1) = "v̂🔭"
#guard ("abcde".toSlice.drop 1).replace (· == 'c') "C" = "bCde"
#guard (("ac bc cc cd".toSlice.split " ").map (·.replace 'c' "C") |>.toList) = ["aC", "bC", "CC", "Cd"]
#guard "red green blue".replace (fun c => c == 'u' || c == 'e') "" = "rd grn bl"
#guard "aab".replace "ab" "X" = "aX"
#guard " \n ".replace "\n" "\n" = " \n "