Merge pull request #1609 from colobot/1608-strutilsremovecomments-breaks-some-levels
Fix RemoveComments()dev
commit
7d22b607cc
|
@ -139,37 +139,29 @@ void StrUtils::Trim(std::string& str)
|
|||
|
||||
void StrUtils::RemoveComments(std::string& text)
|
||||
{
|
||||
for (size_t i = 0; i < text.size();)
|
||||
for (size_t i = 0; i < text.size(); i++)
|
||||
{
|
||||
// Skip string literal of form "text"
|
||||
if (size_t start = text.find_first_of('"', i); start != std::string::npos)
|
||||
char c = text[i];
|
||||
|
||||
// If a string literal of form "text" or 'text', skip
|
||||
if (c == '"' || c == '\'')
|
||||
{
|
||||
size_t end = text.find_first_of('"', start + 1);
|
||||
size_t j = i + 1;
|
||||
|
||||
if (end == std::string::npos) break;
|
||||
while (j < text.size())
|
||||
{
|
||||
if (text[j] == c) break;
|
||||
|
||||
i = end + 1;
|
||||
continue;
|
||||
j++;
|
||||
}
|
||||
|
||||
i = j;
|
||||
}
|
||||
|
||||
// Skip string literal of form 'text'
|
||||
if (size_t start = text.find_first_of('\'', i); start != std::string::npos)
|
||||
// If a comment of form // comment, remove and end processing
|
||||
else if (text[i] == '/' && text[i + 1] == '/')
|
||||
{
|
||||
size_t end = text.find_first_of('\'', start + 1);
|
||||
|
||||
if (end == std::string::npos) break;
|
||||
|
||||
i = end + 1;
|
||||
continue;
|
||||
text.erase(std::next(text.begin(), i), text.end());
|
||||
break;
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -60,21 +60,41 @@ TEST(StringUtilTests, RemoveComments)
|
|||
StrUtils::RemoveComments(text);
|
||||
EXPECT_EQ(text, "qwerty");
|
||||
|
||||
text = "qwerty // comment";
|
||||
text = R"(qwerty // comment)";
|
||||
StrUtils::RemoveComments(text);
|
||||
EXPECT_EQ(text, "qwerty ");
|
||||
EXPECT_EQ(text, R"(qwerty )");
|
||||
|
||||
text = "qwerty 'test' // comment";
|
||||
text = R"(qwerty 'test' // comment)";
|
||||
StrUtils::RemoveComments(text);
|
||||
EXPECT_EQ(text, "qwerty 'test' ");
|
||||
EXPECT_EQ(text, R"(qwerty 'test' )");
|
||||
|
||||
text = "qwerty \"test\" // comment";
|
||||
text = R"(qwerty "test" // comment)";
|
||||
StrUtils::RemoveComments(text);
|
||||
EXPECT_EQ(text, "qwerty \"test\" ");
|
||||
EXPECT_EQ(text, R"(qwerty "test" )");
|
||||
|
||||
text = "qwerty 'test // test'";
|
||||
text = R"(qwerty 'test // test')";
|
||||
StrUtils::RemoveComments(text);
|
||||
EXPECT_EQ(text, "qwerty 'test // test'");
|
||||
EXPECT_EQ(text, R"(qwerty 'test // test')");
|
||||
|
||||
text = R"(qwerty "test // test")";
|
||||
StrUtils::RemoveComments(text);
|
||||
EXPECT_EQ(text, R"(qwerty "test // test")");
|
||||
|
||||
text = R"(// comment "text")";
|
||||
StrUtils::RemoveComments(text);
|
||||
EXPECT_EQ(text, "");
|
||||
|
||||
text = R"(// comment 'text')";
|
||||
StrUtils::RemoveComments(text);
|
||||
EXPECT_EQ(text, "");
|
||||
|
||||
text = R"("qwerty"//comment)";
|
||||
StrUtils::RemoveComments(text);
|
||||
EXPECT_EQ(text, R"("qwerty")");
|
||||
|
||||
text = R"('qwerty'//comment)";
|
||||
StrUtils::RemoveComments(text);
|
||||
EXPECT_EQ(text, R"('qwerty')");
|
||||
}
|
||||
|
||||
TEST(StringUtilTests, SplitSingle)
|
||||
|
|
Loading…
Reference in New Issue