diff --git a/base/deprecated.jl b/base/deprecated.jl index 11751f36ad6fb..e82234ff83d6c 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1344,14 +1344,19 @@ end function replace(s::AbstractString, pat, f, n::Integer) if n <= 0 depwarn(string("`replace(s, pat, r, count)` with `count <= 0` is deprecated, use ", - "`replace(s, pat, r, typemax(Int))` or `replace(s, pat, r)` instead"), + "`replace(s, pat=>r, count=typemax(Int))` or `replace(s, pat=>r)` instead"), :replace) - replace(s, pat, f) + replace(s, pat=>f) else - replace_new(String(s), pat, f, n) + depwarn(string("`replace(s, pat, r, count)` is deprecated, use ", + "`replace(s, pat=>r, count=count)`"), + :replace) + replace(String(s), pat=>f, count=n) end end +@deprecate replace(s::AbstractString, pat, f) replace(s, pat=>f) + # PR #22475 @deprecate ntuple(f, ::Type{Val{N}}) where {N} ntuple(f, Val(N)) @deprecate fill_to_length(t, val, ::Type{Val{N}}) where {N} fill_to_length(t, val, Val(N)) false diff --git a/base/libgit2/utils.jl b/base/libgit2/utils.jl index 1eacbd5e7d180..5d6568336b5dd 100644 --- a/base/libgit2/utils.jl +++ b/base/libgit2/utils.jl @@ -83,7 +83,7 @@ Standardise the path string `path` to use POSIX separators. """ function posixpath end if Sys.iswindows() - posixpath(path) = replace(path,'\\','/') + posixpath(path) = replace(path,'\\' => '/') elseif Sys.isunix() posixpath(path) = path end diff --git a/base/markdown/GitHub/table.jl b/base/markdown/GitHub/table.jl index 3b10945e37ae1..b449da258bfd3 100644 --- a/base/markdown/GitHub/table.jl +++ b/base/markdown/GitHub/table.jl @@ -11,7 +11,7 @@ function parserow(stream::IO) row = split(line, r"(? strip(replace(x, "\\|", "|")), row, row) + map!(x -> strip(replace(x, "\\|" => "|")), row, row) isempty(row[end]) && pop!(row) return row end @@ -104,7 +104,7 @@ _dash(width, align) = function plain(io::IO, md::Table) cells = mapmap(md.rows) do each - replace(plaininline(each), "|", "\\|") + replace(plaininline(each), "|" => "\\|") end padcells!(cells, md.align, len = length, min = 3) for i = axes(cells,1) diff --git a/base/markdown/render/html.jl b/base/markdown/render/html.jl index 2c350c666680d..a48180509400f 100644 --- a/base/markdown/render/html.jl +++ b/base/markdown/render/html.jl @@ -31,7 +31,7 @@ for ch in "'`!\$%()=+{}[]" end function htmlesc(io::IO, s::AbstractString) - # s1 = replace(s, r"&(?!(\w+|\#\d+);)", "&") + # s1 = replace(s, r"&(?!(\w+|\#\d+);)" => "&") for ch in s print(io, get(_htmlescape_chars, ch, ch)) end diff --git a/base/markdown/render/rst.jl b/base/markdown/render/rst.jl index 377aee7399926..d4bbaf199a9b5 100644 --- a/base/markdown/render/rst.jl +++ b/base/markdown/render/rst.jl @@ -124,7 +124,7 @@ end rstinline(io::IO, f::Footnote) = print(io, "[", f.id, "]_") -rstescape(s) = replace(s, "\\", "\\\\") +rstescape(s) = replace(s, "\\" => "\\\\") rstinline(io::IO, s::AbstractString) = print(io, rstescape(s)) diff --git a/base/markdown/render/terminal/formatting.jl b/base/markdown/render/terminal/formatting.jl index 36a4d390a7138..5055b80e11c12 100644 --- a/base/markdown/render/terminal/formatting.jl +++ b/base/markdown/render/terminal/formatting.jl @@ -51,7 +51,7 @@ end # Wrapping function ansi_length(s) - replace(s, r"\e\[[0-9]+m", "") |> length + replace(s, r"\e\[[0-9]+m" => "") |> length end words(s) = split(s, " ") diff --git a/base/markdown/render/terminal/render.jl b/base/markdown/render/terminal/render.jl index c8ba9455a0ec9..5f6fd32923fcd 100644 --- a/base/markdown/render/terminal/render.jl +++ b/base/markdown/render/terminal/render.jl @@ -114,7 +114,7 @@ function terminline(io::IO, content::Vector) end function terminline(io::IO, md::AbstractString) - print(io, replace(md, r"[\s\t\n]+", " ")) + print(io, replace(md, r"[\s\t\n]+" => " ")) end function terminline(io::IO, md::Bold) diff --git a/base/methodshow.jl b/base/methodshow.jl index 35bae1f1671a0..1430b86ef1d4b 100644 --- a/base/methodshow.jl +++ b/base/methodshow.jl @@ -203,7 +203,7 @@ function url(m::Method) file = string(m.file) line = m.line line <= 0 || ismatch(r"In\[[0-9]+\]", file) && return "" - Sys.iswindows() && (file = replace(file, '\\', '/')) + Sys.iswindows() && (file = replace(file, '\\' => '/')) if inbase(M) if isempty(Base.GIT_VERSION_INFO.commit) # this url will only work if we're on a tagged release diff --git a/base/pkg/reqs.jl b/base/pkg/reqs.jl index b84ce5614993d..b02c2aaa9231a 100644 --- a/base/pkg/reqs.jl +++ b/base/pkg/reqs.jl @@ -19,7 +19,7 @@ struct Requirement <: Line system::Vector{AbstractString} function Requirement(content::AbstractString) - fields = split(replace(content, r"#.*$", "")) + fields = split(replace(content, r"#.*$" => "")) system = AbstractString[] while !isempty(fields) && fields[1][1] == '@' push!(system,shift!(fields)[2:end]) diff --git a/base/process.jl b/base/process.jl index 2534b6d2e923d..3294f8a22ced2 100644 --- a/base/process.jl +++ b/base/process.jl @@ -107,7 +107,7 @@ function show(io::IO, cmd::Cmd) with_output_color(:underline, io) do io print_shell_word(io, arg, shell_special) end - end, '`', "\\`") + end, '`' => "\\`") end, ' ')) print(io, '`') print_env && (print(io, ","); show(io, cmd.env)) diff --git a/base/repl/LineEdit.jl b/base/repl/LineEdit.jl index e0b443634dbe5..19c75910c3ef6 100644 --- a/base/repl/LineEdit.jl +++ b/base/repl/LineEdit.jl @@ -1883,12 +1883,12 @@ function bracketed_paste(s; tabwidth=options(s).tabwidth) ps = state(s, mode(s)) str = readuntil(ps.terminal, "\e[201~") input = str[1:prevind(str, end-5)] - input = replace(input, '\r', '\n') + input = replace(input, '\r' => '\n') if position(buffer(s)) == 0 indent = Base.indentation(input; tabwidth=tabwidth)[1] input = Base.unindent(input, indent; tabwidth=tabwidth) end - return replace(input, '\t', " "^tabwidth) + return replace(input, '\t' => " "^tabwidth) end function tab_should_complete(s) diff --git a/base/repl/REPL.jl b/base/repl/REPL.jl index a3175eb35d901..838c45e95f24b 100644 --- a/base/repl/REPL.jl +++ b/base/repl/REPL.jl @@ -450,7 +450,7 @@ function add_history(hist::REPLHistoryProvider, s) entry = """ # time: $(Libc.strftime("%Y-%m-%d %H:%M:%S %Z", time())) # mode: $mode - $(replace(str, r"^"ms, "\t")) + $(replace(str, r"^"ms => "\t")) """ # TODO: write-lock history file seekend(hist.history_file) @@ -946,7 +946,7 @@ function setup_interface( tail = lstrip(tail) end if isprompt_paste # remove indentation spaces corresponding to the prompt - tail = replace(tail, r"^ {7}"m, "") # 7: jl_prompt_len + tail = replace(tail, r"^ {7}"m => "") # 7: jl_prompt_len end LineEdit.replace_line(s, tail, true) LineEdit.refresh_line(s) @@ -956,7 +956,7 @@ function setup_interface( line = strip(input[oldpos:prevind(input, pos)]) if !isempty(line) if isprompt_paste # remove indentation spaces corresponding to the prompt - line = replace(line, r"^ {7}"m, "") # 7: jl_prompt_len + line = replace(line, r"^ {7}"m => "") # 7: jl_prompt_len end # put the line on the screen and history LineEdit.replace_line(s, line) diff --git a/base/repl/REPLCompletions.jl b/base/repl/REPLCompletions.jl index 345c5a9a8c582..542b66804dd27 100644 --- a/base/repl/REPLCompletions.jl +++ b/base/repl/REPLCompletions.jl @@ -192,7 +192,7 @@ function complete_path(path::AbstractString, pos; use_envpath=false) end end - matchList = String[replace(s, r"\s", "\\ ") for s in matches] + matchList = String[replace(s, r"\s" => "\\ ") for s in matches] startpos = pos - endof(prefix) + 1 - length(matchall(r" ", prefix)) # The pos - endof(prefix) + 1 is correct due to `endof(prefix)-endof(prefix)==0`, # hence we need to add one to get the first index. This is also correct when considering @@ -503,15 +503,15 @@ function completions(string, pos) startpos = nextind(partial, reverseind(partial, m.offset)) r = startpos:pos - expanded = complete_expanduser(replace(string[r], r"\\ ", " "), r) + expanded = complete_expanduser(replace(string[r], r"\\ " => " "), r) expanded[3] && return expanded # If user expansion available, return it - paths, r, success = complete_path(replace(string[r], r"\\ ", " "), pos) + paths, r, success = complete_path(replace(string[r], r"\\ " => " "), pos) if inc_tag == :string && length(paths) == 1 && # Only close if there's a single choice, !isdir(expanduser(replace(string[startpos:prevind(string, start(r))] * paths[1], - r"\\ ", " "))) && # except if it's a directory + r"\\ " => " "))) && # except if it's a directory (length(string) <= pos || string[nextind(string,pos)] != '"') # or there's already a " at the cursor. paths[1] *= "\"" diff --git a/base/repl/latex_symbols.jl b/base/repl/latex_symbols.jl index e268b389a644b..a4792522a8e7a 100644 --- a/base/repl/latex_symbols.jl +++ b/base/repl/latex_symbols.jl @@ -53,7 +53,7 @@ const latex_strings = Set(values(Base.REPLCompletions.latex_symbols)) open(fname) do f for L in eachline(f) x = map(s -> rstrip(s, [' ','\t','\n']), - split(replace(L, r"[{}\"]+", "\t"), "\t")) + split(replace(L, r"[{}\"]+" => "\t"), "\t")) c = Char(parse(Int, x[2], 16)) if (Base.is_id_char(c) || Base.isoperator(Symbol(c))) && string(c) ∉ latex_strings && !isascii(c) diff --git a/base/shell.jl b/base/shell.jl index 0ab8a09f80083..bd362bcb40a93 100644 --- a/base/shell.jl +++ b/base/shell.jl @@ -222,11 +222,11 @@ function print_shell_escaped_posixly(io::IO, args::AbstractString...) return true end if all(isword, arg) - have_single && (arg = replace(arg, '\'', "\\'")) - have_double && (arg = replace(arg, '"', "\\\"")) + have_single && (arg = replace(arg, '\'' => "\\'")) + have_double && (arg = replace(arg, '"' => "\\\"")) print(io, arg) else - print(io, '\'', replace(arg, '\'', "'\\''"), '\'') + print(io, '\'', replace(arg, '\'' => "'\\''"), '\'') end first = false end diff --git a/base/strings/util.jl b/base/strings/util.jl index c709f94ff46b3..9cb8898d465df 100644 --- a/base/strings/util.jl +++ b/base/strings/util.jl @@ -373,8 +373,8 @@ _replace(io, repl, str, r, pattern) = print(io, repl) _replace(io, repl::Function, str, r, pattern) = print(io, repl(SubString(str, first(r), last(r)))) -# TODO: rename to `replace` when `replace` is removed from deprecated.jl -function replace_new(str::String, pattern, repl, count::Integer) +function replace(str::String, pat_repl::Pair; count::Integer=typemax(Int)) + pattern, repl = pat_repl count == 0 && return str count < 0 && throw(DomainError(count, "`count` must be non-negative.")) n = 1 @@ -407,11 +407,11 @@ function replace_new(str::String, pattern, repl, count::Integer) end """ - replace(s::AbstractString, pat, r, [count::Integer]) + replace(s::AbstractString, pat=>r; [count::Integer]) Search for the given pattern `pat` in `s`, and replace each occurrence with `r`. If `count` is provided, replace at most `count` occurrences. -As with [`search`](@ref), the second argument may be a +As with [`search`](@ref), `pat` may be a single character, a vector or a set of characters, a string, or a regular expression. If `r` is a function, each occurrence is replaced with `r(s)` where `s` is the matched substring. If `pat` is a regular expression and `r` is a `SubstitutionString`, then capture group @@ -420,20 +420,18 @@ To remove instances of `pat` from `string`, set `r` to the empty `String` (`""`) # Examples ```jldoctest -julia> replace("Python is a programming language.", "Python", "Julia") +julia> replace("Python is a programming language.", "Python" => "Julia") "Julia is a programming language." -julia> replace("The quick foxes run quickly.", "quick", "slow", 1) +julia> replace("The quick foxes run quickly.", "quick" => "slow", count=1) "The slow foxes run quickly." -julia> replace("The quick foxes run quickly.", "quick", "", 1) +julia> replace("The quick foxes run quickly.", "quick" => "", count=1) "The foxes run quickly." ``` """ -replace(s::AbstractString, pat, f) = replace_new(String(s), pat, f, typemax(Int)) -# TODO: change this to the following when `replace` is removed from deprecated.jl: -# replace(s::AbstractString, pat, f, count::Integer=typemax(Int)) = -# replace(String(s), pat, f, count) +replace(s::AbstractString, pat_f::Pair; count=typemax(Int)) = + replace(String(s), pat_f, count=count) # TODO: allow transform as the first argument to replace? diff --git a/doc/src/stdlib/strings.md b/doc/src/stdlib/strings.md index 04892e3b87e70..74ab2ca518f22 100644 --- a/doc/src/stdlib/strings.md +++ b/doc/src/stdlib/strings.md @@ -38,7 +38,7 @@ Base.searchindex Base.rsearchindex Base.contains(::AbstractString, ::AbstractString) Base.reverse(::Union{String,SubString{String}}) -Base.replace(s::AbstractString, pat, f) +Base.replace(s::AbstractString, ::Pair) Base.split Base.rsplit Base.strip diff --git a/test/libgit2.jl b/test/libgit2.jl index 1235cb6d4c88f..83522c4a4cad1 100644 --- a/test/libgit2.jl +++ b/test/libgit2.jl @@ -2550,7 +2550,7 @@ mktempdir() do dir try # In some environments, namely Macs, the hostname "macbook.local" is bound # to the external address while "macbook" is bound to the loopback address. - unshift!(hostnames, replace(gethostname(), r"\..*$", "")) + unshift!(hostnames, replace(gethostname(), r"\..*$" => "")) end loopback = ip"127.0.0.1" diff --git a/test/logging.jl b/test/logging.jl index fab495d1a86d3..02c10cbfa6f67 100644 --- a/test/logging.jl +++ b/test/logging.jl @@ -241,7 +241,7 @@ end s = String(take!(io)) # Remove the small amount of color, as `Base.print_with_color` can't be # simply controlled. - s = replace(s, r"^\e\[1m\e\[..m(.*)\e\[39m\e\[22m"m, s"\1") + s = replace(s, r"^\e\[1m\e\[..m(.*)\e\[39m\e\[22m"m => s"\1") # println(s) s end diff --git a/test/operators.jl b/test/operators.jl index 9a883c588d43e..ca020d1910cf4 100644 --- a/test/operators.jl +++ b/test/operators.jl @@ -110,8 +110,8 @@ Base.promote_rule(::Type{T19714}, ::Type{Int}) = T19714 end @testset "function negation" begin str = randstring(20) - @test filter(!Base.Unicode.isupper, str) == replace(str, r"[A-Z]", "") - @test filter(!Base.Unicode.islower, str) == replace(str, r"[a-z]", "") + @test filter(!Base.Unicode.isupper, str) == replace(str, r"[A-Z]" => "") + @test filter(!Base.Unicode.islower, str) == replace(str, r"[a-z]" => "") end # issue #19891 diff --git a/test/perf/shootout/regex_dna.jl b/test/perf/shootout/regex_dna.jl index 75643153768e9..5a8863040e529 100644 --- a/test/perf/shootout/regex_dna.jl +++ b/test/perf/shootout/regex_dna.jl @@ -36,7 +36,7 @@ function regex_dna(infile="regexdna-input.txt") seq = read(infile, String) l1 = length(seq) - seq = replace(seq, r">.*\n|\n", "") + seq = replace(seq, r">.*\n|\n" => "") l2 = length(seq) for v in variants @@ -48,7 +48,7 @@ function regex_dna(infile="regexdna-input.txt") end for (u, v) in subs - seq = replace(seq, u, v) + seq = replace(seq, u => v) end # println() @@ -56,4 +56,3 @@ function regex_dna(infile="regexdna-input.txt") # println(l2) # println(length(seq)) end - diff --git a/test/regex.jl b/test/regex.jl index 3d9837f1c2486..f8e7fd37d6827 100644 --- a/test/regex.jl +++ b/test/regex.jl @@ -45,7 +45,7 @@ let m = match(r"(?.)(.)(?.)", "xyz") end # Backcapture reference in substitution string -@test replace("abcde", r"(..)(?Pd)", s"\gxy\\\1") == "adxy\\bce" +@test replace("abcde", r"(..)(?Pd)" => s"\gxy\\\1") == "adxy\\bce" @test_throws ErrorException replace("a", r"(?P)", s"\g") # Proper unicode handling diff --git a/test/replcompletions.jl b/test/replcompletions.jl index a941a417aeff2..497baa63a096d 100644 --- a/test/replcompletions.jl +++ b/test/replcompletions.jl @@ -704,7 +704,7 @@ end let path = tempdir(), space_folder = randstring() * " α", dir = joinpath(path, space_folder), - dir_space = replace(space_folder, " ", "\\ ") + dir_space = replace(space_folder, " " => "\\ ") mkdir(dir) cd(path) do diff --git a/test/show.jl b/test/show.jl index b98d113fd96ce..3838f33e02621 100644 --- a/test/show.jl +++ b/test/show.jl @@ -302,7 +302,7 @@ end # issue #7921 @test replace(sprint(show, Expr(:function, :(==(a, b)), Expr(:block,:(return a == b)))), - r"\s+", " ") == ":(function ==(a, b) return a == b end)" + r"\s+" => " ") == ":(function ==(a, b) return a == b end)" # unicode operator printing @test sprint(show, :(1 ⊕ (2 ⊗ 3))) == ":(1 ⊕ 2 ⊗ 3)" @@ -581,7 +581,7 @@ let ex, @test string(l2) == "#= myfile:42 =#" @test string(l1) == string(l2n) ex = Expr(:block, l1, :x, l2, :y, l2n, :z) - @test replace(string(ex)," ","") == replace(""" + @test replace(string(ex)," " => "") == replace(""" begin #= line 42 =# x @@ -589,7 +589,7 @@ let ex, y #= line 42 =# z - end""", " ", "") + end""", " " => "") end # Test the printing of whatever form of line number representation # that is used in the arguments to a macro looks the same as for diff --git a/test/strings/util.jl b/test/strings/util.jl index f2d829357a637..97a417fe86e8c 100644 --- a/test/strings/util.jl +++ b/test/strings/util.jl @@ -153,110 +153,110 @@ end end @testset "replace" begin - @test replace("\u2202", '*', '\0') == "\u2202" - - @test replace("foobar", 'o', '0') == "f00bar" - @test replace("foobar", 'o', '0', 1) == "f0obar" - @test replace("foobar", 'o', "") == "fbar" - @test replace("foobar", 'o', "", 1) == "fobar" - @test replace("foobar", 'f', 'F') == "Foobar" - @test replace("foobar", 'r', 'R') == "foobaR" - - @test replace("foofoofoo", "foo", "bar") == "barbarbar" - @test replace("foobarfoo", "foo", "baz") == "bazbarbaz" - @test replace("barfoofoo", "foo", "baz") == "barbazbaz" - - @test replace("", "", "") == "" - @test replace("", "", "x") == "x" - @test replace("", "x", "y") == "" - - @test replace("abcd", "", "^") == "^a^b^c^d^" - @test replace("abcd", "b", "^") == "a^cd" - @test replace("abcd", r"b?", "^") == "^a^c^d^" - @test replace("abcd", r"b+", "^") == "a^cd" - @test replace("abcd", r"b?c?", "^") == "^a^d^" - @test replace("abcd", r"[bc]?", "^") == "^a^^d^" - - @test replace("foobarfoo", r"(fo|ba)", "xx") == "xxoxxrxxo" - @test replace("foobarfoo", r"(foo|ba)", "bar") == "barbarrbar" - - @test replace("foobar", 'o', 'ø') == "føøbar" - @test replace("foobar", 'o', 'ø', 1) == "føobar" - @test replace("føøbar", 'ø', 'o') == "foobar" - @test replace("føøbar", 'ø', 'o', 1) == "foøbar" - @test replace("føøbar", 'ø', 'ö') == "fööbar" - @test replace("føøbar", 'ø', 'ö', 1) == "föøbar" - @test replace("føøbar", 'ø', "") == "fbar" - @test replace("føøbar", 'ø', "", 1) == "føbar" - @test replace("føøbar", 'f', 'F') == "Føøbar" - @test replace("ḟøøbar", 'ḟ', 'F') == "Føøbar" - @test replace("føøbar", 'f', 'Ḟ') == "Ḟøøbar" - @test replace("ḟøøbar", 'ḟ', 'Ḟ') == "Ḟøøbar" - @test replace("føøbar", 'r', 'R') == "føøbaR" - @test replace("føøbaṙ", 'ṙ', 'R') == "føøbaR" - @test replace("føøbar", 'r', 'Ṙ') == "føøbaṘ" - @test replace("føøbaṙ", 'ṙ', 'Ṙ') == "føøbaṘ" - - @test replace("ḟøøḟøøḟøø", "ḟøø", "bar") == "barbarbar" - @test replace("ḟøøbarḟøø", "ḟøø", "baz") == "bazbarbaz" - @test replace("barḟøøḟøø", "ḟøø", "baz") == "barbazbaz" - - @test replace("foofoofoo", "foo", "ƀäṙ") == "ƀäṙƀäṙƀäṙ" - @test replace("fooƀäṙfoo", "foo", "baz") == "bazƀäṙbaz" - @test replace("ƀäṙfoofoo", "foo", "baz") == "ƀäṙbazbaz" - - @test replace("foofoofoo", "foo", "bar") == "barbarbar" - @test replace("foobarfoo", "foo", "ƀäż") == "ƀäżbarƀäż" - @test replace("barfoofoo", "foo", "ƀäż") == "barƀäżƀäż" - - @test replace("ḟøøḟøøḟøø", "ḟøø", "ƀäṙ") == "ƀäṙƀäṙƀäṙ" - @test replace("ḟøøƀäṙḟøø", "ḟøø", "baz") == "bazƀäṙbaz" - @test replace("ƀäṙḟøøḟøø", "ḟøø", "baz") == "ƀäṙbazbaz" - - @test replace("ḟøøḟøøḟøø", "ḟøø", "bar") == "barbarbar" - @test replace("ḟøøbarḟøø", "ḟøø", "ƀäż") == "ƀäżbarƀäż" - @test replace("barḟøøḟøø", "ḟøø", "ƀäż") == "barƀäżƀäż" - - @test replace("ḟøøḟøøḟøø", "ḟøø", "ƀäṙ") == "ƀäṙƀäṙƀäṙ" - @test replace("ḟøøƀäṙḟøø", "ḟøø", "ƀäż") == "ƀäżƀäṙƀäż" - @test replace("ƀäṙḟøøḟøø", "ḟøø", "ƀäż") == "ƀäṙƀäżƀäż" - - @test replace("", "", "ẍ") == "ẍ" - @test replace("", "ẍ", "ÿ") == "" - - @test replace("äƀçđ", "", "π") == "πäπƀπçπđπ" - @test replace("äƀçđ", "ƀ", "π") == "äπçđ" - @test replace("äƀçđ", r"ƀ?", "π") == "πäπçπđπ" - @test replace("äƀçđ", r"ƀ+", "π") == "äπçđ" - @test replace("äƀçđ", r"ƀ?ç?", "π") == "πäπđπ" - @test replace("äƀçđ", r"[ƀç]?", "π") == "πäππđπ" - - @test replace("foobarfoo", r"(fo|ba)", "ẍẍ") == "ẍẍoẍẍrẍẍo" - - @test replace("ḟøøbarḟøø", r"(ḟø|ba)", "xx") == "xxøxxrxxø" - @test replace("ḟøøbarḟøø", r"(ḟøø|ba)", "bar") == "barbarrbar" - - @test replace("fooƀäṙfoo", r"(fo|ƀä)", "xx") == "xxoxxṙxxo" - @test replace("fooƀäṙfoo", r"(foo|ƀä)", "ƀäṙ") == "ƀäṙƀäṙṙƀäṙ" - - @test replace("ḟøøƀäṙḟøø", r"(ḟø|ƀä)", "xx") == "xxøxxṙxxø" - @test replace("ḟøøƀäṙḟøø", r"(ḟøø|ƀä)", "ƀäṙ") == "ƀäṙƀäṙṙƀäṙ" - - @test replace("foo", "oo", Base.Unicode.uppercase) == "fOO" + @test replace("\u2202", '*' => '\0') == "\u2202" + + @test replace("foobar", 'o' => '0') == "f00bar" + @test replace("foobar", 'o' => '0', count=1) == "f0obar" + @test replace("foobar", 'o' => "") == "fbar" + @test replace("foobar", 'o' => "", count=1) == "fobar" + @test replace("foobar", 'f' => 'F') == "Foobar" + @test replace("foobar", 'r' => 'R') == "foobaR" + + @test replace("foofoofoo", "foo" => "bar") == "barbarbar" + @test replace("foobarfoo", "foo" => "baz") == "bazbarbaz" + @test replace("barfoofoo", "foo" => "baz") == "barbazbaz" + + @test replace("", "" => "") == "" + @test replace("", "" => "x") == "x" + @test replace("", "x" => "y") == "" + + @test replace("abcd", "" => "^") == "^a^b^c^d^" + @test replace("abcd", "b" => "^") == "a^cd" + @test replace("abcd", r"b?" => "^") == "^a^c^d^" + @test replace("abcd", r"b+" => "^") == "a^cd" + @test replace("abcd", r"b?c?" => "^") == "^a^d^" + @test replace("abcd", r"[bc]?" => "^") == "^a^^d^" + + @test replace("foobarfoo", r"(fo|ba)" => "xx") == "xxoxxrxxo" + @test replace("foobarfoo", r"(foo|ba)" => "bar") == "barbarrbar" + + @test replace("foobar", 'o' => 'ø') == "føøbar" + @test replace("foobar", 'o' => 'ø', count=1) == "føobar" + @test replace("føøbar", 'ø' => 'o') == "foobar" + @test replace("føøbar", 'ø' => 'o', count=1) == "foøbar" + @test replace("føøbar", 'ø' => 'ö') == "fööbar" + @test replace("føøbar", 'ø' => 'ö', count=1) == "föøbar" + @test replace("føøbar", 'ø' => "") == "fbar" + @test replace("føøbar", 'ø' => "", count=1) == "føbar" + @test replace("føøbar", 'f' => 'F') == "Føøbar" + @test replace("ḟøøbar", 'ḟ' => 'F') == "Føøbar" + @test replace("føøbar", 'f' => 'Ḟ') == "Ḟøøbar" + @test replace("ḟøøbar", 'ḟ' => 'Ḟ') == "Ḟøøbar" + @test replace("føøbar", 'r' => 'R') == "føøbaR" + @test replace("føøbaṙ", 'ṙ' => 'R') == "føøbaR" + @test replace("føøbar", 'r' => 'Ṙ') == "føøbaṘ" + @test replace("føøbaṙ", 'ṙ' => 'Ṙ') == "føøbaṘ" + + @test replace("ḟøøḟøøḟøø", "ḟøø" => "bar") == "barbarbar" + @test replace("ḟøøbarḟøø", "ḟøø" => "baz") == "bazbarbaz" + @test replace("barḟøøḟøø", "ḟøø" => "baz") == "barbazbaz" + + @test replace("foofoofoo", "foo" => "ƀäṙ") == "ƀäṙƀäṙƀäṙ" + @test replace("fooƀäṙfoo", "foo" => "baz") == "bazƀäṙbaz" + @test replace("ƀäṙfoofoo", "foo" => "baz") == "ƀäṙbazbaz" + + @test replace("foofoofoo", "foo" => "bar") == "barbarbar" + @test replace("foobarfoo", "foo" => "ƀäż") == "ƀäżbarƀäż" + @test replace("barfoofoo", "foo" => "ƀäż") == "barƀäżƀäż" + + @test replace("ḟøøḟøøḟøø", "ḟøø" => "ƀäṙ") == "ƀäṙƀäṙƀäṙ" + @test replace("ḟøøƀäṙḟøø", "ḟøø" => "baz") == "bazƀäṙbaz" + @test replace("ƀäṙḟøøḟøø", "ḟøø" => "baz") == "ƀäṙbazbaz" + + @test replace("ḟøøḟøøḟøø", "ḟøø" => "bar") == "barbarbar" + @test replace("ḟøøbarḟøø", "ḟøø" => "ƀäż") == "ƀäżbarƀäż" + @test replace("barḟøøḟøø", "ḟøø" => "ƀäż") == "barƀäżƀäż" + + @test replace("ḟøøḟøøḟøø", "ḟøø" => "ƀäṙ") == "ƀäṙƀäṙƀäṙ" + @test replace("ḟøøƀäṙḟøø", "ḟøø" => "ƀäż") == "ƀäżƀäṙƀäż" + @test replace("ƀäṙḟøøḟøø", "ḟøø" => "ƀäż") == "ƀäṙƀäżƀäż" + + @test replace("", "" => "ẍ") == "ẍ" + @test replace("", "ẍ" => "ÿ") == "" + + @test replace("äƀçđ", "" => "π") == "πäπƀπçπđπ" + @test replace("äƀçđ", "ƀ" => "π") == "äπçđ" + @test replace("äƀçđ", r"ƀ?" => "π") == "πäπçπđπ" + @test replace("äƀçđ", r"ƀ+" => "π") == "äπçđ" + @test replace("äƀçđ", r"ƀ?ç?" => "π") == "πäπđπ" + @test replace("äƀçđ", r"[ƀç]?" => "π") == "πäππđπ" + + @test replace("foobarfoo", r"(fo|ba)" => "ẍẍ") == "ẍẍoẍẍrẍẍo" + + @test replace("ḟøøbarḟøø", r"(ḟø|ba)" => "xx") == "xxøxxrxxø" + @test replace("ḟøøbarḟøø", r"(ḟøø|ba)" => "bar") == "barbarrbar" + + @test replace("fooƀäṙfoo", r"(fo|ƀä)" => "xx") == "xxoxxṙxxo" + @test replace("fooƀäṙfoo", r"(foo|ƀä)" => "ƀäṙ") == "ƀäṙƀäṙṙƀäṙ" + + @test replace("ḟøøƀäṙḟøø", r"(ḟø|ƀä)" => "xx") == "xxøxxṙxxø" + @test replace("ḟøøƀäṙḟøø", r"(ḟøø|ƀä)" => "ƀäṙ") == "ƀäṙƀäṙṙƀäṙ" + + @test replace("foo", "oo" => Base.Unicode.uppercase) == "fOO" # Issue 13332 - @test replace("abc", 'b', 2.1) == "a2.1c" + @test replace("abc", 'b' => 2.1) == "a2.1c" # test replace with a count for String and GenericString # check that replace is a no-op if count==0 for s in ["aaa", Test.GenericString("aaa")] - # @test replace("aaa", 'a', 'z', 0) == "aaa" # enable when undeprecated - @test replace(s, 'a', 'z', 1) == "zaa" - @test replace(s, 'a', 'z', 2) == "zza" - @test replace(s, 'a', 'z', 3) == "zzz" - @test replace(s, 'a', 'z', 4) == "zzz" - @test replace(s, 'a', 'z', typemax(Int)) == "zzz" - @test replace(s, 'a', 'z') == "zzz" + # @test replace("aaa", 'a' => 'z', count=0) == "aaa" # enable when undeprecated + @test replace(s, 'a' => 'z', count=1) == "zaa" + @test replace(s, 'a' => 'z', count=2) == "zza" + @test replace(s, 'a' => 'z', count=3) == "zzz" + @test replace(s, 'a' => 'z', count=4) == "zzz" + @test replace(s, 'a' => 'z', count=typemax(Int)) == "zzz" + @test replace(s, 'a' => 'z') == "zzz" end end diff --git a/test/testdefs.jl b/test/testdefs.jl index 3191d553cb193..c6af76df2b8c8 100644 --- a/test/testdefs.jl +++ b/test/testdefs.jl @@ -9,7 +9,7 @@ function runtests(name, path, isolate=true; seed=nothing) if isolate # Simple enough to type and random enough so that no one will hard # code it in the test - mod_name = Symbol("Test", rand(1:100), "Main_", replace(name, '/', '_')) + mod_name = Symbol("Test", rand(1:100), "Main_", replace(name, '/' => '_')) m = @eval(Main, module $mod_name end) else m = Main