-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_utils.cpp
More file actions
93 lines (75 loc) · 2.56 KB
/
string_utils.cpp
File metadata and controls
93 lines (75 loc) · 2.56 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
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
90
91
92
93
#include <stdexcept>
#include "string_utils.h"
using std::string;
// Returns a substring starting at the end of the last part, ending at the first occurrence of the specified delimiter.
// Throws std::runtime_error if the delimiter cannot be found.
string StringParser::next_by_delim(const string& delim) {
int part_end = data.find(delim, cur_offset);
if (part_end == string::npos) {
throw st
7C44
d::runtime_error("Failed to find next part by delimiter.");
}
string part = data.substr(cur_offset, part_end - cur_offset);
cur_offset = part_end + delim.length();
return part;
}
bool StringParser::has_data_left() const {
return cur_offset < data.length();
}
bool caseInsensitiveEquals(const string& a, const string& b) {
if (a.length() != b.length())
return false;
for (size_t i = 0; i < a.length(); i++) {
if (tolower(a[i]) != tolower(b[i]))
return false;
}
return true;
}
// Hex char to int value. Throws if illegal char.
int hexNibble(char c) {
if ('0' <= c && c <= '9') {
return c - '0';
} else if ('a' <= c && c <= 'f') {
return 10 + c - 'a';
} else if ('A' <= c && c <= 'F') {
return 10 + c - 'A';
} else {
throw std::runtime_error("Illegal hex nibble");
}
}
// Decodes URI "%HH" escape sequences. Throws if contains an illegal escape sequence.
string decodeEscapeSequences(const string& str) {
// If the str doesn't contain escape seqences no work needs to be done
if (str.find('%') == string::npos)
return str;
string decoded;
// The actual length will be shorter, but most URIs don't contain many escape sequences,
// so reserving enough memory for the entire string should be beneficial.
decoded.reserve(str.length());
for (size_t i = 0; i < str.length(); i++) {
if (str[i] != '%') {
decoded += str[i];
} else if (i < str.length() - 2) {
// Hex encoded ascii characters
int decoded_char = hexNibble(str[i + 1]) * 16 + hexNibble(str[i + 2]);
if (decoded_char >= 32 && decoded_char < 127) { // We allow printable characters only
decoded += (char)decoded_char;
}
i += 2;
} else {
throw std::runtime_error("Invalid URI escape sequence");
}
}
return decoded;
}
// Returns the file extension of the path, including the dot. e.g. `.txt`
// Returns an empty string if no extension was found.
string getFileExtension(const string& filepath) {
int last_dot = filepath.find_last_of('.');
if (last_dot == string::npos) {
return "";
} else {
// TODO: Do we need to handle folders with dots in their name?
// i.e. Should `some_dir/another.dir/file_with_no_ext` resolve to no extenstion
return filepath.substr(last_dot);
}
}