1 #ifndef SimTK_SimTKCOMMON_STRING_H_ 2 #define SimTK_SimTKCOMMON_STRING_H_ 39 #pragma warning(disable:4996) 74 explicit String(
char c) {push_back(c);}
85 operator const char*()
const {
return c_str(); }
90 return std::string::operator[]((std::string::size_type)i);
96 return std::string::operator[]((std::string::size_type)i);
100 char&
operator[](std::string::size_type i) {
return std::string::operator[](i);}
102 char operator[](std::string::size_type i)
const {
return std::string::operator[](i);}
106 int size()
const {
return (
int)std::string::size();}
110 int length()
const {
return (
int)std::string::length();}
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; }
155 const char* fmt=
"%.21Lg");
160 explicit String(std::complex<float> r,
const char* fmt=
"%.9g")
161 { (*this)=
"(" +
String(r.real(),fmt) +
"," +
String(r.imag(),fmt) +
")"; }
165 explicit String(std::complex<double> r,
const char* fmt=
"%.17g")
166 { (*this)=
"(" +
String(r.real(),fmt) +
"," +
String(r.imag(),fmt) +
")"; }
171 explicit String(std::complex<long double> r,
const char* fmt=
"%.21Lg")
172 { (*this)=
"(" +
String(r.real(),fmt) +
"," +
String(r.imag(),fmt) +
")"; }
176 explicit String(
bool b) :
std::string(b?
"true":
"false") { }
182 template <
class T>
inline explicit String(
const T& t);
186 template <
class T>
explicit 193 new (
this)
String(T(nt), fmt);
198 template <
class T>
explicit 200 new (
this)
String(std::complex<T>(ct));
205 new (
this)
String(std::complex<T>(ct), fmt);
241 template <
class T>
inline bool tryConvertTo(T& out)
const;
249 template <
class T>
inline void convertTo(T& out)
const;
258 { T temp; convertTo<T>(temp);
return temp; }
316 {
return String(in).toUpper(); }
320 {
return String(in).toLower(); }
329 {
return String(in).replaceAllChar(oldChar, newChar); }
339 template <
class T>
inline 340 auto stringStreamInsertHelper(std::ostringstream& os,
const T& t,
bool)
341 -> decltype(static_cast<std::ostringstream&>(os << t)) {
347 template <
class T>
inline 348 auto stringStreamInsertHelper(std::ostringstream& os,
const T& t,
int)
349 -> std::ostringstream& {
351 "Type T=%s has no stream insertion operator<<(T) and there " 352 "is no specialized String(T) constructor.",
357 template <
class T>
inline 358 auto stringStreamExtractHelper(std::istringstream& is, T& t,
bool)
359 -> decltype(static_cast<std::istringstream&>(is >> t)) {
364 template <
class T>
inline 365 auto stringStreamExtractHelper(std::istringstream& is, T& t,
int)
366 -> std::istringstream& {
368 "String::tryConvertTo<T>()",
369 "Type T=%s has no stream extraction operator>>(T) and there " 370 "is no specialized tryConvertTo<T>() constructor.",
382 template <
class T>
inline 384 std::ostringstream os;
385 *
this = stringStreamInsertHelper(os, t,
true).str();
393 template <
class T>
inline static 395 std::istringstream sstream(value);
396 stringStreamExtractHelper(sstream, out,
true);
397 if (sstream.fail())
return false;
398 if (sstream.eof())
return true;
402 return sstream.eof();
432 { out = value;
return true; }
437 { out = value;
return true; }
440 template <
class T>
inline 449 template <
class T>
inline 451 std::complex<T> cmplx;
460 template<
class T>
inline static 463 "Can't interpret a string as a pointer (%s*).",
468 template <
class T>
inline bool 472 template <
class T>
inline void 474 const int MaxStr = 50;
475 const bool convertOK = tryConvertTo<T>(out);
476 if (convertOK)
return;
480 String shorter = this->substr(0, MaxStr);
481 if (shorter.
size() < this->
size()) shorter +=
" ...";
483 "Couldn't interpret string '%s' as type T=%s.",
498 template <
class T>
inline static 512 template <
class T>
inline static 522 #endif // SimTK_SimTKCOMMON_STRING_H_ char operator[](std::string::size_type i) const
Pass through to string::operator[].
Definition: String.h:102
String & replaceAllChar(const std::string &in, char oldChar, char newChar)
Copy the input std::string to a new SimTK::String while substituting newChar for oldChar wherever old...
Definition: String.h:328
String(unsigned long long s, const char *fmt="%llu")
Format an unsigned long long as a printable String.
Definition: String.h:135
bool tryConvertToBool(bool &out) const
Special-purpose method for interpreting this String as a bool.
String(long i, const char *fmt="%ld")
Format a long as a printable String.
Definition: String.h:123
#define SimTK_SimTKCOMMON_EXPORT
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:224
String(unsigned long s, const char *fmt="%lu")
Format an unsigned long as a printable String.
Definition: String.h:132
String(std::complex< long double > r, const char *fmt="%.21Lg")
Format a complex<long double> as a printable String (real,imag) with parentheses and a comma as shown...
Definition: String.h:171
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37
SimTK::conjugate<R> should be instantiated only for float, double, long double.
Definition: String.h:45
bool tryConvertTo(T &out) const
Attempt to convert this String to an object of type T, returning a status value to indicate success o...
Definition: String.h:469
void convertTo(T &out) const
Convert this String to an object of type T using the tryConvertTo<T>() method but throwing an error o...
Definition: String.h:473
bool tryConvertToLongDouble(long double &out) const
Special-purpose method for interpreting this String as a long double.
static void convertStringTo(const String &in, T &out)
This method converts its String argument to type T and returns it into the variable supplied as its s...
Definition: String.h:499
String(std::complex< double > r, const char *fmt="%.17g")
Format a complex<double> as a printable String (real,imag) with parentheses and a comma as shown...
Definition: String.h:165
bool tryConvertToFloat(float &out) const
Special-purpose method for interpreting this String as a float.
char & operator[](int i)
Add operator[] that takes int index instead of size_type.
Definition: String.h:88
String(const conjugate< T > &ct, const char *fmt)
Same, but allows for format specification.
Definition: String.h:204
static const std::string & namestr()
The default implementation of namestr() attempts to return a nicely demangled and canonicalized type ...
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:868
String(long long i, const char *fmt="%lld")
Format a long long as a printable String.
Definition: String.h:126
#define SimTK_ERRCHK2_ALWAYS(cond, whereChecked, fmt, a1, a2)
Definition: ExceptionMacros.h:289
int length() const
Override std::string length() method to return an int instead of the inconvenient unsigned type size_...
Definition: String.h:110
negator<N>, where N is a number type (real, complex, conjugate), is represented in memory identically...
Definition: String.h:44
String(char c)
We allow creating a String from a char but you have to do it explicitly.
Definition: String.h:74
This file contains macros which are convenient to use for sprinkling error checking around liberally ...
String(unsigned int s, const char *fmt="%u")
Format an unsigned int as a printable String.
Definition: String.h:129
int size() const
Override std::string size() method to return an int instead of the inconvenient unsigned type size_ty...
Definition: String.h:106
String(const negator< T > &nt)
Constructing a String from a negated value converts to the underlying native type and then uses one o...
Definition: String.h:187
#define SimTK_ERRCHK1_ALWAYS(cond, whereChecked, fmt, a1)
Definition: ExceptionMacros.h:285
char operator[](int i) const
Add operator[] that takes int index instead of size_type.
Definition: String.h:94
String & toLower()
Downshift the given String in place, so that uppercase letters are replaced with their lowercase equi...
String(const String &s, int start, int len)
Construct a String as a copy of a substring beginning at position start with length len...
Definition: String.h:81
String & replaceAllChar(char oldChar, char newChar)
Substitute in place newChar for oldChar wherever oldChar appears in this String.
String(int i, const char *fmt="%d")
Format an int as a printable String.
Definition: String.h:120
bool tryConvertToDouble(double &out) const
Special-purpose method for interpreting this String as a double.
SimTK::String is a plug-compatible std::string replacement (plus some additional functionality) inten...
Definition: String.h:62
char & operator[](std::string::size_type i)
Pass through to string::operator[].
Definition: String.h:100
Mandatory first inclusion for any Simbody source or header file.
static T convertStringTo(const String &in)
This method converts its String argument to type T and returns it as its function value; this is part...
Definition: String.h:513
Obtain human-readable and XML-usable names for arbitrarily-complicated C++ types. ...
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:859
static String toLower(const std::string &in)
Downshift the given std::string returning a new SimTK::String in which all the letters have be made l...
Definition: String.h:319
String(const negator< T > &nt, const char *fmt)
Same, but allows for format specification.
Definition: String.h:192
String & trimWhiteSpace()
Trim this String in place, removing all the initial leading and trailing white space, as defined by std::isspace() which typically includes space, tab (\t), newline (\n), return (\r), and form feed (\f).
T convertTo() const
A more convenient form of convertTo<T>() that returns the result as its function argument, although this may involve an extra copy operation.
Definition: String.h:257
String & toUpper()
Upshift the given String in place, so that lowercase letters are replaced with their uppercase equiva...
String(bool b)
Format a bool as a printable String "true" or "false"; if you want "1" or "0" cast the bool to an int...
Definition: String.h:176
String(const char *s)
This is an implicit conversion from const char* to String.
Definition: String.h:71
String()
Default constructor produces an empty string.
Definition: String.h:66
static String toUpper(const std::string &in)
Upshift the given std::string returning a new SimTK::String in which all the letters have been made u...
Definition: String.h:315
String(std::complex< float > r, const char *fmt="%.9g")
Format a complex<float> as a printable String (real,imag) with parentheses and a comma as shown...
Definition: String.h:160
String(const std::string &s)
This is an implicit conversion from std::string to String.
Definition: String.h:77
String(const conjugate< T > &ct)
Constructing a String from a conjugate value converts to the underlying complex type and then uses on...
Definition: String.h:199
static bool tryConvertStringTo(const String &value, T &out)
Definition: String.h:394