From a6c2813a1d81f9081b50277ac819b58f57f8e8a6 Mon Sep 17 00:00:00 2001 From: Joseph Kleinhenz Date: Mon, 7 Nov 2016 13:02:22 -0500 Subject: [PATCH] add raw_str macro for strings with no interpolation/unescaping --- base/exports.jl | 9 +++++---- base/strings/io.jl | 2 ++ doc/src/manual/strings.md | 8 ++++++++ test/strings/io.jl | 17 +++++++++++++++++ 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/base/exports.jl b/base/exports.jl index c9be9f24ad702..91282c2cb8864 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -1303,10 +1303,11 @@ export @cmd, # `commands` # notation for certain types - @b_str, # byte vector - @r_str, # regex - @s_str, # regex substitution string - @v_str, # version number + @b_str, # byte vector + @r_str, # regex + @s_str, # regex substitution string + @v_str, # version number + @raw_str, # raw string with no interpolation/unescaping # documentation @text_str, diff --git a/base/strings/io.jl b/base/strings/io.jl index 0d6e35a3a1c14..fade6421445a3 100644 --- a/base/strings/io.jl +++ b/base/strings/io.jl @@ -310,6 +310,8 @@ unescape_string(s::AbstractString) = sprint(endof(s), unescape_string, s) macro b_str(s); :($(unescape_string(s)).data); end +macro raw_str(s); s; end + ## multiline strings ## """ diff --git a/doc/src/manual/strings.md b/doc/src/manual/strings.md index cc4be463dba38..f537b5d127be5 100644 --- a/doc/src/manual/strings.md +++ b/doc/src/manual/strings.md @@ -883,3 +883,11 @@ scheme. Besides being used for the [`VERSION`](@ref) constant, `VersionNumber` objects are widely used in the `Pkg` module, to specify packages versions and their dependencies. + +## [Raw String Literals](@id man-raw-string-literals) + +Raw strings without interpolation or unescaping can be expressed with +non-standard string literals of the form `raw"..."`. Raw string literals +create ordinary `String` objects which contain the enclosed contents exactly +as entered with no interpolation or unescaping. This is useful for strings which +contain code or markup in other languages which use `$` or `\` as special characters. diff --git a/test/strings/io.jl b/test/strings/io.jl index dee71b655cc18..da07b114a98ab 100644 --- a/test/strings/io.jl +++ b/test/strings/io.jl @@ -183,3 +183,20 @@ join(myio, "", "", 1) @test Base.unindent("\n \tfoo",4) == "\n foo" @test Base.unindent("\n\t\n \tfoo",4) == "\n \n foo" @test Base.unindent("\n\tfoo\tbar",4) == "\n foo bar" + +# Tests of raw_str macro +@test raw"$" == "\$" +@test raw"\n" == "\\n" +@test raw"\t" == "\\t" + +s1 = raw""" + lorem ipsum\n + $x = 1$ + """ + +s2 = """ + lorem ipsum\\n + \$x = 1\$ + """ + +@test s1 == s2