From 191608a91867b69d013ab4b6a9015c543c653a3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivo=20Elezovi=C4=87?= Date: Wed, 11 Sep 2024 09:28:21 +0200 Subject: [PATCH] Fix for StringOfItem nested quoted culties. (#265) --- ParserHelpers.cpp | 16 ++++++++++-- tests/ParserHelperTests.cpp | 51 +++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/ParserHelpers.cpp b/ParserHelpers.cpp index 0d379760..1595fb8c 100644 --- a/ParserHelpers.cpp +++ b/ParserHelpers.cpp @@ -480,6 +480,7 @@ stringOfItem::stringOfItem(std::istream& theStream) if (next == "{") { + bool inQuotes = false; auto braceDepth = 1; while (true) { @@ -493,11 +494,22 @@ stringOfItem::stringOfItem(std::istream& theStream) theString += inputChar; - if (inputChar == '{') + if (inputChar == '\"') + { + if (!inQuotes) + { + inQuotes = true; + } + else + { + inQuotes = false; + } + } + if (inputChar == '{' && !inQuotes) { braceDepth++; } - else if (inputChar == '}') + else if (inputChar == '}' && !inQuotes) { braceDepth--; if (braceDepth == 0) diff --git a/tests/ParserHelperTests.cpp b/tests/ParserHelperTests.cpp index 0e9abbdd..f75d1571 100644 --- a/tests/ParserHelperTests.cpp +++ b/tests/ParserHelperTests.cpp @@ -766,6 +766,15 @@ TEST(ParserHelper_Tests, SingleStringGetsQuotedStringAfterEquals) ASSERT_EQ("foo", theString.getString()); } +TEST(ParserHelper_Tests, SingleStringGetsQuotedCurly) +{ + std::stringstream input{" = \"{\" "}; + + const commonItems::singleString theString(input); + + ASSERT_EQ("{", theString.getString()); +} + TEST(ParserHelper_Tests, StringOfObjectConvertsBracedObjectsToStrings) { @@ -801,6 +810,33 @@ TEST(ParserHelper_Tests, StringOfItemConvertsBracedObjectsToStrings) ASSERT_EQ(input.str(), theItem.getString()); } +TEST(ParserHelper_Tests, StringOfItemHandlesQuotedCurlyBracesInString) +{ + std::stringstream input; + input >> std::noskipws; + input << "= \"blah { blah \""; + + const commonItems::stringOfItem theItem(input); + + ASSERT_EQ(input.str(), theItem.getString()); +} + +TEST(ParserHelper_Tests, StringOfItemHandlesNestedQuotedCurlyBraces) +{ + std::stringstream input; + input >> std::noskipws; + input << "= {\n"; + input << "\t{\n"; + input << "\t\tid = \"{\"\n"; + input << "\t\ttype = 46\n"; + input << "\t} bla\n"; + input << "}"; + + const commonItems::stringOfItem theItem(input); + + ASSERT_EQ(input.str(), theItem.getString()); +} + TEST(ParserHelper_Tests, StringOfItemGetsStringAfterEquals) { @@ -849,6 +885,21 @@ TEST(ParserHelper_Tests, StringOfItemNamesConvertsItemNamesWithinBracesToStrings ASSERT_EQ(expectedStrings, theItemNames.getStrings()); } +TEST(ParserHelper_Tests, StringOfItemNamesIgnoresQuotedCurlyBraceValues) +{ + std::stringstream input; + input >> std::noskipws; + input << "= {\n"; + input << "\tfoo = \"{\" \n"; + input << "\tbar = baz\n"; + input << "}"; + + const commonItems::stringsOfItemNames theItemNames(input); + + const auto expectedStrings = std::vector{"foo", "bar"}; + ASSERT_EQ(expectedStrings, theItemNames.getStrings()); +} + TEST(ParserHelper_Tests, AssignmentItemsWithinBracesToKeyValuePairs) {