sources/mystring.h

changeset 69
eb4c25284a19
parent 66
bd28a5730fd0
child 73
07dda51a7a8e
--- a/sources/mystring.h	Tue Dec 16 23:50:56 2014 +0200
+++ b/sources/mystring.h	Mon May 04 15:51:03 2015 +0300
@@ -57,77 +57,83 @@
 	String (const Vector<char>& data) :
 		m_string (data.data(), data.size()) {}
 
-	inline METHOD append (const char* data) -> void;
-	inline METHOD append (char data) -> void;
-	inline METHOD append (const String& data) -> void;
-	inline METHOD begin() -> std::string::iterator;
-	inline METHOD begin() const -> std::string::const_iterator;
-	inline METHOD clear() -> void;
-	       METHOD compare (const String& other) const -> int;
-	       METHOD count (char needle) const -> int;
-	inline METHOD chars() const -> const char*;
-	inline METHOD end() -> std::string::iterator;
-	inline METHOD end() const -> std::string::const_iterator;
-	       METHOD ends_with (const String& other) -> bool;
-	       METHOD find (const char* c, int a = 0) const -> int;
-	       METHOD to_lowercase() const -> String;
-	inline METHOD index_difference (int a, int b) -> int;
-	inline METHOD insert (int pos, char c) -> void;
-	inline METHOD insert (int pos, const char* c) -> void;
-	inline METHOD is_empty() const -> bool;
-	       METHOD is_numeric() const -> bool;
-	       METHOD find_last (const char* c, int a = -1) const -> int;
-	inline METHOD length() const -> int;
-	       METHOD mask_against (const String& pattern) const -> bool;
-	       METHOD md5() const -> String;
-	       METHOD mid (long a, long b = -1) const -> String;
-	inline METHOD modify_index (int& a) -> void;
-	       METHOD normalize (int (*filter)(int) = &std::isspace) -> void;
-	inline METHOD prepend (String a) -> void;
-	inline METHOD remove (int pos, int len) -> void;
-	inline METHOD remove_at (int pos) -> void;
-	inline METHOD remove_from_end (int len) -> void;
-	inline METHOD remove_from_start (int len) -> void;
-	       METHOD replace (const char* a, const char* b) -> void;
-	inline METHOD replace (int pos, int n, const String& a) -> void;
-	inline METHOD shrink_to_fit() -> void;
-	       METHOD split (const String& del) const -> StringList;
-	       METHOD split (char del) const -> StringList;
-	       METHOD sprintf (const char* fmtstr, ...) -> void;
-	       METHOD starts_with (const String& other) -> bool;
-	inline METHOD std_string() const -> const std::string&;
-	inline METHOD strip (char unwanted) -> String;
-	       METHOD strip (const List<char>& unwanted) -> String;
-	       METHOD to_double (bool* ok = nullptr) const -> double;
-	       METHOD to_float (bool* ok = nullptr) const -> float;
-	       METHOD to_int (bool* ok = nullptr, int base = 10) const -> long;
-	       METHOD trim (int n) -> void;
-	       METHOD to_uppercase() const -> String;
-	       METHOD word_position (int n) const -> int;
+	using Iterator = std::string::iterator;
+	using ConstIterator = std::string::const_iterator;
+
+	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;
+	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 (long a, long b) const;
+	StringList split (const String &del) const;
+	StringList split (char del) const;
+	const std::string& std_string() const { return m_string; }
+	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;
 
-	static METHOD from_number (short int a) -> String;
-	static METHOD from_number (int a) -> String;
-	static METHOD from_number (long int a) -> String;
-	static METHOD from_number (unsigned short int a) -> String;
-	static METHOD from_number (unsigned int a) -> String;
-	static METHOD from_number (unsigned long int a) -> String;
-	static METHOD from_number (double a) -> String;
+	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) = &std::isspace);
+	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 sprintf (const char* fmtstr, ...);
+	bool starts_with (const String &other);
+	String strip (char unwanted) { return strip ({unwanted}); }
+	String strip (const List<char> &unwanted);
+	void trim (int n);
 
-	       METHOD operator+ (const String& data) const -> String;
-	       METHOD operator+ (const char* data) const -> String;
-	inline METHOD operator+ (int num) const -> String;
-	inline METHOD operator+= (const String& data) -> String&;
-	inline METHOD operator+= (const char* data) -> String&;
-	inline METHOD operator+= (int num) -> String&;
-	inline METHOD operator+= (char data) -> String&;
-	inline METHOD operator== (const String& other) const -> bool;
-	inline METHOD operator== (const char* other) const -> bool;
-	inline METHOD operator!= (const String& other) const -> bool;
-	inline METHOD operator!= (const char* other) const -> bool;
-	inline METHOD operator> (const String& other) const -> bool;
-	inline METHOD operator< (const String& other) const -> bool;
-	inline operator const char*() const;
-	inline operator const std::string&() const;
+	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);
+
+	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; }
+	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(); }
 
 private:
 	std::string m_string;
@@ -138,328 +144,27 @@
 class StringList : public List<String>
 {
 public:
-	template<typename... Args>
-	StringList (Args... args) :
-		List<String> (args...) {}
+	using Super = List<String>;
+	using Super::Super;
+
+	StringList() {}
 
-	METHOD join (const String& delim) -> String;
+	StringList (const Super& other) :
+		Super (other) {}
+
+	String join (const String& delim);
 };
 
 // -------------------------------------------------------------------------------------------------
 //
-inline FUNCTION operator== (const char* a, const String& b) -> bool;
-inline FUNCTION operator+ (const char* a, const String& b) -> String;
-
-// -------------------------------------------------------------------------------------------------
-//
-inline METHOD
-String::is_empty() const -> bool
-{
-	return m_string[0] == '\0';
-}
-
-// -------------------------------------------------------------------------------------------------
-//
-inline METHOD
-String::append (const char* data) -> void
-{
-	m_string.append (data);
-}
-
-// -------------------------------------------------------------------------------------------------
-//
-inline METHOD
-String::append (char data) -> void
-{
-	m_string.push_back (data);
-}
-
-// -------------------------------------------------------------------------------------------------
-//
-inline METHOD
-String::append (const String& data) -> void
-{
-	m_string.append (data.chars());
-}
-
-// -------------------------------------------------------------------------------------------------
-//
-inline METHOD
-String::begin() -> std::string::iterator
-{
-	return m_string.begin();
-}
-
-// -------------------------------------------------------------------------------------------------
-//
-inline METHOD
-String::begin() const -> std::string::const_iterator
-{
-	return m_string.cbegin();
-}
-
-// -------------------------------------------------------------------------------------------------
-//
-inline const char* String::chars() const
-{
-	return m_string.c_str();
-}
-
-// -------------------------------------------------------------------------------------------------
-//
-inline METHOD
-String::end() -> std::string::iterator
-{
-	return m_string.end();
-}
-
-// -------------------------------------------------------------------------------------------------
-//
-inline METHOD
-String::end() const -> std::string::const_iterator
-{
-	return m_string.end();
-}
-
-// -------------------------------------------------------------------------------------------------
-//
-inline METHOD
-String::clear() -> void
-{
-	m_string.clear();
-}
-
-// -------------------------------------------------------------------------------------------------
-//
-inline METHOD
-String::remove_at (int pos) -> void
-{
-	m_string.erase (m_string.begin() + pos);
-}
-
-// -------------------------------------------------------------------------------------------------
-//
-inline METHOD
-String::insert (int pos, char c) -> void
-{
-	m_string.insert (m_string.begin() + pos, c);
-}
-
-// -------------------------------------------------------------------------------------------------
-//
-inline METHOD
-String::insert (int pos, const char* c) -> void
-{
-	m_string.insert (pos, c);
-}
-
-// -------------------------------------------------------------------------------------------------
-//
-inline METHOD
-String::length() const -> int
-{
-	return m_string.length();
-}
-
-// -------------------------------------------------------------------------------------------------
-//
-inline METHOD
-String::remove (int pos, int len) -> void
-{
-	m_string.replace (pos, len, "");
-}
-
-// -------------------------------------------------------------------------------------------------
-//
-inline METHOD
-String::remove_from_start (int len) -> void
-{
-	remove (0, len);
-}
-
-// -------------------------------------------------------------------------------------------------
-//
-inline METHOD
-String::remove_from_end (int len) -> void
-{
-	remove (length() - len, len);
-}
-
-// -------------------------------------------------------------------------------------------------
-//
-inline METHOD
-String::replace (int pos, int n, const String& a) -> void
-{
-	m_string.replace (pos, n, a.chars());
-}
-
-// -------------------------------------------------------------------------------------------------
-//
-inline METHOD
-String::shrink_to_fit() -> void
-{
-	m_string.shrink_to_fit();
-}
-
-// -------------------------------------------------------------------------------------------------
-//
-inline const std::string& String::std_string() const
-{
-	return m_string;
-}
-
-// -------------------------------------------------------------------------------------------------
-//
-inline METHOD
-String::strip (char unwanted) -> String
-{
-	return strip ({unwanted});
-}
-
-// -------------------------------------------------------------------------------------------------
-//
-inline METHOD
-String::operator+ (int num) const -> String
-{
-	return *this + String::from_number (num);
-}
-
-// -------------------------------------------------------------------------------------------------
-//
-inline METHOD
-String::operator+= (const String& data) -> String&
-{
-	append (data);
-	return *this;
-}
-
-// -------------------------------------------------------------------------------------------------
-//
-inline METHOD
-String::operator+= (const char* data) -> String&
-{
-	append (data);
-	return *this;
-}
-
-// -------------------------------------------------------------------------------------------------
-//
-inline METHOD
-String::operator+= (int num) -> String&
-{
-	return operator+= (String::from_number (num));
-}
-
-// -------------------------------------------------------------------------------------------------
-//
-inline METHOD
-String::prepend (String a) -> void
-{
-	m_string = (a + m_string).std_string();
-}
-
-// -------------------------------------------------------------------------------------------------
-
-inline METHOD
-String::operator+= (char data) -> String&
-{
-	append (data);
-	return *this;
-}
-
-// -------------------------------------------------------------------------------------------------
-//
-inline METHOD
-String::operator== (const String& other) const -> bool
-{
-	return std_string() == other.std_string();
-}
-
-// -------------------------------------------------------------------------------------------------
-//
-inline METHOD
-String::operator== (const char* other) const -> bool
-{
-	return operator== (String (other));
-}
-
-// -------------------------------------------------------------------------------------------------
-//
-inline METHOD
-String::operator!= (const String& other) const -> bool
-{
-	return std_string() != other.std_string();
-}
-
-// -------------------------------------------------------------------------------------------------
-//
-inline METHOD
-String::operator!= (const char* other) const -> bool
-{
-	return operator!= (String (other));
-}
-
-// -------------------------------------------------------------------------------------------------
-//
-inline METHOD
-String::operator> (const String& other) const -> bool
-{
-	return std_string() > other.std_string();
-}
-
-// -------------------------------------------------------------------------------------------------
-//
-inline METHOD
-String::operator< (const String& other) const -> bool
-{
-	return std_string() < other.std_string();
-}
-
-// -------------------------------------------------------------------------------------------------
-//
-inline String::operator const char*() const
-{
-	return chars();
-}
-
-// -------------------------------------------------------------------------------------------------
-//
-inline String::operator const std::string&() const
-{
-	return std_string();
-}
-
-// -------------------------------------------------------------------------------------------------
-//
-inline METHOD
-String::modify_index (int& a) -> void
-{
-	if (a < 0)
-		a = length() - a;
-}
-
-// -------------------------------------------------------------------------------------------------
-//
-inline METHOD
-String::index_difference (int a, int b) -> int
-{
-	modify_index (a);
-	modify_index (b);
-	return b - a;
-}
-
-// -------------------------------------------------------------------------------------------------
-//
-inline FUNCTION
-operator== (const char* a, const String& b) -> bool
+inline bool operator== (const char* a, const String& b)
 {
 	return b == a;
 }
 
 // -------------------------------------------------------------------------------------------------
 //
-inline FUNCTION
-operator+ (const char* a, const String& b) -> String
+inline String operator+ (const char* a, const String& b)
 {
 	return String (a) + b;
 }

mercurial