Skip to content

Commit

Permalink
Fix issues parsing some composition strings
Browse files Browse the repository at this point in the history
The parser was having issues in cases where there was both a space following the
colon and a comma following the value.
  • Loading branch information
speth committed Jan 28, 2017
1 parent 5fcbfde commit 3f6f580
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/base/stringUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,17 @@ compositionMap parseCompString(const std::string& ss,

double value;
try {
value = fpValueCheck(ss.substr(valstart, stop-colon-1));
value = fpValueCheck(ss.substr(valstart, stop-valstart));
} catch (CanteraError& err) {
// If we have a key containing a colon, we expect this to fail. In
// this case, take the current substring as part of the key and look
// to the right of the next colon for the corresponding value.
// Otherwise, this is an invalid composition string.
std::string testname = ss.substr(start, stop-colon-1);
std::string testname = ss.substr(start, stop-start);
if (testname.find_first_of(" \n\t") != npos) {
// Space, tab, and newline are never allowed in names
throw;
} else if (ss.substr(valstart, stop-colon-1).find(':') != npos) {
} else if (ss.substr(valstart, stop-valstart).find(':') != npos) {
left = colon + 1;
stop = 0; // Force another iteration of this loop
continue;
Expand Down
9 changes: 9 additions & 0 deletions test/general/string_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ TEST(parseCompString, space_separated)
ASSERT_DOUBLE_EQ(1e-4, c["baz"]);
}

TEST(parseCompString, comma_separated)
{
compositionMap c = parseCompString("foo:1.0, bar: 2, baz:1e-4");
ASSERT_EQ((size_t) 3, c.size());
ASSERT_DOUBLE_EQ(1.0, c["foo"]);
ASSERT_DOUBLE_EQ(2.0, c["bar"]);
ASSERT_DOUBLE_EQ(1e-4, c["baz"]);
}

TEST(parseCompString, extra_spaces)
{
compositionMap c = parseCompString("foo: 1.0 bar: 2 baz : 1e-4");
Expand Down

0 comments on commit 3f6f580

Please sign in to comment.