Properly parse spaces and escaped/quoted spaces in expansion braces

This commit is contained in:
Mahmoud Al-Qudsi 2018-03-11 19:51:54 -05:00
parent 00f95a978e
commit 1629d339de
3 changed files with 14 additions and 14 deletions

View file

@ -1288,10 +1288,10 @@ 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 allow_incomplete = static_cast<bool>(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;

View file

@ -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;

View file

@ -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{};