From 26fd3c82408230995d8d493830b9137a013400bf Mon Sep 17 00:00:00 2001 From: Jameson Nash Date: Fri, 29 Jan 2021 13:34:10 -0500 Subject: [PATCH] render Regex and SubstitionString correctly for repr (#39422) Fixes #29580 --- base/regex.jl | 10 ++++++---- base/strings/io.jl | 10 ---------- test/regex.jl | 20 ++++++++++---------- 3 files changed, 16 insertions(+), 24 deletions(-) diff --git a/base/regex.jl b/base/regex.jl index 9d5b9e169b66d..ca285de4a1f6d 100644 --- a/base/regex.jl +++ b/base/regex.jl @@ -119,8 +119,9 @@ function show(io::IO, re::Regex) imsxa = PCRE.CASELESS|PCRE.MULTILINE|PCRE.DOTALL|PCRE.EXTENDED|PCRE.UCP opts = re.compile_options if (opts & ~imsxa) == (DEFAULT_COMPILER_OPTS & ~imsxa) - print(io, 'r') - print_quoted_literal(io, re.pattern) + print(io, "r\"") + escape_raw_string(io, re.pattern) + print(io, "\"") if (opts & PCRE.CASELESS ) != 0; print(io, 'i'); end if (opts & PCRE.MULTILINE) != 0; print(io, 'm'); end if (opts & PCRE.DOTALL ) != 0; print(io, 's'); end @@ -485,8 +486,9 @@ isvalid(s::SubstitutionString, i::Integer) = isvalid(s.string, i)::Bool iterate(s::SubstitutionString, i::Integer...) = iterate(s.string, i...)::Union{Nothing,Tuple{AbstractChar,Int}} function show(io::IO, s::SubstitutionString) - print(io, "s") - print_quoted_literal(io, s.string) + print(io, "s\"") + escape_raw_string(io, s.string) + print(io, "\"") end """ diff --git a/base/strings/io.jl b/base/strings/io.jl index 984c00408a597..30ffe03354604 100644 --- a/base/strings/io.jl +++ b/base/strings/io.jl @@ -186,16 +186,6 @@ write(io::IO, s::Union{String,SubString{String}}) = GC.@preserve s Int(unsafe_write(io, pointer(s), reinterpret(UInt, sizeof(s))))::Int print(io::IO, s::Union{String,SubString{String}}) = (write(io, s); nothing) -## printing literal quoted string data ## - -# this is the inverse of print_unescaped_chars(io, s, "\\\") - -function print_quoted_literal(io, s::AbstractString) - print(io, '"') - for c = s; c == '"' ? print(io, "\\\"") : print(io, c); end - print(io, '"') -end - """ repr(x; context=nothing) diff --git a/test/regex.jl b/test/regex.jl index f18f03fd08114..0a28d3464579d 100644 --- a/test/regex.jl +++ b/test/regex.jl @@ -34,11 +34,6 @@ @test map(m -> m.match, eachmatch(r"(\p{L}+)", "Tú lees.")) == ["Tú", "lees"] @test map(m -> m.match, eachmatch(r"(\p{L}+)", "¿Cuál es tu pregunta?")) == ["Cuál", "es", "tu", "pregunta"] - # Issue 9545 (32 bit) - buf = PipeBuffer() - show(buf, r"") - @test read(buf, String) == "r\"\"" - # see #10994, #11447: PCRE2 allows NUL chars in the pattern @test occursin(Regex("^a\0b\$"), "a\0b") @@ -52,12 +47,17 @@ subst = s"FROM: \g\n MESSAGE: \1" @test replace(msg, re => subst) == "FROM: Julia\n MESSAGE: Hello" + # Issue #9545 (32 bit) + @test repr(r"") == "r\"\"" # Issue #36550 - @test repr(s"\x") == "s\"\\x\"" - @test repr(s"\\x") == "s\"\\\\x\"" - @test repr(s"\\\x") == "s\"\\\\\\x\"" - @test repr(s"x\\") == "s\"x\\\"" - @test repr(s"a\1b") == "s\"a\\1b\"" + @test repr(s"\x") == raw"s\"\x\"" + @test repr(s"\\x") == raw"s\"\\x\"" + @test repr(s"\\\x") == raw"s\"\\\x\"" + @test repr(s"x\\") == raw"s\"x\\\\\"" + @test repr(s"a\1b") == raw"s\"a\1b\"" + # Issue #29580 + @test repr(r"\\\"") == raw"r\"\\\\\\\"\"" + @test repr(s"\\\"\\") == raw"s\"\\\\\\\"\\\\\"" # findall @test findall(r"\w+", "foo bar") == [1:3, 5:7]