1 #ifndef SimTK_SimTKCOMMON_STRING_H_
2 #define SimTK_SimTKCOMMON_STRING_H_
29 #pragma warning(disable:4996)
44 template <
class N>
class negator;
45 template <
class R>
class conjugate;
71 String(
const char* s) : std::string(s) { }
74 explicit String(
char c) {push_back(c);}
77 String(
const std::string& s) : std::string(s) { }
81 String(
const String& s,
int start,
int len) : std::string(s,start,len) { }
85 operator const char*()
const {
return c_str(); }
120 explicit String(
int i,
const char* fmt=
"%d")
121 {
char buf[32]; sprintf(buf,fmt,i); (*this)=buf; }
123 explicit String(
long i,
const char* fmt=
"%ld")
124 {
char buf[64]; sprintf(buf,fmt,i); (*this)=buf; }
126 explicit String(
long long i,
const char* fmt=
"%lld")
127 {
char buf[64]; sprintf(buf,fmt,i); (*this)=buf; }
129 explicit String(
unsigned int s,
const char* fmt=
"%u")
130 {
char buf[32]; sprintf(buf,fmt,s); (*this)=buf; }
132 explicit String(
unsigned long s,
const char* fmt=
"%lu")
133 {
char buf[64]; sprintf(buf,fmt,s); (*this)=buf; }
135 explicit String(
unsigned long long s,
const char* fmt=
"%llu")
136 {
char buf[64]; sprintf(buf,fmt,s); (*this)=buf; }
149 const char* fmt=
"%.20Lg");
154 explicit String(std::complex<float> r,
const char* fmt=
"%.7g")
155 { (*this)=
"(" +
String(r.real(),fmt) +
"," +
String(r.imag(),fmt) +
")"; }
159 explicit String(std::complex<double> r,
const char* fmt=
"%.15g")
160 { (*this)=
"(" +
String(r.real(),fmt) +
"," +
String(r.imag(),fmt) +
")"; }
165 explicit String(std::complex<long double> r,
const char* fmt=
"%.20Lg")
166 { (*this)=
"(" +
String(r.real(),fmt) +
"," +
String(r.imag(),fmt) +
")"; }
170 explicit String(
bool b) : std::string(b?
"true":
"false") { }
176 template <
class T>
inline explicit String(
const T& t);
180 template <
class T>
explicit
187 new (
this)
String(T(nt), fmt);
192 template <
class T>
explicit
194 new (
this)
String(std::complex<T>(ct));
199 new (
this)
String(std::complex<T>(ct), fmt);
235 template <
class T>
inline bool tryConvertTo(T& out)
const;
243 template <
class T>
inline void convertTo(T& out)
const;
252 { T temp; convertTo<T>(temp);
return temp; }
310 {
return String(in).toUpper(); }
314 {
return String(in).toLower(); }
323 {
return String(in).replaceAllChar(oldChar, newChar); }
334 template <
class T>
inline
336 std::ostringstream stream;
338 *
this = stream.str();
346 template <
class T>
inline static
348 std::istringstream sstream(value);
349 sstream >> out;
if (sstream.fail())
return false;
350 if (sstream.eof())
return true;
354 return sstream.eof();
384 { out = value;
return true; }
389 { out = value;
return true; }
392 template <
class T>
inline
401 template <
class T>
inline
403 std::complex<T> cmplx;
412 template<
class T>
inline static
415 "Can't interpret a string as a pointer (%s*).",
420 template <
class T>
inline bool
424 template <
class T>
inline void
426 const int MaxStr = 50;
427 const bool convertOK = tryConvertTo<T>(out);
428 if (convertOK)
return;
432 String shorter = this->substr(0, MaxStr);
433 if (shorter.
size() < this->
size()) shorter +=
" ...";
435 "Couldn't interpret string '%s' as type T=%s.",
450 template <
class T>
inline static
451 void convertStringTo(
const String& in, T& out)
464 template <
class T>
inline static
469 #endif // SimTK_SimTKCOMMON_STRING_H_