libxcks  0.1.0.1
strutil.cpp
Go to the documentation of this file.
1 /*
2  * libxcks
3  * Copyright (C) 2022 Julien Couot
4  *
5  * This program is free software: you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
13  * License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
25 //---------------------------------------------------------------------------
26 #include <sstream>
27 #include <algorithm>
28 #include <iomanip>
29 #include <cctype>
30 
31 #include "strutil.hpp"
32 #include "libxcks/defs.hpp"
33 //---------------------------------------------------------------------------
34 
35 
37 using namespace std;
38 //---------------------------------------------------------------------------
39 
40 
41 namespace libxcks
42 {
43 //###########################################################################
44 // Trim functions.
45 // From https://stackoverflow.com/questions/216823/how-to-trim-a-stdstring
46 //###########################################################################
47 
53 void ltrim(std::string &s)
54 {
55  s.erase(s.begin(), find_if(s.begin(), s.end(), [](unsigned char ch) {
56  return !isspace(ch);
57  }));
58 }
59 //---------------------------------------------------------------------------
60 
61 
67 void rtrim(std::string &s)
68 {
69  s.erase(find_if(s.rbegin(), s.rend(), [](unsigned char ch) {
70  return !isspace(ch);
71  }).base(), s.end());
72 }
73 //---------------------------------------------------------------------------
74 
75 
81 void trim(std::string &s)
82 {
83  ltrim(s);
84  rtrim(s);
85 }
86 //---------------------------------------------------------------------------
87 
88 
95 std::string ltrim_copy(std::string s)
96 {
97  ltrim(s);
98  return s;
99 }
100 //---------------------------------------------------------------------------
101 
102 
109 std::string rtrim_copy(std::string s)
110 {
111  rtrim(s);
112  return s;
113 }
114 //---------------------------------------------------------------------------
115 
116 
123 std::string trim_copy(std::string s)
124 {
125  trim(s);
126  return s;
127 }
128 //---------------------------------------------------------------------------
129 
130 
131 
132 //###########################################################################
133 // Replacement functions.
134 //###########################################################################
135 
136 // From https://stackoverflow.com/a/24315631
145 std::string replaceAllCopy(std::string str, const std::string& from, const std::string& to)
146 {
147  size_t start_pos = 0;
148  while ((start_pos = str.find(from, start_pos)) != string::npos)
149  {
150  str.replace(start_pos, from.length(), to);
151  start_pos += to.length();
152  }
153  return str;
154 }
155 //---------------------------------------------------------------------------
156 
157 
166 void replaceAll(std::string& str, const std::string& from, const std::string& to)
167 {
168  size_t start_pos = 0;
169  while ((start_pos = str.find(from, start_pos)) != string::npos)
170  {
171  str.replace(start_pos, from.length(), to);
172  start_pos += to.length();
173  }
174 }
175 //---------------------------------------------------------------------------
176 
177 
178 
179 //###########################################################################
180 // Comparison functions.
181 //###########################################################################
182 
191 bool stringICompare(const std::string & str1, const std::string &str2)
192 {
193  return ((str1.size() == str2.size()) &&
194  equal(str1.cbegin(), str1.cend(), str2.cbegin(),
195  [](const unsigned char& c1, const unsigned char& c2) {
196  return (c1 == c2 || toupper(c1) == toupper(c2)); }));
197 }
198 //---------------------------------------------------------------------------
199 
200 
201 
202 //###########################################################################
203 // Explicit conversion functions for std::filesystem::path::u8string
204 // From http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1423r2.html
205 // std::filesystem::path p = ...;
206 // std::string s = from_u8string(p.u8string()); // C++17 or C++20
207 //###########################################################################
208 
217 std::string from_u8string(const std::string &s)
218 {
219  return s;
220 }
221 
222 
231 //---------------------------------------------------------------------------
232 std::string from_u8string(std::string &&s)
233 {
234  return move(s);
235 }
236 //---------------------------------------------------------------------------
237 
238 
239 #if defined(__cpp_lib_char8_t)
246 std::string from_u8string(const std::u8string &s)
247 {
248  return string(s.begin(), s.end());
249 }
250 //---------------------------------------------------------------------------
251 #endif
252 
253 
254 
255 //###########################################################################
256 // Date/time conversion.
257 //###########################################################################
258 
265 time_t parseISO8601(const std::string& iso8601)
266 {
267  time_t res = Invalid_DateTime;
268  {
269  istringstream ss(iso8601);
270  tm t = {};
271  ss >> std::get_time(&t, "%Y-%m-%dT%H:%M:%S");
272  if (!ss.fail())
273  res = mktime(&t);
274  }
275  if (res == Invalid_DateTime)
276  {
277  istringstream ss(iso8601);
278  tm t = {};
279  ss >> std::get_time(&t, "%Y%m%dT%H%M%S");
280  if (!ss.fail())
281  res = mktime(&t);
282  }
283  if (res == Invalid_DateTime)
284  {
285  istringstream ss(iso8601);
286  tm t = {};
287  ss >> std::get_time(&t, "%Y-%m-%dT%H:%M:%SZ");
288  if (!ss.fail())
289  res = mktime(&t);
290  }
291  if (res == Invalid_DateTime)
292  {
293  istringstream ss(iso8601);
294  tm t = {};
295  ss >> std::get_time(&t, "%Y%m%dT%H%M%SZ");
296  if (!ss.fail())
297  res = mktime(&t);
298  }
299 
300  return res;
301 }
302 //---------------------------------------------------------------------------
303 
304 
305 //###########################################################################
306 // XML utilities.
307 //###########################################################################
308 
318 std::string escapeForXML(const std::string& str)
319 {
320  string res = str;
321 
322  replaceAll(res, "&", "&amp;");
323  replaceAll(res, "\"", "&quot;");
324  replaceAll(res, "'", "&apos;");
325  replaceAll(res, "<", "&lt;");
326  replaceAll(res, ">", "&gt;");
327 
328  return res;
329 }
330 //---------------------------------------------------------------------------
331 
332 } // namespace libxcks
333 //---------------------------------------------------------------------------
Common definitions for libxcks.
constexpr std::time_t Invalid_DateTime
Invalid date/time.
Definition: defs.hpp:42
void replaceAll(std::string &str, const std::string &from, const std::string &to)
Replace in-place all occurrences of a substring by another in a string.
Definition: strutil.cpp:166
void ltrim(std::string &s)
Trim from start (in place).
Definition: strutil.cpp:53
void rtrim(std::string &s)
Trim from end (in place).
Definition: strutil.cpp:67
std::string trim_copy(std::string s)
Trim from both ends (copying).
Definition: strutil.cpp:123
std::string rtrim_copy(std::string s)
Trim from end (copying).
Definition: strutil.cpp:109
void trim(std::string &s)
Trim from both ends (in place).
Definition: strutil.cpp:81
bool stringICompare(const std::string &str1, const std::string &str2)
Case-insensitive comparison of two strings.
Definition: strutil.cpp:191
time_t parseISO8601(const std::string &iso8601)
Parse ISO 8601 date/time.
Definition: strutil.cpp:265
std::string from_u8string(const std::string &s)
Returns an UTF-8 encoded string in an object of type std::string (C++17).
Definition: strutil.cpp:217
std::string escapeForXML(const std::string &str)
Escapes a string for writing it in a XML stream.
Definition: strutil.cpp:318
std::string ltrim_copy(std::string s)
Trim from start (copying).
Definition: strutil.cpp:95
std::string replaceAllCopy(std::string str, const std::string &from, const std::string &to)
Replace all occurrences of a substring by another in a string.
Definition: strutil.cpp:145
String utilities.