25 #ifndef WSTRINGUTILS_H
26 #define WSTRINGUTILS_H
38 #include "exceptions/WTypeMismatch.h"
66 template<
typename Target >
80 std::stringstream ss( from );
85 throw WTypeMismatch(
"Specified string could not be converted to target type." );
119 template<
typename T >
122 std::stringstream ss;
134 inline std::string
toString(
const unsigned char& value )
136 std::stringstream ss;
138 ss << static_cast< int >( value );
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 );
172 template<
typename T >
195 std::string
rTrim(
const std::string& source,
const std::string& t =
WHITESPACE );
205 std::string
lTrim(
const std::string& source,
const std::string& t =
216 std::string
trim(
const std::string& source,
const std::string& t =
WHITESPACE );
225 std::string
toUpper(
const std::string& source );
234 std::string
toLower(
const std::string& source );
246 std::vector< std::string >
tokenize(
const std::string& source,
248 bool compress =
true );
258 template<
class T > std::ostream&
operator<<( std::ostream& os,
const std::vector< T >& v )
260 std::stringstream result;
261 result <<
"[" << std::scientific << std::setprecision( 16 );
262 std::copy( v.begin(), v.end(), std::ostream_iterator< T >( result,
", " ) );
263 os <<
rTrim( result.str(),
", " ) <<
"]";
281 template<
class T > std::istream&
operator>>( std::istream& in, std::vector< T >& v )
286 std::vector< std::string > tokens =
tokenize( str,
", " );
288 v.reserve( tokens.size() );
289 for(
size_t i = 0; i < tokens.size(); ++i )
291 v.push_back( fromString< T >( tokens[i] ) );
304 template<
class T > std::ostream&
operator<<( std::ostream& os,
const std::list< T >& l )
306 std::stringstream result;
307 result <<
"<" << std::scientific;
308 std::copy( l.begin(), l.end(), std::ostream_iterator< T >( result,
", " ) );
309 os <<
rTrim( result.str(),
", " ) <<
">";
321 template<
class T > std::ostream&
operator<<( std::ostream& os,
const std::set< T >& s )
323 std::stringstream result;
324 result <<
"{" << std::scientific;
325 std::copy( s.begin(), s.end(), std::ostream_iterator< T >( result,
", " ) );
326 os <<
rTrim( result.str(),
", " ) <<
"}";
Indicates invalid type of something.
Some utilities for string manipulation and output operations.
std::string toLower(const std::string &source)
Transforms all characters in the given string into lower case characters.
std::ostream & operator<<(std::ostream &os, const std::vector< T > &v)
Writes every vector to an output stream such as cout, if its elements have an output operator defined...
std::string toUpper(const std::string &source)
Transforms all characters in the given string into upper case characters.
std::vector< std::string > tokenize(const std::string &source, const std::string &delim=WHITESPACE, bool compress=true)
Splits the given string into a vector of strings (so called tokens).
std::istream & operator>>(std::istream &in, std::vector< T > &v)
Write an input stream into the given vector.
std::string trim(const std::string &source, const std::string &t=WHITESPACE)
Trims any occurence of each character given in parameter t from both ends (right and left side) of th...
T fromString(const std::string &str)
Convert a given string to a value of a certain type.
const std::string WHITESPACE
We consider the following characters as whitespace:
std::string rTrim(const std::string &source, const std::string &t=WHITESPACE)
Trims any occurence of each character given in parameter t from the end (or right side) of the given ...
std::string toString(const T &value)
Convert a given value to a string.
std::string lTrim(const std::string &source, const std::string &t=WHITESPACE)
Trims any occurence of each character given in parameter t from the start (or left side) of the given...
static std::string fromString(const std::string &from)
Convert a given string to the target type.
Conversion class to convert a string to a given target type.
static Target fromString(const std::string &from)
Convert a given string to the target type.