OpenWalnut  1.4.0
Classes | Functions | Variables
string_utils Namespace Reference

Some utilities for string manipulation and output operations. More...

Classes

struct  fromStringImpl
 Conversion class to convert a string to a given target type. More...
struct  fromStringImpl< std::string >
 Conversion class to convert a string to a given target type. More...

Functions

template<typename T >
std::string toString (const T &value)
 Convert a given value to a string.
std::string toString (const unsigned char &value)
 Convert a given value to a string.
template<typename T >
std::string toString (const T &value, const size_t width, const size_t precision)
 Convert a given value to a string.
template<typename T >
fromString (const std::string &str)
 Convert a given string to a value of a certain type.
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 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 string.
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 the given string.
std::string toUpper (const std::string &source)
 Transforms all characters in the given string into upper case characters.
std::string toLower (const std::string &source)
 Transforms all characters in the given string into lower 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).
template<class T >
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.
template<class T >
std::istream & operator>> (std::istream &in, std::vector< T > &v)
 Write an input stream into the given vector.
template<class T >
std::ostream & operator<< (std::ostream &os, const std::list< T > &l)
 Writes every list to an output stream such as cout, if its elements have an output operator defined.
template<class T >
std::ostream & operator<< (std::ostream &os, const std::set< T > &s)
 Writes every set to an output stream such as cout, if its elements have an output operator defined.

Variables

const std::string WHITESPACE
 We consider the following characters as whitespace:

Detailed Description

Some utilities for string manipulation and output operations.

Please note that the overloaded ostream output operators aren't in a separate namespace but the string manipulation functions. This is because of short use of e.g. the << operator instead of string_utils::operator( cout, myVector).

The reason for not using the Boost trimming functions is, that Boost providing just Whitespace trimming depending on the current locale, but we might want to trim other character sets too.

The reason for not using the Boost case switching functions is that we want those functions to return a std::string copy which is modified to make some call chains ala: foo( rTrim( toLower( str ), "bar" ) );.

The reason for not using Boosts Tokenizer is, that this tokenizer, is much most simplest to use :).


Function Documentation

template<typename T >
T string_utils::fromString ( const std::string &  str) [inline]

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.

Template Parameters:
Tthe source type.
Parameters:
strthe value to cast to string
Exceptions:
WTypeMismatchif the string cannot be converted properly.
Returns:
the string.

Definition at line 173 of file WStringUtils.h.

References string_utils::fromStringImpl< Target >::fromString().

std::string string_utils::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 string.

Parameters:
sourceString to trim
tString representing a set containg all trimmable characters
Returns:
A copy of the trimmed string

Definition at line 42 of file WStringUtils.cpp.

Referenced by trim().

template<class T >
std::ostream& string_utils::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.

Parameters:
osThe output stream where the elements are written to
vVector containing the elements
Returns:
The output stream again.

Definition at line 258 of file WStringUtils.h.

References rTrim().

template<class T >
std::ostream& string_utils::operator<< ( std::ostream &  os,
const std::list< T > &  l 
)

Writes every list to an output stream such as cout, if its elements have an output operator defined.

Parameters:
osThe output stream where the elements are written to
lList containing the elements
Returns:
The output stream again.

Definition at line 304 of file WStringUtils.h.

References rTrim().

template<class T >
std::ostream& string_utils::operator<< ( std::ostream &  os,
const std::set< T > &  s 
)

Writes every set to an output stream such as cout, if its elements have an output operator defined.

Parameters:
osThe output stream where the elements are written to
sset containing the elements
Returns:
The output stream again.

Definition at line 321 of file WStringUtils.h.

References rTrim().

template<class T >
std::istream& string_utils::operator>> ( std::istream &  in,
std::vector< T > &  v 
)

Write an input stream into the given vector.

The delimiter is implicitly set to ", ". Also wrapping brackets '[' ']' are expected. In general this is the opposite of the output operator above.

Warning:
The inputstream is first written into a string then the convertion into T via fromString takes place.
The delimiter should not be in an elements string representation since then the tokenizer may gets confused
Parameters:
inInput stream
vVector where to store the elements.
Returns:
The input stream again

Definition at line 281 of file WStringUtils.h.

References tokenize(), and trim().

std::string string_utils::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 string.

Parameters:
sourceString to trim
tString representing a set containg all trimmable characters
Returns:
A copy of the trimmed string

Definition at line 35 of file WStringUtils.cpp.

Referenced by operator<<(), and trim().

std::vector< std::string > string_utils::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).

Parameters:
sourceString to tokenize
compressIf true, characters matching between two tokens are collapsed and handled as just one character.
delimString representing a set containing all characters considered as whitespace.
Returns:
A vector of strings containing the tokens.

Definition at line 69 of file WStringUtils.cpp.

Referenced by PROPERTY_TYPE_HELPER::WStringConversion< WPVBaseTypes::PV_MATRIX4X4 >::create(), PROPERTY_TYPE_HELPER::WStringConversion< WPVBaseTypes::PV_TRANSFERFUNCTION >::create(), PROPERTY_TYPE_HELPER::WStringConversion< WPVBaseTypes::PV_POSITION >::create(), PROPERTY_TYPE_HELPER::WStringConversion< WPVBaseTypes::PV_INTERVAL >::create(), WItemSelector::newSelector(), operator>>(), WPropertyStruct< T0, T1, T2, T3, T4, T5, T6, T7, T8, T9 >::setAsString(), and WStructuredTextParser::StructuredValueTree::traverse().

std::string string_utils::toLower ( const std::string &  source)

Transforms all characters in the given string into lower case characters.

Parameters:
sourceString to transpose.
Returns:
A copy of the lower case only string

Definition at line 62 of file WStringUtils.cpp.

template<typename T >
std::string string_utils::toString ( const T &  value) [inline]

Convert a given value to a string.

The input value must provide a operator<< or be a standard scalar type.

Template Parameters:
Tthe source type. You do not need to specify this directly as it can be deducted from the given parameter
Parameters:
valuethe value to cast to string
Returns:
the string.

Definition at line 120 of file WStringUtils.h.

Referenced by WDataHandler::addSubject(), WModuleProjectFileCombiner::apply(), PROPERTY_TYPE_HELPER::WStringConversion< T >::asString(), PROPERTY_TYPE_HELPER::WStringConversion< WPVBaseTypes::PV_SELECTION >::asString(), WMatrixFixed< T, 3, 1 >::at(), wge::bindTexture(), WGEColormapping::callback(), WDataHandler::clear(), WPropertyGroupBase::findProperty(), WGEProjectFileIO::parse(), WGEShaderDefine< ValueType >::process(), WDataHandler::removeSubject(), WProjectFile::threadMain(), and wge::vector2label().

std::string string_utils::toString ( const unsigned char &  value) [inline]

Convert a given value to a string.

The input value must provide a operator<< or be a standard scalar type.

Parameters:
valuethe value to cast to string
Returns:
the string.

Definition at line 134 of file WStringUtils.h.

template<typename T >
std::string string_utils::toString ( const T &  value,
const size_t  width,
const size_t  precision 
) [inline]

Convert a given value to a string.

The input value must provide a operator<< or be a standard scalar type. This method additionally allows setting width and precision flags of the used std::stringstream.

Template Parameters:
Tthe source type. You do not need to specify this directly as it can be deducted from the given parameter
Parameters:
valuethe value to cast to string
precisionthe precision
widththe width
Returns:
the string.

Definition at line 154 of file WStringUtils.h.

std::string string_utils::toUpper ( const std::string &  source)

Transforms all characters in the given string into upper case characters.

Parameters:
sourceString to transpose.
Returns:
A copy of the upper case only string

Definition at line 55 of file WStringUtils.cpp.

Referenced by WEEGPositionsLibrary::getPosition(), and WEEGPositionsLibrary::WEEGPositionsLibrary().

std::string string_utils::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 the given string.

Parameters:
sourceString to trim
tString representing a set containg all trimmable characters
Returns:
A copy of the trimmed string

Definition at line 49 of file WStringUtils.cpp.

References lTrim(), and rTrim().

Referenced by operator>>().


Variable Documentation

const std::string string_utils::WHITESPACE

We consider the following characters as whitespace:

These characters are regarded as whitespaces.

  • \r carriage return
  • \n newline
  • \t tab
  • ' ' space