Fix: substring throws an exception when selecting a substring at the end
authorArjen Baart <arjen@andromeda.nl>
Sat, 3 Oct 2020 08:27:47 +0000 (10:27 +0200)
committerArjen Baart <arjen@andromeda.nl>
Sat, 3 Oct 2020 08:27:47 +0000 (10:27 +0200)
src/string.cpp
test/string_substring.cpp
test/string_substring.exp

index 0659652..22d35af 100644 (file)
@@ -335,7 +335,7 @@ substring String::operator()(int start, int len)
 
    _start = start;   //  Proper type conversion
 
-   if (_start >= strlen(p->s) || _start + len >= strlen(p->s))
+   if (_start > strlen(p->s) || _start + len > strlen(p->s))
    {
       throw StringException("Substring Out of bounds: (" 
                     + String(start) + ", " + String(len) + ")");
index a6119b7..3615195 100644 (file)
@@ -21,6 +21,13 @@ int main()
    std::cout << "The substring (3,5) of " << s1 << " is " << s2 << "\n";
    assert(s2 == "defgh");
 
+   // Select a substring at the end of the string
+   s1 = "abcdefghijkl";
+   s2 = s1(6,6);         // s2 = "ghijkl"
+
+   std::cout << "The substring (6,6) of " << s1 << " is " << s2 << "\n";
+   assert(s2 == "ghijkl");
+
    String s3 = "abcdefghijkl";
    String s4 = "12345678";
 
index 2fe771e..9a66d1a 100644 (file)
@@ -1,4 +1,5 @@
 The substring (3,5) of abcdefghijkl is defgh
+The substring (6,6) of abcdefghijkl is ghijkl
 Replace substring (3,5) of abcdefghijkl with 12345678 : abc12345678ijkl
 Insert "12345678" into "abcdefghijkl" : abc12345678defghijkl
 Remove part of "abc12345678defghijkl" : abc12345678ijkl