sources/mystring.cpp

changeset 129
a556ce001e26
parent 110
cad1163333b9
child 137
485cb6d6b98c
child 131
4996c8684b93
--- 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);
 }
 
 // -------------------------------------------------------------------------------------------------

mercurial