changeset 1428:0a355eeeb351

optimization
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 29 Jun 2015 13:26:34 +0200
parents d710ea64f0fd
children 7366a0bdda6a
files Core/Toolbox.cpp OrthancServer/OrthancRestApi/OrthancRestModalities.cpp UnitTestsSources/UnitTestsMain.cpp
diffstat 3 files changed, 66 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/Core/Toolbox.cpp	Mon Jun 29 12:42:54 2015 +0200
+++ b/Core/Toolbox.cpp	Mon Jun 29 13:26:34 2015 +0200
@@ -685,7 +685,43 @@
 
   bool Toolbox::IsSHA1(const std::string& str)
   {
-    if (str.size() != 44)
+    if (str.size() == 0)
+    {
+      return false;
+    }
+
+    const char* start = &str[0];
+    const char* end = start + str.size();
+
+    // Trim the beginning of the string
+    while (start < end)
+    {
+      if (*start == '\0' ||
+          isspace(*start))
+      {
+        start++;
+      }
+      else
+      {
+        break;
+      }
+    }
+
+    // Trim the trailing of the string
+    while (start < end)
+    {
+      if (*(end - 1) == '\0' ||
+          isspace(*(end - 1)))
+      {
+        end--;
+      }
+      else
+      {
+        break;
+      }
+    }
+
+    if (end - start != 44)
     {
       return false;
     }
@@ -697,12 +733,12 @@
           i == 26 ||
           i == 35)
       {
-        if (str[i] != '-')
+        if (start[i] != '-')
           return false;
       }
       else
       {
-        if (!isalnum(str[i]))
+        if (!isalnum(start[i]))
           return false;
       }
     }
--- a/OrthancServer/OrthancRestApi/OrthancRestModalities.cpp	Mon Jun 29 12:42:54 2015 +0200
+++ b/OrthancServer/OrthancRestApi/OrthancRestModalities.cpp	Mon Jun 29 13:26:34 2015 +0200
@@ -553,20 +553,20 @@
    * DICOM C-Store SCU
    ***************************************************************************/
 
-  static bool GetInstancesToExport(Json::Value& request,
+  static bool GetInstancesToExport(Json::Value& otherArguments,
                                    std::list<std::string>& instances,
                                    const std::string& remote,
                                    RestApiPostCall& call)
   {
+    otherArguments = Json::objectValue;
     ServerContext& context = OrthancRestApi::GetContext(call);
 
-    std::string stripped = Toolbox::StripSpaces(call.GetPostBody());
-
-    request = Json::objectValue;
-    if (Toolbox::IsSHA1(stripped))
+    Json::Value request;
+    if (Toolbox::IsSHA1(call.GetPostBody()))
     {
       // This is for compatibility with Orthanc <= 0.5.1.
-      request = stripped;
+      request = Json::arrayValue;
+      request.append(Toolbox::StripSpaces(call.GetPostBody()));
     }
     else if (!call.ParseJsonRequest(request))
     {
@@ -576,13 +576,9 @@
 
     if (request.isString())
     {
-      if (Configuration::GetGlobalBoolParameter("LogExportedResources", true))
-      {
-        context.GetIndex().LogExportedResource(request.asString(), remote);
-      }
-
-      context.GetIndex().GetChildInstances(instances, request.asString());
-      return true;
+      std::string item = request.asString();
+      request = Json::arrayValue;
+      request.append(item);
     }
 
     const Json::Value* resources;
@@ -603,6 +599,13 @@
       {
         return false;
       }
+
+      // Copy the remaining arguments
+      Json::Value::Members members = request.getMemberNames();
+      for (Json::Value::ArrayIndex i = 0; i < members.size(); i++)
+      {
+        otherArguments[members[i]] = request[members[i]];
+      }
     }
 
     for (Json::Value::ArrayIndex i = 0; i < resources->size(); i++)
--- a/UnitTestsSources/UnitTestsMain.cpp	Mon Jun 29 12:42:54 2015 +0200
+++ b/UnitTestsSources/UnitTestsMain.cpp	Mon Jun 29 13:26:34 2015 +0200
@@ -78,6 +78,17 @@
   ASSERT_FALSE(Toolbox::IsSHA1("012345678901234567890123456789012345678901234"));
   ASSERT_TRUE(Toolbox::IsSHA1("b5ed549f-956400ce-69a8c063-bf5b78be-2732a4b9"));
 
+  std::string sha = "         b5ed549f-956400ce-69a8c063-bf5b78be-2732a4b9          ";
+  ASSERT_TRUE(Toolbox::IsSHA1(sha));
+  sha[3] = '\0';
+  sha[53] = '\0';
+  ASSERT_TRUE(Toolbox::IsSHA1(sha));
+  sha[40] = '\0';
+  ASSERT_FALSE(Toolbox::IsSHA1(sha));
+  ASSERT_FALSE(Toolbox::IsSHA1("       "));
+
+  ASSERT_TRUE(Toolbox::IsSHA1("16738bc3-e47ed42a-43ce044c-a3414a45-cb069bd0"));
+
   std::string s;
   Toolbox::ComputeSHA1(s, "The quick brown fox jumps over the lazy dog");
   ASSERT_TRUE(Toolbox::IsSHA1(s));