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 b3bd36947d
commit 0748b12472
No known key found for this signature in database
GPG Key ID: D9B44E93BF0C375A
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();
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;

View File

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