GEOS
StringUtilities.hpp
Go to the documentation of this file.
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2024 Total, S.A
7  * Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
8  * Copyright (c) 2023-2024 Chevron
9  * Copyright (c) 2019- GEOS/GEOSX Contributors
10  * All rights reserved
11  *
12  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
13  * ------------------------------------------------------------------------------------------------------------
14  */
15 
20 #ifndef GEOS_COMMON_FORMAT_STRINGUTILITIES_HPP_
21 #define GEOS_COMMON_FORMAT_STRINGUTILITIES_HPP_
22 
23 #include "common/DataTypes.hpp"
24 
25 #include <iomanip>
26 #include <sstream>
27 
28 namespace geos
29 {
30 namespace stringutilities
31 {
32 
38 string toLower( string const & input );
39 
49 template< typename IT, typename S = char >
50 string join( IT first, IT last, S const & delim = S() )
51 {
52  if( first == last )
53  {
54  return {};
55  }
56  std::ostringstream oss;
57  oss << *first;
58  while( ++first != last )
59  {
60  oss << delim << *first;
61  }
62  return oss.str();
63 }
64 
73 template< typename CONTAINER, typename S = char >
74 string join( CONTAINER const & container, S const & delim = S() )
75 {
76  return join( std::begin( container ), std::end( container ), delim );
77 }
78 
90 template< typename IT, typename S, typename LAMBDA >
91 string joinLamda( IT first, IT last, S const & delim, LAMBDA formattingFunc )
92 {
93  if( first == last )
94  {
95  return {};
96  }
97  std::ostringstream oss;
98  oss << formattingFunc( first );
99  while( ++first != last )
100  {
101  oss << delim << formattingFunc( first );
102  }
103  return oss.str();
104 }
105 
116 template< typename CONTAINER, typename S, typename LAMBDA >
117 string joinLamda( CONTAINER const & container, S const & delim, LAMBDA formattingFunc )
118 {
119  return joinLamda( std::begin( container ), std::end( container ), delim, formattingFunc );
120 }
121 
132 template< typename S = char, typename T, typename ... Ts >
133 string concat( S const & delim, T const & v, Ts const & ... vs )
134 {
135  std::ostringstream oss;
136  oss << v;
137  // Use array initializer and comma trick to get "fold expression" pre C++-17
138  using expander = int[];
139  (void) expander{ 0, ( void ( oss << delim << vs ), 0) ... };
140  return oss.str();
141 }
142 
154 template< template< class ... > class CONTAINER = std::vector >
155 CONTAINER< string > tokenize( string const & str,
156  string const & delimiters,
157  bool const treatConsecutiveDelimAsOne = true,
158  bool const preTrimStr = false )
159 {
160  CONTAINER< string > tokens;
161  string::size_type tokenBegin, tokenEnd, strEnd;
162 
163  if( preTrimStr )
164  {
165  tokenBegin = str.find_first_not_of( delimiters );
166  strEnd = str.find_last_not_of( delimiters ) + 1;
167  }
168  else
169  {
170  tokenBegin = 0;
171  strEnd = str.size();
172  }
173 
174  while( ( ( tokenEnd = str.find_first_of( delimiters, tokenBegin ) ) < strEnd ) && tokenBegin < strEnd )
175  {
176  tokens.emplace_back( str.substr( tokenBegin, tokenEnd - tokenBegin ) );
177  tokenBegin = !treatConsecutiveDelimAsOne ? tokenEnd + 1 : str.find_first_not_of( delimiters, tokenEnd );
178  }
179 
180  if( tokenBegin < strEnd )
181  {
182  tokens.emplace_back( str.substr( tokenBegin, strEnd-tokenBegin ));
183  }
184  else if( !preTrimStr && str.find_first_of( delimiters, strEnd - 1 ) != string::npos )
185  {
186  tokens.emplace_back( "" );
187  }
188 
189  return tokens;
190 }
191 
199 template< template< class ... > class CONTAINER = std::vector >
200 CONTAINER< string > tokenizeBySpaces( string const & str )
201 {
202  return tokenize< CONTAINER >( str, " \f\n\r\t\v", true, true );
203 }
204 
212  string_view charsToRemove );
213 
220 
228  string_view strToRemove );
229 
236 template< typename T >
237 string addCommaSeparators( T const & num );
238 
245 template< typename T >
246 array1d< T > fromStringToArray( string const & str )
247 {
248  array1d< T > v;
249  T sub;
250 
251  std::istringstream iss( str );
252  while( iss >> sub )
253  {
254  v.emplace_back( sub );
255  }
256  return v;
257 }
258 
267 template< typename T >
268 string toMetricPrefixString( T const & value );
269 
275 constexpr size_t cstrlen( char const * const str )
276 {
277  if( str )
278  {
279  char const * ptr = str;
280  for(; *ptr != '\0'; ++ptr )
281  {}
282  return ptr - str;
283  }
284  else
285  {
286  return 0;
287  }
288 }
289 
295 constexpr bool startsWith( std::string_view str, std::string_view prefix )
296 {
297  return str.size() >= prefix.size() &&
298  str.compare( 0, prefix.size(), prefix ) == 0;
299 }
300 
306 constexpr bool endsWith( std::string_view str, std::string_view suffix )
307 {
308  return str.size() >= suffix.size() &&
309  str.compare( str.size()-suffix.size(), suffix.size(), suffix ) == 0;
310 }
311 
323 template< typename T >
324 std::ostream & operator<<( std::ostream & os, std::optional< T > const & optValue )
325 {
326  if( optValue )
327  {
328  os << optValue.value();
329  }
330  return os;
331 }
332 
333 } // namespace stringutilities
334 } // namespace geos
335 
336 #endif /* GEOS_COMMON_FORMAT_STRINGUTILITIES_HPP_ */
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.
Definition: DataTypes.hpp:356
Array< T, 1 > array1d
Alias for 1D array.
Definition: DataTypes.hpp:176
std::string_view string_view
String type.
Definition: DataTypes.hpp:94