/* ======================================================================== * (C) 2000 Vienna University of Technology * ======================================================================== * PROJECT: Urban Viz * ========================================================================*/ /** very handy template String class. * * for char-strings, 'String' is typedef'd for convenience * Note: can be used standalone in all std-conforming C++ environments * * $Header: /usr/local/cvsyare/cvsyamp/include/mstring.h,v 1.1 2002/11/14 15:22:48 wimmer Exp $ * @author Michael Wimmer * @file */ /* ========================================================================*/ #ifndef NO_PRAGMA_ONCE #pragma once #endif #ifndef MSTRING_H #define MSTRING_H // remove completely stupid MSVC warning #ifdef _MSC_VER #pragma warning(disable: 4786) // 'identifier' : identifier was truncated to // 'number' characters in the debug information #endif // Note: this always uses namespace std. no namespace precautions! #include using std::basic_string; // --------------------------------------------------------------------------- // class mbasic_string definition // --------------------------------------------------------------------------- // internally used static const char *MSTRING_WHITESPACE = " \t"; /** Very handy String class. Normally, you would use \c mbasic_string, here typedef'd as 'String'. Improves upon basic_string by adding: - most importantly, a cast to const char * - some useful Basic like operations, such as Left, Mid, Right, Trim, Upper, ... */ template class mbasic_string : public basic_string { public: // size_type not automatically looked up on sgis typedef typename basic_string::size_type size_type; //-- Constructors /// various constructors mbasic_string() : basic_string() {} /// mbasic_string(const E *s) : basic_string(s) {} /// mbasic_string(size_type n, const E c) : basic_string(n, c) {} /// mbasic_string(const E c) : basic_string(1, c) {} /// mbasic_string(const mbasic_string& mbs) : basic_string(mbs) {} /// mbasic_string(const basic_string& bs) : basic_string(bs) {} //-- Assignment /// various assignment ops mbasic_string& operator=(const E c) { basic_string::operator=(c); return *this; } /// mbasic_string& operator=(const E *s) { basic_string::operator=(s); return *this; } /// mbasic_string& operator=(const mbasic_string& rhs) { basic_string::operator=((const basic_string&)rhs); return *this; } /// very useful! operator const char*() const { return this->c_str(); } //-- special access /// n characters from position pos (n may be out of range) mbasic_string substr(size_type pos = 0, size_type n = npos) const { basic_string res = basic_string::substr(pos,n); return res; } /// leftmost nr characters (no problem if nr too large) mbasic_string Left(size_type nr) const { return substr(0,nr); } /// rightmost nr characters (no problem if nr too large) mbasic_string Right(size_type nr) const; /** substring from position 'from' to position 'to' (no problems if args out of range) */ mbasic_string Mid(size_type from, size_type to) const { if (from >= size() || from > to) return ""; else return substr(from,to-from+1); } //-- padding mbasic_string Pad(char padchar, int length) const; //-- trimming /// @name Trimming, converting upper/lower case //@{ /// removes whitespace (space, tab) from the right mbasic_string& TrimRightSelf(); /// removes whitespace (space, tab) from the left mbasic_string& TrimLeftSelf(); /// removes whitespace from both left and right mbasic_string& TrimSelf(); /// returns a 'right-trimmed' copy of string mbasic_string TrimRight() const { return mbasic_string(*this).TrimRightSelf(); } /// returns a 'left-trimmed' copy of string mbasic_string TrimLeft() const { return mbasic_string(*this).TrimLeftSelf(); } /// returns a 'trimmed' copy of string mbasic_string Trim() const { return mbasic_string(*this).TrimSelf();} //-- upper/lower conversion /// converts string to uppercase mbasic_string& UpperSelf(); /// converts string to uppercase mbasic_string& LowerSelf(); /// returns an uppercase copy of string mbasic_string Upper() const { return mbasic_string(*this).UpperSelf();} /// returns an uppercase copy of string mbasic_string Lower() const { return mbasic_string(*this).LowerSelf();} //@} //-- other stuff /** in contrast to empty(), also returns true if string is only whitespace (space, tab) */ bool Empty() const; }; /// for convenience (that's what you'll use) typedef mbasic_string String; // --------------------------------------------------------------------------- // class mbasic_string template member implementation // --------------------------------------------------------------------------- template inline mbasic_string mbasic_string::Right(size_type nr) const { // take care that pos doesn't become negative if(nr > size()) { return *this; } else { return substr(size()-nr, nr); } } // ----- WARNING: the following only useful for E = char or similar! template inline bool mbasic_string::Empty() const { return (find_first_not_of(MSTRING_WHITESPACE) == npos); } template inline mbasic_string& mbasic_string::TrimRightSelf() { if (!empty()) { size_type pos = find_last_not_of(MSTRING_WHITESPACE); if(pos == npos) // everything whitespace erase(); else if(pos != size()-1) // pos = size()-1 -> no whitespace erase(pos+1); } return *this; } template inline mbasic_string& mbasic_string::TrimLeftSelf() { if (!empty()) { size_type pos = find_first_not_of(MSTRING_WHITESPACE); if(pos == npos) // everything whitespace erase(); else if(pos != 0) erase(0 , pos); } return *this; } template inline mbasic_string& mbasic_string::TrimSelf() { this->TrimRightSelf(); this->TrimLeftSelf(); return *this; } template inline mbasic_string &mbasic_string::UpperSelf() { if (!empty()) for(size_type i = 0; i < length(); i++) at(i) = (E)toupper(at(i)); return *this; } template inline mbasic_string &mbasic_string::LowerSelf() { if (!empty()) for(size_type i = 0; i < length(); i++) at(i) = (E)tolower(at(i)); return *this; } template inline mbasic_string mbasic_string::Pad(char padchar, int length) const { mbasic_string result(*this); while (result.length() < length) result += padchar; return result; } // --------- operator+() definitions /// string concatentation (defined multiple times to allow good overloads) template inline mbasic_string operator+(const mbasic_string& lhs, const mbasic_string& rhs) { return mbasic_string(lhs) += rhs; } /// template inline mbasic_string operator+(const mbasic_string& lhs, const E *rhs) { return mbasic_string(lhs) += rhs; } /// template inline mbasic_string operator+(const mbasic_string& lhs, const E rhs) { return mbasic_string(lhs) += rhs; } /// template inline mbasic_string operator+(const E *lhs, const mbasic_string& rhs) { return mbasic_string(lhs) += rhs; } /// template inline mbasic_string operator+(const E lhs, const mbasic_string& rhs) { return mbasic_string(1,lhs) += rhs; } #endif // _MSTRING_H