-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParseUtils.hpp
39 lines (32 loc) · 1.15 KB
/
ParseUtils.hpp
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
#pragma once
#include "common.hpp"
namespace hlcup {
struct ParseUtils {
static const constexpr u8 kUnhexTable[64] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0};
inline static u8 hexToInt(u8 ch) { return kUnhexTable[(ch - '0') & 0x3f]; }
static ssize_t unescapeUrlValue(const u8 *&p, const u8 *pe, u8 *&out) {
while (p != pe) {
u8 ch = *p;
if (ch == '&' || ch == ' ' || ch == '\0') {
return 0;
} else if (ch == '%') {
ssize_t dp = pe - p;
if (dp < 3) {
return 3 - dp;
} else {
*out++ = u8(hexToInt(p[1]) << 4) | hexToInt(p[2]);
p += 3;
}
} else if (ch == '+') {
*out++ = ' ';
++p;
} else {
*out++ = ch;
++p;
}
}
return 1;
}
};
} // namespace hlcup