镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content
Open
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
10 changes: 6 additions & 4 deletions tomlkit/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,12 @@ def string(

For more information, please check the spec: `<https://toml.io/en/v1.0.0#string>`__.

Common escaping rules will be applied for basic strings.
This can be controlled by explicitly setting ``escape=False``.
Please note that, if you disable escaping, you will have to make sure that
the given strings don't contain any forbidden character or sequence.
Dumping produces a TOML string that faithfully represents the given string.
This includes character escaping for basic strings.
This behaviour can be disabled by setting ``escape=False``.
In that case, the given string is used verbatim for the content of the TOML string.
Please note that this makes you responsible for ensuring the given string does not
contain any forbidden character or sequence.
"""
type_ = _StringType.select(literal, multiline)
return String.from_raw(raw, type_, escape)
Expand Down
27 changes: 13 additions & 14 deletions tomlkit/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -2305,20 +2305,19 @@ def from_raw(
if any(c in checked for c in invalid):
raise InvalidStringError(value, invalid, type_.value)

escaped = type_.escaped_sequences
string_value = escape_string(value, escaped) if escape and escaped else value

if type_ is StringType.MLB and escape:
string_value = _BARE_CR.sub(r"\\r", string_value)

if type_.is_multiline() and string_value[:1] in ("\n", "\r"):
# A newline immediately following the opening delimiter of a
# multiline string is trimmed by the parser, so a value that
# starts with a newline would otherwise lose it on round-trip.
# Emit an extra leading newline (the trimmed one) to preserve it.
string_value = "\n" + string_value

return cls(type_, decode(value), string_value, Trivia())
string_value = value
if escape:
string_value = escape_string(string_value, type_.escaped_sequences)
if type_ is StringType.MLB and escape:
string_value = _BARE_CR.sub(r"\\r", string_value)
if type_.is_multiline() and string_value[:1] in ("\n", "\r"):
# A newline immediately following the opening delimiter of a
# multiline string is trimmed by the parser, so a value that
# starts with a newline would otherwise lose it on round-trip.
# Emit an extra leading newline (the trimmed one) to preserve it.
string_value = "\n" + string_value

return cls(type_, value, string_value, Trivia())


class AoT(Item, _CustomList): # type: ignore[type-arg]
Expand Down
Loading