From 0748b124725527ddbf71442e054528fcfa03820d Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Fri, 17 Jul 2020 10:08:46 -0400 Subject: [PATCH] when processing quoted strings, the quotes need to be removed --- src/utils.cpp | 13 ++++++++----- unittest/utils/test_utils.cpp | 27 ++++++++++++++++++++------- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/utils.cpp b/src/utils.cpp index 745cb72b56..4efa35a8d1 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -436,6 +436,7 @@ std::vector utils::split_words(const std::string &text) const char *buf = text.c_str(); std::size_t beg = 0; std::size_t len = 0; + std::size_t add = 0; char c = *buf; while (c) { @@ -452,8 +453,9 @@ std::vector utils::split_words(const std::string &text) // handle single quote if (c == '\'') { + ++beg; + add = 1; c = *++buf; - ++len; while (((c != '\'') && (c != '\0')) || ((c == '\\') && (buf[1] == '\''))) { if ((c == '\\') && (buf[1] == '\'')) { @@ -463,13 +465,14 @@ std::vector utils::split_words(const std::string &text) c = *++buf; ++len; } + if (c != '\'') ++len; c = *++buf; - ++len; // handle double quote } else if (c == '"') { + ++beg; + add = 1; c = *++buf; - ++len; while (((c != '"') && (c != '\0')) || ((c == '\\') && (buf[1] == '"'))) { if ((c == '\\') && (buf[1] == '"')) { @@ -479,8 +482,8 @@ std::vector utils::split_words(const std::string &text) c = *++buf; ++len; } + if (c != '"') ++len; c = *++buf; - ++len; } // unquoted @@ -496,7 +499,7 @@ std::vector utils::split_words(const std::string &text) if ((c == ' ') || (c == '\t') || (c == '\r') || (c == '\n') || (c == '\f') || (c == '\0')) { list.push_back(text.substr(beg,len)); - beg += len; + beg += len + add; break; } c = *++buf; diff --git a/unittest/utils/test_utils.cpp b/unittest/utils/test_utils.cpp index 2a634dd053..22a647f460 100644 --- a/unittest/utils/test_utils.cpp +++ b/unittest/utils/test_utils.cpp @@ -21,13 +21,14 @@ #include using namespace LAMMPS_NS; -using ::testing::Eq; using ::testing::EndsWith; +using ::testing::Eq; +using ::testing::StrEq; TEST(Utils, trim_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) @@ -59,24 +60,36 @@ TEST(Utils, split_words_simple) { std::vector list = utils::split_words("one two three"); 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) { std::vector list = utils::split_words("one 'two' \"three\""); 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) { std::vector list = utils::split_words("1\\' '\"two\"' 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) { std::vector list = utils::split_words("one 't\\'wo' \"th\\\"ree\""); 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) @@ -334,13 +347,13 @@ TEST(Utils, strmatch_whitespace_nonwhitespace) TEST(Utils, guesspath) { char buf[256]; - FILE *fp = fopen("test_guesspath.txt","w"); + FILE *fp = fopen("test_guesspath.txt", "w"); #if defined(__linux__) - const char *path = utils::guesspath(buf,sizeof(buf),fp); - ASSERT_THAT(path,EndsWith("test_guesspath.txt")); + const char *path = utils::guesspath(buf, sizeof(buf), fp); + ASSERT_THAT(path, EndsWith("test_guesspath.txt")); #else - const char *path = utils::guesspath(buf,sizeof(buf),fp); - ASSERT_THAT(path,EndsWith("(unknown)")); + const char *path = utils::guesspath(buf, sizeof(buf), fp); + ASSERT_THAT(path, EndsWith("(unknown)")); #endif fclose(fp); }