Add Str.replaceLast builtin

This commit is contained in:
Jan Van Bruggen 2022-09-12 00:42:38 -06:00 committed by Jan Van Bruggen
parent c5b6aef21a
commit 2b65659a11

View file

@ -34,6 +34,7 @@ interface Str
toI8,
toScalars,
replaceFirst,
replaceLast,
splitFirst,
splitLast,
walkUtf8WithIndex,
@ -288,6 +289,17 @@ replaceFirst = \haystack, needle, flower ->
"\(before)\(flower)\(after)"
err -> err
## Returns the string with the last occurrence of a substring replaced with a replacement.
## If the substring is not found, returns `Err NotFound`.
##
## Str.replaceLast "foo/bar/baz" "/" "_" == Ok "foo/bar_baz"
replaceLast : Str, Str, Str -> Result Str [NotFound]*
replaceLast = \haystack, needle, flower ->
when splitLast haystack needle is
Ok { before, after } ->
"\(before)\(flower)\(after)"
err -> err
## Returns the string before the first occurrence of a delimiter, as well as the
## rest of the string after that occurrence. If the delimiter is not found, returns `Err`.
##