#ifndef KGKSTRING_H #define KGKSTRING_H #include "commondefs.h" #include "kchar.h" #include #include #include #define StringList std::vector #define StringListIter StringList::iterator #define StringListConstIter StringList::const_iterator #define foreachstring(var, list) for (StringListIter var = list.begin() ; var != list.end() ; ++var) #define foreachconststring(var, list) for (StringListConstIter var = list.begin() ; var != list.end() ; ++var) namespace kg { /** * This is a helper class that provides a lot of useful methods when working with strings. */ class KString { public: KString(); KString(const KChar& c); KString(const char *s); KString(const std::string& s); KString(const KString& s); ~KString(); const char* c_str() const; std::string str() const; uint length() const; uint size() const; bool empty() const; KChar at(int i) const; KString left(uint cnt) const; KString right(uint cnt) const; KString mid(uint i, uint cnt) const; KString range(uint from, uint to) const; KString rightOf(const KString& s, int fromIndex=0) const; KString leftOf(const KString& s, int fromIndex=0) const; KString lowerCase() const; KString upperCase() const; int compare(const KString& s, bool cs=true) const; bool startsWith(const KString& s, bool cs=true) const; bool endsWith(const KString& s, bool cs=true) const; int find(const KString& s, int fromIndex=0) const; int findRev(const KString& s, int fromIndex=-1) const; int contains(const KString& s) const; KString replace(const KString& s1, const KString& s2) const; KString removeWhitespace() const; StringList split(const KString& sep) const; KString& append(const KString& s); KString& append(const KChar& c); KString& prepend(const KString& s); KString& prepend(const KChar& c); integer toInt() const; real toReal() const; KString& operator=(const KString& s); KString operator+(const KString& s) const; KString operator+(const KChar& c) const; KString& operator+=(const KString& s); KString& operator+=(const KChar& c); bool operator==(const KString& s) const; bool operator!=(const KString& s) const; bool operator<(const KString& s) const; bool operator>(const KString& s) const; bool operator<=(const KString& s) const; bool operator>=(const KString& s) const; KChar operator[](int i) const; static KString fromFile(const KString& file); private: std::string S; }; /** * Construct an empty KString. */ inline KString::KString() : S("") { } /** * Construct a KString from the specified character. * * @param c The character from which to construct this KString. */ inline KString::KString(const KChar& c) : S(1, c) { } /** * Construct a KString from the specified string. * * @param s The string from which to construct this KString. */ inline KString::KString(const char *s) : S(s) { } /** * Construct a KString from the specified string. * * @param s The string from which to construct this KString. */ inline KString::KString(const std::string& s) : S(s) { } /** * Copy constructor. * * @param s The KString to be copied. */ inline KString::KString(const KString& s) : S(s.S) { } /** * Just another destructor. */ inline KString::~KString() { } /** * Get the c-string. * * @return A const char array containing this string. */ inline const char* KString::c_str() const { return S.c_str(); } /** * Get the std::string. * * @return A std::string containing this string. */ inline std::string KString::str() const { return S; } /** * Get the length of this string. * * @return The number of characters contained in this string. */ inline uint KString::length() const { return S.length(); } /** * The same as length(). * * @return length(). */ inline uint KString::size() const { return length(); } /** * Returns true if this string is empty. * * @return True if this string is empty. */ inline bool KString::empty() const { return length() == 0; } /** * Get the ith character of this string. * * @param i The index of the character. * @return The ith character. */ inline KChar KString::at(int i) const { return S.at(i); } /** * Get the cnt left part characters of the string, i.e. the beginning. * * @param cnt The number of characters to be returned. * @return A KString containing cnt characters from the left of this KString. */ inline KString KString::left(uint cnt) const { return mid(0, cnt); } /** * Get the cnt right part characters of the string, i.e. the ending. * * @param cnt The number of characters to be returned. * @return A KString containing cnt characters from the right of this KString. */ inline KString KString::right(uint cnt) const { if (cnt > length()) cnt = length(); return mid(length()-cnt, cnt); } /** * Get a mid part of the string. * * @param i The index from which to start the returned string. * @param cnt The number of characters contained in the returned string. * @return The part of this string starting at i and having cnt characters. */ inline KString KString::mid(uint i, uint cnt) const { return S.substr(i, cnt); } /** * Get a mid part of the string. * * @param from The index from which to start the returned string. * @param to The index where to stop the returned string. The character at that index is not * contained in the returned string. * @return The part of this string between from and to. */ inline KString KString::range(uint from, uint to) const { return mid(from, to-from); } /** * Searches for the first occurence of s after fromIndex and returns the part * of this string that follows s. If s is not found an empty string will be returned. * * @param s The string to be searched for. * @param fromIndex Determines where to start the search for s. * @return The part of this string that follows s or an empty string if s was not found. */ inline KString KString::rightOf(const KString& s, int fromIndex) const { int index = find(s, fromIndex); return index < 0 ? "" : right(length()-index-s.length()); } /** * Searches for the first occurence of s after fromIndex and returns the parts * of this string that precede s. If s is not found an empty string will be returned. * * @param s The string to be searched for. * @param fromIndex Determines where to start the search for s. * @return The parts of this string that precede s or an empty string if s was not found. */ inline KString KString::leftOf(const KString& s, int fromIndex) const { int index = find(s, fromIndex); return index < 0 ? "" : left(index); } /** * Determine if this string starts with the specified one. * * @param s The string with which this string should be tested. * @param cs If true the comparison will be case sensitiv (default). * @return True if the beginning and the specified string s are equal. */ inline bool KString::startsWith(const KString& s, bool cs) const { return left(s.length()).compare(s, cs) == 0; } /** * Determine if this string ends with the specified one. * * @param s The string with which this string should be tested. * @param cs If true the comparison will be case sensitiv (default). * @return True if the ending and the specified string s are equal. */ inline bool KString::endsWith(const KString& s, bool cs) const { return right(s.length()).compare(s, cs) == 0; } /** * Searches the specified string s from the specified index fromIndex and returns the position * of the string in this string or -1 if s could not be found. * * @param s The string to be searched. * @param fromIndex The index where to start the search. * @return The index of s in this string of -1. */ inline int KString::find(const KString& s, int fromIndex) const { uint i = S.find(s.str(), fromIndex); return i == std::string::npos ? -1 : (int)i; } /** * Searches the specified string s from the specified index fromIndex backwards and returns * the position of the string in this string or -1 if s could not be found. * * @param s The string to be searched. * @param fromIndex The index where to start the search. * @return The index of s in this string of -1. */ inline int KString::findRev(const KString& s, int fromIndex) const { uint i = S.rfind(s.str(), fromIndex < 0 ? length()-1 : fromIndex); return i == std::string::npos ? -1 : (int)i; } /** * Removes whitespace from this string. At the moment only spaces are identified as whitespace. * * @return A new KString without any whitespace. */ inline KString KString::removeWhitespace() const { return replace(" ", ""); } /** * Append the specified string to this string. * * @param s The string to be appended. * @return This string after appending s. */ inline KString& KString::append(const KString& s) { S += s.S; return *this; } /** * Append the specified character to this string. * * @param c The string to be appended. * @return This string after appending c. */ inline KString& KString::append(const KChar& c) { S += c; return *this; } /** * Prepend the specified string to this string. * * @param s The string to be prepended. * @return This string after prepending s. */ inline KString& KString::prepend(const KString& s) { S = s.S+S; return *this; } /** * Prepend the specified character to this string. * * @param c The string to be prepended. * @return This string after prepending c. */ inline KString& KString::prepend(const KChar& c) { S = ((char)c)+S; return *this; } /** * Convert this string to an integer. * * @return An integer representation of this string. */ inline integer KString::toInt() const { return atoi(c_str()); } /** * Convert this string to a real. * * @return A real representation of this string. */ inline real KString::toReal() const { return atof(c_str()); } /** * Assign the value of s to this KString. * * @param s The new value of this KString. * @return This KString with the new value. */ inline KString& KString::operator=(const KString& s) { S = s.S; return *this; } /** * Construct a new string that is a concatenation of this string and s. * * @param s The string to be concatenated with this string. * @return The concatenation of this string and s. */ inline KString KString::operator+(const KString& s) const { return S+s.S; } /** * Construct a new string that is a concatenation of this string and c. * * @param c The character to be concatenated with this string. * @return The concatenation of this string and c. */ inline KString KString::operator+(const KChar& c) const { return S+((char)c); } /** * Append the specified string to this string. * * @param s The string to be appended. * @return This string after appending s. */ inline KString& KString::operator+=(const KString& s) { return append(s); } /** * Append the specified character to this string. * * @param c The character to be appended. * @return This string after appending c. */ inline KString& KString::operator+=(const KChar& c) { return append(c); } /** * Compare this string with the specified one and return true if they are equal. * * @param s The string to be compared with this string. * @return True if both strings are equal. */ inline bool KString::operator==(const KString& s) const { return compare(s) == 0; } /** * Compare this string with the specified one and return true if they are not equal. * * @param s The string to be compared with this string. * @return False if both strings are equal. */ inline bool KString::operator!=(const KString& s) const { return compare(s) != 0; } /** * Compare this string with the specified one and return true if this string is lexigraphically * smaller than s. * * @param s The string to be compared to this string. * @return True if this < s. */ inline bool KString::operator<(const KString& s) const { return compare(s) < 0; } /** * Compare this string with the specified one and return true if this string is lexigraphically * greater than s. * * @param s The string to be compared to this string. * @return True if this > s. */ inline bool KString::operator>(const KString& s) const { return compare(s) > 0; } /** * Compare this string with the specified one and return true if this string is lexigraphically * smaller than or equal to s. * * @param s The string to be compared to this string. * @return True if this <= s. */ inline bool KString::operator<=(const KString& s) const { return compare(s) <= 0; } /** * Compare this string with the specified one and return true if this string is lexigraphically * greater than or equal to s. * * @param s The string to be compared to this string. * @return True if this >= s. */ inline bool KString::operator>=(const KString& s) const { return compare(s) >= 0; } /** * Get the ith character of this string. * * @param i The index of the character. * @return The ith character. */ inline KChar KString::operator[](int i) const { return S[i]; } /** * Get the ith character of this string. * * @param i The index of the character. * @return The ith character. */ /*inline const char& KString::operator[](int i) const { return S[i]; }*/ /** * Get the concatenation of s1 and s2. * * @param s1 The front part of the returned concatenation. * @param s2 The back part of the returned concatenation. * @return The concatenation of s1 and s2. */ inline KString operator+(const char *s1, const KString& s2) { return KString(s1).append(s2); } /** * Print the specified KString to the specified ostream. * * @param os The ostream where to print the KString. * @param s The KString to be printed. */ inline std::ostream& operator<<(std::ostream& os, const KString& s) { return os << s.str(); } } #endif