Skip to content

Commit 1ea0924

Browse files
author
José Valim
committed
Clarify escaping rules in sigils, closes #9471
1 parent 96c9500 commit 1ea0924

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

lib/elixir/lib/kernel.ex

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4884,16 +4884,24 @@ defmodule Kernel do
48844884
@doc ~S"""
48854885
Handles the sigil `~S` for strings.
48864886
4887-
It simply returns a string without escaping characters and without
4888-
interpolations.
4887+
It returns a string without interpolations and without escape
4888+
characters, except for the escaping of the closing sigil character
4889+
itself.
48894890
48904891
## Examples
48914892
48924893
iex> ~S(foo)
48934894
"foo"
4894-
48954895
iex> ~S(f#{o}o)
48964896
"f\#{o}o"
4897+
iex> ~S(\o/)
4898+
"\\o/"
4899+
4900+
However, if you want to re-use the sigil character itself on
4901+
the string, you need to escape it:
4902+
4903+
iex> ~S((\))
4904+
"()"
48974905
48984906
"""
48994907
defmacro sigil_S(term, modifiers)
@@ -4930,8 +4938,9 @@ defmodule Kernel do
49304938
@doc ~S"""
49314939
Handles the sigil `~C` for charlists.
49324940
4933-
It simply returns a charlist without escaping characters and without
4934-
interpolations.
4941+
It returns a charlist without interpolations and without escape
4942+
characters, except for the escaping of the closing sigil character
4943+
itself.
49354944
49364945
## Examples
49374946
@@ -5011,8 +5020,10 @@ defmodule Kernel do
50115020
@doc ~S"""
50125021
Handles the sigil `~R` for regular expressions.
50135022
5014-
It returns a regular expression pattern without escaping
5015-
nor interpreting interpolations.
5023+
It returns a regular expression pattern without interpolations and
5024+
without escape characters. Note it still supports escape of Regex
5025+
tokens (such as escaping `+` or `?`) and it also requires you to
5026+
escape the closing sigil character itself if it appears on the Regex.
50165027
50175028
More information on regexes can be found in the `Regex` module.
50185029
@@ -5175,8 +5186,9 @@ defmodule Kernel do
51755186
@doc ~S"""
51765187
Handles the sigil `~W` for list of words.
51775188
5178-
It returns a list of "words" split by whitespace without escaping nor
5179-
interpreting interpolations.
5189+
It returns a list of "words" split by whitespace without interpolations
5190+
and without escape characters, except for the escaping of the closing
5191+
sigil character itself.
51805192
51815193
## Modifiers
51825194

0 commit comments

Comments
 (0)