-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdecoder_string.cc
More file actions
119 lines (100 loc) · 4.14 KB
/
Copy pathdecoder_string.cc
File metadata and controls
119 lines (100 loc) · 4.14 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <sourcemeta/jsonbinpack/runtime_decoder.h>
#include <cassert> // assert
#include <cstdint> // std::uint8_t, std::uint16_t, std::uint64_t
#include <iomanip> // std::setw, std::setfill
#include <sstream> // std::basic_ostringstream
namespace sourcemeta::jsonbinpack {
auto Decoder::UTF8_STRING_NO_LENGTH(const struct UTF8_STRING_NO_LENGTH &options)
-> sourcemeta::core::JSON {
return sourcemeta::core::JSON{this->get_string_utf8(options.size)};
}
auto Decoder::FLOOR_VARINT_PREFIX_UTF8_STRING_SHARED(
const struct FLOOR_VARINT_PREFIX_UTF8_STRING_SHARED &options)
-> sourcemeta::core::JSON {
const std::uint64_t prefix{this->get_varint()};
const bool is_shared{prefix == 0};
const std::uint64_t length{(is_shared ? this->get_varint() : prefix) +
options.minimum - 1};
assert(length >= options.minimum);
if (is_shared) {
const std::uint64_t position{this->position()};
const std::uint64_t current{this->rewind(this->get_varint(), position)};
const sourcemeta::core::JSON value{this->get_string_utf8(length)};
this->seek(current);
return value;
}
return UTF8_STRING_NO_LENGTH({length});
}
auto Decoder::ROOF_VARINT_PREFIX_UTF8_STRING_SHARED(
const struct ROOF_VARINT_PREFIX_UTF8_STRING_SHARED &options)
-> sourcemeta::core::JSON {
const std::uint64_t prefix{this->get_varint()};
const bool is_shared{prefix == 0};
const std::uint64_t length{options.maximum -
(is_shared ? this->get_varint() : prefix) + 1};
assert(length <= options.maximum);
if (is_shared) {
const std::uint64_t position{this->position()};
const std::uint64_t current{this->rewind(this->get_varint(), position)};
const sourcemeta::core::JSON value{UTF8_STRING_NO_LENGTH({length})};
this->seek(current);
return value;
}
return UTF8_STRING_NO_LENGTH({length});
}
auto Decoder::BOUNDED_8BIT_PREFIX_UTF8_STRING_SHARED(
const struct BOUNDED_8BIT_PREFIX_UTF8_STRING_SHARED &options)
-> sourcemeta::core::JSON {
assert(options.minimum <= options.maximum);
assert(sourcemeta::core::is_byte(options.maximum - options.minimum));
const std::uint8_t prefix{this->get_byte()};
const bool is_shared{prefix == 0};
const std::uint64_t length{(is_shared ? this->get_byte() : prefix) +
options.minimum - 1};
assert(sourcemeta::core::is_within(length, options.minimum, options.maximum));
if (is_shared) {
const std::uint64_t position{this->position()};
const std::uint64_t current{this->rewind(this->get_varint(), position)};
const sourcemeta::core::JSON value{UTF8_STRING_NO_LENGTH({length})};
this->seek(current);
return value;
}
return UTF8_STRING_NO_LENGTH({length});
}
auto Decoder::RFC3339_DATE_INTEGER_TRIPLET(
[[maybe_unused]] const struct RFC3339_DATE_INTEGER_TRIPLET &options)
-> sourcemeta::core::JSON {
const std::uint16_t year{this->get_word()};
const std::uint8_t month{this->get_byte()};
const std::uint8_t day{this->get_byte()};
assert(year <= 9999);
assert(month >= 1 && month <= 12);
assert(day >= 1 && day <= 31);
std::basic_ostringstream<sourcemeta::core::JSON::Char,
sourcemeta::core::JSON::CharTraits>
output;
output << std::setfill('0');
output << std::setw(4) << year;
output << "-";
// Cast the bytes to a larger integer, otherwise
// they will be interpreted as characters.
output << std::setw(2) << static_cast<std::uint16_t>(month);
output << "-";
output << std::setw(2) << static_cast<std::uint16_t>(day);
return sourcemeta::core::JSON{output.str()};
}
auto Decoder::PREFIX_VARINT_LENGTH_STRING_SHARED(
const struct PREFIX_VARINT_LENGTH_STRING_SHARED &options)
-> sourcemeta::core::JSON {
const std::uint64_t prefix{this->get_varint()};
if (prefix == 0) {
const std::uint64_t position{this->position()};
const std::uint64_t current{this->rewind(this->get_varint(), position)};
const sourcemeta::core::JSON value{
PREFIX_VARINT_LENGTH_STRING_SHARED(options)};
this->seek(current);
return value;
}
return sourcemeta::core::JSON{this->get_string_utf8(prefix - 1)};
}
} // namespace sourcemeta::jsonbinpack