Added StrUtils::RemoveComments() and used it instead of std::regex in parsing level commands
parent
3c4b9f6d01
commit
8e128ff08c
|
@ -36,7 +36,6 @@
|
|||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include <set>
|
||||
#include <regex>
|
||||
|
||||
CLevelParser::CLevelParser()
|
||||
{
|
||||
|
@ -167,29 +166,11 @@ void CLevelParser::Load()
|
|||
|
||||
line = StrUtils::Replace(line, "\t", " "); // replace tab by space
|
||||
|
||||
// ignore comments
|
||||
size_t pos = 0;
|
||||
std::string linesuffix = line;
|
||||
std::regex commentRegex{ R"(("[^"]*")|('[^']*')|(//.*$))" };
|
||||
std::smatch matches;
|
||||
while (std::regex_search(linesuffix, matches, commentRegex))
|
||||
{
|
||||
if (matches[3].matched)
|
||||
{
|
||||
pos += std::distance(linesuffix.cbegin(), matches.prefix().second);
|
||||
line = line.substr(0, pos);
|
||||
linesuffix = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
pos += std::distance(linesuffix.cbegin(), matches.suffix().first);
|
||||
linesuffix = matches.suffix().str();
|
||||
}
|
||||
}
|
||||
StrUtils::RemoveComments(line);
|
||||
|
||||
StrUtils::Trim(line);
|
||||
|
||||
pos = line.find_first_of(" \t\n");
|
||||
size_t pos = line.find_first_of(" \t\n");
|
||||
std::string command = line.substr(0, pos);
|
||||
if (pos != std::string::npos)
|
||||
{
|
||||
|
|
|
@ -136,3 +136,40 @@ void StrUtils::Trim(std::string& str)
|
|||
TrimLeft(str);
|
||||
TrimRight(str);
|
||||
}
|
||||
|
||||
void StrUtils::RemoveComments(std::string& text)
|
||||
{
|
||||
for (size_t i = 0; i < text.size();)
|
||||
{
|
||||
// Skip string literal of form "text"
|
||||
if (size_t start = text.find_first_of('"', i); start != std::string::npos)
|
||||
{
|
||||
size_t end = text.find_first_of('"', start + 1);
|
||||
|
||||
if (end == std::string::npos) break;
|
||||
|
||||
i = end + 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Skip string literal of form 'text'
|
||||
if (size_t start = text.find_first_of('\'', i); start != std::string::npos)
|
||||
{
|
||||
size_t end = text.find_first_of('\'', start + 1);
|
||||
|
||||
if (end == std::string::npos) break;
|
||||
|
||||
i = end + 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Find and remove comment of form // comment
|
||||
if (size_t start = text.find_first_of("//", i); start != std::string::npos)
|
||||
{
|
||||
text.erase(std::next(text.begin(), start), text.end());
|
||||
}
|
||||
|
||||
// Nothing else to skip or remove
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -82,6 +82,9 @@ void TrimRight(std::string& str);
|
|||
//! Remove whitespace from both ends of the given string (in place)
|
||||
void Trim(std::string& str);
|
||||
|
||||
//! Removes comments of form // comment
|
||||
void RemoveComments(std::string& text);
|
||||
|
||||
//! Converts a wide Unicode char to a single UTF-8 encoded char
|
||||
std::string UnicodeCharToUtf8(unsigned int ch);
|
||||
|
||||
|
|
|
@ -54,6 +54,29 @@ TEST(StringUtilTests, ReplaceSameLength)
|
|||
EXPECT_EQ(result, expected);
|
||||
}
|
||||
|
||||
TEST(StringUtilTests, RemoveComments)
|
||||
{
|
||||
std::string text = "qwerty";
|
||||
StrUtils::RemoveComments(text);
|
||||
EXPECT_EQ(text, "qwerty");
|
||||
|
||||
text = "qwerty // comment";
|
||||
StrUtils::RemoveComments(text);
|
||||
EXPECT_EQ(text, "qwerty ");
|
||||
|
||||
text = "qwerty 'test' // comment";
|
||||
StrUtils::RemoveComments(text);
|
||||
EXPECT_EQ(text, "qwerty 'test' ");
|
||||
|
||||
text = "qwerty \"test\" // comment";
|
||||
StrUtils::RemoveComments(text);
|
||||
EXPECT_EQ(text, "qwerty \"test\" ");
|
||||
|
||||
text = "qwerty 'test // test'";
|
||||
StrUtils::RemoveComments(text);
|
||||
EXPECT_EQ(text, "qwerty 'test // test'");
|
||||
}
|
||||
|
||||
TEST(StringUtilTests, SplitSingle)
|
||||
{
|
||||
std::string text = "CreateObject test value 123";
|
||||
|
|
Loading…
Reference in New Issue