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



commit 0fd861c484fc5378cb0404b46f7ee39f33d30fef
Author: Aurimas Černius <aurisc4 gmail com>
Date:   Sun Jan 22 16:35:30 2017 +0200

    Use Glib::ustring and no boost in string_replace_all

 src/sharp/string.cpp           |   27 +++++++++++++++++++++++----
 src/sharp/string.hpp           |    6 +++---
 src/test/unit/stringutests.cpp |   28 ++++++++++++++++++++++++++++
 3 files changed, 54 insertions(+), 7 deletions(-)
---
diff --git a/src/sharp/string.cpp b/src/sharp/string.cpp
index 5f45deb..67664f6 100644
--- a/src/sharp/string.cpp
+++ b/src/sharp/string.cpp
@@ -33,7 +33,6 @@
 #include <boost/algorithm/string/case_conv.hpp>
 #include <boost/algorithm/string/classification.hpp>
 #include <boost/algorithm/string/predicate.hpp>
-#include <boost/algorithm/string/replace.hpp>
 #include <boost/algorithm/string/split.hpp>
 #include <boost/algorithm/string/trim.hpp>
 
@@ -63,10 +62,30 @@ namespace sharp {
     return result;
   }
 
-  std::string string_replace_all(const std::string & source, const std::string & from,
-                                 const std::string & with)
+  Glib::ustring string_replace_all(const Glib::ustring & source, const Glib::ustring & what,
+                                   const Glib::ustring & with)
   {
-    return boost::replace_all_copy(source, from, with);
+    if(source.empty() || what.empty() || what == with) {
+      return source;
+    }
+
+    Glib::ustring result;
+    Glib::ustring::size_type start = 0;
+    do {
+      auto pos = source.find(what, start);
+      if(pos != Glib::ustring::npos) {
+        result += source.substr(start, pos - start);
+        result += with;
+        start = pos + what.size();
+      }
+      else {
+        result += source.substr(start);
+        start = source.size();
+      }
+    }
+    while(start < source.size());
+
+    return result;
   }
 
   std::string string_replace_regex(const std::string & source,
diff --git a/src/sharp/string.hpp b/src/sharp/string.hpp
index 03ff93c..68531ec 100644
--- a/src/sharp/string.hpp
+++ b/src/sharp/string.hpp
@@ -43,11 +43,11 @@ namespace sharp {
                                      const Glib::ustring & with);
 
   /**
-   * replace all instances of %from with %with 
+   * replace all instances of %what with %with
    * in string %source and return the result
    */
-  std::string string_replace_all(const std::string & source, const std::string & from,
-                                 const std::string & with);
+  Glib::ustring string_replace_all(const Glib::ustring & source, const Glib::ustring & what,
+                                   const Glib::ustring & with);
   /** 
    * regex replace in %source with matching %regex with %with
    * and return a copy */
diff --git a/src/test/unit/stringutests.cpp b/src/test/unit/stringutests.cpp
index cf01cbd..08f8e0d 100644
--- a/src/test/unit/stringutests.cpp
+++ b/src/test/unit/stringutests.cpp
@@ -46,5 +46,33 @@ SUITE(String)
     res = sharp::string_replace_first("foo bar baz", "boo", "bingo");
     CHECK_EQUAL("foo bar baz", res);
   }
+
+  TEST(replace_all)
+  {
+    Glib::ustring res;
+    res = sharp::string_replace_all("", "foo", "bar");
+    CHECK_EQUAL("", res);
+
+    res = sharp::string_replace_all("foo bar baz", "", "bar");
+    CHECK_EQUAL("foo bar baz", res);
+
+    res = sharp::string_replace_all("foo bar baz", "boo", "bingo");
+    CHECK_EQUAL("foo bar baz", res);
+
+    res = sharp::string_replace_all("foo bar baz", "bar", "baz");
+    CHECK_EQUAL("foo baz baz", res);
+
+    res = sharp::string_replace_all("foo bar baz", "bar", "");
+    CHECK_EQUAL("foo  baz", res);
+
+    res = sharp::string_replace_all("foo bar baz", "ba", "bingo");
+    CHECK_EQUAL("foo bingor bingoz", res);
+
+    res = sharp::string_replace_all("ffooo foo", "foo", "o");
+    CHECK_EQUAL("foo o", res);
+
+    res = sharp::string_replace_all("foo bar baz", "baz", "bar");
+    CHECK_EQUAL("foo bar bar", res);
+  }
 }
 


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