bpp-core3  3.0.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

◆ count()

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 388 of file TextTools.cpp.

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

◆ endsWith()

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 411 of file TextTools.cpp.

Referenced by to().

◆ fromString()

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 140 of file TextTools.h.

References toDouble(), and toInt().

◆ hasSubstring()

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 420 of file TextTools.cpp.

Referenced by removeSubstrings(), and to().

◆ isDecimalInteger()

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 173 of file TextTools.cpp.

References isDecimalNumber(), and isEmpty().

Referenced by toInt().

◆ isDecimalNumber() [1/2]

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 131 of file TextTools.cpp.

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

◆ isDecimalNumber() [2/2]

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 135 of file TextTools.cpp.

References isDecimalNumber(), and isEmpty().

◆ isEmpty()

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

◆ isNewLineCharacter()

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 104 of file TextTools.cpp.

Referenced by removeLastNewLines(), and removeNewLines().

◆ isWhiteSpaceCharacter()

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 53 of file TextTools.cpp.

◆ removeChar()

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 379 of file TextTools.cpp.

Referenced by to().

◆ removeFirstWhiteSpaces()

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 69 of file TextTools.cpp.

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

◆ removeLastNewLines()

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 120 of file TextTools.cpp.

References isNewLineCharacter().

◆ removeLastWhiteSpaces()

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 79 of file TextTools.cpp.

◆ removeNewLines()

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 108 of file TextTools.cpp.

References isNewLineCharacter().

◆ removeSubstrings() [1/2]

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 282 of file TextTools.cpp.

Referenced by to().

◆ removeSubstrings() [2/2]

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 311 of file TextTools.cpp.

References hasSubstring(), and toString().

◆ removeSurroundingWhiteSpaces()

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 90 of file TextTools.cpp.

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

◆ removeWhiteSpaces()

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 57 of file TextTools.cpp.

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

◆ replaceAll()

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 427 of file TextTools.cpp.

Referenced by to().

◆ resizeLeft() [1/2]

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.

◆ resizeLeft() [2/2]

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

Definition at line 244 of file TextTools.cpp.

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

◆ resizeRight() [1/2]

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.

◆ resizeRight() [2/2]

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

◆ split() [1/2]

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.

◆ split() [2/2]

std::vector<std::string> bpp::TextTools::split ( const std::string &  s,
std::size_t  n 
)

Definition at line 263 of file TextTools.cpp.

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

Referenced by to().

◆ startsWith()

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 402 of file TextTools.cpp.

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

◆ 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 170 of file TextTools.h.

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

◆ toDouble()

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 217 of file TextTools.cpp.

References isDecimalNumber().

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

◆ toInt()

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 208 of file TextTools.cpp.

References isDecimalInteger().

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

◆ toLower()

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 41 of file TextTools.cpp.

◆ toString() [1/2]

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 115 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::FunctionTools::computeGrid(), 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::GlobalGraph::edgeMustExist_(), bpp::AssociationGraphImplObserver< N, E, TreeGraphImpl >::edgeToString(), 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::AbstractDiscreteDistribution::getCategoryIndex(), bpp::XFigColorManager::getColor(), bpp::GlobalGraph::getDegree(), bpp::IntervalConstraint::getDescription(), bpp::ApplicationTools::getDoubleParameter(), bpp::AssociationGraphImplObserver< N, E, TreeGraphImpl >::getEdgeGraphid(), bpp::TreeGraphImpl< GraphImpl >::getFatherOfNode(), bpp::AbstractFontManager< int >::getFont(), bpp::ApplicationTools::getIntParameter(), bpp::ApplicationTools::getMatrixParameter(), bpp::AssociationGraphImplObserver< N, E, TreeGraphImpl >::getNodeGraphid(), bpp::ApplicationTools::getParameter(), bpp::NumCalcApplicationTools::getParameterGrid(), bpp::ParametrizableCollection< N >::getParametersForObject(), bpp::AbstractDiscreteDistribution::getValueCategory(), bpp::ApplicationTools::getVectorOfVectorsParameter(), bpp::ApplicationTools::getVectorParameter(), bpp::ReparametrizationFunctionWrapper::init_(), bpp::BppODiscreteDistributionFormat::initialize_(), bpp::GlobalGraph::isLeaf(), bpp::GlobalGraph::link(), bpp::MixtureOfDiscreteDistributions::MixtureOfDiscreteDistributions(), bpp::GlobalGraph::nodeMustExist_(), bpp::AssociationGraphImplObserver< N, E, TreeGraphImpl >::nodeToString(), bpp::ConstantOperator::output(), bpp::AbstractOptimizer::printPoint(), bpp::BppODiscreteDistributionFormat::readDiscreteDistribution(), 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::writeDiscreteDistribution().

◆ toString() [2/2]

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 128 of file TextTools.h.

◆ toUpper()

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 29 of file TextTools.cpp.