String.h
Go to the documentation of this file.00001 #ifndef SimTK_SimTKCOMMON_STRING_H_
00002 #define SimTK_SimTKCOMMON_STRING_H_
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #ifdef _MSC_VER
00037 #pragma warning(disable:4996)
00038 #endif
00039
00040
00041 #include "SimTKcommon/internal/common.h"
00042
00043 #include <cstdio>
00044 #include <string>
00045 #include <limits>
00046 #include <complex>
00047
00048 namespace SimTK {
00049
00065 class String : public std::string {
00066 public:
00068 String() { }
00069
00070
00071
00073 String(const char* s) : std::string(s) { }
00074
00077 explicit String(char c) {push_back(c);}
00078
00080 String(const std::string& s) : std::string(s) { }
00081
00084 String(const String& s, int start, int len) : std::string(s,start,len) { }
00085
00088 operator const char*() const { return c_str(); }
00089
00091 char& operator[](int i) {
00092 assert(i >= 0);
00093 return std::string::operator[]((std::string::size_type)i);
00094 }
00095
00097 char operator[](int i) const {
00098 assert(i >= 0);
00099 return std::string::operator[]((std::string::size_type)i);
00100 }
00101
00103 char& operator[](std::string::size_type i) {return std::string::operator[](i);}
00105 char operator[](std::string::size_type i) const {return std::string::operator[](i);}
00106
00109 int size() const {return (int)std::string::size();}
00110
00113 int length() const {return (int)std::string::length();}
00114
00118
00119
00120 explicit String(int i) { char buf[32]; sprintf(buf,"%d",i); (*this)=buf; }
00122 explicit String(long i) { char buf[32]; sprintf(buf,"%ld",i); (*this)=buf; }
00124 explicit String(long long i) { char buf[64]; sprintf(buf,"%lld",i); (*this)=buf; }
00126 explicit String(unsigned int s) { char buf[32]; sprintf(buf,"%u",s); (*this)=buf; }
00128 explicit String(unsigned long s) { char buf[32]; sprintf(buf,"%lu",s); (*this)=buf; }
00130 explicit String(unsigned long long s) { char buf[64]; sprintf(buf,"%llu",s); (*this)=buf; }
00132 explicit String(float r) { char buf[64]; sprintf(buf,"%.8g",r); (*this)=buf; }
00134 explicit String(double r) { char buf[64]; sprintf(buf,"%.16g",r); (*this)=buf; }
00136 explicit String(long double r) { char buf[64]; sprintf(buf,"%.20Lg",r); (*this)=buf; }
00138 explicit String(std::complex<float> r)
00139 { char buf[128]; sprintf(buf,"(%.8g,%.8g)",r.real(),r.imag()); (*this)=buf; }
00141 explicit String(std::complex<double> r)
00142 { char buf[128]; sprintf(buf,"(%.16g,%.16g)",r.real(),r.imag()); (*this)=buf; }
00144 explicit String(std::complex<long double> r)
00145 { char buf[128]; sprintf(buf,"(%.20Lg,%.20Lg)",r.real(),r.imag()); (*this)=buf; }
00147 explicit String(bool b) : std::string(b?"true":"false") { }
00149
00157
00158
00159
00160 String& toUpper();
00163 String& toLower();
00167 String& trimWhiteSpace();
00170 String& replaceAllChar(char oldChar, char newChar);
00172
00173
00177
00178
00179
00180
00181 static const String& getAs(const std::string& s)
00182 { return reinterpret_cast<const String&>(s); }
00186 static String& updAs(std::string& s)
00187 { return reinterpret_cast<String&>(s); }
00190 static String toUpper(const std::string& in)
00191 { return String(in).toUpper(); }
00194 static String toLower(const std::string& in)
00195 { return String(in).toLower(); }
00200 static String trimWhiteSpace(const std::string& in);
00203 String& replaceAllChar(const std::string& in, char oldChar, char newChar)
00204 { return String(in).replaceAllChar(oldChar, newChar); }
00206 };
00207
00208 }
00209 #endif // SimTK_SimTKCOMMON_STRING_H_