sources/mystring.h

changeset 145
d0aedc9be448
parent 129
a556ce001e26
child 146
81357dcd3da4
--- a/sources/mystring.h	Wed Jul 20 15:07:57 2016 +0300
+++ b/sources/mystring.h	Wed Jul 20 16:01:10 2016 +0300
@@ -34,123 +34,106 @@
 #include <stdarg.h>
 #include "basics.h"
 #include "list.h"
+
 BEGIN_ZFC_NAMESPACE
 
-class String;
-class StringList;
 
-// -------------------------------------------------------------------------------------------------
-//
 class String
 {
 public:
-	String() {}
-
-	String (char a)
-	{
-		char buffer[2] = { a, '0' };
-		m_string = buffer;
-	}
-
-	String (const char* data) :
-		m_string (data) {}
-
-	String (const std::string& data) :
-		m_string (data) {}
-
-	String (const Vector<char>& data) :
-		m_string (data.data(), data.size()) {}
-
 	typedef std::string::iterator Iterator;
 	typedef std::string::const_iterator ConstIterator;
 
-	ConstIterator begin() const { return m_string.cbegin(); }
-	int compare (const String &other) const;
-	int count (char needle) const;
-	const char* chars() const { return m_string.c_str(); }
-	ConstIterator end() const { return m_string.end(); }
-	int find (const char* c, int a = 0) const;
-	int find (char ch, int a = 0) const;
-	bool is_empty() const { return m_string[0] == '\0'; }
-	bool is_numeric() const;
-	int find_last (const char*c, int a) const;
-	int length() const { return m_string.length(); }
-	bool mask_against (const String &pattern) const;
-	String md5() const;
-	String mid (int a, int b) const;
-	String right (int length) const;
-	StringList split (const String &del) const;
-	StringList split (char del) const;
-	const std::string& std_string() const { return m_string; }
-	String strip (char unwanted) const;
-	String strip (const List<char> &unwanted) const;
-	double to_double (bool* ok = nullptr) const;
-	float to_float (bool* ok = nullptr) const;
-	long to_int (bool* ok = nullptr, int base = 10) const;
-	String to_lowercase() const;
-	String to_uppercase() const;
-	int word_position (int n) const;
+	String();
+	String(char a);
+	String(const char* data);
+	String(const std::string& data);
+	String(const Vector<char>& data);
 
-	void append (const char* data) { m_string.append (data); }
-	void append (char data) { m_string.push_back (data); }
-	void append (const String& data) { m_string.append (data.chars()); }
-	Iterator begin() { return m_string.begin(); }
-	void clear() { m_string.clear(); }
-	Iterator end() { return m_string.end(); }
-	bool ends_with (const String &other);
-	int index_difference (int a, int b) { modify_index (a); modify_index (b); return b - a; }
-	void insert (int pos, char c) { m_string.insert (m_string.begin() + pos, c); }
-	void insert (int pos, const char*c) { m_string.insert (pos, c); }
-	void modify_index (int &a) { if (a < 0) { a = length() - a; } }
-	void normalize (int (*filter)(int) = &isspace);
-	String normalized (int (*filter)(int) = &isspace) const;
-	void prepend (String a) { m_string = (a + m_string).std_string(); }
-	void remove (int pos, int len) { m_string.replace (pos, len, ""); }
-	void remove_at (int pos) { m_string.erase (m_string.begin() + pos); }
-	void remove_from_end (int len) { remove (length() - len, len); }
-	void remove_from_start (int len) { remove (0, len); }
-	void replace (const char* a, const char* b);
-	void replace (int pos, int n, const String &a) { m_string.replace (pos, n, a.chars()); }
-	void shrink_to_fit() { m_string.shrink_to_fit(); }
-	void __cdecl sprintf (const char* fmtstr, ...);
-	void vsprintf (const char* fmtstr, va_list args);
-	bool starts_with (const String &other) const;
-	void trim (int n);
+	void                        append(const char* data);
+	void                        append(char data);
+	void                        append(const String& data);
+	ConstIterator               begin() const;
+	Iterator                    begin();
+	int                         compare(const String &other) const;
+	int                         count(char needle) const;
+	const char*                 chars() const;
+	void                        clear();
+	ConstIterator               end() const;
+	Iterator                    end();
+	bool                        endsWith(const String &other) const;
+	int                         find(const char* c, int a = 0) const;
+	int                         find(char ch, int a = 0) const;
+	int                         indexDifference(int a, int b);
+	void                        insert(int pos, char c);
+	void                        insert(int pos, const char* c);
+	bool                        isEmpty() const;
+	bool                        isNumeric() const;
+	void                        modifyIndex(int &a) const;
+	int                         findLast(const char*c, int a) const;
+	int                         length() const;
+	bool                        maskAgainst(const String &pattern) const;
+	String                      md5() const;
+	String                      mid(int a, int b) const;
+	void                        normalize(int(*filter)(int) = &isspace);
+	String                      normalized(int(*filter)(int) = &isspace) const;
+	void                        prepend(String a);
+	void                        remove(int pos, int len);
+	void                        removeAt(int pos);
+	void                        removeFromEnd(int len);
+	void                        removeFromStart(int len);
+	void                        replace(const char* a, const char* b);
+	void                        replace(int pos, int n, const String &a);
+	String                      right(int length) const;
+	void                        shrinkToFit();
+	class StringList            split(const String &del) const;
+	class StringList            split(char del) const;
+	void __cdecl                sprintf(const char* fmtstr, ...);
+	bool                        startsWith(const String &other) const;
+	const std::string&          stdString() const;
+	String                      strip(char unwanted) const;
+	String                      strip(const List<char> &unwanted) const;
+	double                      toDouble(bool* ok = nullptr) const;
+	float                       toFloat(bool* ok = nullptr) const;
+	long                        toInt(bool* ok = nullptr, int base = 10) const;
+	String                      toLowerCase() const;
+	String                      toUpperCase() const;
+	void                        trim(int n);
+	void                        vsprintf(const char* fmtstr, va_list args);
 
-	static String from_number (short int a);
-	static String from_number (int a);
-	static String from_number (long int a);
-	static String from_number (unsigned short int a);
-	static String from_number (unsigned int a);
-	static String from_number (unsigned long int a);
-	static String from_number (double a);
+	static String               fromNumber(short int a);
+	static String               fromNumber(int a);
+	static String               fromNumber(long int a);
+	static String               fromNumber(unsigned short int a);
+	static String               fromNumber(unsigned int a);
+	static String               fromNumber(unsigned long int a);
+	static String               fromNumber(double a);
 
-	String operator+ (const String& data) const;
-	String operator+ (const char* data) const;
-	String operator+ (int num) const { return *this + String::from_number (num); }
-	String& operator+= (const String& data) { append (data); return *this; }
-	String& operator+= (const char* data) { append (data); return *this; }
-	String& operator+= (int num) { return operator+= (String::from_number (num)); }
-	String& operator+= (char data) { append (data); return *this; }
-	char& operator[] (int i) { return m_string[i]; }
-	char operator[] (int i) const { return m_string[i]; }
-	bool operator== (const String& other) const { return std_string() == other.std_string(); }
-	bool operator== (const char* other) const { return m_string == other; }
-	bool operator!= (const String& other) const { return std_string() != other.std_string(); }
-	bool operator!= (const char* other) const { return m_string != other; }
-	bool operator> (const String& other) const { return std_string() > other.std_string(); }
-	bool operator< (const String& other) const { return std_string() < other.std_string(); }
-	bool operator>= (const String& other) const { return std_string() >= other.std_string(); }
-	bool operator<= (const String& other) const { return std_string() <= other.std_string(); }
-	operator const char*() const { return chars(); }
-	operator const std::string&() const { return std_string(); }
+	String                      operator+(const String& data) const;
+	String                      operator+(const char* data) const;
+	String                      operator+(int num) const;
+	String&                     operator+=(const String& data);
+	String&                     operator+=(const char* data);
+	String&                     operator+=(int num);
+	String&                     operator+=(char data);
+	char&                       operator[](int i);
+	char                        operator[](int i) const;
+	bool                        operator==(const String& other) const;
+	bool                        operator==(const char* other) const;
+	bool                        operator!=(const String& other) const;
+	bool                        operator!=(const char* other) const;
+	bool                        operator>(const String& other) const;
+	bool                        operator<(const String& other) const;
+	bool                        operator>=(const String& other) const;
+	bool                        operator<=(const String& other) const;
+	                            operator const char*() const;
+	                            operator const std::string&() const;
 
 private:
 	std::string m_string;
 };
 
-// -------------------------------------------------------------------------------------------------
-//
+
 class StringList : public List<String>
 {
 public:
@@ -158,27 +141,302 @@
 
 	StringList() {}
 
-	StringList (int numvalues) :
-		Super (numvalues) {}
+	StringList(int numvalues) :
+		Super(numvalues) {}
 
-	StringList (const Super& other) :
-		Super (other) {}
+	StringList(const Super& other) :
+		Super(other) {}
 
-	String join (const String& delim);
+	String join(const String& delim);
 };
 
-// -------------------------------------------------------------------------------------------------
-//
-inline bool operator== (const char* a, const String& b)
+
+inline bool operator==(const char* a, const String& b);
+inline String operator+(const char* a, const String& b);
+
+// --------------------------------------------------------------------------------------------------------------------
+// Inline implementations
+
+
+inline String::String() {}
+
+
+inline String::String(char a)
+{
+	char buffer[2] = { a, '\0' };
+	m_string = buffer;
+}
+
+
+inline String::String(const char* data) :
+	m_string(data) {}
+
+
+inline String::String(const std::string& data) :
+	m_string(data) {}
+
+
+inline String::String(const Vector<char>& data) :
+	m_string(data.data(), data.size()) {}
+
+
+inline String::ConstIterator String::begin() const
+{
+	return m_string.cbegin();
+}
+
+
+inline const char* String::chars() const
+{
+	return m_string.c_str();
+}
+
+
+inline String::ConstIterator String::end() const
+{
+	return m_string.end();
+}
+
+
+inline bool String::isEmpty() const
+{
+	return m_string[0] == '\0';
+}
+
+
+inline int String::length() const
+{
+	return m_string.length();
+}
+
+
+inline const std::string& String::stdString() const
+{
+	return m_string;
+}
+
+
+inline void String::append(const char* data)
+{
+	m_string.append(data);
+}
+
+
+inline void String::append(char data)
+{
+	m_string.push_back(data);
+}
+
+
+inline void String::append(const String& data)
+{
+	m_string.append(data.chars());
+}
+
+
+inline String::Iterator String::begin()
+{
+	return m_string.begin();
+}
+
+
+inline void String::clear()
+{
+	m_string.clear();
+}
+
+
+inline String::Iterator String::end()
+{
+	return m_string.end();
+}
+
+
+inline int String::indexDifference(int a, int b)
+{
+	modifyIndex(a);
+	modifyIndex(b);
+	return b - a;
+}
+
+
+inline void String::insert(int pos, char c)
+{
+	m_string.insert(m_string.begin() + pos, c);
+}
+
+
+inline void String::insert(int pos, const char* c)
+{
+	m_string.insert(pos, c);
+}
+
+
+inline void String::modifyIndex(int& a) const
+{
+	if (a < 0)
+		a = length() - a;
+}
+
+
+inline void String::prepend(String a)
+{
+	m_string = (a + m_string).stdString();
+}
+
+
+inline void String::remove(int pos, int len)
+{
+	m_string.replace(pos, len, "");
+}
+
+
+inline void String::removeAt(int pos)
+{
+	m_string.erase(m_string.begin() + pos);
+}
+
+
+inline void String::removeFromEnd(int len)
+{
+	remove(length() - len, len);
+}
+
+
+inline void String::removeFromStart(int len)
+{
+	remove(0, len);
+}
+
+
+inline void String::replace(int pos, int n, const String& a)
+{
+	m_string.replace(pos, n, a.chars());
+}
+
+
+inline void String::shrinkToFit()
+{
+	m_string.shrink_to_fit();
+}
+
+
+inline String String::operator+(int num) const
+{
+	return *this + String::fromNumber(num);
+}
+
+
+inline String& String::operator+=(const String& data)
+{
+	append(data);
+	return *this;
+}
+
+
+inline String& String::operator+=(const char* data)
+{
+	append(data);
+	return *this;
+}
+
+
+inline String& String::operator+=(int num)
+{
+	return operator+=(String::fromNumber(num));
+}
+
+
+inline String& String::operator+=(char data)
+{
+	append(data);
+	return *this;
+}
+
+
+inline char& String::operator[](int i)
+{
+	return m_string[i];
+}
+
+
+inline char String::operator[](int i) const
+{
+	return m_string[i];
+}
+
+
+inline bool String::operator==(const String& other) const
+{
+	return stdString() == other.stdString();
+}
+
+
+inline bool String::operator==(const char* other) const
+{
+	return m_string == other;
+}
+
+
+inline bool String::operator!=(const String& other) const
+{
+	return stdString() != other.stdString();
+}
+
+
+inline bool String::operator!=(const char* other) const
+{
+	return m_string != other;
+}
+
+
+inline bool String::operator>(const String& other) const
+{
+	return stdString() > other.stdString();
+}
+
+
+inline bool String::operator<(const String& other) const
+{
+	return stdString() < other.stdString();
+}
+
+
+inline bool String::operator>=(const String& other) const
+{
+	return stdString() >= other.stdString();
+}
+
+
+inline bool String::operator<=(const String& other) const
+{
+	return stdString() <= other.stdString();
+}
+
+
+inline String::operator const char*() const
+{
+	return chars();
+}
+
+
+inline String::operator const std::string&() const
+{
+	return stdString();
+}
+
+
+
+inline bool operator==(const char* a, const String& b)
 {
 	return b == a;
 }
 
-// -------------------------------------------------------------------------------------------------
-//
-inline String operator+ (const char* a, const String& b)
+
+inline String operator+(const char* a, const String& b)
 {
-	return String (a) + b;
+	return String(a) + b;
 }
 
+
 END_ZFC_NAMESPACE

mercurial