bpp-core  2.4.0
bpp::TextTools Namespace Reference

Some utilitary functions that work on strings. More...

Functions

bool isEmpty (const std::string &s)
 Tell if a string is empty. A string is considered to be 'empty' if it is only made of white spaces. More...
 
std::string toUpper (const std::string &s)
 Make the string uppercase. More...
 
std::string toLower (const std::string &s)
 Make the string lowercase. More...
 
bool isWhiteSpaceCharacter (char c)
 Tell if a character is a white space or not. More...
 
std::string removeWhiteSpaces (const std::string &s)
 Remove all white spaces characters in a string. More...
 
std::string removeFirstWhiteSpaces (const std::string &s)
 Remove all white spaces characters at the beginning of a string. More...
 
std::string removeLastWhiteSpaces (const std::string &s)
 Remove all white spaces characters at the end of a string. More...
 
std::string removeSurroundingWhiteSpaces (const std::string &s)
 Remove all white spaces characters at the beginning and the end of a string. More...
 
bool isNewLineCharacter (char c)
 Tell if a character is a new line character or not. More...
 
std::string removeNewLines (const std::string &s)
 Remove all new line characters in a string. More...
 
std::string removeLastNewLines (const std::string &s)
 Remove all new line characters at the end of a string. More...
 
bool isDecimalNumber (char c)
 Tell is a given character describes a decimal number. More...
 
bool isDecimalNumber (const std::string &s, char dec= '.', char scientificNotation= 'e')
 Tell is a given character string describes a decimal number. FIXME : for now, this parser will not recognize thousands delimiters, and not the scientific notation neither. More...
 
bool isDecimalInteger (const std::string &s, char scientificNotation= 'e')
 Tell is a given character string describes a decimal integer. FIXME: for now, this parser will not recognize thousands delimiters, and not the scientific notation neither. More...
 
int toInt (const std::string &s, char scientificNotation= 'e')
 Convert from string to int. More...
 
double toDouble (const std::string &s, char dec= '.', char scientificNotation= 'e')
 Convert from string to double. More...
 
std::string resizeRight (const std::string &s, std::size_t newSize, char fill)
 
std::string resizeLeft (const std::string &s, std::size_t newSize, char fill)
 
std::vector< std::string > split (const std::string &s, std::size_t n)
 
std::string removeSubstrings (const std::string &s, char blockBeginning, char blockEnding)
 Remove substrings from a string. All substrings beginning with blockBeginning and ending with blockEnding will be removed. Nesting blocks are allowed, the most extern block will be removed. More...
 
std::string removeSubstrings (const std::string &s, char blockBeginning, char blockEnding, std::vector< std::string > &exceptionsBeginning, std::vector< std::string > &exceptionsEnding)
 Remove substrings from a string, unless they match some specific substrings. All substrings beginning with blockBeginning and ending with blockEnding will be removed, except if they begin with a string included in the vector exceptionsBeginning or end with a string included in the vector exceptionsEnding. Nesting blocks are allowed, the most extern block will be removed. More...
 
std::string removeChar (const std::string &s, char c)
 Remove all occurences of a character in a string. More...
 
std::size_t count (const std::string &s, const std::string &pattern)
 Count the occurences of a given pattern in a string. More...
 
bool startsWith (const std::string &s, const std::string &pattern)
 Tell is a string begins with a certain motif. More...
 
bool endsWith (const std::string &s, const std::string &pattern)
 Tell is a string ends with a certain motif. More...
 
bool hasSubstring (const std::string &s, const std::string &pattern)
 Tell is a string contains a certain motif. More...
 
void replaceAll (std::string &target, const std::string &query, const std::string &replacement)
 Replacement of all non-overlapping occurrences of a certain motif in a string. More...
 
template<class T >
std::string toString (T t)
 General template method to convert to a string. More...
 
template<class T >
std::string toString (T t, int precision)
 Template string conversion. More...
 
template<class T >
fromString (const std::string &s)
 General template method to convert from string. More...
 
template<class T >
to (const std::string &s)
 Template to string conversion. More...
 
std::string resizeRight (const std::string &s, size_t newSize, char fill= ' ')
 Send a string of size 'newSize', which is a copy of 's' truncated or filled with character 'fill' at the end. More...
 
std::string resizeLeft (const std::string &s, size_t newSize, char fill= ' ')
 Send a string of size 'newSize', which is a copy of 's' truncated or filled with character 'fill' at the beginning. More...
 
std::vector< std::string > split (const std::string &s, size_t n)
 Split a string into parts of size 'n'. The last part may contain < n chars. More...
 

Detailed Description

Some utilitary functions that work on strings.

Function Documentation

std::size_t bpp::TextTools::count ( const std::string &  s,
const std::string &  pattern 
)

Count the occurences of a given pattern in a string.

Parameters
sThe string to search.
patternThe pattern to use (this is a mere string, not a regexp!).
Returns
The number of occurences of 'pattern' in 's'.

Definition at line 406 of file TextTools.cpp.

Referenced by bpp::ContingencyTableTest::ContingencyTableTest(), bpp::NewtonOneDimension::doStep(), bpp::NestedStringTokenizer::NestedStringTokenizer(), and to().

bool bpp::TextTools::endsWith ( const std::string &  s,
const std::string &  pattern 
)

Tell is a string ends with a certain motif.

Parameters
sThe string to search.
patternThe pattern to use (this is a mere string, not a regexp!).
Returns
true/false

Definition at line 429 of file TextTools.cpp.

Referenced by to().

template<class T >
T bpp::TextTools::fromString ( const std::string &  s)

General template method to convert from string.

Parameters
sThe string to convert.
Returns
An object from string t.

Definition at line 178 of file TextTools.h.

References toDouble(), and toInt().

bool bpp::TextTools::hasSubstring ( const std::string &  s,
const std::string &  pattern 
)

Tell is a string contains a certain motif.

Parameters
sThe string to search.
patternThe pattern to use (this is a mere string, not a regexp!).
Returns
true/false

Definition at line 438 of file TextTools.cpp.

Referenced by removeSubstrings(), and to().

bool bpp::TextTools::isDecimalInteger ( const std::string &  s,
char  scientificNotation = 'e' 
)

Tell is a given character string describes a decimal integer. FIXME: for now, this parser will not recognize thousands delimiters, and not the scientific notation neither.

Parameters
sThe string to parse.
scientificNotationcharacter to use for scientific notation (typically 'e' or 'E').
Returns
true if the given string is the representation of a decimal integer.

Definition at line 193 of file TextTools.cpp.

References isDecimalNumber(), and isEmpty().

Referenced by toInt().

bool bpp::TextTools::isDecimalNumber ( char  c)

Tell is a given character describes a decimal number.

Parameters
cThe character to check.
Returns
true if the given character is the reprensentation of a decimal number.

Definition at line 151 of file TextTools.cpp.

Referenced by isDecimalInteger(), isDecimalNumber(), and toDouble().

bool bpp::TextTools::isDecimalNumber ( const std::string &  s,
char  dec = '.',
char  scientificNotation = 'e' 
)

Tell is a given character string describes a decimal number. FIXME : for now, this parser will not recognize thousands delimiters, and not the scientific notation neither.

Parameters
sThe string to parse.
decThe decimal separator.
scientificNotationcharacter to use for scientific notation (typically 'e' or 'E').
Returns
true if the given string is the representation of a decimal number.

Definition at line 155 of file TextTools.cpp.

References isDecimalNumber(), and isEmpty().

bool bpp::TextTools::isEmpty ( const std::string &  s)
bool bpp::TextTools::isNewLineCharacter ( char  c)

Tell if a character is a new line character or not.

Parameters
cThe character to check.
Returns
True if c is one of the following: '\n' or '\r'.

Definition at line 128 of file TextTools.cpp.

Referenced by removeLastNewLines(), and removeNewLines().

bool bpp::TextTools::isWhiteSpaceCharacter ( char  c)

Tell if a character is a white space or not.

Parameters
cThe character to check.
Returns
True if c is one of the following: ' ', '\t', '\n', '\r', '\f', '\v'.

Definition at line 85 of file TextTools.cpp.

std::string bpp::TextTools::removeChar ( const std::string &  s,
char  c 
)

Remove all occurences of a character in a string.

Parameters
sThe string to parse.
cThe character to remove.
Returns
The string with all specified chars removed.

Definition at line 397 of file TextTools.cpp.

Referenced by to().

std::string bpp::TextTools::removeFirstWhiteSpaces ( const std::string &  s)

Remove all white spaces characters at the beginning of a string.

Parameters
sThe string to parse.
Returns
A copy of 's' beginning with the first non-white character.

Definition at line 99 of file TextTools.cpp.

Referenced by bpp::KeyvalTools::changeKeyvals(), and bpp::KeyvalTools::parseProcedure().

std::string bpp::TextTools::removeLastNewLines ( const std::string &  s)

Remove all new line characters at the end of a string.

Parameters
sThe string to parse.
Returns
A copy of 's' ending with the last non-new line character.

Definition at line 142 of file TextTools.cpp.

References isNewLineCharacter().

std::string bpp::TextTools::removeLastWhiteSpaces ( const std::string &  s)

Remove all white spaces characters at the end of a string.

Parameters
sThe string to parse.
Returns
A copy of 's' ending with the last non-white character.

Definition at line 107 of file TextTools.cpp.

std::string bpp::TextTools::removeNewLines ( const std::string &  s)

Remove all new line characters in a string.

Parameters
sThe string to parse.
Returns
A copy of 's' without new line characters.

Definition at line 132 of file TextTools.cpp.

References isNewLineCharacter().

std::string bpp::TextTools::removeSubstrings ( const std::string &  s,
char  blockBeginning,
char  blockEnding 
)

Remove substrings from a string. All substrings beginning with blockBeginning and ending with blockEnding will be removed. Nesting blocks are allowed, the most extern block will be removed.

Parameters
sThe string to parse.
blockBeginningThe character specifying the beginning of each block.
blockEndingThe character specifying the end of each block.
Returns
The string with all blocks removed.
Exceptions
ExceptionIf some blocks are not well formed.

Definition at line 300 of file TextTools.cpp.

Referenced by to().

std::string bpp::TextTools::removeSubstrings ( const std::string &  s,
char  blockBeginning,
char  blockEnding,
std::vector< std::string > &  exceptionsBeginning,
std::vector< std::string > &  exceptionsEnding 
)

Remove substrings from a string, unless they match some specific substrings. All substrings beginning with blockBeginning and ending with blockEnding will be removed, except if they begin with a string included in the vector exceptionsBeginning or end with a string included in the vector exceptionsEnding. Nesting blocks are allowed, the most extern block will be removed.

Parameters
sThe string to parse.
blockBeginningThe character specifying the beginning of each block.
blockEndingThe character specifying the end of each block.
exceptionsBeginningA vector containing all strings specifying the beginning of blocks that should not be removed.
exceptionsEndingA vector containing all strings specifying the ending of blocks that should not be removed.
Returns
The string with all blocks removed.
Exceptions
ExceptionIf some blocks are not well formed.

Definition at line 329 of file TextTools.cpp.

References hasSubstring(), and toString().

std::string bpp::TextTools::removeSurroundingWhiteSpaces ( const std::string &  s)

Remove all white spaces characters at the beginning and the end of a string.

Parameters
sThe string to parse.
Returns
A copy of 's' beginning with the first non-white character and ending with the last one.

Definition at line 116 of file TextTools.cpp.

Referenced by bpp::KeyvalTools::changeKeyvals(), and bpp::KeyvalTools::multipleKeyvals().

std::string bpp::TextTools::removeWhiteSpaces ( const std::string &  s)

Remove all white spaces characters in a string.

Parameters
sThe string to parse.
Returns
A copy of 's' without white spaces characters.

Definition at line 89 of file TextTools.cpp.

Referenced by bpp::ComputationTree::ComputationTree(), and bpp::AttributesTools::getAttributesMap().

void bpp::TextTools::replaceAll ( std::string &  target,
const std::string &  query,
const std::string &  replacement 
)

Replacement of all non-overlapping occurrences of a certain motif in a string.

Parameters
targetString to be modified
queryThe motif to look for
replacementThe replacement string

Definition at line 445 of file TextTools.cpp.

Referenced by to().

std::string bpp::TextTools::resizeLeft ( const std::string &  s,
size_t  newSize,
char  fill = ' ' 
)

Send a string of size 'newSize', which is a copy of 's' truncated or filled with character 'fill' at the beginning.

Parameters
sThe string to parse.
newSizeThe new string size.
fillThe character to use to fill the string id length < newSize.
Returns
A string of size newsize which is a copy from the right of s.
std::string bpp::TextTools::resizeLeft ( const std::string &  s,
std::size_t  newSize,
char  fill 
)

Definition at line 264 of file TextTools.cpp.

Referenced by bpp::ApplicationTools::displayGauge(), and to().

std::string bpp::TextTools::resizeRight ( const std::string &  s,
size_t  newSize,
char  fill = ' ' 
)

Send a string of size 'newSize', which is a copy of 's' truncated or filled with character 'fill' at the end.

Parameters
sThe string to parse.
newSizeThe new string size.
fillThe character to use to fill the string id length < newSize.
Returns
A string of size newsize which is a copy from the left of s.
std::string bpp::TextTools::resizeRight ( const std::string &  s,
std::size_t  newSize,
char  fill 
)
std::vector<std::string> bpp::TextTools::split ( const std::string &  s,
size_t  n 
)

Split a string into parts of size 'n'. The last part may contain < n chars.

Parameters
sThe string to parse.
nThe number of tokens.
Returns
A vector of strings with all tokens.
std::vector<std::string> bpp::TextTools::split ( const std::string &  s,
std::size_t  n 
)

Definition at line 283 of file TextTools.cpp.

References bpp::IntegerTools::divideDown(), and bpp::IntegerTools::divideUp().

Referenced by to().

bool bpp::TextTools::startsWith ( const std::string &  s,
const std::string &  pattern 
)

Tell is a string begins with a certain motif.

Parameters
sThe string to search.
patternThe pattern to use (this is a mere string, not a regexp!).
Returns
true/false

Definition at line 420 of file TextTools.cpp.

Referenced by bpp::AbstractParametrizable::getParameterNameWithoutNamespace(), bpp::AbstractParametrizable::setNamespace(), bpp::AbstractParameterAliasable::setNamespace(), and to().

template<class T >
T bpp::TextTools::to ( const std::string &  s)

Template to string conversion.

Parameters
sThe string to parse.
Returns
An object of class R corresponding to s.

Definition at line 208 of file TextTools.h.

References count(), endsWith(), hasSubstring(), removeChar(), removeSubstrings(), replaceAll(), resizeLeft(), resizeRight(), split(), and startsWith().

double bpp::TextTools::toDouble ( const std::string &  s,
char  dec = '.',
char  scientificNotation = 'e' 
)

Convert from string to double.

Parameters
sThe string to parse.
decThe decimal separator.
scientificNotationcharacter to use for scientific notation (typically 'e' or 'E').
Returns
The double corresponding to s.
Exceptions
Exceptionif the string does not specify a valid number.

Definition at line 237 of file TextTools.cpp.

References isDecimalNumber().

Referenced by fromString(), bpp::ApplicationTools::getDoubleParameter(), bpp::NumCalcApplicationTools::getVector(), bpp::BppODiscreteDistributionFormat::read(), bpp::IntervalConstraint::readDescription(), and bpp::ComputationTree::readFormula_().

int bpp::TextTools::toInt ( const std::string &  s,
char  scientificNotation = 'e' 
)

Convert from string to int.

Parameters
sThe string to parse.
scientificNotationcharacter to use for scientific notation (typically 'e' or 'E').
Returns
The integer corresponding to s.
Exceptions
Exceptionif the string does not specify a valid number.

Definition at line 228 of file TextTools.cpp.

References isDecimalInteger().

Referenced by fromString(), bpp::ApplicationTools::getIntParameter(), bpp::NumCalcApplicationTools::getVector(), bpp::BppODiscreteDistributionFormat::read(), and bpp::NumCalcApplicationTools::seqFromString().

std::string bpp::TextTools::toLower ( const std::string &  s)

Make the string lowercase.

Parameters
sThe string to analyse.
Returns
A copy of the string with all chars lowercase.

Definition at line 75 of file TextTools.cpp.

template<class T >
std::string bpp::TextTools::toString ( t)

General template method to convert to a string.

Parameters
tThe object to convert.
Returns
A string equal to t.

Definition at line 153 of file TextTools.h.

Referenced by bpp::ParametrizableCollection< N >::addObject(), bpp::AbstractParameterAliasable::aliasParameters(), bpp::DirichletDiscreteDistribution::applyParameters(), bpp::AutoCorrelationTransitionMatrix::AutoCorrelationTransitionMatrix(), bpp::SvgGraphicDevice::colorToText(), bpp::RescaledHmmLikelihood::computeForward_(), bpp::ContingencyTableTest::ContingencyTableTest(), bpp::GlobalGraph::deleteNode(), bpp::DirichletDiscreteDistribution::DirichletDiscreteDistribution(), bpp::ApplicationTools::displayGauge(), bpp::ApplicationTools::displayUnlimitedGauge(), bpp::GoldenSectionSearch::doInit(), bpp::BrentOneDimension::doInit(), bpp::NewtonOneDimension::doStep(), bpp::SvgGraphicDevice::drawCircle(), bpp::SvgGraphicDevice::drawLine(), bpp::SvgGraphicDevice::drawRect(), bpp::SvgGraphicDevice::drawText(), bpp::XFigGraphicDevice::drawText(), bpp::EigenValue< Real >::EigenValue(), bpp::PgfGraphicDevice::end(), bpp::AutoCorrelationTransitionMatrix::fireParameterChanged(), bpp::SimpleDiscreteDistribution::fireParameterChanged(), bpp::MixtureOfDiscreteDistributions::fireParameterChanged(), bpp::Simplex::fireParameterChanged(), bpp::FullHmmTransitionMatrix::FullHmmTransitionMatrix(), bpp::ApplicationTools::getBooleanParameter(), bpp::XFigColorManager::getColor(), bpp::GlobalGraph::getDegree(), bpp::IntervalConstraint::getDescription(), bpp::ApplicationTools::getDoubleParameter(), bpp::AssociationGraphImplObserver< N, E, TreeGraphImpl >::getEdgeIndex(), bpp::TreeGraphImpl< GraphImpl >::getFather(), bpp::AbstractFontManager< int >::getFont(), bpp::ApplicationTools::getIntParameter(), bpp::ApplicationTools::getMatrixParameter(), bpp::AssociationGraphImplObserver< N, E, TreeGraphImpl >::getNodeIndex(), bpp::ApplicationTools::getParameter(), bpp::NumCalcApplicationTools::getParameterGrid(), bpp::ParametrizableCollection< N >::getParametersForObject(), bpp::AbstractDiscreteDistribution::getValueCategory(), bpp::ApplicationTools::getVectorParameter(), bpp::ReparametrizationFunctionWrapper::init_(), bpp::BppODiscreteDistributionFormat::initialize_(), bpp::GlobalGraph::isLeaf(), bpp::GlobalGraph::link(), bpp::MixtureOfDiscreteDistributions::MixtureOfDiscreteDistributions(), bpp::ConstantOperator::output(), bpp::AbstractOptimizer::printPoint(), bpp::BppODiscreteDistributionFormat::read(), removeSubstrings(), bpp::AttributesTools::resolveVariables(), bpp::SimpleDiscreteDistribution::restrictToConstraint(), bpp::PgfGraphicDevice::setCurrentBackgroundColor(), bpp::PgfGraphicDevice::setCurrentForegroundColor(), bpp::PgfGraphicDevice::setCurrentLayer(), bpp::AbstractGraphicDevice::setCurrentLineType(), bpp::PgfGraphicDevice::setCurrentLineType(), bpp::XFigGraphicDevice::setCurrentLineType(), bpp::Simplex::setFrequencies(), bpp::FullHmmTransitionMatrix::setTransitionProbabilities(), bpp::SimpleDiscreteDistribution::SimpleDiscreteDistribution(), bpp::Simplex::Simplex(), bpp::GlobalGraph::switchNodes(), bpp::RGBColor::toString(), bpp::Number< unsigned int >::toString(), bpp::Font::toString(), bpp::Range< T >::toString(), bpp::GlobalGraph::unlinkInEdgeStructure_(), bpp::GlobalGraph::unlinkInNodeStructure_(), and bpp::BppODiscreteDistributionFormat::write().

template<class T >
std::string bpp::TextTools::toString ( t,
int  precision 
)

Template string conversion.

Parameters
tThe object to convert.
precisionTo use (for numbers).
Returns
A string equal to t.

Definition at line 166 of file TextTools.h.

std::string bpp::TextTools::toUpper ( const std::string &  s)

Make the string uppercase.

Parameters
sThe string to analyse.
Returns
A copy of the string with all chars uppercase.

Definition at line 65 of file TextTools.cpp.