-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStringUtils-inl.h
82 lines (68 loc) · 2.08 KB
/
StringUtils-inl.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
#include <algorithm>
#include <cctype>
#include <cstring>
#include "StringUtils.h"
void shitty::trimTrailingLWS(std::string& s) {
size_t initial_size = s.size();
size_t i = initial_size;
while (i != 0) {
--i;
if (s[i] == ' ' || s[i] == '\t')
continue;
break;
}
size_t new_size = i + 1;
if (new_size != initial_size)
s.resize(i);
}
static inline bool isLWS(char c) {
return c == ' ' || c == '\t';
}
void shitty::trimLeadingLWS(std::string& s) {
size_t leading_whitespace = 0;
size_t slen = s.size();
for (size_t i = 0; i < slen && isLWS(s[i]); ++i)
++leading_whitespace;
if (leading_whitespace == 0)
return;
size_t new_size = slen - leading_whitespace;
memmove(s.data(), s.data() + leading_whitespace, new_size);
s.resize(new_size);
}
void shitty::trimLWS(std::string& s) {
trimTrailingLWS(s);
trimLeadingLWS(s);
}
void shitty::asciiLower(std::string& s) {
std::transform(s.begin(), s.end(), s.begin(), [](char c) {
return static_cast<char>(::tolower(c));
});
}
// A moving-split operation that only allocates one new string, reusing the
// input for one of the output parameters.
// Convenient in combination with C++17 structured bindings:
// auto [left, right] = split(std::move(my_string), separator);
std::pair<std::string, std::string>
shitty::split(std::string&& src, std::string::size_type pos) {
std::string second = src.substr(pos);
src.resize(pos);
return {std::move(src), std::move(second)};
}
bool shitty::ascii::CaseInsensitiveEqual::operator()(
const std::string& left,
const std::string& right) const
{
return std::equal(
left.cbegin(),
left.cend(),
right.cbegin(),
right.cend(),
[](char l, char r) -> bool {
return ::tolower(l) == ::tolower(r);
});
}
size_t shitty::ascii::CaseInsensitiveHash::operator()(const std::string& s) const {
std::string lower(s);
asciiLower(lower);
return std::hash<std::string>()(lower);
}