EventAnalysis  1.3.0
Typedefs | Functions
EA::StringUtils Namespace Reference

A namespace with some utility methods for strings. More...

Typedefs

using Tokens = std::vector< std::string >
 

Functions

Tokens Tokenize (const std::string &str, char delimiter=' ')
 Extracts words from a string. More...
 
std::string Trim (const std::string &str)
 Trims a string. More...
 
bool IsInteger (const std::string &str)
 Checks if a string is an a integer. More...
 
bool IsReal (const std::string &str)
 Checks if a string is a real number. More...
 
bool ToBoolean (const std::string &str)
 Converts a string into a boolean value. More...
 
bool IsGlobExpression (const std::string &str)
 Check if the given string is a glob expression. More...
 
std::string RegexFromGlob (const std::string &str)
 Build a regex starting from a glob expression. More...
 
template<typename Container >
std::enable_if_t<!(is_pointer< typename Container::value_type >::value), std::string > FindSimilar (const std::string &str, const Container &cont)
 Find a similar string inside a container. More...
 
template<typename Container >
std::enable_if_t< is_pointer< typename Container::value_type >::value, std::string > FindSimilar (const std::string &str, const Container &cont)
 Find a similar string inside a container. More...
 
template<typename Container >
std::enable_if_t<!(EA::is_pointer< typename Container::value_type >::value), std::string > FindSimilar (const std::string &str, const Container &cont)
 Find a similar string inside a container. More...
 
template<typename Container >
std::enable_if_t< EA::is_pointer< typename Container::value_type >::value, std::string > FindSimilar (const std::string &str, const Container &cont)
 Find a similar string inside a container. More...
 

Detailed Description

A namespace with some utility methods for strings.

Typedef Documentation

◆ Tokens

using EA::StringUtils::Tokens = typedef std::vector<std::string>

Function Documentation

◆ FindSimilar() [1/4]

template<typename Container >
std::enable_if_t<!(EA::is_pointer<typename Container::value_type>::value), std::string> EA::StringUtils::FindSimilar ( const std::string &  str,
const Container &  cont 
)

Find a similar string inside a container.

Searches for the "most similar string" inside the given container (SFINAE version for containers of strings).

Template Parameters
ContainerThe container type.
Parameters
strThe string to search.
contThe container.
Returns
The most similar string found in the container.

◆ FindSimilar() [2/4]

template<typename Container >
std::enable_if_t<EA::is_pointer<typename Container::value_type>::value, std::string> EA::StringUtils::FindSimilar ( const std::string &  str,
const Container &  cont 
)

Find a similar string inside a container.

Searches for the "most similar string" inside the given container (SFINAE version for containers of strings).

Template Parameters
ContainerThe container type.
Parameters
strThe string to search.
contThe container.
Returns
The most similar string found in the container.

◆ FindSimilar() [3/4]

template<typename Container >
std::enable_if_t<!(is_pointer<typename Container::value_type>::value), std::string> EA::StringUtils::FindSimilar ( const std::string &  str,
const Container &  cont 
)

Find a similar string inside a container.

Searches for the "most similar string" inside the given container (SFINAE version for containers of strings).

Template Parameters
ContainerThe container type.
Parameters
strThe string to search.
contThe container.
Returns
The most similar string found in the container.

◆ FindSimilar() [4/4]

template<typename Container >
std::enable_if_t<is_pointer<typename Container::value_type>::value, std::string> EA::StringUtils::FindSimilar ( const std::string &  str,
const Container &  cont 
)

Find a similar string inside a container.

Searches for the "most similar string" inside the given container (SFINAE version for containers of pointers to strings).

Template Parameters
ContainerThe container type.
Parameters
strThe string to search.
contThe container.
Returns
The most similar string found in the container.

Searches for the "most similar string" inside the given container (SFINAE version for containers of strings).

Template Parameters
ContainerThe container type.
Parameters
strThe string to search.
contThe container.
Returns
The most similar string found in the container.

◆ IsGlobExpression()

bool EA::StringUtils::IsGlobExpression ( const std::string &  str)

Check if the given string is a glob expression.

A glob expression is recognized by looking at '*' or '?' characters in the string. Other glob wildcards are ignored.

Parameters
strThe string to be checked.
Returns
true if the string is a glob expression.

◆ IsInteger()

bool EA::StringUtils::IsInteger ( const std::string &  str)

Checks if a string is an a integer.

Checks if the trimmed input string represents an integer value, possibly with sign.

Parameters
strThe input string.
Returns
true if the input string represents an integer value.

◆ IsReal()

bool EA::StringUtils::IsReal ( const std::string &  str)

Checks if a string is a real number.

Checks if the trimmed input string represents a real value, possibly with sign, in the standard notation (e.g. 10.2, not 1.02e1). Note that integer numbers e.g. 10 are real numbers so they are recognized as real by this function.

Parameters
strThe input string.
Returns
true if the input string represents a real number in standard notation.

◆ RegexFromGlob()

std::string EA::StringUtils::RegexFromGlob ( const std::string &  str)

Build a regex starting from a glob expression.

This function manages only the '*' and '?' glob wildcards.

Parameters
strThe glob expression.
Returns
The regex string corresponding to the given glob expression.

◆ ToBoolean()

bool EA::StringUtils::ToBoolean ( const std::string &  str)

Converts a string into a boolean value.

Valid values for the string are : "0", "1", "true" (any case), "false" (any case). Non-trimmed strings (e.g. "true ", " 1 ") are considered invalid.

Parameters
strThe input string.
Returns
true if the input string represents a true boolean value, false otherwise.
Exceptions
std::invalid_argumentif the string does not represent a boolean value

◆ Tokenize()

StringUtils::Tokens EA::StringUtils::Tokenize ( const std::string &  str,
char  delimiter = ' ' 
)

Extracts words from a string.

The string must be a sequence of words separated by a given delimiter. A string portion enclosed within double quotation marks will be treated as a single word (the quotation marks will be removed). If the closing double quotation is missing then a std::runtime_error is thrown.

Parameters
strThe string to tokenize.
delimiterA vector used to return the words in the string.
Returns
A collection of tokens (i.e. strings) in which the input string has been broken into.
Exceptions
std::runtime_errorif a non-terminated double-quotation-enclosed sequence is found.

◆ Trim()

std::string EA::StringUtils::Trim ( const std::string &  str)

Trims a string.

Removes all leading and trailing invisible characters (spaces, tabs etc.).

Parameters
strThe input string.
Returns
a new string containing the trimmed input string.