-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_util.cpp
More file actions
62 lines (48 loc) · 1.46 KB
/
string_util.cpp
File metadata and controls
62 lines (48 loc) · 1.46 KB
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
// https://en.wikipedia.org/wiki/Quoted-printable
#include <iostream>
#include <sstream>
std::string decodeQuotedPrinted(std::string const& encoded) {
std::string out;
for (std::size_t i = 0; i < encoded.length(); i++) {
char ch = encoded.at(i);
if (ch != '=') {
out += ch;
continue;
}
auto nextChar = encoded.at(i + 1);
if (nextChar == '\n') {
i += 1;
continue;
}
std::string next2Chars = encoded.substr(i+1, 2);
unsigned int x;
std::stringstream ss;
ss << std::hex << next2Chars;
ss >> x;
out += x;
i += 2;
}
return out;
}
std::string strToLower(std::string const& str) {
std::string strCopy {};
for (std::size_t i = 0; i < str.length(); i++) {
strCopy += std::tolower(str.at(i));
}
return strCopy;
}
bool startsWith(std::string const& line, std::string const& prefix) {
return line.substr(0, prefix.length()) == prefix;
}
std::string trimPrefix(std::string const& line, std::string const& prefix) {
return line.substr(prefix.length());
}
std::string trimSuffix(std::string const& line, std::string const& suffix) {
std::size_t beforeSuffixLen = line.length() - suffix.length();
std::string lastXChars = line.substr(beforeSuffixLen);
if (suffix != lastXChars) {
// no match, return original
return line;
}
return line.substr(0, beforeSuffixLen);
}