sources/mystring.h

changeset 182
20ca0a6be175
parent 179
7fc34735178e
child 183
9b6a0daedfc0
--- a/sources/mystring.h	Wed Jan 27 13:17:11 2021 +0200
+++ b/sources/mystring.h	Wed Jan 27 14:04:53 2021 +0200
@@ -37,544 +37,21 @@
 
 BEGIN_ZFC_NAMESPACE
 
-using StringList = std::vector<class String>;
-
-class String
-{
-public:
-	typedef std::string::iterator Iterator;
-	typedef std::string::const_iterator ConstIterator;
-
-	String();
-	String(char a);
-	String(const char* data);
-	String(const std::string& data);
-	String(const Vector<char>& data);
-
-	void                        append(const char* text);
-	void                        append(char character);
-	void                        append(const String& text);
-	ConstIterator               begin() const;
-	Iterator                    begin();
-	int                         compare(const String &other) const;
-	int                         count(char character) const;
-	const char*                 chars() const;
-	void                        clear();
-	ConstIterator               end() const;
-	Iterator                    end();
-	bool                        endsWith(const String &other) const;
-	int                         find(const char* subString, int startingPosition = 0) const;
-	int                         find(char character, int startingPosition = 0) const;
-	int                         indexDifference(int a, int b);
-	void                        insert(int position, char character);
-	void                        insert(int position, const char* string);
-	bool                        isEmpty() const;
-	bool                        isNumeric() const;
-	void                        modifyIndex(int &a) const;
-	int                         findLast(const char* subString, int startingPosition = -1) const;
-	int                         length() const;
-	bool                        maskAgainst(const String &pattern) const;
-	String                      md5() const;
-	String                      mid(int rangeBegin, int rangeEnd) const;
-	void                        normalize(int(*filter)(int) = &isspace);
-	String                      normalized(int(*filter)(int) = &isspace) const;
-	void                        prepend(String text);
-	void                        remove(int position, int length);
-	void                        removeAt(int position);
-	void                        removeFromEnd(int length);
-	void                        removeFromStart(int length);
-	void                        replace(const char* text, const char* replacement);
-	void                        replace(int position, int amount, const String &text);
-	String                      right(int length) const;
-	void                        shrinkToFit();
-	StringList            split(const String &delimeter) const;
-	StringList            split(char delimeter) const;
-	void __cdecl                sprintf(const char* fmtstr, ...);
-	bool                        startsWith(const String &other) const;
-	const std::string&          stdString() const;
-	const unsigned char*        toBytes() 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                        vsprintf(const char* fmtstr, va_list args);
-
-	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);
-	static String               fromBytes(const ByteArray& bytes);
-
-	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;
-};
-
-String join_string_list(const StringList& strings, const String& delim);
-
-inline bool operator==(const char* a, const String& b);
-inline String operator+(const char* a, const String& b);
-
-// --------------------------------------------------------------------------------------------------------------------
-
-/*!
- * \brief Constructs an empty string.
- */
-inline String::String() {}
-
-/*!
- * \brief Constructs a string from a single character.
- * \param character Character to create a string out of.
- */
-inline String::String(char character)
-{
-	char buffer[2] = { character, '\0' };
-	m_string = buffer;
-}
-
-/*!
- * \brief Constructs a string from a char-array.
- * \param string char-array to convert.
- */
-inline String::String(const char* string) :
-	m_string(string) {}
-
-/*!
- * \brief Constructs a string out of a \c std::string .
- * \param string \c std::string to base the construction on.
- */
-inline String::String(const std::string& string) :
-	m_string(string) {}
-
-/*!
- * \brief Constructs a string out of a vector of characters. The vector does not have to be null-terminated.
- * \param charVector Vector of characters to construct the string out of.
- */
-inline String::String(const Vector<char>& charVector) :
-	m_string(charVector.data(), charVector.size()) {}
-
-/*!
- * \returns a constant iterator to the beginning of the string.
- */
-inline String::ConstIterator String::begin() const
-{
-	return m_string.cbegin();
-}
-
-/*!
- * \returns the string's contents as a char-array.
- */
-inline const char* String::chars() const
-{
-	return m_string.c_str();
-}
-
-/*!
- * \returns the string's constant end-iterator.
- */
-inline String::ConstIterator String::end() const
-{
-	return m_string.end();
-}
-
-/*!
- * \returns whether or not the string is empty.
- */
-inline bool String::isEmpty() const
-{
-	return m_string[0] == '\0';
-}
-
-/*!
- * \returns the length of the string.
- */
-inline int String::length() const
-{
-	return m_string.length();
-}
-
-/*!
- * \returns the underlying \c std::string .
- */
-inline const std::string& String::stdString() const
-{
-	return m_string;
-}
-
-/*!
- * \brief Adds text from a char-array to the end of the string.
- * \param text Text to append.
- */
-inline void String::append(const char* text)
-{
-	m_string.append(text);
-}
-
-/*!
- * \brief Adds text to the end of the string.
- * \param text Text to append.
- */
-inline void String::append(char character)
-{
-	m_string.push_back(character);
-}
-
-/*!
- * \brief Adds text from another string to the end of this string.
- * \param text Text to append.
- */
-inline void String::append(const String& text)
-{
-	m_string.append(text.chars());
-}
-
-/*!
- * \returns a mutable iterator to the beginning of the string.
- */
-inline String::Iterator String::begin()
-{
-	return m_string.begin();
-}
-
-/*!
- * \brief Clears the string.
- */
-inline void String::clear()
-{
-	m_string.clear();
-}
-
-/*!
- * \returns the string's mutable end-iterator.
- */
-inline String::Iterator String::end()
-{
-	return m_string.end();
-}
-
-/*!
- * \brief Compares two string indices, supporting negatives as offsets from the end of string.
- * \param a First index to compare
- * \param b Second index to compare
- * \returns the difference of two indices.
- */
-inline int String::indexDifference(int a, int b)
-{
-	modifyIndex(a);
-	modifyIndex(b);
-	return b - a;
-}
-
-/*!
- * \brief Inserts a character into the string.
- * \param position Position in the string where to insert the character into.
- * \param character Character to insert into the string.
- */
-inline void String::insert(int position, char character)
-{
-	m_string.insert(m_string.begin() + position, character);
-}
+using String = std::string;
+using StringList = std::vector<std::string>;
+using namespace std::string_literals;
 
-/*!
- * \brief Inserts a substring into the string.
- * \param position Position in the string where to insert the substring.
- * \param string Substring to insert.
- */
-inline void String::insert(int position, const char* string)
-{
-	m_string.insert(position, string);
-}
-
-/*!
- * \brief Modifies the given index so that if it is negative, it is translated into a positive index starting from the
- *        end of the string. For example, an index of -1 will be modified to point to the last character in the string,
- *        -2 to the second last, etc.
- * \param index Index to translate.
- */
-inline void String::modifyIndex(int& index) const
-{
-	if (index < 0)
-		index = length() - index;
-}
-
-/*!
- * \brief Prepends the given text to the beginning of the string.
- * \param text Text to prepend.
- */
-inline void String::prepend(String text)
-{
-	m_string = (text + m_string).stdString();
-}
-
-/*!
- * \brief Removes a range of text from the string.
- * \param position Position where to start removing text.
- * \param length Amount of characters to remove.
- */
-inline void String::remove(int position, int length)
-{
-	m_string.replace(position, length, "");
-}
-
-/*!
- * \brief Removes a single character from the string.
- * \param position Position of the character to remove string from.
- */
-inline void String::removeAt(int position)
-{
-	m_string.erase(m_string.begin() + position);
-}
-
-/*!
- * \brief Removes a number of characters from the end of the string.
- * \param length Amount of characters to remove.
- */
-inline void String::removeFromEnd(int length)
-{
-	remove(this->length() - length, length);
-}
-
-/*!
- * \brief Removes a number of characters from the beginning of the string.
- * \param length Amount of characters to remove.
- */
-inline void String::removeFromStart(int length)
-{
-	remove(0, length);
-}
-
-/*!
- * \brief Replaces a range of text in the string with another.
- * \param position Position where to start replacing text.
- * \param amount Amount of characters to replace.
- * \param text Replacement string.
- */
-inline void String::replace(int position, int amount, const String& text)
-{
-	m_string.replace(position, amount, text.chars());
-}
-
-/*!
- * \brief Shrinks the string so that it does not allocate more characters than necessary.
- */
-inline void String::shrinkToFit()
-{
-	m_string.shrink_to_fit();
-}
-
-/*!
- * \brief Converts a number into a string, and returns a new string with the number appended to the end of the string.
- * \param number Number to convert and append.
- * \returns the resulting string.
- */
-inline String String::operator+(int number) const
-{
-	return *this + String::fromNumber(number);
-}
-
-/*!
- * \brief Appends text into the string.
- * \param text Text to append.
- * \returns a reference to this string.
- */
-inline String& String::operator+=(const String& text)
-{
-	append(text);
-	return *this;
-}
-
-/*!
- * \brief Appends text into the string.
- * \param text Text to append.
- * \returns a reference to this string.
- */
-inline String& String::operator+=(const char* text)
-{
-	append(text);
-	return *this;
-}
-
-/*!
- * \brief Converts a number into a string, and appends it into this string.
- * \param number The number to append.
- * \returns a refence to this string.
- */
-inline String& String::operator+=(int number)
-{
-	return operator+=(String::fromNumber(number));
-}
-
-/*!
- * \brief Appends a character into this string.
- * \param character The character to append.
- * \return a reference to this string.
- */
-inline String& String::operator+=(char character)
-{
-	append(character);
-	return *this;
-}
-
-/*!
- * \param index Index referring to a character of this string.
- * \returns an editable reference to the character pointed by the given index.
- */
-inline char& String::operator[](int index)
-{
-	return m_string[index];
-}
-
-/*!
- * \param index Index referring to a character of this string.
- * \returns an const reference to the character pointed by the given index.
- */
-inline char String::operator[](int index) const
-{
-	return m_string[index];
-}
-
-/*!
- * \param other String to compare with.
- * \returns whether or not this string is the same as the other string.
- */
-inline bool String::operator==(const String& other) const
-{
-	return stdString() == other.stdString();
-}
-
-/*!
- * \param other String to compare with.
- * \returns whether or not this string is the same as the other string.
- */
-inline bool String::operator==(const char* other) const
-{
-	return m_string == other;
-}
-
-/*!
- * \param other String to compare with.
- * \returns whether or not this string is different than the other string.
- */
-inline bool String::operator!=(const String& other) const
-{
-	return stdString() != other.stdString();
-}
-
-/*!
- * \param other String to compare with.
- * \returns whether or not this string is different than the other string.
- */
-inline bool String::operator!=(const char* other) const
-{
-	return m_string != other;
-}
-
-/*!
- * \param other String to compare with.
- * \return whether or not this string is lexicographically greater than the other string.
- */
-inline bool String::operator>(const String& other) const
-{
-	return stdString() > other.stdString();
-}
-
-/*!
- * \param other String to compare with.
- * \return whether or not this string is lexicographically lesser than the other string.
- */
-inline bool String::operator<(const String& other) const
-{
-	return stdString() < other.stdString();
-}
-
-/*!
- * \param other String to compare with.
- * \return whether or not this string is lexicographically at least as great as the other string.
- */
-inline bool String::operator>=(const String& other) const
-{
-	return stdString() >= other.stdString();
-}
-
-/*!
- * \param other String to compare with.
- * \return whether or not this string is lexicographically at most as great as the other string.
- */
-inline bool String::operator<=(const String& other) const
-{
-	return stdString() <= other.stdString();
-}
-
-/*!
- * \returns a char-array representation of this string.
- */
-inline String::operator const char*() const
-{
-	return chars();
-}
-
-/*!
- * \returns the underlying \c std::string of this string.
- */
-inline String::operator const std::string&() const
-{
-	return stdString();
-}
-
-/*!
- * \returns the underlying char-array representation of this string, casted to unsigned chars.
- */
-inline const unsigned char* String::toBytes() const
-{
-	return reinterpret_cast<const unsigned char*>(chars());
-}
-
-/*!
- * \brief An \c operator== implementation that allows a char-array to be at the left side of a string comparison
- *        with a \c String.
- * \param one A char-array representation of a string to compare.
- * \param other A string to compare.
- * \returns whether or not the two parameters are equal.
- */
-inline bool operator==(const char* one, const String& other)
-{
-	return other == one;
-}
-
-/*!
- * \brief An \c operator+ implementation that allows a char-array to be at the left side of a string catenation
- *        with a \c String.
- * \param one A char-array representation of a string to catenate.
- * \param other A string to catenate.
- * \returns the catenated string.
- */
-inline String operator+(const char* one, const String& other)
-{
-	return String(one) + other;
-}
-
+std::string to_lowercase(const std::string& string);
+std::string join_string_list(const StringList& strings, const String& delim);
+std::string mid(const std::string& str, int rangeBegin, int rangeEnd);
+std::string right(const std::string& str, int length);
+std::string vsprintf(const char* formatString, va_list args);
+std::string __cdecl sprintf(const char* formatString, ...);
+std::string remove_range(const std::string& string, int start, int end);
+void replace_all(std::string& str, const char* text, const char* replacement);
+bool starts_with(const std::string& str, const String& other);
+StringList split(const std::string& string, const String& delimeter);
+std::optional<long> to_int(const char* str, int base = 10);
+void normalize(std::string& string, int (*filter)(int) = std::isspace);
 
 END_ZFC_NAMESPACE

mercurial