From: Arjen Baart Date: Sat, 3 Oct 2020 08:27:47 +0000 (+0200) Subject: Fix: substring throws an exception when selecting a substring at the end X-Git-Url: http://www.andromeda.nl/gitweb/?a=commitdiff_plain;h=6f45481cd0ed65b23d38e2d5345ac600733b9d72;p=libacl.git Fix: substring throws an exception when selecting a substring at the end --- diff --git a/src/string.cpp b/src/string.cpp index 0659652..22d35af 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -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) + ")"); diff --git a/test/string_substring.cpp b/test/string_substring.cpp index a6119b7..3615195 100644 --- a/test/string_substring.cpp +++ b/test/string_substring.cpp @@ -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"; diff --git a/test/string_substring.exp b/test/string_substring.exp index 2fe771e..9a66d1a 100644 --- a/test/string_substring.exp +++ b/test/string_substring.exp @@ -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