Skip to content

Commit

Permalink
render Regex and SubstitionString correctly for repr (#39422)
Browse files Browse the repository at this point in the history
Fixes #29580
  • Loading branch information
vtjnash authored Jan 29, 2021
1 parent 750f42a commit 26fd3c8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 24 deletions.
10 changes: 6 additions & 4 deletions base/regex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

"""
Expand Down
10 changes: 0 additions & 10 deletions base/strings/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 10 additions & 10 deletions test/regex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@
@test map(m -> m.match, eachmatch(r"(\p{L}+)", "Tú lees.")) == ["", "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")

Expand All @@ -52,12 +47,17 @@
subst = s"FROM: \g<name>\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]
Expand Down

0 comments on commit 26fd3c8

Please sign in to comment.