Revamp sub/3 to resolve most issues with gsub (and sub with "g") (#2641)

The primary purpose of this commit is to rectify most problems with
`gsub` (and also `sub` with the `g` option), in particular fix #1425 ('\b'),
fix #2354 (lookahead), and fix #2532 (regex == `"^(?!cd ).*$|^cd "`).

This commit also partly resolves #2148 and resolves #1206 in that
`gsub` no longer loops infinitely; however, because the new `gsub`
depends critically on `match/2`, the behavior when regex == `""` is
sometimes non-standard.

The documentation has been updated to reflect the fact that `sub`
and `gsub` are intended to be regular in the second argument.

Also, `_nwise/1` has been tweaked to take advantage of TCO.
This commit is contained in:
pkoppstein authored and GitHub committed 2023-07-04 07:46:29 +09:00
1 parent edb0d88e3b
commit 83f375cc83
5 files changed
+113 -55

No files matched your search

+65
View File
@@ -75,6 +75,45 @@ gsub( "(.*)"; ""; "x")
""
""
gsub( ""; "a"; "g")
""
"a"
gsub( "^"; ""; "g")
"a"
"a"
# The following is a regression test and should not be construed as a requirement other than that execution should terminate:
gsub( ""; "a"; "g")
"a"
"aa"
gsub( "$"; "a"; "g")
"a"
"aa"
gsub( "^"; "a")
""
"a"
gsub("(?=u)"; "u")
"qux"
"quux"
gsub("^.*a"; "b")
"aaa"
"b"
gsub("^.*?a"; "b")
"aaa"
"baa"
# The following is for regression testing and should not be construed as a requirement:
[gsub("a"; "b", "c")]
"a"
["b","c"]
[.[] | scan(", ")]
["a,b, c, d, e,f",", a,b, c, d, e,f, "]
[", ",", ",", ",", ",", ",", ",", ",", "]
@@ -92,7 +131,33 @@ gsub("(?<x>.)[^a]*"; "+\(.x)-")
"Abcabc"
"+A-+a-"
gsub("(?<x>.)(?<y>[0-9])"; "\(.x|ascii_downcase)\(.y)")
"A1 B2 CD"
"a1 b2 CD"
gsub("\\b(?<x>.)"; "\(.x|ascii_downcase)")
"ABC DEF"
"aBC dEF"
# utf-8
sub("(?<x>.)"; "\(.x)!")
"’"
"’!"
[sub("a"; "b", "c")]
"a"
["b","c"]
[sub("(?<a>.)"; "\(.a|ascii_upcase)", "\(.a|ascii_downcase)", "c")]
"aB"
["AB","aB","cB"]
[gsub("(?<a>.)"; "\(.a|ascii_upcase)", "\(.a|ascii_downcase)", "c")]
"aB"
["AB","ab","cc"]
# splits and _nwise
[splits("")]
"ab"
["","a","b"]