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 TotalEnergies
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 joinLambda( 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 joinLambda( CONTAINER const & container, S const & delim, LAMBDA formattingFunc )
118 {
119  return joinLambda( 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 
227 
235  string_view strToRemove );
236 
243 template< typename T >
244 string addCommaSeparators( T const & num );
245 
253 std::vector< std::string > wrapTextToMaxLength( std::vector< std::string > const & lines, size_t maxLength );
254 
261 template< typename T >
262 array1d< T > fromStringToArray( string const & str )
263 {
264  array1d< T > v;
265  T sub;
266 
267  std::istringstream iss( str );
268  while( iss >> sub )
269  {
270  v.emplace_back( sub );
271  }
272  return v;
273 }
274 
283 template< typename T >
284 string toMetricPrefixString( T const & value );
285 
291 constexpr size_t cstrlen( char const * const str )
292 {
293  if( str )
294  {
295  char const * ptr = str;
296  for(; *ptr != '\0'; ++ptr )
297  {}
298  return ptr - str;
299  }
300  else
301  {
302  return 0;
303  }
304 }
305 
311 constexpr bool startsWith( std::string_view str, std::string_view prefix )
312 {
313  return str.size() >= prefix.size() &&
314  str.compare( 0, prefix.size(), prefix ) == 0;
315 }
316 
322 constexpr bool endsWith( std::string_view str, std::string_view suffix )
323 {
324  return str.size() >= suffix.size() &&
325  str.compare( str.size()-suffix.size(), suffix.size(), suffix ) == 0;
326 }
327 
339 template< typename T >
340 std::ostream & operator<<( std::ostream & os, std::optional< T > const & optValue )
341 {
342  if( optValue )
343  {
344  os << optValue.value();
345  }
346  return os;
347 }
348 
349 } // namespace stringutilities
350 } // namespace geos
351 
352 #endif /* GEOS_COMMON_FORMAT_STRINGUTILITIES_HPP_ */
string_view trimSpaces(string_view str)
Trim the string so it does not starts nor ends with any whitespaces.
string joinLambda(IT first, IT last, S const &delim, LAMBDA formattingFunc)
Join strings or other printable objects returned by a formatter functor.
constexpr size_t cstrlen(char const *const str)
std::vector< std::string > wrapTextToMaxLength(std::vector< std::string > const &lines, size_t maxLength)
Format all the lines by detecting spaces and by dividing each lines with maximum length specified....
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_view ltrimSpaces(string_view s)
Trim the left 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