sources/mystring.cpp

changeset 69
eb4c25284a19
parent 66
bd28a5730fd0
child 73
07dda51a7a8e
--- a/sources/mystring.cpp	Tue Dec 16 23:50:56 2014 +0200
+++ b/sources/mystring.cpp	Mon May 04 15:51:03 2015 +0300
@@ -36,16 +36,14 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-String::compare (const String& other) const -> int
+int String::compare (const String& other) const
 {
 	return m_string.compare (other.std_string());
 }
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-String::trim (int n) -> void
+void String::trim (int n)
 {
 	if (n > 0)
 		m_string = mid (0, length() - n).std_string();
@@ -55,8 +53,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-String::strip (const List<char>& unwanted) -> String
+String String::strip (const List<char>& unwanted)
 {
 	String copy (m_string);
 
@@ -71,8 +68,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-String::to_uppercase() const -> String
+String String::to_uppercase() const
 {
 	String newstr (m_string);
 
@@ -87,8 +83,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-String::to_lowercase() const -> String
+String String::to_lowercase() const
 {
 	String newstr (m_string);
 
@@ -103,8 +98,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-String::split (char del) const -> StringList
+StringList String::split (char del) const
 {
 	String delimstr;
 	delimstr += del;
@@ -113,8 +107,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-String::split (const String& del) const -> StringList
+StringList String::split (const String& del) const
 {
 	StringList res;
 	int a = 0;
@@ -140,8 +133,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-String::replace (const char* a, const char* b) -> void
+void String::replace (const char* a, const char* b)
 {
 	long pos;
 
@@ -151,8 +143,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-String::count (char needle) const -> int
+int String::count (char needle) const
 {
 	int needles = 0;
 
@@ -165,8 +156,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-String::mid (long a, long b) const -> String
+String String::mid (long a, long b) const
 {
 	if (b == -1 or b > length())
 		b = length();
@@ -190,8 +180,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-String::word_position (int n) const -> int
+int String::word_position (int n) const
 {
 	int count = 0;
 
@@ -208,8 +197,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-String::find (const char* c, int a) const -> int
+int String::find (const char* c, int a) const
 {
 	int pos = m_string.find (c, a);
 
@@ -221,8 +209,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-String::find_last (const char* c, int a) const -> int
+int String::find_last (const char* c, int a) const
 {
 	if (a == -1 or a >= length())
 		a = length() - 1;
@@ -236,8 +223,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-String::to_int (bool* ok, int base) const -> long
+long String::to_int (bool* ok, int base) const
 {
 	errno = 0;
 	char* endptr;
@@ -251,8 +237,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-String::to_float (bool* ok) const -> float
+float String::to_float (bool* ok) const
 {
 	errno = 0;
 	char* endptr;
@@ -266,8 +251,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-String::to_double (bool* ok) const -> double
+double String::to_double (bool* ok) const
 {
 	errno = 0;
 	char* endptr;
@@ -281,8 +265,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-String::operator+ (const String& data) const -> String
+String String::operator+ (const String& data) const
 {
 	String newString = *this;
 	newString.append (data);
@@ -291,8 +274,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-String::operator+ (const char* data) const -> String
+String String::operator+ (const char* data) const
 {
 	String newstr = *this;
 	newstr.append (data);
@@ -301,39 +283,16 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-String::is_numeric() const -> bool
+bool String::is_numeric() const
 {
-	bool gotDot = false;
-
-	for (const char & c : m_string)
-	{
-		// Allow leading hyphen for negatives
-		if (&c == &m_string[0] and c == '-')
-			continue;
-
-		// Check for decimal point
-		if (!gotDot and c == '.')
-		{
-			gotDot = true;
-			continue;
-		}
-
-		if (c >= '0' and c <= '9')
-			continue; // Digit
-
-		// If the above cases didn't catch this character, it was
-		// illegal and this is therefore not a number.
-		return false;
-	}
-
-	return true;
+	char* endptr;
+	strtol (chars(), &endptr, 10);
+	return not (endptr && *endptr);
 }
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-String::ends_with (const String& other) -> bool
+bool String::ends_with (const String& other)
 {
 	if (length() < other.length())
 		return false;
@@ -344,8 +303,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-String::starts_with (const String& other) -> bool
+bool String::starts_with (const String& other)
 {
 	if (length() < other.length())
 		return false;
@@ -355,16 +313,18 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-String::sprintf (const char* fmtstr, ...) -> void
+void String::sprintf (const char* fmtstr, ...)
 {
-	char* buf;
+	char* buf = nullptr;
 	int bufsize = 256;
 	va_list va;
 	va_start (va, fmtstr);
 
 	do
+	{
+		delete[] buf;
 		buf = new char[bufsize];
+	}
 	while (vsnprintf (buf, bufsize, fmtstr, va) >= bufsize);
 
 	va_end (va);
@@ -374,8 +334,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-StringList::join (const String& delim) -> String
+String StringList::join (const String& delim)
 {
 	String result;
 
@@ -392,8 +351,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-String::mask_against (const String& pattern) const -> bool
+bool String::mask_against (const String& pattern) const
 {
 	// Elevate to uppercase for case-insensitive matching
 	String pattern_upper = pattern.to_uppercase();
@@ -441,8 +399,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-String::from_number (short int a) -> String
+String String::from_number (short int a)
 {
 	char buf[32];
 	::sprintf (buf, "%d", a);
@@ -451,8 +408,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-String::from_number (int a) -> String
+String String::from_number (int a)
 {
 	char buf[32];
 	::sprintf (buf, "%d", a);
@@ -461,8 +417,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-String::from_number (long int a) -> String
+String String::from_number (long int a)
 {
 	char buf[32];
 	::sprintf (buf, "%ld", a);
@@ -471,8 +426,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-String::from_number (unsigned short int a) -> String
+String String::from_number (unsigned short int a)
 {
 	char buf[32];
 	::sprintf (buf, "%u", a);
@@ -481,8 +435,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-String::from_number (unsigned int a) -> String
+String String::from_number (unsigned int a)
 {
 	char buf[32];
 	::sprintf (buf, "%u", a);
@@ -491,8 +444,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-String::from_number (unsigned long int a) -> String
+String String::from_number (unsigned long int a)
 {
 	char buf[32];
 	::sprintf (buf, "%lu", a);
@@ -501,8 +453,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-String::from_number (double a) -> String
+String String::from_number (double a)
 {
 	char buf[64];
 	::sprintf (buf, "%f", a);
@@ -511,8 +462,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-String::md5() const -> String
+String String::md5() const
 {
 	char checksum[33];
 	CalculateMD5 (reinterpret_cast<const unsigned char*> (chars()), length(), checksum);
@@ -522,8 +472,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-String::normalize (int (*filter)(int)) -> void
+void String::normalize (int (*filter)(int))
 {
 	int a = 0;
 	int b = length() - 1;

mercurial