Skip to content

Commit 1c22486

Browse files
vtjnashKristofferC
authored andcommitted
render Regex and SubstitionString correctly for repr (#39422)
Fixes #29580 (cherry picked from commit 26fd3c8)
1 parent 44a392d commit 1c22486

File tree

3 files changed

+16
-24
lines changed

3 files changed

+16
-24
lines changed

base/regex.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,9 @@ function show(io::IO, re::Regex)
119119
imsxa = PCRE.CASELESS|PCRE.MULTILINE|PCRE.DOTALL|PCRE.EXTENDED|PCRE.UCP
120120
opts = re.compile_options
121121
if (opts & ~imsxa) == (DEFAULT_COMPILER_OPTS & ~imsxa)
122-
print(io, 'r')
123-
print_quoted_literal(io, re.pattern)
122+
print(io, "r\"")
123+
escape_raw_string(io, re.pattern)
124+
print(io, "\"")
124125
if (opts & PCRE.CASELESS ) != 0; print(io, 'i'); end
125126
if (opts & PCRE.MULTILINE) != 0; print(io, 'm'); end
126127
if (opts & PCRE.DOTALL ) != 0; print(io, 's'); end
@@ -456,8 +457,9 @@ isvalid(s::SubstitutionString, i::Integer) = isvalid(s.string, i)::Bool
456457
iterate(s::SubstitutionString, i::Integer...) = iterate(s.string, i...)::Union{Nothing,Tuple{AbstractChar,Int}}
457458

458459
function show(io::IO, s::SubstitutionString)
459-
print(io, "s")
460-
print_quoted_literal(io, s.string)
460+
print(io, "s\"")
461+
escape_raw_string(io, s.string)
462+
print(io, "\"")
461463
end
462464

463465
"""

base/strings/io.jl

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -186,16 +186,6 @@ write(io::IO, s::Union{String,SubString{String}}) =
186186
GC.@preserve s Int(unsafe_write(io, pointer(s), reinterpret(UInt, sizeof(s))))::Int
187187
print(io::IO, s::Union{String,SubString{String}}) = (write(io, s); nothing)
188188

189-
## printing literal quoted string data ##
190-
191-
# this is the inverse of print_unescaped_chars(io, s, "\\\")
192-
193-
function print_quoted_literal(io, s::AbstractString)
194-
print(io, '"')
195-
for c = s; c == '"' ? print(io, "\\\"") : print(io, c); end
196-
print(io, '"')
197-
end
198-
199189
"""
200190
repr(x; context=nothing)
201191

test/regex.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@
3434
@test map(m -> m.match, eachmatch(r"(\p{L}+)", "Tú lees.")) == ["", "lees"]
3535
@test map(m -> m.match, eachmatch(r"(\p{L}+)", "¿Cuál es tu pregunta?")) == ["Cuál", "es", "tu", "pregunta"]
3636

37-
# Issue 9545 (32 bit)
38-
buf = PipeBuffer()
39-
show(buf, r"")
40-
@test read(buf, String) == "r\"\""
41-
4237
# see #10994, #11447: PCRE2 allows NUL chars in the pattern
4338
@test occursin(Regex("^a\0b\$"), "a\0b")
4439

@@ -52,12 +47,17 @@
5247
subst = s"FROM: \g<name>\n MESSAGE: \1"
5348
@test replace(msg, re => subst) == "FROM: Julia\n MESSAGE: Hello"
5449

50+
# Issue #9545 (32 bit)
51+
@test repr(r"") == "r\"\""
5552
# Issue #36550
56-
@test repr(s"\x") == "s\"\\x\""
57-
@test repr(s"\\x") == "s\"\\\\x\""
58-
@test repr(s"\\\x") == "s\"\\\\\\x\""
59-
@test repr(s"x\\") == "s\"x\\\""
60-
@test repr(s"a\1b") == "s\"a\\1b\""
53+
@test repr(s"\x") == raw"s\"\x\""
54+
@test repr(s"\\x") == raw"s\"\\x\""
55+
@test repr(s"\\\x") == raw"s\"\\\x\""
56+
@test repr(s"x\\") == raw"s\"x\\\\\""
57+
@test repr(s"a\1b") == raw"s\"a\1b\""
58+
# Issue #29580
59+
@test repr(r"\\\"") == raw"r\"\\\\\\\"\""
60+
@test repr(s"\\\"\\") == raw"s\"\\\\\\\"\\\\\""
6161

6262
# findall
6363
@test findall(r"\w+", "foo bar") == [1:3, 5:7]

0 commit comments

Comments
 (0)