Simbody 3.7
Loading...
Searching...
No Matches
String.h
Go to the documentation of this file.
1#ifndef SimTK_SimTKCOMMON_STRING_H_
2#define SimTK_SimTKCOMMON_STRING_H_
3
4/* -------------------------------------------------------------------------- *
5 * Simbody(tm): SimTKcommon *
6 * -------------------------------------------------------------------------- *
7 * This is part of the SimTK biosimulation toolkit originating from *
8 * Simbios, the NIH National Center for Physics-Based Simulation of *
9 * Biological Structures at Stanford, funded under the NIH Roadmap for *
10 * Medical Research, grant U54 GM072970. See https://simtk.org/home/simbody. *
11 * *
12 * Portions copyright (c) 2005-15 Stanford University and the Authors. *
13 * Authors: Michael Sherman *
14 * Contributors: *
15 * *
16 * Licensed under the Apache License, Version 2.0 (the "License"); you may *
17 * not use this file except in compliance with the License. You may obtain a *
18 * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. *
19 * *
20 * Unless required by applicable law or agreed to in writing, software *
21 * distributed under the License is distributed on an "AS IS" BASIS, *
22 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
23 * See the License for the specific language governing permissions and *
24 * limitations under the License. *
25 * -------------------------------------------------------------------------- */
26
29
30#include <cstdio>
31#include <string>
32#include <limits>
33#include <complex>
34#include <sstream>
35
36// Keeps MS VC++ quiet about sprintf, strcpy, etc.
37#ifdef _MSC_VER
38#pragma warning(push)
39#pragma warning(disable:4996)
40#endif
41
42namespace SimTK {
43
44template <class N> class negator;
45template <class R> class conjugate;
46
62class String : public std::string {
63public:
64
66String() { }
67
68// uses default copy constructor, copy assignment, and destructor
69
71String(const char* s) : std::string(s) { }
72
74explicit String(char c) {push_back(c);}
75
77String(const std::string& s) : std::string(s) { }
78
81String(const String& s, int start, int len) : std::string(s,start,len) { }
82
85operator const char*() const { return c_str(); }
86
88char& operator[](int i) {
89 assert(i >= 0);
90 return std::string::operator[]((std::string::size_type)i);
91}
92
94char operator[](int i) const {
95 assert(i >= 0);
96 return std::string::operator[]((std::string::size_type)i);
97}
98
100char& operator[](std::string::size_type i) {return std::string::operator[](i);}
102char operator[](std::string::size_type i) const {return std::string::operator[](i);}
103
106int size() const {return (int)std::string::size();}
107
110int length() const {return (int)std::string::length();}
111
120explicit String(int i, const char* fmt="%d")
121{ char buf[32]; sprintf(buf,fmt,i); (*this)=buf; }
123explicit String(long i, const char* fmt="%ld")
124{ char buf[64]; sprintf(buf,fmt,i); (*this)=buf; }
126explicit String(long long i, const char* fmt="%lld")
127{ char buf[64]; sprintf(buf,fmt,i); (*this)=buf; }
129explicit String(unsigned int s, const char* fmt="%u")
130{ char buf[32]; sprintf(buf,fmt,s); (*this)=buf; }
132explicit String(unsigned long s, const char* fmt="%lu")
133{ char buf[64]; sprintf(buf,fmt,s); (*this)=buf; }
135explicit String(unsigned long long s, const char* fmt="%llu")
136{ char buf[64]; sprintf(buf,fmt,s); (*this)=buf; }
137
142SimTK_SimTKCOMMON_EXPORT explicit String(float r, const char* fmt="%.9g");
143
148SimTK_SimTKCOMMON_EXPORT explicit String(double r, const char* fmt="%.17g");
149
153explicit String(std::complex<float> r, const char* fmt="%.9g")
154{ (*this)="(" + String(r.real(),fmt) + "," + String(r.imag(),fmt) + ")"; }
158explicit String(std::complex<double> r, const char* fmt="%.17g")
159{ (*this)="(" + String(r.real(),fmt) + "," + String(r.imag(),fmt) + ")"; }
160
163explicit String(bool b) : std::string(b?"true":"false") { }
164
169template <class T> inline explicit String(const T& t); // see below
170
173template <class T> explicit
174String(const negator<T>& nt) {
175 new (this) String(T(nt));
176}
178template <class T>
179String(const negator<T>& nt, const char* fmt) {
180 new (this) String(T(nt), fmt);
181}
182
185template <class T> explicit
187 new (this) String(std::complex<T>(ct));
188}
190template <class T>
191String(const conjugate<T>& ct, const char* fmt) {
192 new (this) String(std::complex<T>(ct), fmt);
193}
194
195
228template <class T> inline bool tryConvertTo(T& out) const; // see below
229
236template <class T> inline void convertTo(T& out) const; // see below
237
244template <class T> T convertTo() const
245{ T temp; convertTo<T>(temp); return temp; }
246
252
258
286SimTK_SimTKCOMMON_EXPORT String& replaceAllChar(char oldChar, char newChar);
296static String toUpper(const std::string& in)
297{ return String(in).toUpper(); }
300static String toLower(const std::string& in)
301{ return String(in).toLower(); }
306static SimTK_SimTKCOMMON_EXPORT String trimWhiteSpace(const std::string& in);
309String& replaceAllChar(const std::string& in, char oldChar, char newChar)
310{ return String(in).replaceAllChar(oldChar, newChar); }
313};
314
315// All std::stream activity should be dealt with inline so that we don't have
316// to worry about binary compatibility issues that can arise when passing
317// streams through the API.
318
// Hide from Doxygen
320template <class T> inline
321auto stringStreamInsertHelper(std::ostringstream& os, const T& t, bool)
322 -> decltype(static_cast<std::ostringstream&>(os << t)) {
323 os << t;
324 return os;
325}
326
327
328template <class T> inline
329auto stringStreamInsertHelper(std::ostringstream& os, const T& t, int)
330 -> std::ostringstream& {
331 SimTK_ERRCHK1_ALWAYS(!"no stream insertion operator", "String::String(T)",
332 "Type T=%s has no stream insertion operator<<(T) and there "
333 "is no specialized String(T) constructor.",
334 NiceTypeName<T>::namestr().c_str());
335 return os;
336}
337
338template <class T> inline
339auto stringStreamExtractHelper(std::istringstream& is, T& t, bool)
340 -> decltype(static_cast<std::istringstream&>(is >> t)) {
341 is >> t;
342 return is;
343}
344
345template <class T> inline
346auto stringStreamExtractHelper(std::istringstream& is, T& t, int)
347 -> std::istringstream& {
348 SimTK_ERRCHK1_ALWAYS(!"no stream extraction operator",
349 "String::tryConvertTo<T>()",
350 "Type T=%s has no stream extraction operator>>(T) and there "
351 "is no specialized tryConvertTo<T>() constructor.",
352 NiceTypeName<T>::namestr().c_str());
353 return is;
354}
355
363template <class T> inline
364String::String(const T& t) {
365 std::ostringstream os;
366 *this = stringStreamInsertHelper(os, t, true).str();
367}
368
369
370// This namespace-level static method should not be necessary but gcc 4.1
371// still has trouble with template specialization for template member
372// functions. So rather than specializing the tryConvertTo() member, I'm
373// specializing this helper function instead.
374template <class T> inline static
375bool tryConvertStringTo(const String& value, T& out) {
376 std::istringstream sstream(value);
377 stringStreamExtractHelper(sstream, out, true);
378 if (sstream.fail()) return false;
379 if (sstream.eof()) return true;
380 // Successful conversion but didn't use all the characters. Maybe the
381 // rest is just whitespace?
382 std::ws(sstream); // Skip trailing whitespace if any.
383 return sstream.eof(); // We must have used up the whole string now.
384}
385
386// This specialization ensures that "true" and "false" are recognized as
387// values for bools (with any case).
388template <> inline
389bool tryConvertStringTo(const String& value, bool& out)
390{ return value.tryConvertToBool(out); }
391
392// Specialization to ensure recognition of non-finite values NaN, Inf, etc.
393template <> inline
394bool tryConvertStringTo(const String& value, float& out)
395{ return value.tryConvertToFloat(out); }
396
397// Specialization to ensure recognition of non-finite values NaN, Inf, etc.
398template <> inline
399bool tryConvertStringTo(const String& value, double& out)
400{ return value.tryConvertToDouble(out); }
401
402// This specialization ensures that we get the whole String including
403// leading and trailing white space. Of course this is not useful for
404// anything but may occur as a result of some higher-level templatized
405// method that doesn't know what type it is converting here.
406template<> inline
407bool tryConvertStringTo(const String& value, String& out)
408{ out = value; return true; }
409
410// Same as above but for std::string output rather than String.
411template<> inline
412bool tryConvertStringTo(const String& value, std::string& out)
413{ out = value; return true; }
414
416template <class T> inline
417bool tryConvertStringTo(const String& value, negator<T>& out) {
418 T nonnegated;
419 if (!tryConvertStringTo(value, nonnegated)) return false;
420 out = nonnegated;
421 return true;
422}
423
425template <class T> inline
426bool tryConvertStringTo(const String& value, conjugate<T>& out) {
427 std::complex<T> cmplx;
428 if (!tryConvertStringTo(value, cmplx)) return false;
429 out = cmplx;
430 return true;
431}
432
433
434// This partial specialization ensures that you can't interpret
435// a String as a pointer.
436template<class T> inline static
437bool tryConvertStringTo(const String& value, T*& out) {
438 SimTK_ERRCHK1_ALWAYS(false, "SimTK::convertStringTo(value,T*)",
439 "Can't interpret a string as a pointer (%s*).",
440 NiceTypeName<T>::namestr().c_str());
441 return false;
442}
443
444template <class T> inline bool
446{ return tryConvertStringTo(*this, out); }
447
448template <class T> inline void
449String::convertTo(T& out) const {
450 const int MaxStr = 50;
451 const bool convertOK = tryConvertTo<T>(out);
452 if (convertOK) return;
453
454 // Make sure we don't try to output more than MaxStr characters of
455 // the bad string in the error message.
456 String shorter = this->substr(0, MaxStr);
457 if (shorter.size() < this->size()) shorter += " ...";
458 SimTK_ERRCHK2_ALWAYS(convertOK, "String::convertTo()",
459 "Couldn't interpret string '%s' as type T=%s.",
460 shorter.c_str(), NiceTypeName<T>::namestr().c_str());
461}
462
474template <class T> inline static
475void convertStringTo(const String& in, T& out)
476{ in.convertTo<T>(out); }
477
488template <class T> inline static
490{ return in.convertTo<T>(); }
491
492} // namespace SimTK
493
494#ifdef _MSC_VER
495#pragma warning(pop)
496#endif
497
498#endif // SimTK_SimTKCOMMON_STRING_H_
This file contains macros which are convenient to use for sprinkling error checking around liberally ...
#define SimTK_ERRCHK2_ALWAYS(cond, whereChecked, fmt, a1, a2)
Definition ExceptionMacros.h:289
#define SimTK_ERRCHK1_ALWAYS(cond, whereChecked, fmt, a1)
Definition ExceptionMacros.h:285
Mandatory first inclusion for any Simbody source or header file.
#define SimTK_SimTKCOMMON_EXPORT
Definition SimTKcommon/include/SimTKcommon/internal/common.h:224
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) const
Pass through to string::operator[].
Definition String.h:102
int size() const
Override std::string size() method to return an int instead of the inconvenient unsigned type size_ty...
Definition String.h:106
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:300
String(const char *s)
This is an implicit conversion from const char* to String.
Definition String.h:71
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:186
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:475
String(const negator< T > &nt, const char *fmt)
Same, but allows for format specification.
Definition String.h:179
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:174
String(unsigned long s, const char *fmt="%lu")
Format an unsigned long as a printable String.
Definition String.h:132
String(char c)
We allow creating a String from a char but you have to do it explicitly.
Definition String.h:74
String(const conjugate< T > &ct, const char *fmt)
Same, but allows for format specification.
Definition String.h:191
String & toLower()
Downshift the given String in place, so that uppercase letters are replaced with their lowercase equi...
String(long long i, const char *fmt="%lld")
Format a long long as a printable String.
Definition String.h:126
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:153
String()
Default constructor produces an empty string.
Definition String.h:66
String & toUpper()
Upshift the given String in place, so that lowercase letters are replaced with their uppercase equiva...
bool tryConvertToDouble(double &out) const
Special-purpose method for interpreting this String as a double.
String(unsigned int s, const char *fmt="%u")
Format an unsigned int as a printable String.
Definition String.h:129
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:489
int length() const
Override std::string length() method to return an int instead of the inconvenient unsigned type size_...
Definition String.h:110
String(float r, const char *fmt="%.9g")
Format a float as a printable String.
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(double r, const char *fmt="%.17g")
Format a double as a printable String.
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:158
char & operator[](int i)
Add operator[] that takes int index instead of size_type.
Definition String.h:88
String(const std::string &s)
This is an implicit conversion from std::string to String.
Definition String.h:77
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:309
bool tryConvertToBool(bool &out) const
Special-purpose method for interpreting this String as a bool.
String & trimWhiteSpace()
Trim this String in place, removing all the initial leading and trailing white space,...
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:445
static String trimWhiteSpace(const std::string &in)
Copy the input std::string to a new SimTK::String leaving off all the initial leading and trailing wh...
String(int i, const char *fmt="%d")
Format an int as a printable String.
Definition String.h:120
String & replaceAllChar(char oldChar, char newChar)
Substitute in place newChar for oldChar wherever oldChar appears in this String.
char & operator[](std::string::size_type i)
Pass through to string::operator[].
Definition String.h:100
String(long i, const char *fmt="%ld")
Format a long as a printable String.
Definition String.h:123
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:296
T convertTo() const
A more convenient form of convertTo<T>() that returns the result as its function argument,...
Definition String.h:244
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:449
String(unsigned long long s, const char *fmt="%llu")
Format an unsigned long long as a printable String.
Definition String.h:135
bool tryConvertToFloat(float &out) const
Special-purpose method for interpreting this String as a float.
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:163
char operator[](int i) const
Add operator[] that takes int index instead of size_type.
Definition String.h:94
SimTK::conjugate<R> should be instantiated only for float, double.
Definition conjugate.h:178
negator<N>, where N is a number type (real, complex, conjugate), is represented in memory identically...
Definition negator.h:75
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition Assembler.h:37
static bool tryConvertStringTo(const String &value, T &out)
Definition String.h:375
Obtain human-readable and XML-usable names for arbitrarily-complicated C++ types.
Definition SimTKcommon/include/SimTKcommon/internal/common.h:858
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:867