-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStringOp.hh
89 lines (82 loc) · 2.62 KB
/
StringOp.hh
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
80
81
82
83
84
85
86
87
88
89
#ifndef STRINGOP_HH
#define STRINGOP_HH
#include <string>
#include <string_view>
#include <utility>
// Minimal re-implementation of the corresponding functions in openMSX.
namespace StringOp
{
inline void trimRight(std::string& str, const char* chars) {
if (auto pos = str.find_last_not_of(chars); pos != std::string::npos) {
str.erase(pos + 1);
} else {
str.clear();
}
}
inline void trimRight(std::string& str, char chars) {
if (auto pos = str.find_last_not_of(chars); pos != std::string::npos) {
str.erase(pos + 1);
} else {
str.clear();
}
}
inline void trimRight(std::string_view& str, std::string_view chars) {
while (!str.empty() && (chars.find(str.back()) != std::string_view::npos)) {
str.remove_suffix(1);
}
}
inline void trimRight(std::string_view& str, char chars) {
while (!str.empty() && (str.back() == chars)) {
str.remove_suffix(1);
}
}
inline void trimLeft(std::string& str, const char* chars) {
str.erase(0, str.find_first_not_of(chars));
}
inline void trimLeft(std::string& str, char chars) {
str.erase(0, str.find_first_not_of(chars));
}
inline void trimLeft(std::string_view& str, std::string_view chars) {
while (!str.empty() && (chars.find(str.front()) != std::string_view::npos)) {
str.remove_prefix(1);
}
}
inline void trimLeft(std::string_view& str, char chars) {
while (!str.empty() && (str.front() == chars)) {
str.remove_prefix(1);
}
}
[[nodiscard]] inline std::pair<std::string_view, std::string_view> splitOnFirst(std::string_view str, std::string_view chars)
{
if (auto pos = str.find_first_of(chars); pos == std::string_view::npos) {
return {str, std::string_view{}};
} else {
return {str.substr(0, pos), str.substr(pos + 1)};
}
}
[[nodiscard]] inline std::pair<std::string_view, std::string_view> splitOnFirst(std::string_view str, char chars)
{
if (auto pos = str.find_first_of(chars); pos == std::string_view::npos) {
return {str, std::string_view{}};
} else {
return {str.substr(0, pos), str.substr(pos + 1)};
}
}
[[nodiscard]] inline std::pair<std::string_view, std::string_view> splitOnLast(std::string_view str, std::string_view chars)
{
if (auto pos = str.find_last_of(chars); pos == std::string_view::npos) {
return {std::string_view{}, str};
} else {
return {str.substr(0, pos), str.substr(pos + 1)};
}
}
[[nodiscard]] inline std::pair<std::string_view, std::string_view> splitOnLast(std::string_view str, char chars)
{
if (auto pos = str.find_last_of(chars); pos == std::string_view::npos) {
return {std::string_view{}, str};
} else {
return {str.substr(0, pos), str.substr(pos + 1)};
}
}
}
#endif