# HG changeset patch # User Teemu Piippo # Date 1452524339 -7200 # Node ID a556ce001e26858f128597f6d929bf430d9ec8e6 # Parent e5d185b62b7f6712e73ababbcb6df6c1e51f1051 Adjusted String::mid again diff -r e5d185b62b7f -r a556ce001e26 sources/mystring.cpp --- 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); } // ------------------------------------------------------------------------------------------------- diff -r e5d185b62b7f -r a556ce001e26 sources/mystring.h --- 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;