From 2f80292176312b1fa3c387ec05f5d49a4bf8e8a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Kapu=C5=9Bci=C5=84ski?= Date: Thu, 24 Aug 2023 18:45:16 +0200 Subject: [PATCH] Fix RemoveComments() --- colobot-common/src/common/stringutils.cpp | 42 +++++++++-------------- test/src/common/stringutils_test.cpp | 36 ++++++++++++++----- 2 files changed, 45 insertions(+), 33 deletions(-) diff --git a/colobot-common/src/common/stringutils.cpp b/colobot-common/src/common/stringutils.cpp index 65a86e4e..46b11615 100644 --- a/colobot-common/src/common/stringutils.cpp +++ b/colobot-common/src/common/stringutils.cpp @@ -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; } } diff --git a/test/src/common/stringutils_test.cpp b/test/src/common/stringutils_test.cpp index 75c90e79..464293ca 100644 --- a/test/src/common/stringutils_test.cpp +++ b/test/src/common/stringutils_test.cpp @@ -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)