From c445f339904a0fbe70f255bea7e706ab46e5c2ca Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Mon, 3 May 2021 21:20:43 -0400 Subject: [PATCH] [ox/mc] Cleanup (synced from a415e86ae5aa7b5599ae3415f205da62c09aa6b2) --- src/ox/mc/intops.hpp | 15 +++++++++------ src/ox/mc/presenceindicator.cpp | 1 - src/ox/mc/read.hpp | 25 +++++++++++-------------- src/ox/mc/write.hpp | 14 +++++++------- 4 files changed, 27 insertions(+), 28 deletions(-) diff --git a/src/ox/mc/intops.hpp b/src/ox/mc/intops.hpp index c378a9c22..ec297d8db 100644 --- a/src/ox/mc/intops.hpp +++ b/src/ox/mc/intops.hpp @@ -23,12 +23,13 @@ static constexpr auto Bits = sizeof(T) << 3; * Bit numbering starts at 0. */ template -[[nodiscard]] constexpr std::size_t highestBit(I val) noexcept { +[[nodiscard]] +constexpr std::size_t highestBit(I val) noexcept { int shiftStart = sizeof(I) * 8 - 1; // find most significant non-sign indicator bit std::size_t highestBit = 0; // start at one bit lower if signed - if constexpr(ox::is_signed_v) { + if constexpr(is_signed_v) { --shiftStart; } for (auto i = shiftStart; i > -1; --i) { @@ -56,7 +57,8 @@ struct McInt { }; template -[[nodiscard]] constexpr McInt encodeInteger(I input) noexcept { +[[nodiscard]] +constexpr McInt encodeInteger(I input) noexcept { McInt out; // move input to uint64_t to allow consistent bit manipulation, and to avoid // overflow concerns @@ -65,7 +67,7 @@ template if (val) { // bits needed to represent number factoring in space possibly // needed for signed bit - const auto bits = highestBit(val) + 1 + (ox::is_signed_v ? 1 : 0); + const auto bits = highestBit(val) + 1 + (is_signed_v ? 1 : 0); // bytes needed to store value std::size_t bytes = bits / 8 + (bits % 8 != 0); const auto bitsAvailable = bytes * 8; // bits available to integer value @@ -97,7 +99,8 @@ template * Returns the number of bytes indicated by the bytes indicator of a variable * length integer. */ -[[nodiscard]] static constexpr std::size_t countBytes(uint8_t b) noexcept { +[[nodiscard]] +static constexpr std::size_t countBytes(uint8_t b) noexcept { std::size_t i = 0; for (; (b >> i) & 1; i++); return i + 1; @@ -128,7 +131,7 @@ Result decodeInteger(const uint8_t buff[9], std::size_t buffLen, std::size_t decoded >>= bytes; auto out = static_cast(decoded); // move sign bit - if constexpr(ox::is_signed_v) { + if constexpr(is_signed_v) { const auto valBits = bytes << 3; // get sign uint64_t sign = decoded >> (valBits - 1); diff --git a/src/ox/mc/presenceindicator.cpp b/src/ox/mc/presenceindicator.cpp index 41384bbdf..b9f72953e 100644 --- a/src/ox/mc/presenceindicator.cpp +++ b/src/ox/mc/presenceindicator.cpp @@ -6,7 +6,6 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#include #include "err.hpp" #include "presenceindicator.hpp" diff --git a/src/ox/mc/read.hpp b/src/ox/mc/read.hpp index a17df8c53..594aa9c32 100644 --- a/src/ox/mc/read.hpp +++ b/src/ox/mc/read.hpp @@ -186,15 +186,14 @@ Error MetalClawReader::field(const char *name, T *val, std::size_t valLen) { return OxError(MC_BUFFENDED); } std::size_t bytesRead = 0; - auto len = mc::decodeInteger(&m_buff[m_buffIt], m_buffLen - m_buffIt, &bytesRead); + oxRequire(len, mc::decodeInteger(&m_buff[m_buffIt], m_buffLen - m_buffIt, &bytesRead)); m_buffIt += bytesRead; - oxReturnError(len.error); // read the list - if (valLen >= len.value) { + if (valLen >= len) { auto reader = child(""); - reader.setTypeInfo("List", len.value); - for (std::size_t i = 0; i < len.value; i++) { + reader.setTypeInfo("List", len); + for (std::size_t i = 0; i < len; i++) { oxReturnError(reader.field("", &val[i])); } } else { @@ -216,15 +215,14 @@ Error MetalClawReader::field(const char*, HashMap *val) { return OxError(MC_BUFFENDED); } std::size_t bytesRead = 0; - auto len = mc::decodeInteger(&m_buff[m_buffIt], m_buffLen - m_buffIt, &bytesRead); + oxRequire(len, mc::decodeInteger(&m_buff[m_buffIt], m_buffLen - m_buffIt, &bytesRead)); m_buffIt += bytesRead; - oxReturnError(len.error); // read the list auto reader = child(""); - reader.setTypeInfo("List", len.value); - for (std::size_t i = 0; i < len.value; i++) { - auto keyLen = reader.stringLength(nullptr); + reader.setTypeInfo("List", len); + for (std::size_t i = 0; i < len; i++) { + const auto keyLen = reader.stringLength(nullptr); auto wkey = ox_malloca(keyLen + 1, char, 0); oxReturnError(reader.field("", SerStr(wkey.get(), keyLen))); oxReturnError(reader.field("", &val->operator[](wkey.get()))); @@ -244,14 +242,13 @@ Error MetalClawReader::field(const char*, Handler handler) { return OxError(MC_BUFFENDED); } std::size_t bytesRead = 0; - auto len = mc::decodeInteger(&m_buff[m_buffIt], m_buffLen - m_buffIt, &bytesRead); + oxRequire(len, mc::decodeInteger(&m_buff[m_buffIt], m_buffLen - m_buffIt, &bytesRead)); m_buffIt += bytesRead; - oxReturnError(len.error); // read the list auto reader = child(""); - reader.setTypeInfo("List", len.value); - for (std::size_t i = 0; i < len.value; i++) { + reader.setTypeInfo("List", len); + for (std::size_t i = 0; i < len; i++) { T val; oxReturnError(reader.field("", &val)); oxReturnError(handler(i, &val)); diff --git a/src/ox/mc/write.hpp b/src/ox/mc/write.hpp index 5952ad10c..2ef7ad602 100644 --- a/src/ox/mc/write.hpp +++ b/src/ox/mc/write.hpp @@ -13,13 +13,13 @@ #include #include #include +#include #include #include #include #include "intops.hpp" #include "err.hpp" -#include "ox/std/hashmap.hpp" #include "presenceindicator.hpp" #include "types.hpp" @@ -60,7 +60,7 @@ class MetalClawWriter { Error field(const char*, HashMap *val); template - Error field(const char*, ox::BString *val) noexcept; + Error field(const char*, BString *val) noexcept; Error field(const char*, SerStr val) noexcept; @@ -86,7 +86,7 @@ class MetalClawWriter { }; template -Error MetalClawWriter::field(const char *name, ox::BString *val) noexcept { +Error MetalClawWriter::field(const char *name, BString *val) noexcept { return field(name, SerStr(val->data(), val->cap())); } @@ -159,8 +159,8 @@ Error MetalClawWriter::field(const char*, T *val, std::size_t len) { template Error MetalClawWriter::field(const char*, HashMap *val) { - auto &keys = val->keys(); - auto len = keys.size(); + const auto &keys = val->keys(); + const auto len = keys.size(); bool fieldSet = false; if (len && (m_unionIdx == -1 || m_unionIdx == m_field)) { @@ -179,7 +179,7 @@ Error MetalClawWriter::field(const char*, HashMap *val) { // write the array for (std::size_t i = 0; i < len; i++) { - auto &key = keys[i]; + const auto &key = keys[i]; const auto keyLen = ox_strlen(key); auto wkey = ox_malloca(keyLen + 1, char, 0); memcpy(wkey, key.c_str(), keyLen + 1); @@ -206,7 +206,7 @@ Error MetalClawWriter::appendInteger(I val) noexcept { ox_memcpy(&m_buff[m_buffIt], mi.data, mi.length); m_buffIt += mi.length; } else { - oxReturnError(OxError(MC_BUFFENDED)); + return OxError(MC_BUFFENDED); } } oxReturnError(m_fieldPresence.set(m_field, fieldSet));