Unverified Commit cdbcacff authored by Axel Kohlmeyer's avatar Axel Kohlmeyer
Browse files

when processing quoted strings, the quotes need to be removed

parent a0bfe932
Loading
Loading
Loading
Loading
+8 −5
Original line number Diff line number Diff line
@@ -436,6 +436,7 @@ std::vector<std::string> 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<std::string> 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<std::string> 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<std::string> utils::split_words(const std::string &text)
        c = *++buf;
        ++len;
      }
      if (c != '"') ++len;
      c = *++buf;
      ++len;
    }

    // unquoted
@@ -496,7 +499,7 @@ std::vector<std::string> 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;
+20 −7
Original line number Diff line number Diff line
@@ -21,13 +21,14 @@
#include <vector>

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<std::string> 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<std::string> 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<std::string> 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<std::string> 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)