changeset 7207:2a6c2e36d464

optimized SerializationToolbox:GetFirstItem()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 09 Nov 2026 10:48:21 +0100
parents 5d530a9b1088
children 77036da5716c
files OrthancFramework/Sources/SerializationToolbox.cpp OrthancFramework/UnitTestsSources/FrameworkTests.cpp
diffstat 2 files changed, 29 insertions(+), 61 deletions(-) [+]
line wrap: on
line diff
--- 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<T>(value);
+        target = boost::lexical_cast<T>(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<std::string> 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);
   }
 
 
--- 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);