diff --git a/ParserHelpers.cpp b/ParserHelpers.cpp index 0d37976..1595fb8 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 0e9abbd..f75d157 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) {