[gnote] -sharp::string closer to the behaviour of Mono.



commit 78adedb94c43fc977ccfd3f0fd16c7007b27a423
Author: Wade Berrier <wberrier gmail com>
Date:   Wed Apr 15 19:02:00 2009 -0400

    -sharp::string closer to the behaviour of Mono.
    (Close #578956)
    
    Signed-off-by: Hubert Figuiere <hub figuiere net>
---
 src/sharp/string.cpp    |   15 ++++++++++++---
 src/test/stringtest.cpp |    7 ++++++-
 2 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/src/sharp/string.cpp b/src/sharp/string.cpp
index e643a46..5ff45ad 100644
--- a/src/sharp/string.cpp
+++ b/src/sharp/string.cpp
@@ -115,12 +115,15 @@ namespace sharp {
 
   bool string_contains(const std::string & source, const std::string &search)
   {
-    // C# also matches every string against the empty string
-    return search.empty() || string_index_of(source, search) != -1;
+    return string_index_of(source, search) != -1;
   }
 
   int string_last_index_of(const std::string & source, const std::string & search)
   {
+    if(search.empty()) {
+        // Return last index, unless the source is the empty string, return 0
+        return source.empty() ? 0 : source.size() - 1;
+    }
     boost::iterator_range<std::string::const_iterator> iter
       = boost::find_last(source, search);
     if(iter.begin() == source.end()) {
@@ -133,8 +136,9 @@ namespace sharp {
 
   int string_index_of(const std::string & source, const std::string & search)
   {
+    // C# returns index 0 if looking for the empty string
     if(search.empty()) {
-      return -1;
+      return 0;
     }
     boost::iterator_range<std::string::const_iterator> iter
       = boost::find_first(source, search);
@@ -151,6 +155,11 @@ namespace sharp {
     std::string source2(source.begin() + start_at, source.end());
     boost::iterator_range<std::string::const_iterator> iter
       = boost::find_first(source2, search);
+    // C# returns index 0 if looking for the empty string
+    if(search.empty()) {
+      // Note: check this after 'find_first' so boost will throw an exception if start_at > source.size()
+      return start_at;
+    }
     if(iter.begin() == source2.end()) {
       // NOT FOUND
       return -1;
diff --git a/src/test/stringtest.cpp b/src/test/stringtest.cpp
index b99737b..5834e36 100644
--- a/src/test/stringtest.cpp
+++ b/src/test/stringtest.cpp
@@ -51,8 +51,13 @@ int test_main(int /*argc*/, char ** /*argv*/)
   BOOST_CHECK(splits[11] == "");
   BOOST_CHECK(splits[12] == "");
 
-  // C# string.Contains matches true on empty strings
+  // Some Empty string tests to mimic C#
   BOOST_CHECK(string_contains(test5, ""));
+  BOOST_CHECK(string_index_of(test5, "") == 0);
+  BOOST_CHECK(string_index_of(test5, "", 4) == 4);
+  BOOST_CHECK(string_index_of("", "") == 0);
+  BOOST_CHECK(string_last_index_of(test5, "") == (signed int)test5.size() - 1);
+  BOOST_CHECK(string_last_index_of("", "") == 0);
 
   BOOST_CHECK(string_substring(test1, 4) == "bar baz");
   BOOST_CHECK(string_substring(test1, 4, 3) == "bar");



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