unescape_string: Add flag to skip backslash-unescaping

This is sometimes not wanted, like in the case of fish_indent.
This commit is contained in:
Fabian Homborg 2020-03-08 14:03:15 +01:00
parent b25f72f391
commit 2f56462e46
2 changed files with 19 additions and 15 deletions

View file

@ -1429,6 +1429,7 @@ static bool unescape_string_internal(const wchar_t *const input, const size_t in
const bool unescape_special = static_cast<bool>(flags & UNESCAPE_SPECIAL); const bool unescape_special = static_cast<bool>(flags & UNESCAPE_SPECIAL);
const bool allow_incomplete = static_cast<bool>(flags & UNESCAPE_INCOMPLETE); const bool allow_incomplete = static_cast<bool>(flags & UNESCAPE_INCOMPLETE);
const bool ignore_backslashes = static_cast<bool>(flags & UNESCAPE_NO_BACKSLASHES);
// The positions of open braces. // The positions of open braces.
std::vector<size_t> braces; std::vector<size_t> braces;
@ -1451,6 +1452,7 @@ static bool unescape_string_internal(const wchar_t *const input, const size_t in
if (mode == mode_unquoted) { if (mode == mode_unquoted) {
switch (c) { switch (c) {
case L'\\': { case L'\\': {
if (!ignore_backslashes) {
// Backslashes (escapes) are complicated and may result in errors, or appending // Backslashes (escapes) are complicated and may result in errors, or appending
// INTERNAL_SEPARATORs, so we have to handle them specially. // INTERNAL_SEPARATORs, so we have to handle them specially.
auto escape_chars = read_unquoted_escape(input + input_position, &result, auto escape_chars = read_unquoted_escape(input + input_position, &result,
@ -1466,6 +1468,7 @@ static bool unescape_string_internal(const wchar_t *const input, const size_t in
} }
// We've already appended, don't append anything else. // We've already appended, don't append anything else.
to_append_or_none = none(); to_append_or_none = none();
}
break; break;
} }
case L'~': { case L'~': {

View file

@ -121,7 +121,8 @@ enum escape_string_style_t {
enum { enum {
UNESCAPE_DEFAULT = 0, // default behavior UNESCAPE_DEFAULT = 0, // default behavior
UNESCAPE_SPECIAL = 1 << 0, // escape special fish syntax characters like the semicolon UNESCAPE_SPECIAL = 1 << 0, // escape special fish syntax characters like the semicolon
UNESCAPE_INCOMPLETE = 1 << 1 // allow incomplete escape sequences UNESCAPE_INCOMPLETE = 1 << 1, // allow incomplete escape sequences
UNESCAPE_NO_BACKSLASHES = 1 << 2, // don't handle backslash escapes
}; };
typedef unsigned int unescape_flags_t; typedef unsigned int unescape_flags_t;