From df562fe20889c160518a8ba6dee569202dd825cc Mon Sep 17 00:00:00 2001 From: Mathieu Westphal Date: Fri, 27 Dec 2024 08:00:10 +0100 Subject: [PATCH] Options: Add support for parsing double dot in ratio (#1839) --- doc/user/PARSING.md | 1 + library/private/options_tools.h.in | 6 +++--- library/testing/TestSDKOptionsIO.cxx | 2 ++ 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/doc/user/PARSING.md b/doc/user/PARSING.md index 66649df57f..1471c80102 100644 --- a/doc/user/PARSING.md +++ b/doc/user/PARSING.md @@ -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 diff --git a/library/private/options_tools.h.in b/library/private/options_tools.h.in index c4ad8ff38c..30ae3ac09d 100644 --- a/library/private/options_tools.h.in +++ b/library/private/options_tools.h.in @@ -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); diff --git a/library/testing/TestSDKOptionsIO.cxx b/library/testing/TestSDKOptionsIO.cxx index 28448107e6..0ad80e9460 100644 --- a/library/testing/TestSDKOptionsIO.cxx +++ b/library/testing/TestSDKOptionsIO.cxx @@ -61,6 +61,8 @@ int TestSDKOptionsIO(int argc, char* argv[]) test.parse("ratio_t", "0.1234", 0.1234); test.parse("ratio_t", "12.34%", 0.1234); + test.parse("ratio_t", "1/2", 0.5); + test.parse("ratio_t", "1:2", 0.5); test.parse_expect("invalid ratio_t", "12.34&"); test.parse("ratio_t", "-2/-3.5", 2.0 / 3.5); test.parse_expect("invalid ratio_t", "1/2/3");