♻️ adjust positive integer representation

This commit is contained in:
Niels Lohmann 2021-09-11 13:22:23 +02:00
parent 623f0e96e2
commit d798ca24aa
No known key found for this signature in database
GPG Key ID: 7F3CEA63AE251B69
3 changed files with 60 additions and 54 deletions

View File

@ -1723,7 +1723,7 @@ class binary_writer
oa->write_character(to_char_type(0x8D)); oa->write_character(to_char_type(0x8D));
write_number(static_cast<std::int64_t>(value)); write_number(static_cast<std::int64_t>(value));
} }
else if (value < -33554432 || value > 67108863) else if (value < -33554432 || value > 67637031)
{ {
// 32 bit integers // 32 bit integers
oa->write_character(to_char_type(0x8C)); oa->write_character(to_char_type(0x8C));
@ -1764,23 +1764,26 @@ class binary_writer
JSON_ASSERT(value >= 0); JSON_ASSERT(value >= 0);
oa->write_character(static_cast<std::uint8_t>(0x90 + value)); oa->write_character(static_cast<std::uint8_t>(0x90 + value));
} }
else if (value <= 3839) else if (value <= 3879)
{ {
JSON_ASSERT(value >= 0); JSON_ASSERT(value >= 40);
value -= 40;
oa->write_character(static_cast<std::uint8_t>(0xC2 + (value >> 7 & 0x1F))); oa->write_character(static_cast<std::uint8_t>(0xC2 + (value >> 7 & 0x1F)));
oa->write_character(static_cast<std::uint8_t>(value & 0x7F)); oa->write_character(static_cast<std::uint8_t>(value & 0x7F));
} }
else if (value <= 524287) else if (value <= 528167)
{ {
JSON_ASSERT(value >= 0); JSON_ASSERT(value >= 3880);
value -= 3880;
oa->write_character(static_cast<std::uint8_t>(0xE0 + (value >> 15 & 0x0F))); oa->write_character(static_cast<std::uint8_t>(0xE0 + (value >> 15 & 0x0F)));
oa->write_character(static_cast<std::uint8_t>(value >> 8 & 0x7F)); oa->write_character(static_cast<std::uint8_t>(value >> 8 & 0x7F));
oa->write_character(static_cast<std::uint8_t>(value)); oa->write_character(static_cast<std::uint8_t>(value));
} }
else else
{ {
JSON_ASSERT(value >= 0); JSON_ASSERT(value >= 528168);
JSON_ASSERT(value <= 67108863); JSON_ASSERT(value <= 67637031);
value -= 528168;
oa->write_character(static_cast<std::uint8_t>(0xF0 + (value >> 23 & 0x17))); oa->write_character(static_cast<std::uint8_t>(0xF0 + (value >> 23 & 0x17)));
oa->write_character(static_cast<std::uint8_t>(value >> 16 & 0x7F)); oa->write_character(static_cast<std::uint8_t>(value >> 16 & 0x7F));
oa->write_character(static_cast<std::uint8_t>(value >> 8)); oa->write_character(static_cast<std::uint8_t>(value >> 8));

View File

@ -15297,7 +15297,7 @@ class binary_writer
oa->write_character(to_char_type(0x8D)); oa->write_character(to_char_type(0x8D));
write_number(static_cast<std::int64_t>(value)); write_number(static_cast<std::int64_t>(value));
} }
else if (value < -33554432 || value > 67108863) else if (value < -33554432 || value > 67637031)
{ {
// 32 bit integers // 32 bit integers
oa->write_character(to_char_type(0x8C)); oa->write_character(to_char_type(0x8C));
@ -15338,23 +15338,26 @@ class binary_writer
JSON_ASSERT(value >= 0); JSON_ASSERT(value >= 0);
oa->write_character(static_cast<std::uint8_t>(0x90 + value)); oa->write_character(static_cast<std::uint8_t>(0x90 + value));
} }
else if (value <= 3839) else if (value <= 3879)
{ {
JSON_ASSERT(value >= 0); JSON_ASSERT(value >= 40);
value -= 40;
oa->write_character(static_cast<std::uint8_t>(0xC2 + (value >> 7 & 0x1F))); oa->write_character(static_cast<std::uint8_t>(0xC2 + (value >> 7 & 0x1F)));
oa->write_character(static_cast<std::uint8_t>(value & 0x7F)); oa->write_character(static_cast<std::uint8_t>(value & 0x7F));
} }
else if (value <= 524287) else if (value <= 528167)
{ {
JSON_ASSERT(value >= 0); JSON_ASSERT(value >= 3880);
value -= 3880;
oa->write_character(static_cast<std::uint8_t>(0xE0 + (value >> 15 & 0x0F))); oa->write_character(static_cast<std::uint8_t>(0xE0 + (value >> 15 & 0x0F)));
oa->write_character(static_cast<std::uint8_t>(value >> 8 & 0x7F)); oa->write_character(static_cast<std::uint8_t>(value >> 8 & 0x7F));
oa->write_character(static_cast<std::uint8_t>(value)); oa->write_character(static_cast<std::uint8_t>(value));
} }
else else
{ {
JSON_ASSERT(value >= 0); JSON_ASSERT(value >= 528168);
JSON_ASSERT(value <= 67108863); JSON_ASSERT(value <= 67637031);
value -= 528168;
oa->write_character(static_cast<std::uint8_t>(0xF0 + (value >> 23 & 0x17))); oa->write_character(static_cast<std::uint8_t>(0xF0 + (value >> 23 & 0x17)));
oa->write_character(static_cast<std::uint8_t>(value >> 16 & 0x7F)); oa->write_character(static_cast<std::uint8_t>(value >> 16 & 0x7F));
oa->write_character(static_cast<std::uint8_t>(value >> 8)); oa->write_character(static_cast<std::uint8_t>(value >> 8));

View File

@ -98,69 +98,69 @@ TEST_CASE("BON8")
} }
} }
SECTION("40..3839") SECTION("40..3879")
{ {
SECTION("40") SECTION("40")
{ {
json j = 40U; json j = 40U;
std::vector<uint8_t> expected = {0xC2, 0x28}; std::vector<uint8_t> expected = {0xC2, 0x00};
const auto result = json::to_bon8(j); const auto result = json::to_bon8(j);
CHECK(result == expected); CHECK(result == expected);
} }
SECTION("3839") SECTION("3879")
{ {
json j = 3839U; json j = 3879U;
std::vector<uint8_t> expected = {0xDF, 0x7F}; std::vector<uint8_t> expected = {0xDF, 0x7F};
const auto result = json::to_bon8(j); const auto result = json::to_bon8(j);
CHECK(result == expected); CHECK(result == expected);
} }
} }
SECTION("3840..524287") SECTION("3880..524287")
{ {
SECTION("3840") SECTION("3880")
{ {
json j = 3840U; json j = 3880U;
std::vector<uint8_t> expected = {0xE0, 0x0F, 0x00}; std::vector<uint8_t> expected = {0xE0, 0x00, 0x00};
const auto result = json::to_bon8(j); const auto result = json::to_bon8(j);
CHECK(result == expected); CHECK(result == expected);
} }
SECTION("524287") SECTION("528167")
{ {
json j = 524287U; json j = 528167U;
std::vector<uint8_t> expected = {0xEF, 0x7F, 0xFF}; std::vector<uint8_t> expected = {0xEF, 0x7F, 0xFF};
const auto result = json::to_bon8(j); const auto result = json::to_bon8(j);
CHECK(result == expected); CHECK(result == expected);
} }
} }
SECTION("524288..67108863") SECTION("528168..67637031")
{ {
SECTION("524288") SECTION("528168")
{ {
json j = 524288U; json j = 528168U;
std::vector<uint8_t> expected = {0xF0, 0x08, 0x00, 0x00}; std::vector<uint8_t> expected = {0xF0, 0x00, 0x00, 0x00};
const auto result = json::to_bon8(j); const auto result = json::to_bon8(j);
CHECK(result == expected); CHECK(result == expected);
} }
SECTION("67108863") SECTION("67637031")
{ {
json j = 67108863U; json j = 67637031U;
std::vector<uint8_t> expected = {0xF7, 0x7F, 0xFF, 0xFF}; std::vector<uint8_t> expected = {0xF7, 0x7F, 0xFF, 0xFF};
const auto result = json::to_bon8(j); const auto result = json::to_bon8(j);
CHECK(result == expected); CHECK(result == expected);
} }
} }
SECTION("67108864..2147483647 (int32max)") SECTION("67637032..2147483647 (int32max)")
{ {
SECTION("67108864") SECTION("67637032")
{ {
json j = 67108864U; json j = 67637032U;
std::vector<uint8_t> expected = {0x8C, 0x04, 0x00, 0x00, 0x00}; std::vector<uint8_t> expected = {0x8C, 0x04, 0x08, 0x0F, 0x28};
const auto result = json::to_bon8(j); const auto result = json::to_bon8(j);
CHECK(result == expected); CHECK(result == expected);
} }
@ -337,69 +337,69 @@ TEST_CASE("BON8")
} }
} }
SECTION("40..3839") SECTION("40..3879")
{ {
SECTION("40") SECTION("40")
{ {
json j = 40; json j = 40;
std::vector<uint8_t> expected = {0xC2, 0x28}; std::vector<uint8_t> expected = {0xC2, 0x00};
const auto result = json::to_bon8(j); const auto result = json::to_bon8(j);
CHECK(result == expected); CHECK(result == expected);
} }
SECTION("3839") SECTION("3879")
{ {
json j = 3839; json j = 3879;
std::vector<uint8_t> expected = {0xDF, 0x7F}; std::vector<uint8_t> expected = {0xDF, 0x7F};
const auto result = json::to_bon8(j); const auto result = json::to_bon8(j);
CHECK(result == expected); CHECK(result == expected);
} }
} }
SECTION("3840..524287") SECTION("3880..524287")
{ {
SECTION("3840") SECTION("3880")
{ {
json j = 3840; json j = 3880;
std::vector<uint8_t> expected = {0xE0, 0x0F, 0x00}; std::vector<uint8_t> expected = {0xE0, 0x00, 0x00};
const auto result = json::to_bon8(j); const auto result = json::to_bon8(j);
CHECK(result == expected); CHECK(result == expected);
} }
SECTION("524287") SECTION("528167")
{ {
json j = 524287; json j = 528167;
std::vector<uint8_t> expected = {0xEF, 0x7F, 0xFF}; std::vector<uint8_t> expected = {0xEF, 0x7F, 0xFF};
const auto result = json::to_bon8(j); const auto result = json::to_bon8(j);
CHECK(result == expected); CHECK(result == expected);
} }
} }
SECTION("524288..67108863") SECTION("528168..67637031")
{ {
SECTION("524288") SECTION("528168")
{ {
json j = 524288; json j = 528168;
std::vector<uint8_t> expected = {0xF0, 0x08, 0x00, 0x00}; std::vector<uint8_t> expected = {0xF0, 0x00, 0x00, 0x00};
const auto result = json::to_bon8(j); const auto result = json::to_bon8(j);
CHECK(result == expected); CHECK(result == expected);
} }
SECTION("67108863") SECTION("67637031")
{ {
json j = 67108863; json j = 67637031;
std::vector<uint8_t> expected = {0xF7, 0x7F, 0xFF, 0xFF}; std::vector<uint8_t> expected = {0xF7, 0x7F, 0xFF, 0xFF};
const auto result = json::to_bon8(j); const auto result = json::to_bon8(j);
CHECK(result == expected); CHECK(result == expected);
} }
} }
SECTION("67108864..2147483647 (int32max)") SECTION("67637032..2147483647 (int32max)")
{ {
SECTION("67108864") SECTION("67637032")
{ {
json j = 67108864; json j = 67637032;
std::vector<uint8_t> expected = {0x8C, 0x04, 0x00, 0x00, 0x00}; std::vector<uint8_t> expected = {0x8C, 0x04, 0x08, 0x0F, 0x28};
const auto result = json::to_bon8(j); const auto result = json::to_bon8(j);
CHECK(result == expected); CHECK(result == expected);
} }