libxcks  0.1.0.1
All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
strutil.hpp
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 #ifndef INC_STRUTIL_HPP_11067707_AB86_4996_AB0A_086A9EFB75B7
26 #define INC_STRUTIL_HPP_11067707_AB86_4996_AB0A_086A9EFB75B7
27 
28 //---------------------------------------------------------------------------
29 #include <string>
30 #include <ctime>
31 //---------------------------------------------------------------------------
32 
33 
34 namespace libxcks
35 {
36 // Trim from start (in place).
37 void ltrim(std::string &s);
38 
39 // Trim from end (in place).
40 void rtrim(std::string &s);
41 
42 // Trim from both ends (in place).
43 void trim(std::string &s);
44 
45 // Trim from start (copying).
46 std::string ltrim_copy(std::string s);
47 
48 // Trim from end (copying).
49 std::string rtrim_copy(std::string s);
50 
51 // Trim from both ends (copying).
52 std::string trim_copy(std::string s);
53 //---------------------------------------------------------------------------
54 
55 // Replace all occurrences of a substring by another in a string.
56 std::string replaceAllCopy(std::string str, const std::string& from, const std::string& to);
57 
58 // Replace in-place all occurrences of a substring by another in a string.
59 void replaceAll(std::string& str, const std::string& from, const std::string& to);
60 //---------------------------------------------------------------------------
61 
62 // Case-insensitive comparison of two strings.
63 bool stringICompare(const std::string& str1, const std::string& str2);
64 //---------------------------------------------------------------------------
65 
66 // Returns an UTF-8 encoded string in an object of type @c std::string (C++17).
67 std::string from_u8string(const std::string &s);
68 
69 // Returns an UTF-8 encoded string in an object of type @c std::string (C++17).
70 std::string from_u8string(std::string &&s);
71 
72 #if defined(__cpp_lib_char8_t)
73 // Returns an UTF-8 encoded string in an object of type @c std::string (C++20).
74 std::string from_u8string(const std::u8string &s);
75 #endif
76 //---------------------------------------------------------------------------
77 
78 // Parse ISO 8601 date/time
79 std::time_t parseISO8601(const std::string& iso8601);
80 //---------------------------------------------------------------------------
81 
82 // Escapes a string for writing it in a XML stream.
83 std::string escapeForXML(const std::string& str);
84 //---------------------------------------------------------------------------
85 
86 } // namespace libxcks
87 //---------------------------------------------------------------------------
88 
89 #endif // INC_STRUTIL_HPP_11067707_AB86_4996_AB0A_086A9EFB75B7
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