Mon, 11 Jan 2016 16:58:59 +0200
Adjusted String::mid again
sources/mystring.cpp | file | annotate | diff | comparison | revisions | |
sources/mystring.h | file | annotate | diff | comparison | revisions |
--- a/sources/mystring.cpp Mon Jan 11 16:44:26 2016 +0200 +++ b/sources/mystring.cpp Mon Jan 11 16:58:59 2016 +0200 @@ -173,23 +173,20 @@ // ------------------------------------------------------------------------------------------------- // -String String::mid (long a, long b) const +// Returns a substring from [a, b) +// +String String::mid (int a, int b) const { - if (b == -1 or b > length()) + a = max(a, 0); + b = min(b, length()); + + if (b == -1) b = length(); if (b <= a) return ""; - - if (a == 0 and b == length()) - return *this; - - char* newstr = new char[b - a + 1]; - strncpy (newstr, chars() + a, b - a); - newstr[b - a] = '\0'; - String other (newstr); - delete[] newstr; - return other; + else + return m_string.substr(a, b - a); } // -------------------------------------------------------------------------------------------------
--- a/sources/mystring.h Mon Jan 11 16:44:26 2016 +0200 +++ b/sources/mystring.h Mon Jan 11 16:58:59 2016 +0200 @@ -77,7 +77,7 @@ int length() const { return m_string.length(); } bool mask_against (const String &pattern) const; String md5() const; - String mid (long a, long b) const; + String mid (int a, int b) const; String right (int length) const; StringList split (const String &del) const; StringList split (char del) const;