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

RFC: add raw_str macro for strings with no interpolation/unescaping #19900

Merged
merged 1 commit into from
Jan 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 5 additions & 4 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions base/strings/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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 ##

"""
Expand Down
8 changes: 8 additions & 0 deletions doc/src/manual/strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exception is quotation marks, which are still escaped, e.g. raw"\"" is equivalent to "\""

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's worth noting

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

won't get addressed if it remains a comment on a closed pr. open an issue or pr?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated documentation to note exception in #20239

contain code or markup in other languages which use `$` or `\` as special characters.
17 changes: 17 additions & 0 deletions test/strings/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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