1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
#pragma once
#include <algorithm> // std::transform
#include <string>
namespace strutils {
// trim from start (in place)
inline void ltrim(std::string &s)
{
s.erase(s.begin(),
std::find_if(s.begin(), s.end(), [](int ch) { return !std::isspace(ch); }));
}
// trim from end (in place)
inline void rtrim(std::string &s)
{
s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) { return !std::isspace(ch); })
.base(),
s.end());
}
// trim from both ends (in place)
inline void trim(std::string &s)
{
ltrim(s);
rtrim(s);
}
// trim from start (copying)
inline std::string ltrim_copy(std::string s)
{
ltrim(s);
return s;
}
// trim from end (copying)
inline std::string rtrim_copy(std::string s)
{
rtrim(s);
return s;
}
// trim from both ends (copying)
inline std::string trim_copy(std::string s)
{
trim(s);
return s;
}
// trim from end (in place)
inline void rtrim_comments(std::string &s, const std::string &comment_tokens)
{
size_t pos = s.find_first_of(comment_tokens);
if (pos != s.npos)
s.erase(pos);
}
inline void toupper(std::string &s)
{
std::transform(s.begin(), s.end(), s.begin(), ::toupper);
}
inline bool strcontains(const std::string &s, const std::string &sub)
{
return s.find(sub) != std::string::npos;
}
inline bool streq(const std::string &s1, const std::string &s2)
{
return s1 == s2;
}
inline bool iequals(const std::string& a, const std::string& b)
{
return std::equal(a.begin(), a.end(),
b.begin(), b.end(),
[](char a, char b) {
return tolower(a) == tolower(b);
});
}
};
|