[gnote] Fix corner case in string_replace_first



commit 1a052d32cf081b3ffd29253697f70130bb86adea
Author: Aurimas Černius <aurisc4 gmail com>
Date:   Sun Jan 22 16:34:32 2017 +0200

    Fix corner case in string_replace_first

 src/sharp/string.cpp           |   12 +++++++++---
 src/test/unit/stringutests.cpp |    3 +++
 2 files changed, 12 insertions(+), 3 deletions(-)
---
diff --git a/src/sharp/string.cpp b/src/sharp/string.cpp
index a6b59d5..5f45deb 100644
--- a/src/sharp/string.cpp
+++ b/src/sharp/string.cpp
@@ -51,9 +51,15 @@ namespace sharp {
 
     Glib::ustring result;
     auto pos = source.find(what);
-    result += source.substr(0, pos);
-    result += with;
-    result += source.substr(pos + what.size());
+    if(pos != Glib::ustring::npos) {
+      result += source.substr(0, pos);
+      result += with;
+      result += source.substr(pos + what.size());
+    }
+    else {
+      result = source;
+    }
+
     return result;
   }
 
diff --git a/src/test/unit/stringutests.cpp b/src/test/unit/stringutests.cpp
index 5ae3aa1..cf01cbd 100644
--- a/src/test/unit/stringutests.cpp
+++ b/src/test/unit/stringutests.cpp
@@ -42,6 +42,9 @@ SUITE(String)
 
     res = sharp::string_replace_first("", "foo", "bar");
     CHECK_EQUAL("", res);
+
+    res = sharp::string_replace_first("foo bar baz", "boo", "bingo");
+    CHECK_EQUAL("foo bar baz", res);
   }
 }
 


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