changeset 1764:9ead18ef460a

escape
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 29 Oct 2015 16:08:59 +0100
parents f7014cca73c7
children 57b9e6890482
files OrthancServer/Search/LookupIdentifierQuery.cpp OrthancServer/Search/LookupIdentifierQuery.h UnitTestsSources/ServerIndexTests.cpp
diffstat 3 files changed, 30 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/Search/LookupIdentifierQuery.cpp	Thu Oct 29 12:45:20 2015 +0100
+++ b/OrthancServer/Search/LookupIdentifierQuery.cpp	Thu Oct 29 16:08:59 2015 +0100
@@ -171,9 +171,27 @@
 
   std::string LookupIdentifierQuery::NormalizeIdentifier(const std::string& value)
   {
-    std::string s = Toolbox::ConvertToAscii(Toolbox::StripSpaces(value));
-    Toolbox::ToUpperCase(s);
-    return s;
+    std::string t;
+    t.reserve(value.size());
+
+    for (size_t i = 0; i < value.size(); i++)
+    {
+      if (value[i] == '%' ||
+          value[i] == '_')
+      {
+        t.push_back(' ');  // These characters might break wildcard queries in SQL
+      }
+      else if (isascii(value[i]) &&
+               !iscntrl(value[i]) &&
+               (!isspace(value[i]) || value[i] == ' '))
+      {
+        t.push_back(value[i]);
+      }
+    }
+
+    Toolbox::ToUpperCase(t);
+
+    return Toolbox::StripSpaces(t);
   }
 
 
--- a/OrthancServer/Search/LookupIdentifierQuery.h	Thu Oct 29 12:45:20 2015 +0100
+++ b/OrthancServer/Search/LookupIdentifierQuery.h	Thu Oct 29 16:08:59 2015 +0100
@@ -129,8 +129,6 @@
     ResourceType  level_;
     Constraints   constraints_;
 
-    static std::string NormalizeIdentifier(const std::string& value);
-
   public:
     LookupIdentifierQuery(ResourceType level) : level_(level)
     {
@@ -178,6 +176,8 @@
                                  ResourceType level,
                                  const DicomMap& map);
 
+    static std::string NormalizeIdentifier(const std::string& value);
+
     void Print(std::ostream& s) const;
   };
 }
--- a/UnitTestsSources/ServerIndexTests.cpp	Thu Oct 29 12:45:20 2015 +0100
+++ b/UnitTestsSources/ServerIndexTests.cpp	Thu Oct 29 16:08:59 2015 +0100
@@ -828,3 +828,10 @@
   context.Stop();
   db.Close();
 }
+
+
+TEST(LookupIdentifierQuery, NormalizeIdentifier)
+{
+  ASSERT_EQ("H^L.LO", LookupIdentifierQuery::NormalizeIdentifier("   Hé^l.LO  %_  "));
+  ASSERT_EQ("1.2.840.113619.2.176.2025", LookupIdentifierQuery::NormalizeIdentifier("   1.2.840.113619.2.176.2025  "));
+}