summaryrefslogtreecommitdiff
path: root/src/logTypes.cpp
blob: 85c5f4b02d6804d1892f9d72ade7a6e9148f4fea (plain)
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
#include "logTypes.hpp"

namespace scn {
	scan_expected<typename ContextType::iterator>
	scanner<WebStat::QuotedString>::scan(WebStat::QuotedString & value, ContextType & ctx)
	{
		static constexpr auto BS_MAP = []() {
			std::array<char, 128> map {};
			map['f'] = '\f';
			map['n'] = '\n';
			map['r'] = '\r';
			map['t'] = '\t';
			map['v'] = '\v';
			map['"'] = '"';
			map['\\'] = '\\';
			return map;
		}();

		if (auto empty = scn::scan<>(ctx.range(), R"("")")) {
			return empty->begin();
		}

		auto simple = scn::scan<std::string>(ctx.range(), R"("{:[^\"]}")");
		if (simple) {
			value = std::move(simple->value());
			return simple->begin();
		}

		if (auto openQuote = scn::scan<>(ctx.range(), R"(")")) {
			ctx.advance_to(openQuote->begin());
			while (true) {
				if (auto closeQuote = scn::scan<>(ctx.range(), R"(")")) {
					return closeQuote->begin();
				}
				if (auto plain = scn::scan<std::string>(ctx.range(), R"({:[^\"]})")) {
					value.append(plain->value());
					ctx.advance_to(plain->begin());
				}
				else if (auto hex = scn::scan<unsigned char>(ctx.range(), R"HEX(\x{:.2x})HEX")) {
					value.append(1, static_cast<char>(hex->value()));
					ctx.advance_to(hex->begin());
				}
				else if (auto escaped = scn::scan<std::string>(ctx.range(), R"ESC(\{:.1[fnrtv"\]})ESC")) {
					value.append(1, BS_MAP[static_cast<unsigned char>(escaped->value().front())]);
					ctx.advance_to(escaped->begin());
				}
				else {
					return unexpected(simple.error());
				}
			}
		}
		return unexpected(simple.error());
	}

	scan_expected<typename ContextType::iterator>
	scanner<WebStat::QueryString>::scan(WebStat::QueryString & value, ContextType & ctx)
	{
		if (auto null = scn::scan<>(ctx.range(), R"("")")) {
			return null->begin();
		}

		if (auto empty = scn::scan<>(ctx.range(), R"("?")")) {
			value.emplace();
			return empty->begin();
		}

		auto result = scn::scan<std::string>(ctx.range(), R"("?{:[^"]}")");
		if (!result) {
			return unexpected(result.error());
		}
		value = std::move(result->value());
		return result->begin();
	}

	scan_expected<typename ContextType::iterator>
	scanner<WebStat::CLFString>::scan(WebStat::CLFString & value, ContextType & ctx)
	{
		if (auto null = scn::scan<>(ctx.range(), R"("-")")) {
			return null->begin();
		}

		return scn::scanner<WebStat::QuotedString> {}.scan(value.emplace(), ctx);
	}
}