# HG changeset patch # User Sebastien Jodogne # Date 1794217701 -3600 # Node ID 2a6c2e36d46407a5bd9cebda7a4065c74f040cc3 # Parent 5d530a9b10887fca00a7c0b08c5acb608f1e0fdd optimized SerializationToolbox:GetFirstItem() diff -r 5d530a9b1088 -r 2a6c2e36d464 OrthancFramework/Sources/SerializationToolbox.cpp --- a/OrthancFramework/Sources/SerializationToolbox.cpp Mon Nov 09 10:16:27 2026 +0100 +++ b/OrthancFramework/Sources/SerializationToolbox.cpp Mon Nov 09 10:48:21 2026 +0100 @@ -193,7 +193,7 @@ valueObject[field.c_str()].type() != Json::arrayValue) { throw OrthancException(ErrorCode_BadFileFormat, - "List of strings expected in field: " + field); + "List of strings expected in field: " + field); } const Json::Value& arr = valueObject[field.c_str()]; @@ -568,19 +568,25 @@ { try { - std::string value = Toolbox::StripSpaces(source); - if (value.empty()) + if (source.empty()) + { + return false; + } + + const std::string stripped = Toolbox::StripSpaces(source); + + if (stripped.empty()) { return false; } else if (!allowSigned && - value[0] == '-') + stripped[0] == '-') { return false; } else { - target = boost::lexical_cast(value); + target = boost::lexical_cast(stripped); return true; } } @@ -658,20 +664,18 @@ } - static bool GetFirstItem(std::string& target, + static void GetFirstItem(std::string& target, const std::string& source) { - std::vector tokens; - Toolbox::TokenizeString(tokens, source, '\\'); + const size_t pos = source.find('\\'); - if (tokens.empty()) + if (pos == std::string::npos) { - return false; + target = source; } else { - target = tokens[0]; - return true; + target.assign(source, 0, pos); } } @@ -680,14 +684,8 @@ const std::string& source) { std::string first; - if (GetFirstItem(first, source)) - { - return ParseInteger32(target, first); - } - else - { - return false; - } + GetFirstItem(first, source); + return ParseInteger32(target, first); } @@ -695,14 +693,8 @@ const std::string& source) { std::string first; - if (GetFirstItem(first, source)) - { - return ParseInteger64(target, first); - } - else - { - return false; - } + GetFirstItem(first, source); + return ParseInteger64(target, first); } @@ -710,14 +702,8 @@ const std::string& source) { std::string first; - if (GetFirstItem(first, source)) - { - return ParseUnsignedInteger32(target, first); - } - else - { - return false; - } + GetFirstItem(first, source); + return ParseUnsignedInteger32(target, first); } @@ -725,14 +711,8 @@ const std::string& source) { std::string first; - if (GetFirstItem(first, source)) - { - return ParseUnsignedInteger64(target, first); - } - else - { - return false; - } + GetFirstItem(first, source); + return ParseUnsignedInteger64(target, first); } @@ -740,14 +720,8 @@ const std::string& source) { std::string first; - if (GetFirstItem(first, source)) - { - return ParseFloat(target, first); - } - else - { - return false; - } + GetFirstItem(first, source); + return ParseFloat(target, first); } @@ -755,14 +729,8 @@ const std::string& source) { std::string first; - if (GetFirstItem(first, source)) - { - return ParseDouble(target, first); - } - else - { - return false; - } + GetFirstItem(first, source); + return ParseDouble(target, first); } diff -r 5d530a9b1088 -r 2a6c2e36d464 OrthancFramework/UnitTestsSources/FrameworkTests.cpp --- a/OrthancFramework/UnitTestsSources/FrameworkTests.cpp Mon Nov 09 10:16:27 2026 +0100 +++ b/OrthancFramework/UnitTestsSources/FrameworkTests.cpp Mon Nov 09 10:48:21 2026 +0100 @@ -1895,7 +1895,7 @@ ASSERT_EQ(-45ll, Math::llround(-45.005)); ASSERT_EQ(-45ll, Math::llround(-44.995)); - long long a; + long long a = 0; ASSERT_TRUE(Math::llround(a, 46.005f)); ASSERT_EQ(46ll, a); ASSERT_TRUE(Math::llround(a, 45.995f)); ASSERT_EQ(46ll, a); ASSERT_TRUE(Math::llround(a, -46.005f)); ASSERT_EQ(-46ll, a);