From 8728bc4caf7a7fea217e5df50fc7fa5fdf92833b Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Fri, 3 Oct 2025 15:57:22 +0200 Subject: [PATCH 1/2] Refactor: extract common code to separate function --- include/argparse.hpp | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/include/argparse.hpp b/include/argparse.hpp index 9a3878c..ad865d2 100644 --- a/include/argparse.hpp +++ b/include/argparse.hpp @@ -964,6 +964,18 @@ namespace argparse virtual auto get_name_for_error() const -> std::string = 0; + static auto is_negative_number(std::string const & token) -> bool + { + auto iss = std::istringstream(token); + auto num = double(); + iss >> num; + if (!iss.fail() && (iss.eof() || iss.peek() == std::istringstream::traits_type::eof())) + { + return true; + } + return false; + } + private: ArgumentCommonImpl m_impl; }; @@ -1034,10 +1046,7 @@ namespace argparse { return true; } - auto iss = std::istringstream(token.m_token); - auto num = double(); - iss >> num; - if (!iss.fail() && (iss.eof() || iss.peek() == std::istringstream::traits_type::eof())) + if (is_negative_number(token.m_token)) { return true; } @@ -1412,10 +1421,7 @@ namespace argparse { return true; } - auto iss = std::istringstream(token.m_token); - auto num = double(); - iss >> num; - if (!iss.fail() && (iss.eof() || iss.peek() == std::istringstream::traits_type::eof())) + if (is_negative_number(token.m_token)) { return true; } From 1b612a9852f7ac9b26929a630ae377e520cca290 Mon Sep 17 00:00:00 2001 From: Krzysiek Karbowiak Date: Fri, 3 Oct 2025 16:05:07 +0200 Subject: [PATCH 2/2] Refactor: reuse existing implementation --- include/argparse.hpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/include/argparse.hpp b/include/argparse.hpp index ad865d2..b14f4b1 100644 --- a/include/argparse.hpp +++ b/include/argparse.hpp @@ -966,10 +966,8 @@ namespace argparse static auto is_negative_number(std::string const & token) -> bool { - auto iss = std::istringstream(token); auto num = double(); - iss >> num; - if (!iss.fail() && (iss.eof() || iss.peek() == std::istringstream::traits_type::eof())) + if (from_string(token, num)) { return true; }