Skip to content

Commit 127ebc1

Browse files
committed
feature #4650 Documented the characters that provoke a YAML escaping string (javiereguiluz)
This PR was merged into the 2.3 branch. Discussion ---------- Documented the characters that provoke a YAML escaping string | Q | A | ------------- | --- | Doc fix? | no | New docs? | yes | Applies to | all | Fixed tickets | #4197 Commits ------- 2824867 Rewroded some contents and fixed some lists of elements 3a40d5a MAde some tweaks suggested by Wouter 044e88b Fixed a minor typo 63ea659 Added a lot more cases for enclosing strings with quotes ce6e3eb Reworded some explanations to make them more clear caaa272 Addressed all the comments made by Wouter d5be25e Documented the characters that provoke a YAML escaping string
2 parents 1d5966c + 2824867 commit 127ebc1

File tree

1 file changed

+44
-14
lines changed

1 file changed

+44
-14
lines changed

components/yaml/yaml_format.rst

+44-14
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,64 @@ The syntax for scalars is similar to the PHP syntax.
2323
Strings
2424
~~~~~~~
2525

26+
Strings in YAML can be wrapped both in single and double quotes. In some cases,
27+
they can also be unquoted:
28+
2629
.. code-block:: yaml
2730
2831
A string in YAML
2932
30-
.. code-block:: yaml
31-
3233
'A singled-quoted string in YAML'
3334
34-
.. tip::
35+
"A double-quoted string in YAML"
3536
36-
In a single quoted string, a single quote ``'`` must be doubled:
37+
Quoted styles are useful when a string starts or end with one or more relevant
38+
spaces, because unquoted strings are trimmed on both end when parsing their
39+
contents. Quotes are required when the string contains special or reserved characters.
3740

38-
.. code-block:: yaml
39-
40-
'A single quote '' in a single-quoted string'
41+
When using single-quoted strings, any single quote ``'`` inside its contents
42+
must be doubled to escape it:
4143

4244
.. code-block:: yaml
4345
44-
"A double-quoted string in YAML\n"
46+
'A single quote '' inside a single-quoted string'
4547
46-
Quoted styles are useful when a string starts or ends with one or more
47-
relevant spaces.
48+
Strings containing any of the following characters must be quoted. Although you
49+
can use double quotes, for these characters it is more convenient to use single
50+
quotes, which avoids having to escape any backslash ``\``:
4851

49-
.. tip::
52+
* ``:``, ``{``, ``}``, ``[``, ``]``, ``,``, ``&``, ``*``, ``#``, ``?``, ``|``,
53+
``-``, ``<``, ``>``, ``=``, ``!``, ``%``, ``@``, ``\```
54+
55+
The double-quoted style provides a way to express arbitrary strings, by
56+
using ``\`` to escape characters and sequences. For instance, it is very useful
57+
when you need to embed a ``\n`` or a Unicode character in a string.
58+
59+
.. code-block:: yaml
60+
61+
"A double-quoted string in YAML\n"
5062
51-
The double-quoted style provides a way to express arbitrary strings, by
52-
using ``\`` escape sequences. It is very useful when you need to embed a
53-
``\n`` or a unicode character in a string.
63+
If the string contains any of the following control characters, it must be
64+
escaped with double quotes:
65+
66+
* ``\0``, ``\x01``, ``\x02``, ``\x03``, ``\x04``, ``\x05``, ``\x06``, ``\a``,
67+
``\b``, ``\t``, ``\n``, ``\v``, ``\f``, ``\r``, ``\x0e``, ``\x0f``, ``\x10``,
68+
``\x11``, ``\x12``, ``\x13``, ``\x14``, ``\x15``, ``\x16``, ``\x17``, ``\x18``,
69+
``\x19``, ``\x1a``, ``\e``, ``\x1c``, ``\x1d``, ``\x1e``, ``\x1f``, ``\N``,
70+
``\_``, ``\L``, ``\P``
71+
72+
Finally, there are other cases when the strings must be quoted, no matter if
73+
you're using single or double quotes:
74+
75+
* When the string is ``true`` or ``false`` (otherwise, it would be treated as a
76+
boolean value);
77+
* When the string is ``null`` or ``~`` (otherwise, it would be considered as a
78+
``null`` value);
79+
* When the string looks like a number, such as integers (e.g. ``2``, ``14``, etc.),
80+
floats (e.g. ``2.6``, ``14.9``) and exponential numbers (e.g. ``12e7``, etc.)
81+
(otherwise, it would be treated as a numeric value);
82+
* When the string looks like a date (e.g. ``2014-12-31``) (otherwise it would be
83+
automatically converted into a Unix timestamp).
5484

5585
When a string contains line breaks, you can use the literal style, indicated
5686
by the pipe (``|``), to indicate that the string will span several lines. In

0 commit comments

Comments
 (0)