From 1629d339deec25fbe21e7be96818139cf65b5495 Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Sun, 11 Mar 2018 19:51:54 -0500 Subject: [PATCH] Properly parse spaces and escaped/quoted spaces in expansion braces --- src/common.cpp | 17 ++++++++++++----- src/expand.cpp | 4 ++-- src/wcstringutil.cpp | 7 ------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/common.cpp b/src/common.cpp index b3a96db18..da7ac03f2 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -1288,10 +1288,10 @@ static bool unescape_string_internal(const wchar_t *const input, const size_t in const bool unescape_special = static_cast(flags & UNESCAPE_SPECIAL); const bool allow_incomplete = static_cast(flags & UNESCAPE_INCOMPLETE); - int bracket_count = 0; + int brace_count = 0; bool errored = false; - enum { mode_unquoted, mode_single_quotes, mode_double_quotes } mode = mode_unquoted; + enum { mode_unquoted, mode_single_quotes, mode_double_quotes, mode_braces } mode = mode_unquoted; for (size_t input_position = 0; input_position < input_len && !errored; input_position++) { const wchar_t c = input[input_position]; @@ -1352,24 +1352,31 @@ static bool unescape_string_internal(const wchar_t *const input, const size_t in } case L'{': { if (unescape_special) { - bracket_count++; + brace_count++; to_append_or_none = BRACE_BEGIN; } break; } case L'}': { if (unescape_special) { - bracket_count--; + brace_count--; to_append_or_none = BRACE_END; } break; } case L',': { - if (unescape_special && bracket_count > 0) { + if (unescape_special && brace_count > 0) { to_append_or_none = BRACE_SEP; } break; } + case L' ': { + //spaces, unless quoted or escaped, are ignored within braces + // if (unescape_special && brace_count > 0) { + // input_position++; //skip the space + // } + break; + } case L'\'': { mode = mode_single_quotes; to_append_or_none = unescape_special ? wint_t(INTERNAL_SEPARATOR) : NOT_A_WCHAR; diff --git a/src/expand.cpp b/src/expand.cpp index 8aa7fc540..2031dc717 100644 --- a/src/expand.cpp +++ b/src/expand.cpp @@ -942,8 +942,8 @@ static expand_error_t expand_braces(const wcstring &instr, expand_flags_t flags, whole_item.append(in, length_preceding_braces); whole_item.append(item_begin, item_len); whole_item.append(brace_end + 1); - auto whole_item2 = trim(whole_item); - debug(0, L"Found brace item: %ls\n", whole_item2.c_str()); + whole_item = trim(whole_item); + // debug(0, L"Found brace item: '%ls'\n", whole_item.c_str()); expand_braces(whole_item, flags, out, errors); item_begin = pos + 1; diff --git a/src/wcstringutil.cpp b/src/wcstringutil.cpp index 5ed6d7b74..30a3a0111 100644 --- a/src/wcstringutil.cpp +++ b/src/wcstringutil.cpp @@ -47,13 +47,6 @@ wcstring truncate(const wcstring &input, int max_len, ellipsis_type etype) { } wcstring trim(const wcstring &input) { - debug(0, "trimming '%ls'", input.c_str()); - - // auto begin = input.cbegin(); - // for (begin; *begin == L' '; ++begin); - // auto end = input.cbegin() + input.size(); - // for (end; end > begin && *end == L' '; ++end); - auto begin_offset = input.find_first_not_of(whitespace); if (begin_offset == wcstring::npos) { return wcstring{};