From ac43272939e7dcd1925c49ea7f627e7252f23c19 Mon Sep 17 00:00:00 2001 From: Martin Michelsen Date: Sun, 10 Dec 2023 13:33:51 -0800 Subject: [PATCH] fix quoted string behavior in split_context --- src/Strings.cc | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/src/Strings.cc b/src/Strings.cc index 3a160fb..2c23738 100644 --- a/src/Strings.cc +++ b/src/Strings.cc @@ -326,28 +326,37 @@ vector split(const wstring& s, wchar_t delim, size_t max_splits) { vector split_context(const string& s, char delim, size_t max_splits) { vector ret; vector paren_stack; + bool char_is_escaped = false; size_t z, last_start = 0; for (z = 0; z < s.size(); z++) { - if (!paren_stack.empty() && (s[z] == paren_stack.back())) { + if (!char_is_escaped && !paren_stack.empty() && (s[z] == paren_stack.back())) { paren_stack.pop_back(); continue; } - if (s[z] == '(') { - paren_stack.push_back(')'); - } else if (s[z] == '[') { - paren_stack.push_back(']'); - } else if (s[z] == '{') { - paren_stack.push_back('}'); - } else if (s[z] == '<') { - paren_stack.push_back('>'); - } else if (s[z] == '\'') { - paren_stack.push_back('\''); - } else if (s[z] == '\"') { - paren_stack.push_back('\"'); - } else if (paren_stack.empty() && (s[z] == delim) && (!max_splits || (ret.size() < max_splits))) { - ret.push_back(s.substr(last_start, z - last_start)); - last_start = z + 1; + bool in_quoted_string = (!paren_stack.empty() && ((paren_stack.back() == '\'') || (paren_stack.back() == '\"'))); + if (char_is_escaped) { + char_is_escaped = false; + } else if (in_quoted_string && s[z] == '\\') { + char_is_escaped = true; + } + if (!in_quoted_string) { + if (s[z] == '(') { + paren_stack.push_back(')'); + } else if (s[z] == '[') { + paren_stack.push_back(']'); + } else if (s[z] == '{') { + paren_stack.push_back('}'); + } else if (s[z] == '<') { + paren_stack.push_back('>'); + } else if (s[z] == '\'') { + paren_stack.push_back('\''); + } else if (s[z] == '\"') { + paren_stack.push_back('\"'); + } else if (paren_stack.empty() && (s[z] == delim) && (!max_splits || (ret.size() < max_splits))) { + ret.push_back(s.substr(last_start, z - last_start)); + last_start = z + 1; + } } }