BELFEM 0.9.0
Berkeley Lab Finite Element Framework
Loading...
Searching...
No Matches
stringtools.hpp
Go to the documentation of this file.
1/*
2 * BELFEM -- The Berkeley Lab Finite Element Framework
3 * Copyright (c) 2026, The Regents of the University of California,
4 * through Lawrence Berkeley National Laboratory (subject to receipt of any required
5 * approvals from the U.S. Dept. of Energy). All rights reserved.
6 *
7 * Developers: Christian Messe, Gregory Giard
8 *
9 * See the top-level LICENSE file for the complete license and disclaimer.
10 */
11
12#ifndef BELFEM_STRINGTOOLS_HPP
13#define BELFEM_STRINGTOOLS_HPP
14
15#include <memory>
16#include <iostream>
17#include <string>
18#include <cstdio>
19#include <regex>
20#include <algorithm> // for std::all_of
21#include <cctype> // for isdigit
22
23#include "typedefs.hpp"
24#include "fn_sprint.hpp"
25
26#include "cl_Cell.hpp"
27
28namespace belfem
29{
30//------------------------------------------------------------------------------
31
32// temporarily disable warnings
33
34
35#if defined(__clang__)
36#pragma clang diagnostic push
37#pragma clang diagnostic ignored "-Wformat-security"
38#pragma clang diagnostic ignored "-Wformat"
39#pragma clang diagnostic ignored "-Wunused-variable"
40#pragma clang diagnostic ignored "-Wunused-parameter"
41
42#elif defined(__GNUC__) || defined(__GNUG__)
43#pragma GCC diagnostic push
44#pragma GCC diagnostic ignored "-Wformat"
45#pragma GCC diagnostic ignored "-Wunused-variable"
46#pragma GCC diagnostic ignored "-Wunused-parameter"
47
48#elif defined(BELFEM_INTEL)
49 // Intel compiler diagnostics
50#pragma warning(push)
51 // disable format warnings
52#pragma warning(disable: 1011)
53 // disable unused variable warnings
54#pragma warning(disable: 177)
55 // keep your original one
56#pragma warning(disable: 1595)
57
58#endif
59
60//------------------------------------------------------------------------------
61
62 template < typename T >
63 inline std::string
65 {
66 return "unknown";
67 }
68
69// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
70
71 template <>
72 inline std::string
74 {
75 return "bool";
76 }
77
78// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
79
80 template <>
81 inline std::string
83 {
84 return "int";
85 }
86
87// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
88
89 template <>
90 inline std::string
92 {
93 return "unsigned int";
94 }
95
96// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
97
98 template <>
99 inline std::string
101 {
102 return "long unsigned int";
103 }
104
105// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
106
107 template <>
108 inline std::string
110 {
111 return "double";
112 }
113
114// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
115
116 template <>
117 inline std::string
119 {
120 return "string";
121 }
122
123// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
124
125 template <>
126 inline std::string
128 {
129 return "complex<double>";
130 }
131
132//------------------------------------------------------------------------------
133
137 std::string
138 basename( const std::string & aFilePath );
139
140//------------------------------------------------------------------------------
141
145 std::string
146 dirname( const std::string & aFilePath );
147
148//------------------------------------------------------------------------------
149
153 std::string
154 filetype( const std::string & aFilePath );
155
156//------------------------------------------------------------------------------
157
161 std::string
162 filename( const std::string & aFilePath );
163
164//------------------------------------------------------------------------------
165
169 std::string
170 clean_string( const std::string & aString );
171
172//------------------------------------------------------------------------------
173
177 string
178 first_word( const std::string & aString, const char aDelimiter = ' ');
179
180//------------------------------------------------------------------------------
184 Cell< string >
185 string_to_words( const std::string & aString, const char aDelimiter = ' ' );
186
187//------------------------------------------------------------------------------
188
189 std::string
191 const std::string & aString,
192 const std::string & aSearch,
193 const std::string & aReplace );
194
195//------------------------------------------------------------------------------
196
200 std::string
201 string_to_lower( const std::string & aString );
202
203//------------------------------------------------------------------------------
204
208 std::string
209 string_to_upper( const std::string & aString );
210
211//------------------------------------------------------------------------------
212
216 bool
217 string_to_bool( const std::string & aString );
218
219//------------------------------------------------------------------------------
220
224 real
225 to_real( const std::string & aString );
226
227//------------------------------------------------------------------------------
228
229 value
230 unit_to_si( const string & aString );
231
232//------------------------------------------------------------------------------
233
234 string
235 format_with_leading_zeros( const uint aNumber );
236
237//------------------------------------------------------------------------------
238
239 bool
240 is_integer( const string & aString );
241
242//------------------------------------------------------------------------------
243
244 size_t
245 utf8_character_count(const string & aString ) ;
246
247//------------------------------------------------------------------------------
248
249 template < typename T >
250 void
251 string_to_cell( const string & aString, Cell< T > & aValues )
252 {
253
259 clean_string( aString ),
260 "}","" ), "{",""),
261 " ", "" ), ";", " ; " ),
262 ","," " ) );
263
264
265 // count memory
266 for( string tWord : tWords )
267 {
268 if( tWord.find(":") < tWord.size() )
269 {
270 Cell< string > tNumbers = string_to_words( search_and_replace( tWord, ":", " " ) );
271 T tA = ( T ) std::stoll( tNumbers(0) );
272 T tB = ( T ) std::stoll( tNumbers(1) );
273
274 if( tB > tA )
275 {
276 T tN = tB - tA + 1 ;
277
278 T tC = tA ;
279 for( T k=0; k<tN; ++k )
280 {
281 aValues.push( tC );
282 tC += 1 ;
283 }
284 }
285 else
286 {
287 T tN = tA - tB + 1 ;
288
289 T tC = tA ;
290 for( T k=0; k<tN; ++k )
291 {
292 aValues.push( tC );
293 tC -= 1 ;
294 }
295 }
296 }
297 else if( tWord == ";" )
298 {
299 break;
300 }
301 else
302 {
303
304 aValues.push( ( T ) std::stoll( tWord ) );
305
306 }
307 }
308 }
309
310
311 template < typename T >
312 void
313 to_pair( const std::string & aString, Cell< std::pair< std::string, T >> & aResult )
314 {
315 aResult.clear() ;
316
317 // Matches one or more letters ([A-Za-z]+) followed immediately by one or more digits (\d+)
318 // The outer parentheses create the full match; inner ones capture the symbol and the number separately.
319 std::regex re(R"(([A-Za-z]+)(\d+))");
320
321 // std::sregex_iterator walks through ALL non-overlapping matches in the string
322 for (std::sregex_iterator it(aString.begin(), aString.end(), re);
323 it != std::sregex_iterator(); ++it)
324 {
325 const std::smatch& match = *it;
326
327 std::string label = match[1].str(); // group 1 = letters (e.g. "Pb", "Sn", "RRR")
328 T value = std::stoi(match[2].str()); // group 2 = digits converted to int
329
330 aResult.push( { std::move( label ), value } );
331 }
332 }
333
334#ifdef BELFEM_CLANG
335#pragma clang diagnostic pop
336#elif BELFEM_GCC
337#pragma GCC diagnostic pop
338#elif BELFEM_INTEL
339#pragma warning pop
340#endif
341
342//------------------------------------------------------------------------------
343}
344#endif //BELFEM_STRINGTOOLS_HPP
Cell is a wrapper around the standard vector.
Definition cl_Cell.hpp:42
void push(const T &aValue)
push an entry to the end of the cell (copy version)
Definition cl_Cell.hpp:262
USER GUIDES:
Definition cl_Capacitor.cpp:16
std::string datatype_string< std::string >()
Definition stringtools.hpp:118
std::string basename(const std::string &aFilePath)
returns the basename of a file
Definition stringtools.cpp:21
std::string datatype_string()
Definition stringtools.hpp:64
std::string dirname(const std::string &aFilePath)
returns the directory name of a file
Definition stringtools.cpp:33
std::string clean_string(const std::string &aString)
tidy up the string
Definition stringtools.cpp:86
size_t utf8_character_count(const string &aString)
Definition stringtools.cpp:1191
std::string filename(const std::string &aFilePath)
returns the name of a file without the path
Definition stringtools.cpp:70
value unit_to_si(const string &aString)
Definition stringtools.cpp:335
std::pair< real, unit > value
Definition typedefs.hpp:74
unsigned int uint
Definition typedefs.hpp:30
std::string search_and_replace(const std::string &aString, const std::string &aSearch, const std::string &aReplace)
Definition stringtools.cpp:236
real to_real(const std::string &aString)
convert string to real, return NAN if it is not real
Definition stringtools.cpp:316
std::string filetype(const std::string &aFilePath)
returns the filetype of a file
Definition stringtools.cpp:55
bool is_integer(const string &aString)
Definition stringtools.cpp:1173
string first_word(const std::string &aString, char aDelimiter)
return the first word of the string
Definition stringtools.cpp:190
std::string string_to_lower(const std::string &aString)
convert the string to lower case
Definition stringtools.cpp:267
bool string_to_bool(const std::string &aString)
return true if string is either 1, on, true or yes
Definition stringtools.cpp:301
void to_pair(const std::string &aString, Cell< std::pair< std::string, T > > &aResult)
Definition stringtools.hpp:313
std::string string_to_upper(const std::string &aString)
convert the string to upper case
Definition stringtools.cpp:284
void string_to_cell(const string &aString, Cell< T > &aValues)
Definition stringtools.hpp:251
Cell< string > string_to_words(const std::string &aString, char aDelimiter)
create a cell of words from a string
Definition stringtools.cpp:201
double real
Definition typedefs.hpp:36
string format_with_leading_zeros(const uint aNumber)
Definition stringtools.cpp:1127