-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathx_str.h
88 lines (75 loc) · 2.45 KB
/
x_str.h
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
#pragma once
// trim from start (in place)
static inline void ltrim(std::wstring &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) {
return !std::isspace(ch);
}));
}
// trim from end (in place)
static inline void rtrim(std::wstring &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)
static inline void trim(std::wstring &s) {
ltrim(s);
rtrim(s);
}
// trim from start (copying)
static inline std::wstring ltrim_copy(std::wstring s) {
ltrim(s);
return s;
}
// trim from end (copying)
static inline std::wstring rtrim_copy(std::wstring s) {
rtrim(s);
return s;
}
// trim from both ends (copying)
static inline std::wstring trim_copy(std::wstring s) {
trim(s);
return s;
}
static inline bool replace(std::wstring& str, const std::wstring& from, const std::wstring& to) {
size_t start_pos = str.find(from);
if (start_pos == std::wstring::npos)
return false;
str.replace(start_pos, from.length(), to);
return true;
}
static inline void replaceAll(std::wstring& str, const std::wstring& from, const std::wstring& to) {
if (from.empty())
return;
size_t start_pos = 0;
while ((start_pos = str.find(from, start_pos)) != std::wstring::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length();
}
}
static inline std::vector<std::wstring> split(const std::wstring& input, const std::wstring& regex) {
std::wregex re(regex);
std::wsregex_token_iterator first{ input.begin(), input.end(), re, -1 }, last;
return{ first, last };
}
static inline bool startsWith(const std::wstring& s, const std::wstring& prefix) {
return s.size() >= prefix.size() && s.compare(0, prefix.size(), prefix) == 0;
}
static inline bool startsWith(const wchar_t* s, const wchar_t* prefix) {
const size_t slen = wcslen(s);
const size_t plen = wcslen(prefix);
if (plen > slen) return false;
return (wcsncmp(s, prefix, plen) == 0);
}
static inline bool endsWith(const std::wstring& s, wchar_t ch) {
const size_t n = s.size();
return ((n > 0) && (s[n - 1] == ch));
}
static inline void removePrefix(std::wstring& str, const std::wstring& prefix) {
//auto pos = str.find_first_of(prefix);
auto pos = str.find(prefix);
if ((pos != std::wstring::npos) && (pos + prefix.length() < str.length())) {
std::wstring tmp(str.substr(pos + prefix.length()));
str = tmp;
}
}