Skip to content

Documented the characters that provoke a YAML escaping string #4650

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

Merged
merged 7 commits into from
Jan 3, 2015
93 changes: 79 additions & 14 deletions components/yaml/yaml_format.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,99 @@ The syntax for scalars is similar to the PHP syntax.
Strings
~~~~~~~

Strings in YAML can be wrapped both in single and double quotes. In some cases,
they can also be unquoted:

.. code-block:: yaml

A string in YAML

.. code-block:: yaml

'A singled-quoted string in YAML'

.. tip::
"A double-quoted string in YAML"

In a single quoted string, a single quote ``'`` must be doubled:

.. code-block:: yaml
Quoted styles are useful when a string starts or ends with one or more
relevant spaces, because unquoted strings are trimmed on both ends when parsing
their contents.
Copy link
Member

Choose a reason for hiding this comment

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

and also when they contain special chars

Copy link
Member

Choose a reason for hiding this comment

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

@javiereguiluz can you please add something about that in this sentence. E.g. "[...] and also if the string contains special or reserved characters."

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't understand your comments. When you say: "... and also if it contains special chars" ... are you referring to "quoted styles are useful in that case too" or "they are trimmed on both ends in that case too". I think that the resulting phrase would be too long and complex. Could we reword it in two or three short sentences? Thanks!

Copy link
Member

Choose a reason for hiding this comment

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

What about?

Quoted styles are useful when a string starts or end with one or more relevant spaces, because unquoted strings are trimmed on both end when parsing their contents. Quotes are required when the string contains special or reserved characters.


'A single quote '' in a single-quoted string'
When using single-quoted strings, any single quote ``'`` inside its contents
must be doubled to escape it:

.. code-block:: yaml

"A double-quoted string in YAML\n"
'A single quote '' inside a single-quoted string'

If the string contains any of the following characters, it must be escaped with
single quotes:
Copy link
Member

Choose a reason for hiding this comment

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

Most of the times, using double quotes does the same.

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I really understand that. I read some parts of the specs when fixing some issues in the component. But I'm really far from understanding them in depth though.

Hopefully, @stof can have a look at this pull request and provide some feedback. Please. :)

Copy link
Member

Choose a reason for hiding this comment

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

To be more precise in the things that I remember: You can safely use double quotes when the string contains :, {, }, [, ], ,, #, -, % and @.

Copy link
Member

Choose a reason for hiding this comment

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

technically, you can use double quotes all the time if you want. But it makes the escaping harder, because you need to escape \ in it (because of control sequences) instead of just escaping single quotes


* ``:``
* ``{``
* ``}``
* ``[``
* ``]``
* ``,``
* ``&``
* ``*``
* ``#``
* ``?``
* ``|``
* ``-``
* ``<``
* ``>``
* ``=``
* ``!``
* ``%``
* ``@``
* ``\```
Copy link
Member

Choose a reason for hiding this comment

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

I prefer to use a single line list here.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it's confusing to put all these chars in a single line and separate them with a comma. You never know if the comma is a comma or a special char or part of other special char. That's why I used a table originally ;) In any case, tables, single-item lists or multiple-items lists all look awful to me in this case. I can't think of anything that makes this look OK.

Copy link
Contributor

Choose a reason for hiding this comment

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

i think if you wrap every item as a literal using "``" and seperate them with a comma its much more readable


The double-quoted style provides a way to express arbitrary strings, by
using ``\`` escape sequences. It is very useful when you need to embed a
``\n`` or a Unicode character in a string.

Quoted styles are useful when a string starts or ends with one or more
relevant spaces.
.. code-block:: yaml

.. tip::
"A double-quoted string in YAML\n"

The double-quoted style provides a way to express arbitrary strings, by
using ``\`` escape sequences. It is very useful when you need to embed a
``\n`` or a unicode character in a string.
If the string contains any of the following control characters, it must be
escaped with double quotes. In addition, the escaping must use a double slash
``\\`` to avoid parsing issues:
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand what you mean with your last sentence


* ``\0``
* ``\x01``
* ``\x02``
* ``\x03``
* ``\x04``
* ``\x05``
* ``\x06``
* ``\a``
* ``\b``
* ``\t``
* ``\n``
* ``\v``
* ``\f``
* ``\r``
* ``\x0e``
* ``\x0f``
* ``\x10``
* ``\x11``
* ``\x12``
* ``\x13``
* ``\x14``
* ``\x15``
* ``\x16``
* ``\x17``
* ``\x18``
* ``\x19``
* ``\x1a``
* ``\e``
* ``\x1c``
* ``\x1d``
* ``\x1e``
* ``\x1f``
* ``\N``
* ``\_``
* ``\L``
* ``\P``
Copy link
Member

Choose a reason for hiding this comment

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

same here


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