Documentation for OpenFLUID 2.2.1
StringHelpers.hpp
Go to the documentation of this file.
1 /*
2 
3  This file is part of OpenFLUID software
4  Copyright(c) 2021-2026, INRAE
5 
6 
7  == GNU General Public License Usage ==
8 
9  OpenFLUID is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  OpenFLUID is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
21 
22 
23  == Other Usage ==
24 
25  Other Usage means a use of OpenFLUID that is inconsistent with the GPL
26  license, and requires a written agreement between You and INRA.
27  Licensees for Other Usage of OpenFLUID may use this file in accordance
28  with the terms contained in the written agreement between You and INRA.
29 
30 */
31 
32 
33 /**
34  @file StringHelpers.hpp
35 
36  @author Jean-Christophe FABRE <jean-christophe.fabre@inrae.fr>
37  */
38 
39 
40 #ifndef __OPENFLUID_TOOLS_STRINGHELPERS_HPP__
41 #define __OPENFLUID_TOOLS_STRINGHELPERS_HPP__
42 
43 
44 #include <string>
45 #include <list>
46 #include <sstream>
47 #include <vector>
48 #include <set>
49 
51 #include <openfluid/dllexport.hpp>
52 
53 
54 namespace openfluid { namespace tools {
55 
56 
57 /**
58  Converts a string to lower case
59  @snippet misc/strings.cpp str_lower
60  @param[in] Str the string to convert
61  @return the string converted to lower case
62 */
63 std::string OPENFLUID_API toLowerCase(const std::string& Str);
64 
65 
66 /**
67  Converts a string to upper case
68  @snippet misc/strings.cpp str_upper
69  @param[in] Str the string to convert
70  @return the string converted to upper case
71 */
72 std::string OPENFLUID_API toUpperCase(const std::string& Str);
73 
74 
75 /**
76  Trims all the whitespaces character located on the left side of the string
77  @snippet misc/strings.cpp str_triml
78  @param[in] Str the string to trim
79  @return the trimmed string
80 */
81 std::string OPENFLUID_API trimLeft(const std::string& Str);
82 
83 
84 /**
85  Trims all the whitespaces character located on the right side of the string
86  @snippet misc/strings.cpp str_trimr
87  @param[in] Str the string to trim
88  @return the trimmed string
89 */
90 std::string OPENFLUID_API trimRight(const std::string& Str);
91 
92 
93 /**
94  Trims all the whitespaces character located on both right and left sides of the string
95  @snippet misc/strings.cpp str_trim
96  @param[in] Str the string to trim
97  @return the trimmed string
98 */
99 std::string OPENFLUID_API trim(const std::string& Str);
100 
101 
102 /**
103  Converts a string into a numeric value.
104  Due to the poor performance of this generic method, it is preferable to use standard C++ typed methods
105  such as std::stoi, std:stod and others to convert a string into numerical values
106  @snippet misc/strings.cpp str_numbool
107  @param[in] Str the string to convert
108  @param[out] Num the converted numerical value
109  @return true if conversion is successful, false otherwise
110  @sa std::stoi() to convert integer values : https://en.cppreference.com/w/cpp/string/basic_string/stol
111  @sa std::stof() to convert floating point values : https://en.cppreference.com/w/cpp/string/basic_string/stof
112 */
113 template<typename T>
114 inline bool toNumeric(const std::string& Str, T& Num)
115 {
116  std::istringstream iss(Str);
117  char C;
118  return ((iss >> (Num)) && !iss.get(C));
119 }
120 
121 
122 /**
123  Converts a string into a numeric value.
124  Due to the poor performance of this generic method, it is preferable to use standard C++ typed methods
125  such as std::stoi, std:stod and others to convert a string into numerical values
126  @snippet misc/strings.cpp str_num
127  @param[in] Str the string to convert
128  @return the converted numerical value
129  @throws openfluid::base::FrameworkException Error during conversion
130  @sa std::stoi() to convert integer values : https://en.cppreference.com/w/cpp/string/basic_string/stol
131  @sa std::stof() to convert floating point values : https://en.cppreference.com/w/cpp/string/basic_string/stof
132 */
133 template<typename T>
134 inline T toNumeric(const std::string& Str)
135 {
136  T Num;
137  if (!toNumeric(Str,Num))
138  {
139  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Error converting '"+Str+"' to numeric value");
140  }
141  return Num;
142 }
143 
144 
145 /**
146  Converts a string into a boolean.
147  @param[in] Str the string to convert
148  @return the converted boolean value
149  @throws openfluid::base::FrameworkException Error during conversion
150 */
151 bool OPENFLUID_API toBoolean(const std::string& Str);
152 
153 
154 /**
155  Checks if a string starts with a given substring
156  @snippet misc/strings.cpp str_starts
157  @param[in] Str the string to check
158  @param[in] SubStr the substring
159  @return true if the string starts with the substring, false otherwise
160 */
161 bool OPENFLUID_API startsWith(const std::string& Str,const std::string& SubStr);
162 
163 
164 /**
165  Checks if a string ends with a given substring
166  @snippet misc/strings.cpp str_ends
167  @param[in] Str the string to check
168  @param[in] SubStr the substring
169  @return true if the string ends with the substring, false otherwise
170 */
171 bool OPENFLUID_API endsWith(const std::string& Str,const std::string& SubStr);
172 
173 
174 /**
175  Checks if a string contains a given substring
176  @snippet misc/strings.cpp str_contains
177  @param[in] Str the string to check
178  @param[in] SubStr the substring
179  @param[in] CaseSensitive true for case sensitive
180  @return true if the string contains the substring, false otherwise
181 */
182 bool OPENFLUID_API contains(const std::string& Str,const std::string& SubStr, bool CaseSensitive = true);
183 
184 
185 /**
186  Returns the number of substring occurence in the string
187  @snippet misc/strings.cpp str_contains
188  @param[in] Str the string to check
189  @param[in] SubChar the sub character
190  @return the number of sub character occurence in the string
191 */
192 int OPENFLUID_API count(const std::string& Str, const char& SubChar);
193 
194 
195 /**
196  Find all occurences of a substring in a folder, iterating on any subdirectory
197  @param[in] Folder the location to check
198  @param[in] String the string to look for
199  @return a map containing the file path associated with a set of string locations in this file
200 */
201 std::map<std::string, std::set<std::size_t>> OPENFLUID_API searchInFolder(std::string Folder, std::string String);
202 
203 
204 /**
205  Replaces every occurrence in a string of a searched substring by a replacement substring
206  @snippet misc/strings.cpp str_repl
207  @param[in] Str the original string
208  @param[in] SearchStr the searched substring
209  @param[in] ReplaceStr the replacement substring
210  @return a string with replaced parts
211 */
212 std::string OPENFLUID_API replace(const std::string& Str,const std::string& SearchStr, const std::string& ReplaceStr);
213 
214 std::string OPENFLUID_API replace_once(const std::string& Str,const std::string& SearchStr,
215  const std::string& ReplaceStr,
216  std::size_t Pos);
217 
218 /**
219  Formats a string using a format and a variable number of arguments.
220  As it is based on the C snprintf functions, it uses the same format specification,
221  and has the same limitations in usable types, performance and error handling
222  @snippet misc/strings.cpp str_format
223  @param[in] Fmt The format string using POSIX format style
224  @param[in] Args The variadic list of arguments to format
225  @sa snprintf function specification: https://en.cppreference.com/w/cpp/io/c/fprintf
226 */
227 template<class...A>
228 inline std::string format(const std::string& Fmt, A&&...Args)
229 {
230  const int Size = std::snprintf(nullptr,0,Fmt.c_str(), std::forward<A>(Args)...); // first call to compute buffer size
231  char* Buf = new char[Size+1]; // allow buffer, +1 for terminal '\0' char
232  std::snprintf(Buf,Size+1,Fmt.c_str(), std::forward<A>(Args)...); // second call for effective formatting
233 
234  std::string Str(Buf); // convert buffer to string
235  delete[] Buf; // free buffer
236  return Str;
237 }
238 
239 
240 /**
241  Splits a string into a vector of strings using a separator character
242  @snippet misc/strings.cpp str_split
243  @param[in] Str the string to split
244  @param[in] Sep the separator character used to split the string
245  @param[in] KeepEmpty if true, empty strings are returned (disabled by default)
246  @return a vector of strings
247 */
248 std::vector<std::string> OPENFLUID_API split(const std::string& Str, const char Sep, bool KeepEmpty = false);
249 
250 
251 /**
252  Splits a string into a vector of strings using any character of the the string of separators
253  @snippet misc/strings.cpp str_split
254  @param[in] Str the string to split
255  @param[in] SepChars the string of separators used to split the string
256  @param[in] KeepEmpty if true, empty strings are returned (disabled by default)
257  @return a vector of strings
258 */
259 std::vector<std::string> OPENFLUID_API split(const std::string& Str, const std::string& SepChars,
260  bool KeepEmpty = false);
261 
262 
263 /**
264  Joins a vector of strings into a string using a separator
265  @snippet misc/strings.cpp str_join
266  @param[in] Vect the vector of strings to join
267  @param[in] Sep the string used as separator
268  @return the joined string
269 */
270 std::string OPENFLUID_API join(const std::vector<std::string>& Vect, const std::string& Sep);
271 
272 
273 } } // namespaces
274 
275 
276 // =====================================================================
277 // =====================================================================
278 
279 
280 /**
281  Stream operator to append a plain text to a vector of strings
282  @snippet misc/strings.cpp vectstr_opstr
283 */
284 std::vector<std::string>& OPENFLUID_API operator<<(std::vector<std::string>& Vect, std::string&& Str);
285 
286 
287 /**
288  Stream operator to append a string to a vector of strings
289  @snippet misc/strings.cpp vectstr_opstr
290 */
291 std::vector<std::string>& OPENFLUID_API operator<<(std::vector<std::string>& Vect, const std::string& Str);
292 
293 
294 /**
295  Stream operator to append a vector of string to another vector of strings
296  @snippet misc/strings.cpp vectstr_opvect
297 */
298 std::vector<std::string>& OPENFLUID_API operator<<(std::vector<std::string>& Vect,
299  const std::vector<std::string>& OtherVect);
300 
301 
302 #endif /* __OPENFLUID_TOOLS_STRINGHELPERS_HPP__ */
std::vector< std::string > &OPENFLUID_API operator<<(std::vector< std::string > &Vect, std::string &&Str)
Definition: FrameworkException.hpp:51
#define OPENFLUID_API
Definition: dllexport.hpp:86
std::string format(const std::string &Fmt, A &&...Args)
Definition: StringHelpers.hpp:228
bool OPENFLUID_API startsWith(const std::string &Str, const std::string &SubStr)
std::string OPENFLUID_API trimLeft(const std::string &Str)
bool OPENFLUID_API endsWith(const std::string &Str, const std::string &SubStr)
std::string OPENFLUID_API replace(const std::string &Str, const std::string &SearchStr, const std::string &ReplaceStr)
std::string OPENFLUID_API toLowerCase(const std::string &Str)
std::string OPENFLUID_API replace_once(const std::string &Str, const std::string &SearchStr, const std::string &ReplaceStr, std::size_t Pos)
bool OPENFLUID_API toBoolean(const std::string &Str)
bool OPENFLUID_API contains(const std::string &Str, const std::string &SubStr, bool CaseSensitive=true)
std::string OPENFLUID_API trimRight(const std::string &Str)
std::map< std::string, std::set< std::size_t > > OPENFLUID_API searchInFolder(std::string Folder, std::string String)
std::vector< std::string > OPENFLUID_API split(const std::string &Str, const char Sep, bool KeepEmpty=false)
bool toNumeric(const std::string &Str, T &Num)
Definition: StringHelpers.hpp:114
std::string OPENFLUID_API join(const std::vector< std::string > &Vect, const std::string &Sep)
int OPENFLUID_API count(const std::string &Str, const char &SubChar)
std::string OPENFLUID_API trim(const std::string &Str)
std::string OPENFLUID_API toUpperCase(const std::string &Str)
Definition: ApplicationException.hpp:47