diff Core/Toolbox.cpp @ 1428:0a355eeeb351

optimization
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 29 Jun 2015 13:26:34 +0200
parents fe384a9d3b51
children ad94a3583b07
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;
       }
     }