Documentation for OpenFLUID 2.2.0
StringHelpers.hpp
Go to the documentation of this file.
1 /*
2 
3  This file is part of OpenFLUID software
4  Copyright(c) 2007, INRA - Montpellier SupAgro
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 
50 #include <openfluid/dllexport.hpp>
51 
52 
53 namespace openfluid { namespace tools {
54 
55 
56 /**
57  Converts a string to lower case
58  @snippet misc/strings.cpp str_lower
59  @param[in] Str the string to convert
60  @return the string converted to lower case
61 */
62 std::string OPENFLUID_API toLowerCase(const std::string& Str);
63 
64 
65 /**
66  Converts a string to upper case
67  @snippet misc/strings.cpp str_upper
68  @param[in] Str the string to convert
69  @return the string converted to upper case
70 */
71 std::string OPENFLUID_API toUpperCase(const std::string& Str);
72 
73 
74 /**
75  Trims all the whitespaces character located on the left side of the string
76  @snippet misc/strings.cpp str_triml
77  @param[in] Str the string to trim
78  @return the trimmed string
79 */
80 std::string OPENFLUID_API trimLeft(const std::string& Str);
81 
82 
83 /**
84  Trims all the whitespaces character located on the right side of the string
85  @snippet misc/strings.cpp str_trimr
86  @param[in] Str the string to trim
87  @return the trimmed string
88 */
89 std::string OPENFLUID_API trimRight(const std::string& Str);
90 
91 
92 /**
93  Trims all the whitespaces character located on both right and left sides of the string
94  @snippet misc/strings.cpp str_trim
95  @param[in] Str the string to trim
96  @return the trimmed string
97 */
98 std::string OPENFLUID_API trim(const std::string& Str);
99 
100 
101 /**
102  Converts a string into a numeric value.
103  Due to the poor performance of this generic method, it is preferable to use standard C++ typed methods
104  such as std::stoi, std:stod and others to convert a string into numerical values
105  @snippet misc/strings.cpp str_numbool
106  @param[in] Str the string to convert
107  @param[out] Num the converted numerical value
108  @return true if conversion is successful, false otherwise
109  @sa std::stoi() to convert integer values : https://en.cppreference.com/w/cpp/string/basic_string/stol
110  @sa std::stof() to convert floating point values : https://en.cppreference.com/w/cpp/string/basic_string/stof
111 */
112 template<typename T>
113 inline bool toNumeric(const std::string& Str, T& Num)
114 {
115  std::istringstream iss(Str);
116  char C;
117  return ((iss >> (Num)) && !iss.get(C));
118 }
119 
120 
121 /**
122  Converts a string into a numeric value.
123  Due to the poor performance of this generic method, it is preferable to use standard C++ typed methods
124  such as std::stoi, std:stod and others to convert a string into numerical values
125  @snippet misc/strings.cpp str_num
126  @param[in] Str the string to convert
127  @return the converted numerical value
128  @throws openfluid::base::FrameworkException Error during conversion
129  @sa std::stoi() to convert integer values : https://en.cppreference.com/w/cpp/string/basic_string/stol
130  @sa std::stof() to convert floating point values : https://en.cppreference.com/w/cpp/string/basic_string/stof
131 */
132 template<typename T>
133 inline T toNumeric(const std::string& Str)
134 {
135  T Num;
136  if (!toNumeric(Str,Num))
137  {
138  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Error converting '"+Str+"' to numeric value");
139  }
140  return Num;
141 }
142 
143 
144 /**
145  Converts a string into a boolean.
146  @param[in] Str the string to convert
147  @return the converted boolean value
148  @throws openfluid::base::FrameworkException Error during conversion
149 */
150 bool OPENFLUID_API toBoolean(const std::string& Str);
151 
152 
153 /**
154  Checks if a string starts with a given substring
155  @snippet misc/strings.cpp str_starts
156  @param[in] Str the string to check
157  @param[in] SubStr the substring
158  @return true if the string starts with the substring, false otherwise
159 */
160 bool OPENFLUID_API startsWith(const std::string& Str,const std::string& SubStr);
161 
162 
163 /**
164  Checks if a string ends with a given substring
165  @snippet misc/strings.cpp str_ends
166  @param[in] Str the string to check
167  @param[in] SubStr the substring
168  @return true if the string ends with the substring, false otherwise
169 */
170 bool OPENFLUID_API endsWith(const std::string& Str,const std::string& SubStr);
171 
172 
173 /**
174  Checks if a string contains a given substring
175  @snippet misc/strings.cpp str_contains
176  @param[in] Str the string to check
177  @param[in] SubStr the substring
178  @param[in] CaseSensitive true for case sensitive
179  @return true if the string contains the substring, false otherwise
180 */
181 bool OPENFLUID_API contains(const std::string& Str,const std::string& SubStr, bool CaseSensitive = true);
182 
183 
184 /**
185  Replaces every occurrence in a string of a searched substring by a replacement substring
186  @snippet misc/strings.cpp str_repl
187  @param[in] Str the original string
188  @param[in] SearchStr the searched substring
189  @param[in] ReplaceStr the replacement substring
190  @return a string with replaced parts
191 */
192 std::string OPENFLUID_API replace(const std::string& Str,const std::string& SearchStr, const std::string& ReplaceStr);
193 
194 
195 /**
196  Formats a string using a format and a variable number of arguments.
197  As it is based on the C snprintf functions, it uses the same format specification,
198  and has the same limitations in usable types, performance and error handling
199  @snippet misc/strings.cpp str_format
200  @param[in] Fmt The format string using POSIX format style
201  @param[in] Args The variadic list of arguments to format
202  @sa snprintf function specification: https://en.cppreference.com/w/cpp/io/c/fprintf
203 */
204 template<class...A>
205 inline std::string format(const std::string& Fmt, A&&...Args)
206 {
207  const int Size = std::snprintf(nullptr,0,Fmt.c_str(), std::forward<A>(Args)...); // first call to compute buffer size
208  char* Buf = new char[Size+1]; // allow buffer, +1 for terminal '\0' char
209  std::snprintf(Buf,Size+1,Fmt.c_str(), std::forward<A>(Args)...); // second call for effective formatting
210 
211  std::string Str(Buf); // convert buffer to string
212  delete[] Buf; // free buffer
213  return Str;
214 }
215 
216 
217 /**
218  Splits a string into a vector of strings using a separator character
219  @snippet misc/strings.cpp str_split
220  @param[in] Str the string to split
221  @param[in] Sep the separator character used to split the string
222  @param[in] KeepEmpty if true, empty strings are returned (disabled by default)
223  @return a vector of strings
224 */
225 std::vector<std::string> OPENFLUID_API split(const std::string& Str, const char Sep, bool KeepEmpty = false);
226 
227 
228 /**
229  Splits a string into a vector of strings using any character of the the string of separators
230  @snippet misc/strings.cpp str_split
231  @param[in] Str the string to split
232  @param[in] SepChars the string of separators used to split the string
233  @param[in] KeepEmpty if true, empty strings are returned (disabled by default)
234  @return a vector of strings
235 */
236 std::vector<std::string> OPENFLUID_API split(const std::string& Str, const std::string& SepChars,
237  bool KeepEmpty = false);
238 
239 
240 /**
241  Joins a vector of strings into a string using a separator
242  @snippet misc/strings.cpp str_join
243  @param[in] Vect the vector of strings to join
244  @param[in] Sep the string used as separator
245  @return the joined string
246 */
247 std::string OPENFLUID_API join(const std::vector<std::string>& Vect, const std::string& Sep);
248 
249 
250 } } // namespaces
251 
252 
253 // =====================================================================
254 // =====================================================================
255 
256 
257 /**
258  Stream operator to append a plain text to a vector of strings
259  @snippet misc/strings.cpp vectstr_opstr
260 */
261 std::vector<std::string>& OPENFLUID_API operator<<(std::vector<std::string>& Vect, std::string&& Str);
262 
263 
264 /**
265  Stream operator to append a string to a vector of strings
266  @snippet misc/strings.cpp vectstr_opstr
267 */
268 std::vector<std::string>& OPENFLUID_API operator<<(std::vector<std::string>& Vect, const std::string& Str);
269 
270 
271 /**
272  Stream operator to append a vector of string to another vector of strings
273  @snippet misc/strings.cpp vectstr_opvect
274 */
275 std::vector<std::string>& OPENFLUID_API operator<<(std::vector<std::string>& Vect,
276  const std::vector<std::string>& OtherVect);
277 
278 
279 #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:205
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)
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::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:113
std::string OPENFLUID_API join(const std::vector< std::string > &Vect, const std::string &Sep)
std::string OPENFLUID_API trim(const std::string &Str)
std::string OPENFLUID_API toUpperCase(const std::string &Str)
Definition: ApplicationException.hpp:47