sources/mystring.h

Tue, 16 Dec 2014 04:06:53 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Tue, 16 Dec 2014 04:06:53 +0200
changeset 64
5e968dc8d552
parent 60
268721e6be20
child 66
bd28a5730fd0
permissions
-rw-r--r--

- set release mode

/*
	Copyright 2014 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"

class String;
class StringList;

// -------------------------------------------------------------------------------------------------
//
class String
{
public:
	String() {}

	explicit String (char a) :
		m_string ({ a, '\0' }) {}

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

	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;

	static METHOD from_number (int a) -> String;
	static METHOD from_number (long a) -> String;
	static METHOD from_number (unsigned long a) -> String;
	static METHOD from_number (double a) -> String;

	       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;

private:
	std::string m_string;
};

// -------------------------------------------------------------------------------------------------
//
class StringList : public List<String>
{
public:
	template<typename... Args>
	StringList (Args... args) :
		List<String> (args...) {}

	METHOD join (const String& delim) -> String;
};

// -------------------------------------------------------------------------------------------------
//
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
{
	return b == a;
}

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

mercurial