[gnote] Fix string trim when there is only one char left



commit 1e2ddcbfc75b144e638139d637b2f2a9aae00cb3
Author: Aurimas Černius <aurisc4 gmail com>
Date:   Sun Feb 12 17:11:18 2017 +0200

    Fix string trim when there is only one char left

 src/sharp/string.cpp           |    9 +++++++++
 src/test/unit/stringutests.cpp |    5 +++++
 2 files changed, 14 insertions(+), 0 deletions(-)
---
diff --git a/src/sharp/string.cpp b/src/sharp/string.cpp
index 82fb5bb..efd8bff 100644
--- a/src/sharp/string.cpp
+++ b/src/sharp/string.cpp
@@ -159,6 +159,10 @@ namespace sharp {
       }
     }
 
+    if(start == source.end()) {
+      return "";
+    }
+
     auto end = source.end();
     --end;
     while(end != start) {
@@ -171,6 +175,11 @@ namespace sharp {
       }
     }
 
+    if(start == end) {
+      // this happens when there is only one non-white space character
+      ++end;
+    }
+
     return Glib::ustring(start, end);
   }  
 
diff --git a/src/test/unit/stringutests.cpp b/src/test/unit/stringutests.cpp
index 5a8c947..595ff47 100644
--- a/src/test/unit/stringutests.cpp
+++ b/src/test/unit/stringutests.cpp
@@ -105,12 +105,17 @@ SUITE(String)
     CHECK_EQUAL("foo", sharp::string_trim("   foo"));
     CHECK_EQUAL("foo", sharp::string_trim("foo"));
     CHECK_EQUAL("", sharp::string_trim(""));
+    CHECK_EQUAL("", sharp::string_trim("   "));
+    CHECK_EQUAL("f", sharp::string_trim(" f "));
+    CHECK_EQUAL("f", sharp::string_trim("f"));
 
     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("f", sharp::string_trim("+f+", "*+"));
+    CHECK_EQUAL("f", sharp::string_trim("f", "*+"));
   }
 
   TEST(last_index_of)


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