From 3f6f580b25602ebb442769cca1ecf8f5d6818185 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Sat, 28 Jan 2017 00:03:37 -0500 Subject: [PATCH] Fix issues parsing some composition strings The parser was having issues in cases where there was both a space following the colon and a comma following the value. --- src/base/stringUtils.cpp | 6 +++--- test/general/string_processing.cpp | 9 +++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/base/stringUtils.cpp b/src/base/stringUtils.cpp index 6aea5ce76..71ed0d5a8 100644 --- a/src/base/stringUtils.cpp +++ b/src/base/stringUtils.cpp @@ -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; diff --git a/test/general/string_processing.cpp b/test/general/string_processing.cpp index ba9aee59d..ebf1eb02c 100644 --- a/test/general/string_processing.cpp +++ b/test/general/string_processing.cpp @@ -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");