[gnote] Use Glib::ustring and no boost in string_trim



commit df166f14d215947afe815edab7ef2c4dad664c19
Author: Aurimas Černius <aurisc4 gmail com>
Date:   Sun Jan 22 19:47:53 2017 +0200

    Use Glib::ustring and no boost in string_trim

 src/sharp/string.cpp           |   41 +++++++++++++++++++++++++++++++++++----
 src/sharp/string.hpp           |    4 +-
 src/test/unit/stringutests.cpp |   15 ++++++++++++++
 3 files changed, 53 insertions(+), 7 deletions(-)
---
diff --git a/src/sharp/string.cpp b/src/sharp/string.cpp
index 4dc1b45..85eaac0 100644
--- a/src/sharp/string.cpp
+++ b/src/sharp/string.cpp
@@ -34,7 +34,6 @@
 #include <boost/algorithm/string/classification.hpp>
 #include <boost/algorithm/string/predicate.hpp>
 #include <boost/algorithm/string/split.hpp>
-#include <boost/algorithm/string/trim.hpp>
 
 #include "debug.hpp"
 
@@ -129,14 +128,46 @@ namespace sharp {
     return Glib::ustring(source, start, len);
   }
 
-  std::string string_trim(const std::string & source)
+  Glib::ustring string_trim(const Glib::ustring & source)
   {
-    return boost::trim_copy(source);
+    if(source.empty()) {
+      return source;
+    }
+
+    auto start = source.begin();
+    while(start != source.end()) {
+      if(g_unichar_isspace(*start)) {
+        ++start;
+      }
+      else {
+        break;
+      }
+    }
+
+    auto end = source.end();
+    --end;
+    while(end != start) {
+      if(g_unichar_isspace(*end)) {
+        --end;
+      }
+      else {
+        ++end;
+        break;
+      }
+    }
+
+    return Glib::ustring(start, end);
   }  
 
-  std::string string_trim(const std::string & source, const char * set_of_char)
+  Glib::ustring string_trim(const Glib::ustring & source, const Glib::ustring & set_of_char)
   {
-    return boost::trim_copy_if(source, boost::is_any_of(set_of_char));
+    if(source.empty()) {
+      return source;
+    }
+
+    auto start = source.find_first_not_of(set_of_char);
+    auto end = source.find_last_not_of(set_of_char) + 1;
+    return source.substr(start, end - start);
   }
 
   int string_last_index_of(const std::string & source, const std::string & search)
diff --git a/src/sharp/string.hpp b/src/sharp/string.hpp
index 21a118e..3fd0915 100644
--- a/src/sharp/string.hpp
+++ b/src/sharp/string.hpp
@@ -63,8 +63,8 @@ namespace sharp {
   /** copy the substring for %source, starting at %start and running for %len */
   Glib::ustring string_substring(const Glib::ustring & source, int start, int len);
 
-  std::string string_trim(const std::string & source);
-  std::string string_trim(const std::string & source, const char * set_of_char);
+  Glib::ustring string_trim(const Glib::ustring & source);
+  Glib::ustring string_trim(const Glib::ustring & source, const Glib::ustring & set_of_char);
 
   int string_index_of(const std::string & source, const std::string & with);
   int string_index_of(const std::string & source, const std::string & with, int);
diff --git a/src/test/unit/stringutests.cpp b/src/test/unit/stringutests.cpp
index f2440bd..38ecd66 100644
--- a/src/test/unit/stringutests.cpp
+++ b/src/test/unit/stringutests.cpp
@@ -97,5 +97,20 @@ SUITE(String)
     CHECK_EQUAL("bar", sharp::string_substring("foo bar baz", 4, 3));
     CHECK_EQUAL("", sharp::string_substring("foo bar baz", 14, 3));
   }
+
+  TEST(trim)
+  {
+    CHECK_EQUAL("foo", sharp::string_trim("   foo   "));
+    CHECK_EQUAL("foo", sharp::string_trim("foo   "));
+    CHECK_EQUAL("foo", sharp::string_trim("   foo"));
+    CHECK_EQUAL("foo", sharp::string_trim("foo"));
+    CHECK_EQUAL("", sharp::string_trim(""));
+
+    CHECK_EQUAL(" foo ", sharp::string_trim("** foo ++", "*+"));
+    CHECK_EQUAL(" foo ", sharp::string_trim("** foo ", "*+"));
+    CHECK_EQUAL(" foo ", sharp::string_trim(" foo ++", "*+"));
+    CHECK_EQUAL("foo", sharp::string_trim("foo", "*+"));
+    CHECK_EQUAL("", sharp::string_trim("", "*+"));
+  }
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]