0.2.0
Loading...
Searching...
No Matches
StringUtil.hpp
Go to the documentation of this file.
1// This file is part of INSTINCT, the INS Toolkit for Integrated
2// Navigation Concepts and Training by the Institute of Navigation of
3// the University of Stuttgart, Germany.
4//
5// This Source Code Form is subject to the terms of the Mozilla Public
6// License, v. 2.0. If a copy of the MPL was not distributed with this
7// file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
13
14#pragma once
15
16#include <algorithm>
17#include <cctype>
18#include <locale>
19#include <vector>
20#include <string>
21#include <string_view>
22
23namespace NAV::str
24{
27static inline void ltrim(std::string& s)
28{
29 if (!s.empty() && s[0] == '\n')
30 {
31 s.erase(0, 1);
32 }
33 s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) {
34 return !std::isspace(ch);
35 }));
36}
37
40static inline void rtrim(std::string& s)
41{
42 if (!s.empty() && s[s.length() - 1] == '\n')
43 {
44 s.erase(s.length() - 1);
45 }
46 s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) {
47 return !std::isspace(ch);
48 }).base(),
49 s.end());
50}
51
54static inline void trim(std::string& s)
55{
56 ltrim(s);
57 rtrim(s);
58}
59
62static inline void ltrim(std::string_view& sv)
63{
64 sv.remove_prefix(std::min(sv.find_first_not_of(' '), sv.size()));
65}
66
69static inline void rtrim(std::string_view& sv)
70{
71 sv.remove_suffix(std::min(sv.size() - sv.find_last_not_of(' ') - 1, sv.size()));
72}
73
76static inline void trim(std::string_view& sv)
77{
78 ltrim(sv);
79 rtrim(sv);
80}
81
85static inline std::string ltrim_copy(std::string s)
86{
87 ltrim(s);
88 return s;
89}
90
94static inline std::string rtrim_copy(std::string s)
95{
96 rtrim(s);
97 return s;
98}
99
103static inline std::string trim_copy(std::string s)
104{
105 trim(s);
106 return s;
107}
108
112static inline std::string_view ltrim_copy(std::string_view sv)
113{
114 ltrim(sv);
115 return sv;
116}
117
121static inline std::string_view rtrim_copy(std::string_view sv)
122{
123 rtrim(sv);
124 return sv;
125}
126
130static inline std::string_view trim_copy(std::string_view sv)
131{
132 trim(sv);
133 return sv;
134}
135
142
149static inline bool replace(std::string& str, const std::string& from, const std::string& to, CaseSensitivity cs = RespectCase)
150{
151 if (from.empty())
152 {
153 return false;
154 }
155 auto it = std::search(str.begin(), str.end(),
156 from.begin(), from.end(),
157 [cs](char ch1, char ch2) { return cs == RespectCase
158 ? ch1 == ch2
159 : std::toupper(ch1) == std::toupper(ch2); });
160
161 if (it == str.end())
162 {
163 return false;
164 }
165 auto start_pos = static_cast<size_t>(it - str.begin());
166 str.replace(start_pos, from.length(), to);
167 return true;
168}
169
175static inline void replaceAll(std::string& str, const std::string& from, const std::string& to, CaseSensitivity cs = RespectCase)
176{
177 while (replace(str, from, to, cs)) {}
178}
179
186static inline std::string replaceAll_copy(std::string str, const std::string& from, const std::string& to, CaseSensitivity cs = RespectCase)
187{
188 replaceAll(str, from, to, cs);
189 return str;
190}
191
196static inline std::vector<std::string> split(const std::string& str, const std::string& delimiter)
197{
198 size_t pos_start = 0;
199 size_t pos_end = 0;
200 size_t delim_len = delimiter.length();
201 std::vector<std::string> res;
202
203 while ((pos_end = str.find(delimiter, pos_start)) != std::string::npos)
204 {
205 res.push_back(str.substr(pos_start, pos_end - pos_start));
206 pos_start = pos_end + delim_len;
207 }
208 res.push_back(str.substr(pos_start));
209 return res;
210}
211
216static inline std::vector<std::string> split(const std::string& str, char delimiter)
217{
218 return split(str, std::string(1, delimiter));
219}
220
225static inline std::vector<std::string> split_wo_empty(const std::string& str, const std::string& delimiter)
226{
227 size_t pos_start = 0;
228 size_t pos_end = 0;
229 size_t delim_len = delimiter.length();
230 std::vector<std::string> res;
231
232 while ((pos_end = str.find(delimiter, pos_start)) != std::string::npos)
233 {
234 if (pos_start != pos_end)
235 {
236 res.push_back(str.substr(pos_start, pos_end - pos_start));
237 }
238 pos_start = pos_end + delim_len;
239 while (pos_start < str.size() && str.find(delimiter, pos_start) == pos_start)
240 {
241 pos_start += delim_len;
242 }
243 }
244 if (pos_start != str.size())
245 {
246 res.push_back(str.substr(pos_start));
247 }
248 return res;
249}
250
255static inline std::vector<std::string> split_wo_empty(const std::string& str, char delimiter)
256{
257 return split_wo_empty(str, std::string(1, delimiter));
258}
259
261template<typename T>
262concept StdString = std::convertible_to<T, std::string> || std::convertible_to<T, std::wstring>;
263
271template<StdString String>
272int stoi(const String& str, int default_value, std::size_t* pos = nullptr, int base = 10) noexcept
273{
274 try
275 {
276 return std::stoi(str, pos, base);
277 }
278 catch (...) // NOLINT(bugprone-empty-catch)
279 {}
280
281 return default_value;
282}
283
291template<StdString String>
292int64_t stol(const String& str, int64_t default_value, std::size_t* pos = nullptr, int base = 10) noexcept
293{
294 try
295 {
296 return std::stol(str, pos, base);
297 }
298 catch (...) // NOLINT(bugprone-empty-catch)
299 {}
300
301 return default_value;
302}
303
311template<StdString String>
312int64_t stoll(const String& str, int64_t default_value, std::size_t* pos = nullptr, int base = 10) noexcept
313{
314 try
315 {
316 return std::stoll(str, pos, base);
317 }
318 catch (...) // NOLINT(bugprone-empty-catch)
319 {}
320
321 return default_value;
322}
323
330template<StdString String>
331float stof(const String& str, float default_value, std::size_t* pos = nullptr) noexcept
332{
333 try
334 {
335 return std::stof(str, pos);
336 }
337 catch (...) // NOLINT(bugprone-empty-catch)
338 {}
339
340 return default_value;
341}
342
349template<StdString String>
350double stod(const String& str, double default_value, std::size_t* pos = nullptr) noexcept
351{
352 try
353 {
354 return std::stod(str, pos);
355 }
356 catch (...) // NOLINT(bugprone-empty-catch)
357 {}
358
359 return default_value;
360}
361
368template<StdString String>
369long double stold(const String& str, long double default_value, std::size_t* pos = nullptr) noexcept
370{
371 try
372 {
373 return std::stold(str, pos);
374 }
375 catch (...) // NOLINT(bugprone-empty-catch)
376 {}
377
378 return default_value;
379}
380
381} // namespace NAV::str
int stoi(const String &str, int default_value, std::size_t *pos=nullptr, int base=10) noexcept
Interprets a value in the string str.
Definition StringUtil.hpp:272
double stod(const String &str, double default_value, std::size_t *pos=nullptr) noexcept
Interprets a value in the string str.
Definition StringUtil.hpp:350
static void replaceAll(std::string &str, const std::string &from, const std::string &to, CaseSensitivity cs=RespectCase)
Replaces all occurrence of a search pattern with another sequence.
Definition StringUtil.hpp:175
long double stold(const String &str, long double default_value, std::size_t *pos=nullptr) noexcept
Interprets a value in the string str.
Definition StringUtil.hpp:369
CaseSensitivity
Enum for case sensitive tasks.
Definition StringUtil.hpp:138
@ RespectCase
Respect the case.
Definition StringUtil.hpp:139
@ IgnoreCase
Ignore case.
Definition StringUtil.hpp:140
static std::string trim_copy(std::string s)
Trim from both ends (copying)
Definition StringUtil.hpp:103
static std::string ltrim_copy(std::string s)
Trim from start (copying)
Definition StringUtil.hpp:85
static void rtrim(std::string &s)
Trim from end (in place)
Definition StringUtil.hpp:40
static void ltrim(std::string &s)
Trim from start (in place)
Definition StringUtil.hpp:27
static void trim(std::string &s)
Trim from both ends (in place)
Definition StringUtil.hpp:54
int64_t stoll(const String &str, int64_t default_value, std::size_t *pos=nullptr, int base=10) noexcept
Interprets a value in the string str.
Definition StringUtil.hpp:312
float stof(const String &str, float default_value, std::size_t *pos=nullptr) noexcept
Interprets a value in the string str.
Definition StringUtil.hpp:331
static std::string rtrim_copy(std::string s)
Trim from end (copying)
Definition StringUtil.hpp:94
static std::vector< std::string > split_wo_empty(const std::string &str, const std::string &delimiter)
Splits a string into parts at a delimiter and removes empty entries.
Definition StringUtil.hpp:225
static std::string replaceAll_copy(std::string str, const std::string &from, const std::string &to, CaseSensitivity cs=RespectCase)
Replaces all occurrence of a search pattern with another sequence.
Definition StringUtil.hpp:186
static std::vector< std::string > split(const std::string &str, const std::string &delimiter)
Splits a string into parts at a delimiter.
Definition StringUtil.hpp:196
int64_t stol(const String &str, int64_t default_value, std::size_t *pos=nullptr, int base=10) noexcept
Interprets a value in the string str.
Definition StringUtil.hpp:292
static bool replace(std::string &str, const std::string &from, const std::string &to, CaseSensitivity cs=RespectCase)
Replaces the first occurrence of a search pattern with another sequence.
Definition StringUtil.hpp:149
Concept limiting the type to std::string and std::wstring, but also allowing convertible types like c...
Definition StringUtil.hpp:262