From 5b7fbda0cbae968d0967c5afadd0d9a410a5e4c0 Mon Sep 17 00:00:00 2001 From: Matej Kenda Date: Mon, 3 Feb 2025 20:36:57 +0100 Subject: [PATCH] fix(NumberParser): tryParseHex shall not throw, but return boolean --- Foundation/include/Poco/NumericString.h | 3 ++- Foundation/testsuite/src/NumberParserTest.cpp | 6 ++++++ Foundation/testsuite/src/StringTest.h | 3 +++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Foundation/include/Poco/NumericString.h b/Foundation/include/Poco/NumericString.h index 150aa1d4f..4b6643ed0 100644 --- a/Foundation/include/Poco/NumericString.h +++ b/Foundation/include/Poco/NumericString.h @@ -242,7 +242,8 @@ bool strToInt(const char* pStr, I& outResult, short base, char thSep = ',') if (*pStr == thSep) { if (base == 10) continue; - throw Poco::SyntaxException("strToInt: thousand separators only allowed for base 10"); + // thousand separators only allowed for base 10 + return false; } if (result > (limitCheck / base)) return false; if (!safeMultiply(result, result, base)) return false; diff --git a/Foundation/testsuite/src/NumberParserTest.cpp b/Foundation/testsuite/src/NumberParserTest.cpp index d27c8396a..76ceaead5 100644 --- a/Foundation/testsuite/src/NumberParserTest.cpp +++ b/Foundation/testsuite/src/NumberParserTest.cpp @@ -98,6 +98,12 @@ void NumberParserTest::testParse() assertTrue (NumberParser::parseOct64("0123") == 0123); #endif + unsigned int u; + assertFalse (NumberParser::tryParseHex("1,000", u)); + + int i; + assertTrue (NumberParser::tryParse("1,000", i)); + #ifndef POCO_NO_FPENVIRONMENT for (int i = 0; i < 2; ++i) { diff --git a/Foundation/testsuite/src/StringTest.h b/Foundation/testsuite/src/StringTest.h index 1de335301..9f4f264aa 100644 --- a/Foundation/testsuite/src/StringTest.h +++ b/Foundation/testsuite/src/StringTest.h @@ -99,6 +99,9 @@ private: assertTrue (Poco::strToInt("0", result, 010)); assertTrue (result == 0); assertTrue (Poco::strToInt("000", result, 010)); assertTrue (result == 0); + + assertFalse (Poco::strToInt("1,000", result, 0x10)); + assertFalse (Poco::strToInt("ABCDEFG", result, 0x10)); } template