Adjusted String::mid again

Mon, 11 Jan 2016 16:58:59 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Mon, 11 Jan 2016 16:58:59 +0200
changeset 129
a556ce001e26
parent 128
e5d185b62b7f
child 131
4996c8684b93
child 132
8a4690db252e

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;

mercurial