Skip to content

Commit

Permalink
Options: Add support for parsing double dot in ratio (#1839)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwestphal authored Dec 27, 2024
1 parent f586d4e commit df562fe
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
1 change: 1 addition & 0 deletions doc/user/PARSING.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ with removing the point and precision when the value is exactly an integer.

The following formats are supported when parsing a string into a ratio:
- percent% where percent is a double
- dividend:divisor where both are doubles
- dividend/divisor where both are doubles
- double

Expand Down
6 changes: 3 additions & 3 deletions library/private/options_tools.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ ratio_t parse(const std::string& str)
return stodStrict(str.substr(0, str.size() - 1)) / 100;
}

const std::size_t i = str.find('/');
if (i != std::string::npos)
const std::size_t sep = str.find_first_of(":/");
if (sep != std::string::npos)
{
return stodStrict(str.substr(0, i)) / stodStrict(str.substr(i + 1, str.size() - i - 1));
return stodStrict(str.substr(0, sep)) / stodStrict(str.substr(sep + 1));
}

return stodStrict(str);
Expand Down
2 changes: 2 additions & 0 deletions library/testing/TestSDKOptionsIO.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ int TestSDKOptionsIO(int argc, char* argv[])

test.parse<f3d::ratio_t>("ratio_t", "0.1234", 0.1234);
test.parse<f3d::ratio_t>("ratio_t", "12.34%", 0.1234);
test.parse<f3d::ratio_t>("ratio_t", "1/2", 0.5);
test.parse<f3d::ratio_t>("ratio_t", "1:2", 0.5);
test.parse_expect<f3d::ratio_t, parsing_exception>("invalid ratio_t", "12.34&");
test.parse<f3d::ratio_t>("ratio_t", "-2/-3.5", 2.0 / 3.5);
test.parse_expect<f3d::ratio_t, parsing_exception>("invalid ratio_t", "1/2/3");
Expand Down

0 comments on commit df562fe

Please sign in to comment.