OpenWalnut  1.5.0dev
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. More...
 
std::string toString (const unsigned char &value)
 Convert a given value to a string. More...
 
template<typename T >
std::string toString (const T &value, const size_t width, const size_t precision)
 Convert a given value to a string. More...
 
template<typename T >
fromString (const std::string &str)
 Convert a given string to a value of a certain type. More...
 
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. More...
 
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. More...
 
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. More...
 
std::string toUpper (const std::string &source)
 Transforms all characters in the given string into upper case characters. More...
 
std::string toLower (const std::string &source)
 Transforms all characters in the given string into lower case characters. More...
 
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). More...
 
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. More...
 
template<class T >
std::istream & operator>> (std::istream &in, std::vector< T > &v)
 Write an input stream into the given vector. More...
 
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. More...
 
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. More...
 

Variables

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

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

◆ fromString()

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().

+ Here is the call graph for this function:

◆ lTrim()

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().

+ Here is the caller graph for this function:

◆ operator<<() [1/3]

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().

+ Here is the call graph for this function:

◆ operator<<() [2/3]

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().

+ Here is the call graph for this function:

◆ operator<<() [3/3]

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().

+ Here is the call graph for this function:

◆ operator>>()

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().

+ Here is the call graph for this function:

◆ rTrim()

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().

+ Here is the caller graph for this function:

◆ tokenize()

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_INTERVAL >::create(), PROPERTY_TYPE_HELPER::WStringConversion< WPVBaseTypes::PV_MATRIX4X4 >::create(), PROPERTY_TYPE_HELPER::WStringConversion< WPVBaseTypes::PV_POSITION >::create(), PROPERTY_TYPE_HELPER::WStringConversion< WPVBaseTypes::PV_TRANSFERFUNCTION >::create(), WQtMenuFiltered::filterUpdate(), WReaderEEGASCII::load(), WMReadSimpleTextLineData::load(), WMReadVIM::load(), WItemSelector::newSelector(), operator>>(), WReaderELC::read(), WReaderVTK::readCoords(), WReaderVTK::readData(), WMReadVCL::readData(), WMReadDipoles::readFile(), WReaderVTK::readHARDI(), WReaderFiberVTK::readLines(), WReaderFiberVTK::readPoints(), WReaderVTK::readRectilinearGrid(), WReaderVTK::readScalars(), WReaderVTK::readStructuredPoints(), WReaderVTK::readTensors(), WReaderFiberVTK::readValues(), WReaderVTK::readValuesFromFile(), WReaderVTK::readVectors(), WQtModuleConfig::refreshModuleCheckboxes(), WPropertyStruct< T0, T1, T2, T3, T4, T5, T6, T7, T8, T9 >::setAsString(), and WQtColormapper::WQtTextureListItem::WQtTextureListItem().

+ Here is the caller graph for this function:

◆ toLower()

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.

Referenced by WMainWindow::handleGLVendor(), WMReadSimpleTextLineData::load(), WReaderVTK::readCoords(), WReaderFiberVTK::readPoints(), WReaderVTK::readRectilinearGrid(), WReaderVTK::readStructuredPoints(), and WReaderFiberVTK::readValues().

+ Here is the caller graph for this function:

◆ toString() [1/3]

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< ValueT, Rows, Cols, ValueStoreT >::at(), wge::bindTexture(), WGEColormapping::callback(), WDataHandler::clear(), WMHierchClustDisplay::clusterSelection(), WHtreeProcesser::coarseTree(), WMMultiHistogramView::connectors(), WMHistogramView::createFrame(), WMMultiHistogramView::createFrame(), WMHistogramView::createInfo(), WMMultiHistogramView::createInfo(), WMClusterDisplay::createLabel(), WRulerOrtho::createXY(), WRulerOrtho::createXZ(), WRulerOrtho::createYX(), WRulerOrtho::createYZ(), WRulerOrtho::createZX(), WRulerOrtho::createZY(), WMainWindow::event(), WPropertyGroupBase::findProperty(), WHtree::getReport(), WMProbTractDisplay::initSubModules(), WMHierchClustDisplay::moduleMain(), WMHistogramView::moduleMain(), WMMultiHistogramView::moduleMain(), WRulerOrtho::numberToString(), WMeshReaderVTK::operator()(), WGEProjectFileIO::parse(), WQtNetworkEditorProjectFileIO::parse(), WHnode::printAllData(), WHnode::printJointData(), WGEShaderDefine< ValueType >::process(), WMMultiHistogramView::properties(), WMTemplate::properties(), WHtreeProcesser::pruneRandom(), WHtreeProcesser::pruneTree(), WReaderFiberVTK::readHeader(), WDataHandler::removeSubject(), WMClusterDisplayVoxels::renderMesh(), WMWriteMesh::saveJson(), WMWebglSupport::saveSlicesGray(), WMWebglSupport::saveSlicesRGB(), WPropertyBase::set(), WMClusterDisplayVoxels::setButtonLabels(), WProjectFile::threadMain(), WPropertyMatrix4X4Widget::update(), WQtNetworkItem::updater(), wge::vector2label(), WEEGEvent::WEEGEvent(), WMPartition2Mesh::writeLabels(), WHtree::writeTree(), WHtree::writeTreeDebug(), and WHtree::writeTreeOldWalnut().

+ Here is the caller graph for this function:

◆ toString() [2/3]

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.

◆ toString() [3/3]

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.

◆ toUpper()

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(), WReaderVTK::readData(), WReaderVTK::readHARDI(), WReaderFiberVTK::readLines(), WReaderVTK::readScalars(), WReaderVTK::readTensors(), WReaderVTK::readVectors(), and WEEGPositionsLibrary::WEEGPositionsLibrary().

+ Here is the caller graph for this function:

◆ trim()

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 WMReadSimpleTextLineData::load(), and operator>>().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ WHITESPACE

const std::string string_utils::WHITESPACE
extern

We consider the following characters as whitespace:

These characters are regarded as whitespaces.

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