GEOS
Namespaces | Functions
StringUtilities.hpp File Reference
#include "common/DataTypes.hpp"
#include <iomanip>
#include <sstream>

Go to the source code of this file.

Namespaces

 geos
 

Functions

string geos::stringutilities::toLower (string const &input)
 Return a copy of the string in lower case. More...
 
template<typename IT , typename S = char>
string geos::stringutilities::join (IT first, IT last, S const &delim=S())
 Join strings or other printable objects with a delimiter. More...
 
template<typename CONTAINER , typename S = char>
string geos::stringutilities::join (CONTAINER const &container, S const &delim=S())
 Join strings or other printable objects with a delimiter. More...
 
template<typename IT , typename S , typename LAMBDA >
string geos::stringutilities::joinLamda (IT first, IT last, S const &delim, LAMBDA formattingFunc)
 Join strings or other printable objects returned by a formatter functor. More...
 
template<typename CONTAINER , typename S , typename LAMBDA >
string geos::stringutilities::joinLamda (CONTAINER const &container, S const &delim, LAMBDA formattingFunc)
 Join strings or other printable objects returned by a formatter functor. More...
 
template<typename S = char, typename T , typename ... Ts>
string geos::stringutilities::concat (S const &delim, T const &v, Ts const &... vs)
 Concatenate variadic arguments into a string with a delimiter. More...
 
template<template< class ... > class CONTAINER = std::vector>
CONTAINER< string > geos::stringutilities::tokenize (string const &str, string const &delimiters, bool const treatConsecutiveDelimAsOne=true, bool const preTrimStr=false)
 Subdivide the string in substrings by the specified delimiters. More...
 
template<template< class ... > class CONTAINER = std::vector>
CONTAINER< string > geos::stringutilities::tokenizeBySpaces (string const &str)
 Subdivide the string in substrings by whitespaces separators (see std::isspace()). Do not create any empty substrings. More...
 
string_view geos::stringutilities::trim (string_view str, string_view charsToRemove)
 Trim the string. More...
 
string_view geos::stringutilities::trimSpaces (string_view str)
 Trim the string so it does not starts nor ends with any whitespaces. More...
 
string geos::stringutilities::removeStringAndFollowingContent (string_view str, string_view strToRemove)
 Search for a string in the line, and return the line truncated before the string. More...
 
template<typename T >
string geos::stringutilities::addCommaSeparators (T const &num)
 Add comma separators to an integral number for readability. More...
 
template<typename T >
array1d< T > geos::stringutilities::fromStringToArray (string const &str)
 Take a string, and return a array1d with the cast values. More...
 
template<typename T >
string geos::stringutilities::toMetricPrefixString (T const &value)
 Take a numerical value and convert/scale it to a string with a metric prefix. i.e. Kilo, Mega, Giga, Tera, Peta, Exa. More...
 
constexpr size_t geos::stringutilities::cstrlen (char const *const str)
 
constexpr bool geos::stringutilities::startsWith (std::string_view str, std::string_view prefix)
 
constexpr bool geos::stringutilities::endsWith (std::string_view str, std::string_view suffix)
 
template<typename T >
std::ostream & geos::stringutilities::operator<< (std::ostream &os, std::optional< T > const &optValue)
 Overloading operator (<<) for std::optional<T>. More...
 

Function Documentation

◆ addCommaSeparators()

template<typename T >
string geos::stringutilities::addCommaSeparators ( T const &  num)

Add comma separators to an integral number for readability.

Template Parameters
Tthe integral type of the number to format.
Parameters
[in]numthe integral number to format.
Returns
a string representation of the number with comma separators.

◆ concat()

template<typename S = char, typename T , typename ... Ts>
string geos::stringutilities::concat ( S const &  delim,
T const &  v,
Ts const &...  vs 
)

Concatenate variadic arguments into a string with a delimiter.

Template Parameters
Stype of delimiter (printable to std::ostringstream)
Ttype of first argument (printable to std::ostringstream)
Tstypes of remaining arguments (printable to std::ostringstream)
Parameters
delimdelimiter
vfirst value
vsremaining values
Returns
string containing concatenated printed arguments

Definition at line 133 of file StringUtilities.hpp.

◆ cstrlen()

constexpr size_t geos::stringutilities::cstrlen ( char const *const  str)
constexpr
Returns
The length of a constant string computed at compile-time.
Parameters
strThe null-character terminated constant string
Todo:
c++17: this function is to remove in favor of std::string_view

Definition at line 275 of file StringUtilities.hpp.

◆ endsWith()

constexpr bool geos::stringutilities::endsWith ( std::string_view  str,
std::string_view  suffix 
)
constexpr
Returns
true if the string ends with the suffix.
Parameters
strThe string to compare
suffixA suffix we want to know if the string ends with.

Definition at line 306 of file StringUtilities.hpp.

◆ fromStringToArray()

template<typename T >
array1d< T > geos::stringutilities::fromStringToArray ( string const &  str)

Take a string, and return a array1d with the cast values.

Template Parameters
Tthe type to which the string will be cast
Parameters
[in]strthe string to turn into an array1d
Returns
the array1d that stores the cast values

Definition at line 246 of file StringUtilities.hpp.

◆ join() [1/2]

template<typename CONTAINER , typename S = char>
string geos::stringutilities::join ( CONTAINER const &  container,
S const &  delim = S() 
)

Join strings or other printable objects with a delimiter.

Template Parameters
CONTAINERtype of container to join
Sthe type of delimiter, usually char, char const * or string
Parameters
containerthe container to join
delimdelimiter used to glue together strings
Returns
a string containing input values concatenated with a delimiter

Definition at line 74 of file StringUtilities.hpp.

◆ join() [2/2]

template<typename IT , typename S = char>
string geos::stringutilities::join ( IT  first,
IT  last,
S const &  delim = S() 
)

Join strings or other printable objects with a delimiter.

Template Parameters
ITtype of iterator into the range of objects to join
Stype of delimiter, usually char, char const * or string
Parameters
firstiterator to start of the range
lastiterator past-the-end of the range
delimdelimiter used to glue together strings
Returns
a string containing input values concatenated with a delimiter

Definition at line 50 of file StringUtilities.hpp.

◆ joinLamda() [1/2]

template<typename CONTAINER , typename S , typename LAMBDA >
string geos::stringutilities::joinLamda ( CONTAINER const &  container,
S const &  delim,
LAMBDA  formattingFunc 
)

Join strings or other printable objects returned by a formatter functor.

Template Parameters
CONTAINERtype of container to join
Stype of delimiter, usually char, char const * or string
LAMBDAtype of formatter functor, usually []( auto it ) -> string
Parameters
formattingFuncformatter function to get a formattable value from an iterator of the container
containercontainer to join
delimdelimiter used to glue together strings
Returns
a string containing input values concatenated with a delimiter

Definition at line 117 of file StringUtilities.hpp.

◆ joinLamda() [2/2]

template<typename IT , typename S , typename LAMBDA >
string geos::stringutilities::joinLamda ( IT  first,
IT  last,
S const &  delim,
LAMBDA  formattingFunc 
)

Join strings or other printable objects returned by a formatter functor.

Template Parameters
ITtype of iterator into the range of objects to join
Stype of delimiter, usually char, char const * or string
LAMBDAtype of formatter functor, usually []( auto it ) -> string
Parameters
formattingFuncformatter function to get a formattable value from a IT iterator
firstiterator to start of the range
lastiterator past-the-end of the range
delimdelimiter used to glue together strings
Returns
a string containing input values concatenated with a delimiter

Definition at line 91 of file StringUtilities.hpp.

◆ operator<<()

template<typename T >
std::ostream& geos::stringutilities::operator<< ( std::ostream &  os,
std::optional< T > const &  optValue 
)

Overloading operator (<<) for std::optional<T>.

This function displays the value contained in a std::optional<T> object if one exists. Otherwise, it produces no output.

Template Parameters
TThe type of the value contained std::optional.
Parameters
osAn output stream (for example, std::cout).
optValuestd::optional<T> value to display.
Returns
The output stream

Definition at line 324 of file StringUtilities.hpp.

◆ removeStringAndFollowingContent()

string geos::stringutilities::removeStringAndFollowingContent ( string_view  str,
string_view  strToRemove 
)

Search for a string in the line, and return the line truncated before the string.

Parameters
[in]strthe line to truncate
[in]strToRemovethe string to search for in the line
Returns
the new (truncated) string

◆ startsWith()

constexpr bool geos::stringutilities::startsWith ( std::string_view  str,
std::string_view  prefix 
)
constexpr
Returns
true if the string starts with the prefix.
Parameters
strThe string to compare
prefixA prefix we want to know if the string starts with.

Definition at line 295 of file StringUtilities.hpp.

◆ tokenize()

template<template< class ... > class CONTAINER = std::vector>
CONTAINER< string > geos::stringutilities::tokenize ( string const &  str,
string const &  delimiters,
bool const  treatConsecutiveDelimAsOne = true,
bool const  preTrimStr = false 
)

Subdivide the string in substrings by the specified delimiters.

Template Parameters
CONTAINERThe templated class of the results container (std::vector by default).
Parameters
strThe string to subdivide.
delimitersString that contains the list of possible delimiters.
treatConsecutiveDelimAsOneIf enabled, consecutive delimiters will be treated as one. If not enabled, consecutive delimiters will result in empty entries.
preTrimStrIf enabled, delimiters at the borders of the string will be ignored. If not enabled, those delimiters will result in in empty entries.
Returns
The container of the divided substrings.

Definition at line 155 of file StringUtilities.hpp.

◆ tokenizeBySpaces()

template<template< class ... > class CONTAINER = std::vector>
CONTAINER< string > geos::stringutilities::tokenizeBySpaces ( string const &  str)

Subdivide the string in substrings by whitespaces separators (see std::isspace()). Do not create any empty substrings.

Template Parameters
CONTAINERThe templated class of the results container (std::vector by default).
Parameters
strThe string to subdivide.
Returns
CONTAINER< string > The list of the subdivided substrings (std::vector< string > for instance).

Definition at line 200 of file StringUtilities.hpp.

◆ toLower()

string geos::stringutilities::toLower ( string const &  input)

Return a copy of the string in lower case.

Parameters
inputThe input string which is not modified.
Returns
A new string instance.

◆ toMetricPrefixString()

template<typename T >
string geos::stringutilities::toMetricPrefixString ( T const &  value)

Take a numerical value and convert/scale it to a string with a metric prefix. i.e. Kilo, Mega, Giga, Tera, Peta, Exa.

Template Parameters
TType of the value to be converted
Parameters
valueThe value to be converted
Returns
String containging the scaled value.

◆ trim()

string_view geos::stringutilities::trim ( string_view  str,
string_view  charsToRemove 
)

Trim the string.

Parameters
[in]strthe string to trim
[in]charsToRemovethe list of characters to remove
Returns
the trimmed string

◆ trimSpaces()

string_view geos::stringutilities::trimSpaces ( string_view  str)

Trim the string so it does not starts nor ends with any whitespaces.

Parameters
[in]strthe string to trim
Returns
the trimmed string