sources/mystring.h

Sun, 10 Jan 2016 19:38:11 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sun, 10 Jan 2016 19:38:11 +0200
changeset 110
cad1163333b9
parent 109
e4966d7e615d
child 129
a556ce001e26
permissions
-rw-r--r--

Fix compilation on MSVC 2010

/*
	Copyright 2014 - 2016 Teemu Piippo
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions
	are met:

	1. Redistributions of source code must retain the above copyright
	   notice, this list of conditions and the following disclaimer.
	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.
	3. Neither the name of the copyright holder nor the names of its
	   contributors may be used to endorse or promote products derived from
	   this software without specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
	TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
	PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
	OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
	PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
	PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
	LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
	NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
	SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#pragma once
#include <deque>
#include <string>
#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 (long a, long 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;

	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);

	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; }
	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(); }

private:
	std::string m_string;
};

// -------------------------------------------------------------------------------------------------
//
class StringList : public List<String>
{
public:
	typedef List<String> Super;

	StringList() {}

	StringList (int numvalues) :
		Super (numvalues) {}

	StringList (const Super& other) :
		Super (other) {}

	String join (const String& delim);
};

// -------------------------------------------------------------------------------------------------
//
inline bool operator== (const char* a, const String& b)
{
	return b == a;
}

// -------------------------------------------------------------------------------------------------
//
inline String operator+ (const char* a, const String& b)
{
	return String (a) + b;
}

END_ZFC_NAMESPACE

mercurial