Cleansed the string class, moved implementations from header to code file

Mon, 10 Jun 2013 15:51:08 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Mon, 10 Jun 2013 15:51:08 +0300
changeset 272
2074672a6554
parent 271
d5ec224c1879
child 273
0a9141118630

Cleansed the string class, moved implementations from header to code file

src/common.h file | annotate | diff | comparison | revisions
src/file.cpp file | annotate | diff | comparison | revisions
src/string.cpp file | annotate | diff | comparison | revisions
src/string.h file | annotate | diff | comparison | revisions
src/types.h file | annotate | diff | comparison | revisions
--- a/src/common.h	Mon Jun 10 15:15:23 2013 +0300
+++ b/src/common.h	Mon Jun 10 15:51:08 2013 +0300
@@ -224,19 +224,20 @@
 
 static const double pi = 3.14159265358979323846f;
 
+// -----------------------------------------------------------------------------
 #ifdef IN_IDE_PARSER // KDevelop workarounds:
-#error IN_IDE_PARSER is defined (this code is only for KDevelop workarounds)
+# error IN_IDE_PARSER is defined (this code is only for KDevelop workarounds)
 
 // Current function name
 static const char* __func__ = "";
 
-#ifndef va_start
-#define va_start(va, arg)
-#endif // va_start
+# ifndef va_start
+#  define va_start(va, arg)
+# endif // va_start
 
-#ifndef va_end
-#define va_end(va)
-#endif // va_end
+# ifndef va_end
+#  define va_end(va)
+# endif // va_end
 
 typedef void FILE; // :|
 #endif // IN_IDE_PARSER
--- a/src/file.cpp	Mon Jun 10 15:15:23 2013 +0300
+++ b/src/file.cpp	Mon Jun 10 15:51:08 2013 +0300
@@ -437,10 +437,10 @@
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
 void addRecentFile (str path) {
-	size_t pos = io_recentfiles.value.first (path);
+	long pos = io_recentfiles.value.first (path);
 	
 	// If this file already is in the list, pop it out.
-	if (pos != npos) {
+	if (pos != -1) {
 		if (~io_recentfiles.value == ~path)
 			return; // only recent file - do nothing
 		
--- a/src/string.cpp	Mon Jun 10 15:15:23 2013 +0300
+++ b/src/string.cpp	Mon Jun 10 15:51:08 2013 +0300
@@ -20,6 +20,20 @@
 #include "common.h"
 #include "string.h"
 
+String::String () {}
+
+String::String (const char* data) {
+	m_string = data;
+}
+
+String::String (const QString data) {
+	m_string = data.toStdString ();
+}
+
+String::String (std::string data) {
+	m_string = data;
+}
+
 str fmt (const char* fmtstr, ...) {
 	va_list va;
 	
@@ -59,6 +73,88 @@
 	return buf;
 }
 
+void String::append (const char* data) {
+	m_string.append (data);
+}
+
+void String::append (const char data) {
+	m_string.push_back (data);
+}
+
+void String::append (const String data) {
+	m_string.append (data.chars ());
+}
+
+String::it String::begin () {
+	return m_string.begin ();
+}
+
+String::c_it String::begin () const {
+	return m_string.cbegin ();
+}
+
+const char* String::c () const {
+	return chars ();
+}
+
+size_t String::capacity () const {
+	return m_string.capacity ();
+}
+
+const char* String::chars () const {
+	return m_string.c_str ();
+}
+
+int String::compare (const char* other) const {
+	return m_string.compare (other);
+}
+
+int String::compare (String other) const {
+	return m_string.compare (other);
+}
+
+String::it String::end () {
+	return m_string.end ();
+}
+
+String::c_it String::end () const {
+	return m_string.end ();
+}
+
+void String::clear () {
+	m_string.clear ();
+}
+
+bool String::empty() const {
+	return m_string.empty ();
+}
+
+void String::erase (size_t pos) {
+	m_string.erase (m_string.begin () + pos);
+}
+
+void String::insert (size_t pos, char c) {
+	m_string.insert (m_string.begin () + pos, c);
+}
+
+size_t String::len () const {
+	return m_string.length ();
+}
+
+size_t String::maxSize () const {
+	return m_string.max_size ();
+}
+
+void String::resize (size_t n) {
+	m_string.resize (n);
+}
+
+void String::shrinkToFit () {
+	m_string.shrink_to_fit ();
+}
+
+
+
 void String::trim (short n) {
 	if (n > 0)
 		for (short i = 0; i < n; ++i)
@@ -68,6 +164,10 @@
 			m_string.erase (m_string.begin () + i);
 }
 
+String String::strip (char unwanted) {
+	return strip ({unwanted});
+}
+
 String String::strip (std::initializer_list<char> unwanted) {
 	String copy (m_string);
 	
@@ -112,9 +212,9 @@
 	
 	// Find all separators and store the text left to them.
 	while (1) {
-		size_t b = first (del, a);
+		long b = first (del, a);
 		
-		if (b == npos)
+		if (b == -1)
 			break;
 		
 		String sub = substr (a, b);
@@ -132,9 +232,9 @@
 }
 
 void String::replace (const char* a, const char* b) {
-	size_t pos;
+	long pos;
 	
-	while ((pos = first (a)) != npos)
+	while ((pos = first (a)) != -1)
 		m_string = m_string.replace (pos, strlen (a), b);
 }
 
@@ -224,4 +324,98 @@
 	}
 	
 	return -1;
+}
+
+String String::operator+ (const String data) const {
+	String newstr = *this;
+	newstr += data;
+	return newstr;
+}
+
+String String::operator+ (const char* data) const {
+	String newstr = *this;
+	newstr += data;
+	return newstr;
+}
+
+String& String::operator+= (const String data) {
+	append (data);
+	return *this;
+}
+
+String& String::operator+= (const char* data) {
+	append (data);
+	return *this;
+}
+
+String& String::operator+= (const char data) {
+	append (data);
+	return *this;
+}
+
+String String::operator+ () const {
+	return upper ();
+}
+
+String String::operator- () const {
+	return lower ();
+}
+
+String String::operator- (size_t n) const {
+	String newstr = m_string;
+	newstr -= n;
+	return newstr;
+}
+
+String& String::operator-= (size_t n) {
+	trim (n);
+	return *this;
+}
+
+size_t String::operator~ () const {
+	return len ();
+}
+
+vector<String> String::operator/ (String del) const {
+	return split (del);
+}
+
+char& String::operator[] (size_t n) {
+	return m_string[n];
+}
+
+const char& String::operator[] (size_t n) const {
+	return m_string[n];
+}
+
+bool String::operator== (const String other) const {
+	return compare (other) == 0;
+}
+
+bool String::operator== (const char* other) const {
+	return compare (other) == 0;
+}
+
+bool String::operator!= (const String other) const {
+	return compare (other) != 0;
+}
+
+bool String::operator!= (const char* other) const {
+	return compare (other) != 0;
+}
+
+bool String::operator! () const {
+	return empty ();
+}
+
+String::operator const char* () const {
+	return chars ();
+}
+
+String::operator QString () {
+	return chars ();
+}
+
+String::operator const QString () const {
+	return chars ();
 }
\ No newline at end of file
--- a/src/string.h	Mon Jun 10 15:15:23 2013 +0300
+++ b/src/string.h	Mon Jun 10 15:51:08 2013 +0300
@@ -24,87 +24,79 @@
 #include <QString>
 #include "types.h"
 
-// =========================================================================================================================================
-char* dynafmt (const char* fmtstr, va_list va, ulong size);
-
-// =========================================================================================================================================
 typedef class String {
 public:
-#define STR_COMPARE_OPERATOR(T, OP) (T other) const { return compare (other) OP 0; }
-	
 	typedef typename std::string::iterator it;
 	typedef typename std::string::const_iterator c_it;
 	typedef vector<String> stringlist;
 	
-	String () {}
-	String (const char* data) { m_string = data; }
-	String (const QString data) { m_string = data.toStdString (); }
-	String (std::string data) { m_string = data; }
+	String ();
+	String (const char* data);
+	String (const QString data);
+	String (std::string data);
 	
-	void		append			(const char* data) { m_string.append (data); }
-	void		append			(const char data) { m_string.push_back (data); }
-	void		append			(const String data) { m_string.append (data.chars ()); }
-	it			begin			() { return m_string.begin (); }
-	c_it		begin			() const { return m_string.cbegin (); }
-	const char*	c				() const { return chars (); }
-	size_t		capacity		() const { return m_string.capacity (); }
-	const char*	chars			() const { return m_string.c_str (); }
-	int			compare			(const char* other) const { return m_string.compare (other); }
-	int			compare			(String other) const { return m_string.compare (other); }
-	it			end				() { return m_string.end (); }
-	c_it		end				() const { return m_string.end (); }
-	void		clear			() { m_string.clear (); }
-	ushort		count			(const char needle) const;
-	bool		empty			() const { return m_string.empty (); }
-	void		erase			(size_t pos) { m_string.erase (m_string.begin () + pos); }
-	int			first			(const char* c, int a = 0) const;
-	void		format			(const char* fmtstr, ...);
-	void		insert			(size_t pos, char c) { m_string.insert (m_string.begin () + pos, c); }
-	int			last			(const char* c, int a = -1) const;
-	size_t		len				() const { return m_string.length (); }
-	String		lower			() const;
-	size_t		maxSize			() const { return m_string.max_size (); }
-	void		replace			(const char* a, const char* b);
-	void		resize			(size_t n) { m_string.resize (n); }
-	void		shrinkToFit		() { m_string.shrink_to_fit (); }
-	stringlist	split			(String del) const;
-	stringlist	split			(char del) const;
-	String		strip			(char unwanted) { return strip ({unwanted}); }
-	String		strip			(std::initializer_list<char> unwanted);
-	String		substr			(long a, long b) const;
-	void		trim			(short n);
-	String		upper			() const;
-	void		writeToFile		(FILE* fp) const { fwrite (chars (), 1, len (), fp); }
+	void           append         (const char* data);
+	void           append         (const char data);
+	void           append         (const String data);
+	it             begin          ();
+	c_it           begin          () const;
+	const char*    c              () const;
+	size_t         capacity       () const;
+	const char*    chars          () const;
+	int            compare        (const char* other) const;
+	int            compare        (String other) const;
+	it             end            ();
+	c_it           end            () const;
+	void           clear          ();
+	ushort         count          (const char needle) const;
+	bool           empty          () const;
+	void           erase          (size_t pos);
+	int            first          (const char* c, int a = 0) const;
+	void           format         (const char* fmtstr, ...);
+	void           insert         (size_t pos, char c);
+	int            last           (const char* c, int a = -1) const;
+	size_t         len            () const;
+	String         lower          () const;
+	size_t         maxSize        () const;
+	void           replace        (const char* a, const char* b);
+	void           resize         (size_t n);
+	void           shrinkToFit    ();
+	stringlist     split          (String del) const;
+	stringlist     split          (char del) const;
+	String         strip          (char unwanted);
+	String         strip          (std::initializer_list<char> unwanted);
+	String         substr         (long a, long b) const;
+	void           trim           (short n);
+	String         upper          () const;
 	
-	String		operator+		(const String data) const { String newstr = *this; newstr += data; return newstr; }
-	String		operator+		(const char* data) const { String newstr = *this; newstr += data; return newstr; }
-	String&		operator+=		(const String data) { append (data); return *this; }
-	String&		operator+=		(const char* data) { append (data); return *this; }
-	String&		operator+=		(const char data) { append (data); return *this; }
-	String		operator+		() const { return upper (); }
-	String		operator-		() const { return lower (); }
-	String		operator-		(short n) const { String newstr = m_string; newstr -= n; return newstr; }
-	String&		operator-=		(short n) { trim (n); return *this; }
-	size_t		operator~		() const { return len (); }
-	vector<String> operator/	(String del) const { return split (del); }
-	char&		operator[]		(size_t n) { return m_string[n]; }
-	const char&	operator[]		(size_t n) const { return m_string[n]; }
-	bool		operator==		STR_COMPARE_OPERATOR (const char*, ==)
-	bool		operator==		STR_COMPARE_OPERATOR (String, ==)
-	bool		operator!=		STR_COMPARE_OPERATOR (const char*, !=)
-	bool		operator!=		STR_COMPARE_OPERATOR (String,  !=)
-	bool		operator!		() const { return empty (); }
-	operator const char*		() const { return chars (); }
-	operator QString			() { return chars (); }
-	operator const QString	() const { return chars (); }
+	String         operator+      (const String data) const;
+	String         operator+      (const char* data) const;
+	String&        operator+=     (const String data);
+	String&        operator+=     (const char* data);
+	String&        operator+=     (const char data);
+	String         operator+      () const;
+	String         operator-      () const;
+	String         operator-      (size_t n) const;
+	String&        operator-=     (size_t n);
+	size_t         operator~      () const;
+	vector<String> operator/      (String del) const;
+	char&          operator[]     (size_t n);
+	const char&    operator[]     (size_t n) 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;
+	operator const char*          () const;
+	operator QString              ();
+	operator const QString        () const;
 	
 private:
 	std::string m_string;
 } str;
 
-static const std::size_t npos = std::string::npos;
-
-// =========================================================================================================================================
-str fmt (const char* fmtstr, ...);
+// Accessories
+char*          dynafmt        (const char* fmtstr, va_list va, ulong size);
+str            fmt            (const char* fmtstr, ...);
 
 #endif // STR_H
\ No newline at end of file
--- a/src/types.h	Mon Jun 10 15:15:23 2013 +0300
+++ b/src/types.h	Mon Jun 10 15:51:08 2013 +0300
@@ -42,6 +42,7 @@
 typedef uint64_t uint64;
 
 template<class T> using initlist = std::initializer_list<T>;
+using std::size_t;
 
 enum Axis { X, Y, Z };
 static const Axis g_Axes[3] = {X, Y, Z};

mercurial