Skip to content

Commit

Permalink
Fix for StringOfItem nested quoted culties.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zemurin committed Sep 11, 2024
1 parent 2e71027 commit 08df12f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
16 changes: 14 additions & 2 deletions ParserHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ stringOfItem::stringOfItem(std::istream& theStream)

if (next == "{")
{
bool inQuotes = false;
auto braceDepth = 1;
while (true)
{
Expand All @@ -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)
Expand Down
51 changes: 51 additions & 0 deletions tests/ParserHelperTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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<std::string>{"foo", "bar"};
ASSERT_EQ(expectedStrings, theItemNames.getStrings());
}


TEST(ParserHelper_Tests, AssignmentItemsWithinBracesToKeyValuePairs)
{
Expand Down

0 comments on commit 08df12f

Please sign in to comment.