Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Options: Add support for parsing double dot in ratio #1839

Merged
merged 1 commit into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading