60
58
namespace string_utils
61
* Conversion class to convert a string to a given target type. We place this in separate classes as we are not allowed to specialize
62
* function templates. But we need specializations for certain cases.
64
* \tparam Target target type
66
template< typename Target >
70
* Convert a given string to the target type. If this fails, a exception is thrown.
72
* \throw WTypeMismatch if source string cannot be converted to target type value.
74
* \param from source string
76
* \return target value
78
static Target fromString( const std::string& from )
80
std::stringstream ss( from );
85
throw WTypeMismatch( "Specified string could not be converted to target type." );
93
* Conversion class to convert a string to a given target type. This is a specialization whenever a string is given as input type
96
struct fromStringImpl< std::string >
99
* Convert a given string to the target type. Never fails.
101
* \param from source string
103
* \return copy of the source string
105
static std::string fromString( const std::string& from )
112
* Convert a given value to a string. The input value must provide a operator<< or be a standard scalar type.
114
* \tparam T the source type. You do not need to specify this directly as it can be deducted from the given parameter
115
* \param value the value to cast to string
117
* \return the string.
119
template< typename T >
120
inline std::string toString( const T& value )
122
std::stringstream ss;
128
* Convert a given value to a string. The input value must provide a operator<< or be a standard scalar type.
130
* \param value the value to cast to string
132
* \return the string.
134
inline std::string toString( const unsigned char& value ) // NOLINT: stylechecker complains about non const ref!?
136
std::stringstream ss;
137
// NOTE: unsigned chars are interpreted as ASCII chars. We want it to be used as number.
138
ss << static_cast< int >( value );
143
* Convert a given value to a string. The input value must provide a operator<< or be a standard scalar type. This method additionally allows
144
* setting width and precision flags of the used std::stringstream.
146
* \tparam T the source type. You do not need to specify this directly as it can be deducted from the given parameter
147
* \param value the value to cast to string
148
* \param precision the precision
149
* \param width the width
151
* \return the string.
153
template< typename T >
154
inline std::string toString( const T& value, const size_t width, const size_t precision )
156
std::stringstream ss;
158
ss.precision( precision );
164
* Convert a given string to a value of a certain type. The target type must provide a operator>> to work or be a standard scalar type.
166
* \tparam T the source type.
167
* \param str the value to cast to string
169
* \throw WTypeMismatch if the string cannot be converted properly.
170
* \return the string.
172
template< typename T >
173
inline T fromString( const std::string& str )
175
return fromStringImpl< T >::fromString( str );
62
178
/** We consider the following characters as whitespace:
63
179
* - <tt>\\r</tt> carriage return
64
180
* - <tt>\\n</tt> newline
65
181
* - <tt>\\t</tt> tab
66
182
* - <tt>' '</tt> space
68
extern OWCOMMON_EXPORT const std::string WHITESPACE;
184
extern const std::string WHITESPACE;
71
187
* Trims any occurence of each character given in parameter t from the end
97
213
* \param t String representing a set containg all trimmable characters
98
214
* \return A copy of the trimmed string
100
std::string OWCOMMON_EXPORT trim( const std::string& source, const std::string& t = WHITESPACE );
216
std::string trim( const std::string& source, const std::string& t = WHITESPACE );
103
219
* Transforms all characters in the given string into upper case
115
231
* \param source String to transpose.
116
232
* \return A copy of the lower case only string
118
std::string OWCOMMON_EXPORT toLower( const std::string& source );
234
std::string toLower( const std::string& source );
121
237
* Splits the given string into a vector of strings (so called tokens).
123
239
* \param source String to tokenize
124
* \param compress If true, charactes matching between two tokens are
240
* \param compress If true, characters matching between two tokens are
125
241
* collapsed and handled as just one character.
126
* \param delim String representing a set containg all characters considered
242
* \param delim String representing a set containing all characters considered
128
244
* \return A vector of strings containing the tokens.
130
std::vector< std::string > OWCOMMON_EXPORT tokenize( const std::string& source,
131
const std::string& delim = WHITESPACE,
132
bool compress = true );
246
std::vector< std::string > tokenize( const std::string& source,
247
const std::string& delim = WHITESPACE,
248
bool compress = true );
135
251
* Writes every vector to an output stream such as cout, if its elements
153
269
* Also wrapping brackets '[' ']' are expected. In general this is the opposite of the
154
270
* output operator above.
155
271
* \warning The inputstream is first written into a string then the convertion into T
156
* via boost::lexical_cast takes place.
272
* via fromString takes place.
157
273
* \warning The delimiter should not be in an elements string representation since then
158
274
* the tokenizer may gets confused