sources/mystring.cpp

branch
protocol5
changeset 131
4996c8684b93
parent 108
5900be70c619
parent 129
a556ce001e26
child 141
d9073c13dc98
--- a/sources/mystring.cpp	Mon Jan 25 04:15:31 2016 +0200
+++ b/sources/mystring.cpp	Wed Jul 20 12:55:39 2016 +0300
@@ -1,5 +1,5 @@
 /*
-	Copyright 2014, 2015 Teemu Piippo
+	Copyright 2014 - 2016 Teemu Piippo
 	All rights reserved.
 
 	Redistribution and use in source and binary forms, with or without
@@ -54,7 +54,19 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-String String::strip (const List<char>& unwanted)
+String String::strip (char unwanted) const
+{
+	String result (m_string);
+
+	for (int pos = 0; (pos = result.find (unwanted)) != -1;)
+		result.remove_at (pos--);
+
+	return result;
+}
+
+// -------------------------------------------------------------------------------------------------
+//
+String String::strip (const List<char>& unwanted) const
 {
 	String result (m_string);
 
@@ -161,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);
 }
 
 // -------------------------------------------------------------------------------------------------
@@ -221,6 +230,18 @@
 
 // -------------------------------------------------------------------------------------------------
 //
+int String::find (char ch, int a) const
+{
+	int pos = m_string.find (ch, a);
+
+	if (pos == int (std::string::npos))
+		return -1;
+
+	return pos;
+}
+
+// -------------------------------------------------------------------------------------------------
+//
 int String::find_last (const char* c, int a) const
 {
 	if (a == -1 or a >= length())

mercurial