when processing quoted strings, the quotes need to be removed

This commit is contained in:
Axel Kohlmeyer
2020-07-17 10:08:46 -04:00
parent a0bfe932b5
commit cdbcacffce
2 changed files with 28 additions and 12 deletions

View File

@ -436,6 +436,7 @@ std::vector<std::string> utils::split_words(const std::string &text)
const char *buf = text.c_str(); const char *buf = text.c_str();
std::size_t beg = 0; std::size_t beg = 0;
std::size_t len = 0; std::size_t len = 0;
std::size_t add = 0;
char c = *buf; char c = *buf;
while (c) { while (c) {
@ -452,8 +453,9 @@ std::vector<std::string> utils::split_words(const std::string &text)
// handle single quote // handle single quote
if (c == '\'') { if (c == '\'') {
++beg;
add = 1;
c = *++buf; c = *++buf;
++len;
while (((c != '\'') && (c != '\0')) while (((c != '\'') && (c != '\0'))
|| ((c == '\\') && (buf[1] == '\''))) { || ((c == '\\') && (buf[1] == '\''))) {
if ((c == '\\') && (buf[1] == '\'')) { if ((c == '\\') && (buf[1] == '\'')) {
@ -463,13 +465,14 @@ std::vector<std::string> utils::split_words(const std::string &text)
c = *++buf; c = *++buf;
++len; ++len;
} }
if (c != '\'') ++len;
c = *++buf; c = *++buf;
++len;
// handle double quote // handle double quote
} else if (c == '"') { } else if (c == '"') {
++beg;
add = 1;
c = *++buf; c = *++buf;
++len;
while (((c != '"') && (c != '\0')) while (((c != '"') && (c != '\0'))
|| ((c == '\\') && (buf[1] == '"'))) { || ((c == '\\') && (buf[1] == '"'))) {
if ((c == '\\') && (buf[1] == '"')) { if ((c == '\\') && (buf[1] == '"')) {
@ -479,8 +482,8 @@ std::vector<std::string> utils::split_words(const std::string &text)
c = *++buf; c = *++buf;
++len; ++len;
} }
if (c != '"') ++len;
c = *++buf; c = *++buf;
++len;
} }
// unquoted // unquoted
@ -496,7 +499,7 @@ std::vector<std::string> utils::split_words(const std::string &text)
if ((c == ' ') || (c == '\t') || (c == '\r') || (c == '\n') if ((c == ' ') || (c == '\t') || (c == '\r') || (c == '\n')
|| (c == '\f') || (c == '\0')) { || (c == '\f') || (c == '\0')) {
list.push_back(text.substr(beg,len)); list.push_back(text.substr(beg,len));
beg += len; beg += len + add;
break; break;
} }
c = *++buf; c = *++buf;

View File

@ -21,13 +21,14 @@
#include <vector> #include <vector>
using namespace LAMMPS_NS; using namespace LAMMPS_NS;
using ::testing::Eq;
using ::testing::EndsWith; using ::testing::EndsWith;
using ::testing::Eq;
using ::testing::StrEq;
TEST(Utils, trim_comment) TEST(Utils, trim_comment)
{ {
auto trimmed = utils::trim_comment("some text # comment"); auto trimmed = utils::trim_comment("some text # comment");
ASSERT_THAT(trimmed, Eq("some text ")); ASSERT_THAT(trimmed, StrEq("some text "));
} }
TEST(Utils, count_words) TEST(Utils, count_words)
@ -59,24 +60,36 @@ TEST(Utils, split_words_simple)
{ {
std::vector<std::string> list = utils::split_words("one two three"); std::vector<std::string> list = utils::split_words("one two three");
ASSERT_EQ(list.size(), 3); ASSERT_EQ(list.size(), 3);
ASSERT_THAT(list[0], StrEq("one"));
ASSERT_THAT(list[1], StrEq("two"));
ASSERT_THAT(list[2], StrEq("three"));
} }
TEST(Utils, split_words_quoted) TEST(Utils, split_words_quoted)
{ {
std::vector<std::string> list = utils::split_words("one 'two' \"three\""); std::vector<std::string> list = utils::split_words("one 'two' \"three\"");
ASSERT_EQ(list.size(), 3); ASSERT_EQ(list.size(), 3);
ASSERT_THAT(list[0], StrEq("one"));
ASSERT_THAT(list[1], StrEq("two"));
ASSERT_THAT(list[2], StrEq("three"));
} }
TEST(Utils, split_words_escaped) TEST(Utils, split_words_escaped)
{ {
std::vector<std::string> list = utils::split_words("1\\' '\"two\"' 3\\\""); std::vector<std::string> list = utils::split_words("1\\' '\"two\"' 3\\\"");
ASSERT_EQ(list.size(), 3); ASSERT_EQ(list.size(), 3);
ASSERT_THAT(list[0], StrEq("1\\'"));
ASSERT_THAT(list[1], StrEq("\"two\""));
ASSERT_THAT(list[2], StrEq("3\\\""));
} }
TEST(Utils, split_words_quote_in_quoted) TEST(Utils, split_words_quote_in_quoted)
{ {
std::vector<std::string> list = utils::split_words("one 't\\'wo' \"th\\\"ree\""); std::vector<std::string> list = utils::split_words("one 't\\'wo' \"th\\\"ree\"");
ASSERT_EQ(list.size(), 3); ASSERT_EQ(list.size(), 3);
ASSERT_THAT(list[0], StrEq("one"));
ASSERT_THAT(list[1], StrEq("t\\'wo"));
ASSERT_THAT(list[2], StrEq("th\\\"ree"));
} }
TEST(Utils, valid_integer1) TEST(Utils, valid_integer1)