Wed, 20 Jul 2016 16:01:10 +0300
Renamed String methods, and reformatted mystring.h
/* 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 { 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* data); void append(char data); void append(const String& data); ConstIterator begin() const; Iterator begin(); int compare(const String &other) const; int count(char needle) const; const char* chars() const; void clear(); ConstIterator end() const; Iterator end(); bool endsWith(const String &other) const; int find(const char* c, int a = 0) const; int find(char ch, int a = 0) const; int indexDifference(int a, int b); void insert(int pos, char c); void insert(int pos, const char* c); bool isEmpty() const; bool isNumeric() const; void modifyIndex(int &a) const; int findLast(const char*c, int a) const; int length() const; bool maskAgainst(const String &pattern) const; String md5() const; String mid(int a, int b) const; void normalize(int(*filter)(int) = &isspace); String normalized(int(*filter)(int) = &isspace) const; void prepend(String a); void remove(int pos, int len); void removeAt(int pos); void removeFromEnd(int len); void removeFromStart(int len); void replace(const char* a, const char* b); void replace(int pos, int n, const String &a); String right(int length) const; void shrinkToFit(); class StringList split(const String &del) const; class StringList split(char del) const; void __cdecl sprintf(const char* fmtstr, ...); bool startsWith(const String &other) const; const std::string& stdString() const; String strip(char unwanted) const; String strip(const List<char> &unwanted) 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 trim(int n); 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); 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; }; 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); inline String operator+(const char* a, const String& b); // -------------------------------------------------------------------------------------------------------------------- // Inline implementations inline String::String() {} inline String::String(char a) { char buffer[2] = { a, '\0' }; m_string = buffer; } inline String::String(const char* data) : m_string(data) {} inline String::String(const std::string& data) : m_string(data) {} inline String::String(const Vector<char>& data) : m_string(data.data(), data.size()) {} inline String::ConstIterator String::begin() const { return m_string.cbegin(); } inline const char* String::chars() const { return m_string.c_str(); } inline String::ConstIterator String::end() const { return m_string.end(); } inline bool String::isEmpty() const { return m_string[0] == '\0'; } inline int String::length() const { return m_string.length(); } inline const std::string& String::stdString() const { return m_string; } inline void String::append(const char* data) { m_string.append(data); } inline void String::append(char data) { m_string.push_back(data); } inline void String::append(const String& data) { m_string.append(data.chars()); } inline String::Iterator String::begin() { return m_string.begin(); } inline void String::clear() { m_string.clear(); } inline String::Iterator String::end() { return m_string.end(); } inline int String::indexDifference(int a, int b) { modifyIndex(a); modifyIndex(b); return b - a; } inline void String::insert(int pos, char c) { m_string.insert(m_string.begin() + pos, c); } inline void String::insert(int pos, const char* c) { m_string.insert(pos, c); } inline void String::modifyIndex(int& a) const { if (a < 0) a = length() - a; } inline void String::prepend(String a) { m_string = (a + m_string).stdString(); } inline void String::remove(int pos, int len) { m_string.replace(pos, len, ""); } inline void String::removeAt(int pos) { m_string.erase(m_string.begin() + pos); } inline void String::removeFromEnd(int len) { remove(length() - len, len); } inline void String::removeFromStart(int len) { remove(0, len); } inline void String::replace(int pos, int n, const String& a) { m_string.replace(pos, n, a.chars()); } inline void String::shrinkToFit() { m_string.shrink_to_fit(); } inline String String::operator+(int num) const { return *this + String::fromNumber(num); } inline String& String::operator+=(const String& data) { append(data); return *this; } inline String& String::operator+=(const char* data) { append(data); return *this; } inline String& String::operator+=(int num) { return operator+=(String::fromNumber(num)); } inline String& String::operator+=(char data) { append(data); return *this; } inline char& String::operator[](int i) { return m_string[i]; } inline char String::operator[](int i) const { return m_string[i]; } inline bool String::operator==(const String& other) const { return stdString() == other.stdString(); } inline bool String::operator==(const char* other) const { return m_string == other; } inline bool String::operator!=(const String& other) const { return stdString() != other.stdString(); } inline bool String::operator!=(const char* other) const { return m_string != other; } inline bool String::operator>(const String& other) const { return stdString() > other.stdString(); } inline bool String::operator<(const String& other) const { return stdString() < other.stdString(); } inline bool String::operator>=(const String& other) const { return stdString() >= other.stdString(); } inline bool String::operator<=(const String& other) const { return stdString() <= other.stdString(); } inline String::operator const char*() const { return chars(); } inline String::operator const std::string&() const { return stdString(); } 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