# HG changeset patch # User Teemu Piippo # Date 1469019670 -10800 # Node ID d0aedc9be4486cfcb1d4ac3c119cf2ba2ff6d030 # Parent e8d58327cd7f815bcbedbf1d06c8f059d127b0f9 Renamed String methods, and reformatted mystring.h diff -r e8d58327cd7f -r d0aedc9be448 sources/coloredline.cpp --- a/sources/coloredline.cpp Wed Jul 20 15:07:57 2016 +0300 +++ b/sources/coloredline.cpp Wed Jul 20 16:01:10 2016 +0300 @@ -127,11 +127,11 @@ { if (ch == ']') { - String color = m_incomingColorName.to_lowercase(); + String color = m_incomingColorName.toLowerCase(); for (const ColorCodeInfo &colorInfo : colorCodes) { - if (String(colorInfo.name).to_lowercase() == color) + if (String(colorInfo.name).toLowerCase() == color) { activateColor(colorInfo.color, colorInfo.bold); m_colorCodeStage = 0; diff -r e8d58327cd7f -r d0aedc9be448 sources/interface.cpp --- a/sources/interface.cpp Wed Jul 20 15:07:57 2016 +0300 +++ b/sources/interface.cpp Wed Jul 20 16:01:10 2016 +0300 @@ -513,7 +513,7 @@ break; } - if (not text.is_empty()) + if (not text.isEmpty()) text += " | "; text += "Ctrl+N to connect, Ctrl+Q to "; @@ -717,7 +717,7 @@ case '\b': if (m_cursorPosition > 0) { - getEditableInput().remove_at(--m_cursorPosition); + getEditableInput().removeAt(--m_cursorPosition); m_needInputRender = true; } break; @@ -726,7 +726,7 @@ case 'D' - 'A' + 1: // readline ^D if (m_cursorPosition < getCurrentInput().length()) { - getEditableInput().remove_at(m_cursorPosition); + getEditableInput().removeAt(m_cursorPosition); m_needInputRender = true; } break; @@ -758,7 +758,7 @@ break; case 'Y' - 'A' + 1: // readline ^Y - paste previously deleted text - if (not m_pasteBuffer.is_empty()) + if (not m_pasteBuffer.isEmpty()) { getEditableInput().insert(m_cursorPosition, m_pasteBuffer); m_cursorPosition += m_pasteBuffer.length(); @@ -806,7 +806,7 @@ break; case INPUTSTATE_PASSWORD: - if (m_inputState == INPUTSTATE_PASSWORD and not getCurrentInput().is_empty()) + if (m_inputState == INPUTSTATE_PASSWORD and not getCurrentInput().isEmpty()) { m_session.disconnect(); m_session.set_password(getCurrentInput()); @@ -1034,7 +1034,7 @@ { String& input = getEditableInput(); - if (input.starts_with(part)) + if (input.startsWith(part)) { if (input[part.length()] != ' ') complete += ' '; @@ -1053,7 +1053,7 @@ return; StringList args = input.right(input.length() - 1).split(" "); - String command = args[0].to_lowercase(); + String command = args[0].toLowerCase(); args.remove_at(0); if (command == "connect") diff -r e8d58327cd7f -r d0aedc9be448 sources/mystring.cpp --- a/sources/mystring.cpp Wed Jul 20 15:07:57 2016 +0300 +++ b/sources/mystring.cpp Wed Jul 20 16:01:10 2016 +0300 @@ -39,7 +39,7 @@ // int String::compare (const String& other) const { - return m_string.compare (other.std_string()); + return m_string.compare (other.stdString()); } // ------------------------------------------------------------------------------------------------- @@ -47,9 +47,9 @@ void String::trim (int n) { if (n > 0) - m_string = mid (0, length() - n).std_string(); + m_string = mid (0, length() - n).stdString(); else - m_string = mid (n, -1).std_string(); + m_string = mid (n, -1).stdString(); } // ------------------------------------------------------------------------------------------------- @@ -59,7 +59,7 @@ String result (m_string); for (int pos = 0; (pos = result.find (unwanted)) != -1;) - result.remove_at (pos--); + result.removeAt (pos--); return result; } @@ -72,14 +72,14 @@ for (String c : unwanted) for (int pos = 0; (pos = result.find (c)) != -1;) - result.remove_at (pos--); + result.removeAt (pos--); return result; } // ------------------------------------------------------------------------------------------------- // -String String::to_uppercase() const +String String::toUpperCase() const { String result (m_string); @@ -94,7 +94,7 @@ // ------------------------------------------------------------------------------------------------- // -String String::to_lowercase() const +String String::toLowerCase() const { String result (m_string); @@ -197,21 +197,6 @@ // ------------------------------------------------------------------------------------------------- // -int String::word_position (int n) const -{ - int count = 0; - - for (char ch : *this) - { - if (not isspace(ch) or ++count < n) - continue; - } - - return -1; -} - -// ------------------------------------------------------------------------------------------------- -// int String::find (const char* c, int a) const { int pos = m_string.find (c, a); @@ -236,7 +221,7 @@ // ------------------------------------------------------------------------------------------------- // -int String::find_last (const char* c, int a) const +int String::findLast (const char* c, int a) const { if (a == -1 or a >= length()) a = length() - 1; @@ -250,7 +235,7 @@ // ------------------------------------------------------------------------------------------------- // -long String::to_int (bool* ok, int base) const +long String::toInt (bool* ok, int base) const { errno = 0; char* endptr; @@ -264,7 +249,7 @@ // ------------------------------------------------------------------------------------------------- // -float String::to_float (bool* ok) const +float String::toFloat (bool* ok) const { errno = 0; char* endptr; @@ -278,7 +263,7 @@ // ------------------------------------------------------------------------------------------------- // -double String::to_double (bool* ok) const +double String::toDouble (bool* ok) const { errno = 0; char* endptr; @@ -310,7 +295,7 @@ // ------------------------------------------------------------------------------------------------- // -bool String::is_numeric() const +bool String::isNumeric() const { char* endptr; strtol (chars(), &endptr, 10); @@ -319,7 +304,7 @@ // ------------------------------------------------------------------------------------------------- // -bool String::ends_with (const String& other) +bool String::endsWith (const String& other) const { if (length() < other.length()) return false; @@ -330,7 +315,7 @@ // ------------------------------------------------------------------------------------------------- // -bool String::starts_with (const String& other) const +bool String::startsWith (const String& other) const { if (length() < other.length()) return false; @@ -375,7 +360,7 @@ for (const String &item : container()) { - if (result.is_empty() == false) + if (result.isEmpty() == false) result += delim; result += item; @@ -386,11 +371,11 @@ // ------------------------------------------------------------------------------------------------- // -bool String::mask_against (const String& pattern) const +bool String::maskAgainst (const String& pattern) const { // Elevate to uppercase for case-insensitive matching - String pattern_upper = pattern.to_uppercase(); - String this_upper = to_uppercase(); + String pattern_upper = pattern.toUpperCase(); + String this_upper = toUpperCase(); const char* maskstring = pattern_upper.chars(); const char* mptr = &maskstring[0]; @@ -434,7 +419,7 @@ // ------------------------------------------------------------------------------------------------- // -String String::from_number (short int a) +String String::fromNumber (short int a) { char buf[32]; ::sprintf (buf, "%d", a); @@ -443,7 +428,7 @@ // ------------------------------------------------------------------------------------------------- // -String String::from_number (int a) +String String::fromNumber (int a) { char buf[32]; ::sprintf (buf, "%d", a); @@ -452,7 +437,7 @@ // ------------------------------------------------------------------------------------------------- // -String String::from_number (long int a) +String String::fromNumber (long int a) { char buf[32]; ::sprintf (buf, "%ld", a); @@ -461,7 +446,7 @@ // ------------------------------------------------------------------------------------------------- // -String String::from_number (unsigned short int a) +String String::fromNumber (unsigned short int a) { char buf[32]; ::sprintf (buf, "%u", a); @@ -470,7 +455,7 @@ // ------------------------------------------------------------------------------------------------- // -String String::from_number (unsigned int a) +String String::fromNumber (unsigned int a) { char buf[32]; ::sprintf (buf, "%u", a); @@ -479,7 +464,7 @@ // ------------------------------------------------------------------------------------------------- // -String String::from_number (unsigned long int a) +String String::fromNumber (unsigned long int a) { char buf[32]; ::sprintf (buf, "%lu", a); @@ -488,7 +473,7 @@ // ------------------------------------------------------------------------------------------------- // -String String::from_number (double a) +String String::fromNumber (double a) { char buf[64]; ::sprintf (buf, "%f", a); diff -r e8d58327cd7f -r d0aedc9be448 sources/mystring.h --- a/sources/mystring.h Wed Jul 20 15:07:57 2016 +0300 +++ b/sources/mystring.h Wed Jul 20 16:01:10 2016 +0300 @@ -34,123 +34,106 @@ #include #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& 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 (int a, int 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 &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; + String(); + String(char a); + String(const char* data); + String(const std::string& data); + String(const Vector& data); - 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); + 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 &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 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); + 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 { 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(); } + 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 { public: @@ -158,27 +141,302 @@ StringList() {} - StringList (int numvalues) : - Super (numvalues) {} + StringList(int numvalues) : + Super(numvalues) {} - StringList (const Super& other) : - Super (other) {} + StringList(const Super& other) : + Super(other) {} - String join (const String& delim); + String join(const String& delim); }; -// ------------------------------------------------------------------------------------------------- -// -inline bool operator== (const char* a, const String& b) + +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& 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) + +inline String operator+(const char* a, const String& b) { - return String (a) + b; + return String(a) + b; } + END_ZFC_NAMESPACE diff -r e8d58327cd7f -r d0aedc9be448 sources/network/ipaddress.cpp --- a/sources/network/ipaddress.cpp Wed Jul 20 15:07:57 2016 +0300 +++ b/sources/network/ipaddress.cpp Wed Jul 20 16:01:10 2016 +0300 @@ -161,7 +161,7 @@ } if (colonpos != -1) - value.port = (unsigned short) input.mid (colonpos + 1, -1).to_int(); + value.port = (unsigned short) input.mid (colonpos + 1, -1).toInt(); return value; } diff -r e8d58327cd7f -r d0aedc9be448 sources/network/rconsession.cpp --- a/sources/network/rconsession.cpp Wed Jul 20 15:07:57 2016 +0300 +++ b/sources/network/rconsession.cpp Wed Jul 20 16:01:10 2016 +0300 @@ -324,7 +324,7 @@ // bool RCONSession::send_command (const String& message) { - if (m_state != RCON_CONNECTED or message.is_empty()) + if (m_state != RCON_CONNECTED or message.isEmpty()) return false; Bytestream packet; diff -r e8d58327cd7f -r d0aedc9be448 sources/network/udpsocket.cpp --- a/sources/network/udpsocket.cpp Wed Jul 20 15:07:57 2016 +0300 +++ b/sources/network/udpsocket.cpp Wed Jul 20 16:01:10 2016 +0300 @@ -105,7 +105,7 @@ if (::bind (m_socket, reinterpret_cast (&svaddr), sizeof svaddr) == -1) { - m_error = String ("Couldn't bind to port ") + String::from_number (port); + m_error = String ("Couldn't bind to port ") + String::fromNumber (port); return false; }