From a3726b4b79399f3d52981f249d61e36e5775c5c1 Mon Sep 17 00:00:00 2001 From: baila Date: Tue, 9 Jan 2024 21:27:04 +0530 Subject: [PATCH 1/9] adding backward compatibility for string-to-double conversion --- hardware_interface/src/lexical_casts.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/hardware_interface/src/lexical_casts.cpp b/hardware_interface/src/lexical_casts.cpp index c9adcccf83..ed1aaf6137 100644 --- a/hardware_interface/src/lexical_casts.cpp +++ b/hardware_interface/src/lexical_casts.cpp @@ -16,8 +16,10 @@ namespace hardware_interface { + +#if __cplusplus > 201402L double stod(const std::string & s) -{ +{ // convert from string using no locale std::istringstream stream(s); stream.imbue(std::locale::classic()); @@ -29,7 +31,19 @@ double stod(const std::string & s) } return result; } +#else +double stod(const std::string & text) +{ + double result_value; + const auto parse_result = std::from_chars(text.data(), text.data() + text.size(), result_value); + if (parse_result.ec == std::errc()) + { + return result_value; + } + return 0.0; +} +#endif bool parse_bool(const std::string & bool_string) { return bool_string == "true" || bool_string == "True"; From 6a08cd7c6509a39226ed99a94d5ded2085bdd2cd Mon Sep 17 00:00:00 2001 From: baila Date: Tue, 9 Jan 2024 21:49:43 +0530 Subject: [PATCH 2/9] Format correction --- hardware_interface/src/lexical_casts.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hardware_interface/src/lexical_casts.cpp b/hardware_interface/src/lexical_casts.cpp index ed1aaf6137..289a59e7b2 100644 --- a/hardware_interface/src/lexical_casts.cpp +++ b/hardware_interface/src/lexical_casts.cpp @@ -19,7 +19,7 @@ namespace hardware_interface #if __cplusplus > 201402L double stod(const std::string & s) -{ +{ // convert from string using no locale std::istringstream stream(s); stream.imbue(std::locale::classic()); From 2e317c6af318595ed325c49b10566f75a31d6fcf Mon Sep 17 00:00:00 2001 From: bailaC Date: Thu, 11 Jan 2024 17:56:14 +0530 Subject: [PATCH 3/9] Update hardware_interface/src/lexical_casts.cpp Co-authored-by: Sai Kishor Kothakota --- hardware_interface/src/lexical_casts.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hardware_interface/src/lexical_casts.cpp b/hardware_interface/src/lexical_casts.cpp index 289a59e7b2..9e54472122 100644 --- a/hardware_interface/src/lexical_casts.cpp +++ b/hardware_interface/src/lexical_casts.cpp @@ -17,8 +17,8 @@ namespace hardware_interface { -#if __cplusplus > 201402L double stod(const std::string & s) +#if __cplusplus > 201703L { // convert from string using no locale std::istringstream stream(s); From 5b34de25a2c0f79d6c3dc4373f5449fd08f482c5 Mon Sep 17 00:00:00 2001 From: bailaC Date: Thu, 11 Jan 2024 17:56:28 +0530 Subject: [PATCH 4/9] Update hardware_interface/src/lexical_casts.cpp Co-authored-by: Sai Kishor Kothakota --- hardware_interface/src/lexical_casts.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/hardware_interface/src/lexical_casts.cpp b/hardware_interface/src/lexical_casts.cpp index 9e54472122..58e1930447 100644 --- a/hardware_interface/src/lexical_casts.cpp +++ b/hardware_interface/src/lexical_casts.cpp @@ -32,7 +32,6 @@ double stod(const std::string & s) return result; } #else -double stod(const std::string & text) { double result_value; const auto parse_result = std::from_chars(text.data(), text.data() + text.size(), result_value); From 72b3f7e9196c20f4f29b61132c001cc9b48a16b5 Mon Sep 17 00:00:00 2001 From: bailaC Date: Thu, 11 Jan 2024 17:56:39 +0530 Subject: [PATCH 5/9] Update hardware_interface/src/lexical_casts.cpp Co-authored-by: Sai Kishor Kothakota --- hardware_interface/src/lexical_casts.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hardware_interface/src/lexical_casts.cpp b/hardware_interface/src/lexical_casts.cpp index 58e1930447..28553c5e96 100644 --- a/hardware_interface/src/lexical_casts.cpp +++ b/hardware_interface/src/lexical_casts.cpp @@ -41,8 +41,8 @@ double stod(const std::string & s) } return 0.0; -} #endif +} bool parse_bool(const std::string & bool_string) { return bool_string == "true" || bool_string == "True"; From 342293e1536db6163dfa6b19786c236641119625 Mon Sep 17 00:00:00 2001 From: bailaC Date: Thu, 11 Jan 2024 17:59:16 +0530 Subject: [PATCH 6/9] Update lexical_casts.cpp --- hardware_interface/src/lexical_casts.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/hardware_interface/src/lexical_casts.cpp b/hardware_interface/src/lexical_casts.cpp index 28553c5e96..aa5a001787 100644 --- a/hardware_interface/src/lexical_casts.cpp +++ b/hardware_interface/src/lexical_casts.cpp @@ -16,7 +16,6 @@ namespace hardware_interface { - double stod(const std::string & s) #if __cplusplus > 201703L { From 65d4fd11004557266361eb53a4f844f0988e0747 Mon Sep 17 00:00:00 2001 From: baila Date: Thu, 11 Jan 2024 19:08:21 +0530 Subject: [PATCH 7/9] Addressing review comments --- hardware_interface/src/lexical_casts.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/hardware_interface/src/lexical_casts.cpp b/hardware_interface/src/lexical_casts.cpp index aa5a001787..10ba05cba5 100644 --- a/hardware_interface/src/lexical_casts.cpp +++ b/hardware_interface/src/lexical_casts.cpp @@ -17,8 +17,8 @@ namespace hardware_interface { double stod(const std::string & s) -#if __cplusplus > 201703L { +#if __cplusplus < 202002L // convert from string using no locale std::istringstream stream(s); stream.imbue(std::locale::classic()); @@ -29,11 +29,9 @@ double stod(const std::string & s) throw std::invalid_argument("Failed converting string to real number"); } return result; -} #else -{ double result_value; - const auto parse_result = std::from_chars(text.data(), text.data() + text.size(), result_value); + const auto parse_result = std::from_chars(s.data(), s.data() + s.size(), result_value); if (parse_result.ec == std::errc()) { return result_value; From 983dc47c0c47ec21e381b97cc07e0fb6d64633ea Mon Sep 17 00:00:00 2001 From: baila Date: Thu, 11 Jan 2024 19:15:24 +0530 Subject: [PATCH 8/9] adding invalid_argument check. --- .../hardware_interface/lexical_casts.hpp | 1 + hardware_interface/src/lexical_casts.cpp | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/hardware_interface/include/hardware_interface/lexical_casts.hpp b/hardware_interface/include/hardware_interface/lexical_casts.hpp index 1b9bad7018..846d9f757c 100644 --- a/hardware_interface/include/hardware_interface/lexical_casts.hpp +++ b/hardware_interface/include/hardware_interface/lexical_casts.hpp @@ -16,6 +16,7 @@ #define HARDWARE_INTERFACE__LEXICAL_CASTS_HPP_ #include +#include #include #include #include diff --git a/hardware_interface/src/lexical_casts.cpp b/hardware_interface/src/lexical_casts.cpp index 10ba05cba5..3e795563d7 100644 --- a/hardware_interface/src/lexical_casts.cpp +++ b/hardware_interface/src/lexical_casts.cpp @@ -16,10 +16,13 @@ namespace hardware_interface { -double stod(const std::string & s) +namespace impl +{ +std::optional stod(const std::string & s) { #if __cplusplus < 202002L // convert from string using no locale + // Impl with std::istringstream std::istringstream stream(s); stream.imbue(std::locale::classic()); double result; @@ -30,6 +33,7 @@ double stod(const std::string & s) } return result; #else + // Impl with std::from_chars double result_value; const auto parse_result = std::from_chars(s.data(), s.data() + s.size(), result_value); if (parse_result.ec == std::errc()) @@ -37,9 +41,19 @@ double stod(const std::string & s) return result_value; } - return 0.0; + return std::nullopt; + ; #endif } +} // namespace impl +double stod(const std::string & s) +{ + if (const auto result = impl::stod(s)) + { + return *result; + } + throw std::invalid_argument("Failed converting string to real number"); +} bool parse_bool(const std::string & bool_string) { return bool_string == "true" || bool_string == "True"; From cebd45e3d7ce16fb8e11574cc29eccf306c9b19b Mon Sep 17 00:00:00 2001 From: baila Date: Fri, 12 Jan 2024 17:39:37 +0530 Subject: [PATCH 9/9] Addressing review comments. --- hardware_interface/src/lexical_casts.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/hardware_interface/src/lexical_casts.cpp b/hardware_interface/src/lexical_casts.cpp index 3e795563d7..940beb6d0f 100644 --- a/hardware_interface/src/lexical_casts.cpp +++ b/hardware_interface/src/lexical_casts.cpp @@ -29,7 +29,7 @@ std::optional stod(const std::string & s) stream >> result; if (stream.fail() || !stream.eof()) { - throw std::invalid_argument("Failed converting string to real number"); + return std::nullopt; } return result; #else @@ -40,9 +40,7 @@ std::optional stod(const std::string & s) { return result_value; } - return std::nullopt; - ; #endif } } // namespace impl