[gnote] Fix bug with start position in string_substring



commit 2a69f54e7a89ab3110829b54c6cfa8affc6d0b36
Author: Aurimas Černius <aurisc4 gmail com>
Date:   Sun Jan 22 19:09:46 2017 +0200

    Fix bug with start position in string_substring

 src/sharp/string.cpp           |    3 +++
 src/test/unit/stringutests.cpp |    8 ++++++++
 2 files changed, 11 insertions(+), 0 deletions(-)
---
diff --git a/src/sharp/string.cpp b/src/sharp/string.cpp
index 0e6ef4c..4dc1b45 100644
--- a/src/sharp/string.cpp
+++ b/src/sharp/string.cpp
@@ -123,6 +123,9 @@ namespace sharp {
 
   Glib::ustring string_substring(const Glib::ustring & source, int start, int len)
   {
+    if(source.size() <= (unsigned int)start) {
+      return "";
+    }
     return Glib::ustring(source, start, len);
   }
 
diff --git a/src/test/unit/stringutests.cpp b/src/test/unit/stringutests.cpp
index 5e4664c..f2440bd 100644
--- a/src/test/unit/stringutests.cpp
+++ b/src/test/unit/stringutests.cpp
@@ -89,5 +89,13 @@ SUITE(String)
   {
     CHECK(sharp::string_match_iregex("CamelCase", "^Camel.*$"));
   }
+
+  TEST(substring)
+  {
+    CHECK_EQUAL("bar baz", sharp::string_substring("foo bar baz", 4));
+    CHECK_EQUAL("", sharp::string_substring("foo bar baz", 14));
+    CHECK_EQUAL("bar", sharp::string_substring("foo bar baz", 4, 3));
+    CHECK_EQUAL("", sharp::string_substring("foo bar baz", 14, 3));
+  }
 }
 


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