Merge pull request #1606 from colobot/1605-improve-level-loading-time

Improve level loading time
dev
Emxx52 2023-08-23 20:19:17 +02:00 committed by GitHub
commit 53b3062bee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 65 additions and 21 deletions

View File

@ -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)
{

View File

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

View File

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

View File

@ -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";