changeset 2442:330349d712f9

fix Toolbox::IsAsciiString
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 05 Dec 2017 21:58:09 +0100
parents 67c01a6f151e
children c149cee81250
files Core/Toolbox.cpp Core/Toolbox.h UnitTestsSources/UnitTestsMain.cpp
diffstat 3 files changed, 23 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/Core/Toolbox.cpp	Tue Dec 05 12:43:28 2017 +0100
+++ b/Core/Toolbox.cpp	Tue Dec 05 21:58:09 2017 +0100
@@ -534,7 +534,7 @@
 
     for (size_t i = 0; i < size; i++, p++)
     {
-      if (*p > 127 || (*p != 0 && iscntrl(*p)))
+      if (*p > 127 || *p == 0 || iscntrl(*p))
       {
         return false;
       }
@@ -544,6 +544,12 @@
   }
 
 
+  bool Toolbox::IsAsciiString(const std::string& s)
+  {
+    return IsAsciiString(s.c_str(), s.size());
+  }
+  
+
   std::string Toolbox::ConvertToAscii(const std::string& source)
   {
     std::string result;
--- a/Core/Toolbox.h	Tue Dec 05 12:43:28 2017 +0100
+++ b/Core/Toolbox.h	Tue Dec 05 21:58:09 2017 +0100
@@ -152,6 +152,8 @@
     bool IsAsciiString(const void* data,
                        size_t size);
 
+    bool IsAsciiString(const std::string& s);
+
     std::string ConvertToAscii(const std::string& source);
 
     std::string StripSpaces(const std::string& source);
--- a/UnitTestsSources/UnitTestsMain.cpp	Tue Dec 05 12:43:28 2017 +0100
+++ b/UnitTestsSources/UnitTestsMain.cpp	Tue Dec 05 21:58:09 2017 +0100
@@ -461,6 +461,20 @@
 }
 
 
+TEST(Toolbox, IsAsciiString)
+{
+  std::string s = "Hello 12 /";
+  ASSERT_EQ(10u, s.size());
+  ASSERT_TRUE(Toolbox::IsAsciiString(s));
+  ASSERT_TRUE(Toolbox::IsAsciiString(s.c_str(), 10));
+  ASSERT_FALSE(Toolbox::IsAsciiString(s.c_str(), 11));  // Taking the trailing hidden '\0'
+
+  s[2] = '\0';
+  ASSERT_EQ(10u, s.size());
+  ASSERT_FALSE(Toolbox::IsAsciiString(s));
+}
+
+
 #if defined(__linux__)
 TEST(OrthancInitialization, AbsoluteDirectory)
 {