20 #ifndef GEOS_COMMON_FORMAT_STRINGUTILITIES_HPP_
21 #define GEOS_COMMON_FORMAT_STRINGUTILITIES_HPP_
30 namespace stringutilities
49 template<
typename IT,
typename S =
char >
50 string join( IT first, IT last, S
const & delim = S() )
56 std::ostringstream oss;
58 while( ++first != last )
60 oss << delim << *first;
73 template<
typename CONTAINER,
typename S =
char >
74 string join( CONTAINER
const & container, S
const & delim = S() )
76 return join( std::begin( container ), std::end( container ), delim );
90 template<
typename IT,
typename S,
typename LAMBDA >
91 string joinLamda( IT first, IT last, S
const & delim, LAMBDA formattingFunc )
97 std::ostringstream oss;
98 oss << formattingFunc( first );
99 while( ++first != last )
101 oss << delim << formattingFunc( first );
116 template<
typename CONTAINER,
typename S,
typename LAMBDA >
117 string joinLamda( CONTAINER
const & container, S
const & delim, LAMBDA formattingFunc )
119 return joinLamda( std::begin( container ), std::end( container ), delim, formattingFunc );
132 template<
typename S = char,
typename T,
typename ... Ts >
133 string concat( S
const & delim, T
const & v, Ts
const & ... vs )
135 std::ostringstream oss;
138 using expander =
int[];
139 (void) expander{ 0, ( void ( oss << delim << vs ), 0) ... };
154 template<
template<
class ... >
class CONTAINER = std::vector >
156 string const & delimiters,
157 bool const treatConsecutiveDelimAsOne =
true,
158 bool const preTrimStr =
false )
160 CONTAINER< string > tokens;
161 string::size_type tokenBegin, tokenEnd, strEnd;
165 tokenBegin = str.find_first_not_of( delimiters );
166 strEnd = str.find_last_not_of( delimiters ) + 1;
174 while( ( ( tokenEnd = str.find_first_of( delimiters, tokenBegin ) ) < strEnd ) && tokenBegin < strEnd )
176 tokens.emplace_back( str.substr( tokenBegin, tokenEnd - tokenBegin ) );
177 tokenBegin = !treatConsecutiveDelimAsOne ? tokenEnd + 1 : str.find_first_not_of( delimiters, tokenEnd );
180 if( tokenBegin < strEnd )
182 tokens.emplace_back( str.substr( tokenBegin, strEnd-tokenBegin ));
184 else if( !preTrimStr && str.find_first_of( delimiters, strEnd - 1 ) != string::npos )
186 tokens.emplace_back(
"" );
199 template<
template<
class ... >
class CONTAINER = std::vector >
202 return tokenize< CONTAINER >( str,
" \f\n\r\t\v",
true,
true );
236 template<
typename T >
245 template<
typename T >
251 std::istringstream iss( str );
254 v.emplace_back( sub );
267 template<
typename T >
275 constexpr
size_t cstrlen(
char const *
const str )
279 char const * ptr = str;
280 for(; *ptr !=
'\0'; ++ptr )
297 return str.size() >= prefix.size() &&
298 str.compare( 0, prefix.size(), prefix ) == 0;
308 return str.size() >= suffix.size() &&
309 str.compare( str.size()-suffix.size(), suffix.size(), suffix ) == 0;
323 template<
typename T >
324 std::ostream &
operator<<( std::ostream & os, std::optional< T >
const & optValue )
328 os << optValue.value();
string joinLamda(IT first, IT last, S const &delim, LAMBDA formattingFunc)
Join strings or other printable objects returned by a formatter functor.
string_view trimSpaces(string_view str)
Trim the string so it does not starts nor ends with any whitespaces.
constexpr size_t cstrlen(char const *const str)
string join(IT first, IT last, S const &delim=S())
Join strings or other printable objects with a delimiter.
string toLower(string const &input)
Return a copy of the string in lower case.
constexpr bool startsWith(std::string_view str, std::string_view prefix)
string removeStringAndFollowingContent(string_view str, string_view strToRemove)
Search for a string in the line, and return the line truncated before the string.
string addCommaSeparators(T const &num)
Add comma separators to an integral number for readability.
string concat(S const &delim, T const &v, Ts const &... vs)
Concatenate variadic arguments into a string with a delimiter.
string_view trim(string_view str, string_view charsToRemove)
Trim the string.
CONTAINER< string > tokenize(string const &str, string const &delimiters, bool const treatConsecutiveDelimAsOne=true, bool const preTrimStr=false)
Subdivide the string in substrings by the specified delimiters.
array1d< T > fromStringToArray(string const &str)
Take a string, and return a array1d with the cast values.
string toMetricPrefixString(T const &value)
Take a numerical value and convert/scale it to a string with a metric prefix. i.e....
constexpr bool endsWith(std::string_view str, std::string_view suffix)
CONTAINER< string > tokenizeBySpaces(string const &str)
Subdivide the string in substrings by whitespaces separators (see std::isspace())....
std::ostream & operator<<(std::ostream &stream, mapBase< K, V, SORTED > const &map)
Stream output operator for map types.
Array< T, 1 > array1d
Alias for 1D array.
std::string_view string_view
String type.