Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arg. to avoid escaping backslahes (escape_string) #38597

Merged
merged 6 commits into from
Dec 25, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions base/strings/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,8 @@ escape_nul(c::Union{Nothing, AbstractChar}) =
(c !== nothing && '0' <= c <= '7') ? "\\x00" : "\\0"

"""
escape_string(str::AbstractString[, esc])::AbstractString
escape_string(io, str::AbstractString[, esc::])::Nothing
escape_string(str::AbstractString[, esc, bsescaped])::AbstractString
escape_string(io, str::AbstractString[, esc, bsescaped])::Nothing

General escaping of traditional C and Unicode escape sequences. The first form returns the
escaped string, the second prints the result to `io`.
Expand All @@ -323,6 +323,10 @@ unambiguous), unicode code point (`"\\u"` prefix) or hex (`"\\x"` prefix).
The optional `esc` argument specifies any additional characters that should also be
escaped by a prepending backslash (`\"` is also escaped by default in the first form).

By default, this function escapes all backslashes, meaning that `\\cdot` becomes
`\\\\cdot`. If the argument `bsescaped` is `true` then it assumes that all
backslashes are already escaped. Hence, for example, `\\cdot` does not change.

# Examples
```jldoctest
julia> escape_string("aaa\\nbbb")
Expand All @@ -341,15 +345,16 @@ julia> escape_string(string('\\u2135','\\0','0')) # \\0 would be ambiguous
## See also
[`unescape_string`](@ref) for the reverse operation.
"""
function escape_string(io::IO, s::AbstractString, esc="")
function escape_string(io::IO, s::AbstractString, esc="", bsescaped = false)
bse = bsescaped
a = Iterators.Stateful(s)
for c::AbstractChar in a
if c in esc
print(io, '\\', c)
elseif isascii(c)
c == '\0' ? print(io, escape_nul(peek(a)::Union{AbstractChar,Nothing})) :
c == '\e' ? print(io, "\\e") :
c == '\\' ? print(io, "\\\\") :
c == '\\' && !bse ? print(io, "\\\\") :
'\a' <= c <= '\r' ? print(io, '\\', "abtnvfr"[Int(c)-6]) :
isprint(c) ? print(io, c) :
print(io, "\\x", string(UInt32(c), base = 16, pad = 2))
Expand All @@ -368,7 +373,8 @@ function escape_string(io::IO, s::AbstractString, esc="")
end
end

escape_string(s::AbstractString, esc=('\"',)) = sprint(escape_string, s, esc, sizehint=lastindex(s))
escape_string(s::AbstractString, esc=('\"',), bsescaped = false) =
sprint(escape_string, s, esc, bsescaped, sizehint=lastindex(s))

function print_quoted(io, s::AbstractString)
print(io, '"')
Expand Down
2 changes: 2 additions & 0 deletions test/strings/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
@test typeof(escape_string("test", "t")) == String
@test escape_string("test", "t") == "\\tes\\t"

@test escape_string("\\cdot", "", true) == "\\cdot"

for i = 1:size(cx,1)
cp, ch, st = cx[i,:]
@test cp == convert(UInt32, ch)
Expand Down