bpp-core  2.2.0
bpp::TextTools Class Reference

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

#include <Bpp/Text/TextTools.h>

Static Public Member Functions

static bool isEmpty (const std::string &s)
 Tell if a string is empty. More...
 
static std::string toUpper (const std::string &s)
 Make the string uppercase. More...
 
static std::string toLower (const std::string &s)
 Make the string lowercase. More...
 
static bool isWhiteSpaceCharacter (char c)
 Tell if a character is a white space or not. More...
 
static std::string removeWhiteSpaces (const std::string &s)
 Remove all white spaces characters in a string. More...
 
static std::string removeFirstWhiteSpaces (const std::string &s)
 Remove all white spaces characters at the beginning of a string. More...
 
static std::string removeLastWhiteSpaces (const std::string &s)
 Remove all white spaces characters at the end of a string. More...
 
static std::string removeSurroundingWhiteSpaces (const std::string &s)
 Remove all white spaces characters at the beginning and the end of a string. More...
 
static bool isNewLineCharacter (char c)
 Tell if a character is a new line character or not. More...
 
static std::string removeNewLines (const std::string &s)
 Remove all new line characters in a string. More...
 
static std::string removeLastNewLines (const std::string &s)
 Remove all new line characters at the end of a string. More...
 
static bool isDecimalNumber (char c)
 Tell is a given character describes a decimal number. More...
 
static bool isDecimalNumber (const std::string &s, char dec='.', char scientificNotation='e')
 Tell is a given character string describes a decimal number. More...
 
static bool isDecimalInteger (const std::string &s, char scientificNotation='e')
 Tell is a given character string describes a decimal integer. More...
 
template<class T >
static std::string toString (T t)
 General template method to convert to a string. More...
 
template<class T >
static std::string toString (T t, int precision)
 Template string conversion. More...
 
template<class T >
static T fromString (const std::string &s)
 General template method to convert from string. More...
 
static std::string toString (int i)
 Convert from int to string. More...
 
static std::string toString (char c)
 Convert from char to string. More...
 
static std::string toString (double d, int precision=6)
 Convert from double to string. More...
 
static int toInt (const std::string &s, char scientificNotation='e') throw (Exception)
 Convert from string to int. More...
 
static double toDouble (const std::string &s, char dec='.', char scientificNotation='e') throw (Exception)
 Convert from string to double. More...
 
template<class T >
static T to (const std::string &s)
 Template to string conversion. More...
 
static 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...
 
static 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...
 
static std::vector< std::string > split (const std::string &s, size_t n)
 Split a string into parts of size 'n'. More...
 
static std::string removeSubstrings (const std::string &s, char blockBeginning, char blockEnding) throw (Exception)
 Remove substrings from a string. More...
 
static std::string removeSubstrings (const std::string &s, char blockBeginning, char blockEnding, std::vector< std::string > &exceptionsBeginning, std::vector< std::string > &exceptionsEnding) throw (Exception)
 Remove substrings from a string, unless they match some specific substrings. More...
 
static std::string removeChar (const std::string &s, char c)
 Remove all occurences of a character in a string. More...
 
static unsigned int count (const std::string &s, const std::string &pattern)
 Count the occurences of a given pattern in a string. More...
 
static bool startsWith (const std::string &s, const std::string &pattern)
 Tell is a string begins with a certain motif. More...
 
static bool endsWith (const std::string &s, const std::string &pattern)
 Tell is a string ends with a certain motif. More...
 
static bool hasSubstring (const std::string &s, const std::string &pattern)
 Tell is a string contains a certain motif. More...
 
static 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...
 

Detailed Description

Some utilitary functions that work on strings.

Definition at line 58 of file TextTools.h.

Member Function Documentation

◆ count()

unsigned int TextTools::count ( const std::string &  s,
const std::string &  pattern 
)
static

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

Referenced by bpp::NestedStringTokenizer::NestedStringTokenizer().

◆ endsWith()

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

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

◆ fromString()

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

General template method to convert from string.

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

Definition at line 217 of file TextTools.h.

◆ hasSubstring()

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

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

◆ isDecimalInteger()

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

Tell is a given character string describes a decimal integer.

NB: 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 247 of file TextTools.cpp.

◆ isDecimalNumber() [1/2]

bool TextTools::isDecimalNumber ( char  c)
static

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

◆ isDecimalNumber() [2/2]

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

Tell is a given character string describes a decimal number.

NB: 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 216 of file TextTools.cpp.

◆ isEmpty()

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

Tell if a string is empty.

A string is considered to be 'empty' if it is only made of white spaces.

Parameters
sThe string to check.
Returns
True if the string has only white characters.

Definition at line 52 of file TextTools.cpp.

Referenced by bpp::ApplicationTools::getMatrixParameter(), bpp::FileTools::getNextLine(), bpp::ApplicationTools::getVectorParameter(), bpp::ApplicationTools::parameterExists(), bpp::KeyvalTools::parseProcedure(), bpp::BppODiscreteDistributionFormat::read(), and bpp::DataTable::read().

◆ isNewLineCharacter()

bool TextTools::isNewLineCharacter ( char  c)
static

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

◆ isWhiteSpaceCharacter()

bool TextTools::isWhiteSpaceCharacter ( char  c)
static

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' or '\f'.

Definition at line 89 of file TextTools.cpp.

◆ removeChar()

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

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

◆ removeFirstWhiteSpaces()

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

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

Referenced by bpp::KeyvalTools::parseProcedure().

◆ removeLastNewLines()

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

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

◆ removeLastWhiteSpaces()

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

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

◆ removeNewLines()

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

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

◆ removeSubstrings() [1/2]

std::string TextTools::removeSubstrings ( const std::string &  s,
char  blockBeginning,
char  blockEnding 
)
throw (Exception
)
static

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

◆ removeSubstrings() [2/2]

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

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

◆ removeSurroundingWhiteSpaces()

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

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

Referenced by bpp::KeyvalTools::multipleKeyvals().

◆ removeWhiteSpaces()

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

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

◆ replaceAll()

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

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

◆ resizeLeft()

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

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.

Definition at line 334 of file TextTools.cpp.

Referenced by bpp::ApplicationTools::displayGauge().

◆ resizeRight()

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

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.

Definition at line 324 of file TextTools.cpp.

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

◆ split()

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

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.

Definition at line 344 of file TextTools.cpp.

◆ startsWith()

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

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

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

◆ to()

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

Template to string conversion.

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

Definition at line 278 of file TextTools.h.

◆ toDouble()

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

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

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

◆ toInt()

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

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

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

◆ toLower()

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

Make the string lowercase.

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

Definition at line 77 of file TextTools.cpp.

◆ toString() [1/5]

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

General template method to convert to a string.

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

Definition at line 189 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::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::FullHmmTransitionMatrix::fireParameterChanged(), bpp::SimpleDiscreteDistribution::fireParameterChanged(), bpp::MixtureOfDiscreteDistributions::fireParameterChanged(), bpp::Simplex::fireParameterChanged(), bpp::FullHmmTransitionMatrix::FullHmmTransitionMatrix(), bpp::ApplicationTools::getBooleanParameter(), bpp::XFigColorManager::getColor(), bpp::IntervalConstraint::getDescription(), bpp::ApplicationTools::getDoubleParameter(), bpp::AbstractFontManager< int >::getFont(), bpp::ApplicationTools::getIntParameter(), bpp::ApplicationTools::getMatrixParameter(), bpp::ApplicationTools::getParameter(), bpp::NumCalcApplicationTools::getParameterGrid(), bpp::AbstractDiscreteDistribution::getValueCategory(), bpp::ApplicationTools::getVectorParameter(), bpp::ReparametrizationFunctionWrapper::init_(), bpp::BppODiscreteDistributionFormat::initialize_(), bpp::MixtureOfDiscreteDistributions::MixtureOfDiscreteDistributions(), bpp::AbstractOptimizer::printPoint(), bpp::BppODiscreteDistributionFormat::read(), 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::RGBColor::toString(), bpp::Number< unsigned int >::toString(), bpp::Font::toString(), bpp::Range< T >::toString(), and bpp::BppODiscreteDistributionFormat::write().

◆ toString() [2/5]

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

Template string conversion.

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

Definition at line 204 of file TextTools.h.

◆ toString() [3/5]

std::string TextTools::toString ( int  i)
static

Convert from int to string.

Parameters
iThe integer to convert.
Returns
A string equal to i.

Definition at line 275 of file TextTools.cpp.

◆ toString() [4/5]

std::string TextTools::toString ( char  c)
static

Convert from char to string.

Parameters
cThe character to convert.
Returns
A string equal to c.

Definition at line 284 of file TextTools.cpp.

◆ toString() [5/5]

std::string TextTools::toString ( double  d,
int  precision = 6 
)
static

Convert from double to string.

Parameters
dThe double to convert.
precisionTo use (for numbers).
Returns
A string equal to d.

Definition at line 293 of file TextTools.cpp.

◆ toUpper()

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

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.


The documentation for this class was generated from the following files: