[ox/mc] Cleanup

This commit is contained in:
Gary Talent 2021-05-03 21:20:43 -04:00
parent ed598378ae
commit a415e86ae5
4 changed files with 27 additions and 28 deletions

View File

@ -23,12 +23,13 @@ static constexpr auto Bits = sizeof(T) << 3;
* Bit numbering starts at 0.
*/
template<typename I>
[[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<I>) {
if constexpr(is_signed_v<I>) {
--shiftStart;
}
for (auto i = shiftStart; i > -1; --i) {
@ -56,7 +57,8 @@ struct McInt {
};
template<typename I>
[[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<typename I>
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<I> ? 1 : 0);
const auto bits = highestBit(val) + 1 + (is_signed_v<I> ? 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<typename I>
* 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<I> decodeInteger(const uint8_t buff[9], std::size_t buffLen, std::size_t
decoded >>= bytes;
auto out = static_cast<I>(decoded);
// move sign bit
if constexpr(ox::is_signed_v<I>) {
if constexpr(is_signed_v<I>) {
const auto valBits = bytes << 3;
// get sign
uint64_t sign = decoded >> (valBits - 1);

View File

@ -6,7 +6,6 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <ox/std/byteswap.hpp>
#include "err.hpp"
#include "presenceindicator.hpp"

View File

@ -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<ArrayLength>(&m_buff[m_buffIt], m_buffLen - m_buffIt, &bytesRead);
oxRequire(len, mc::decodeInteger<ArrayLength>(&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<String, T> *val) {
return OxError(MC_BUFFENDED);
}
std::size_t bytesRead = 0;
auto len = mc::decodeInteger<ArrayLength>(&m_buff[m_buffIt], m_buffLen - m_buffIt, &bytesRead);
oxRequire(len, mc::decodeInteger<ArrayLength>(&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<ArrayLength>(&m_buff[m_buffIt], m_buffLen - m_buffIt, &bytesRead);
oxRequire(len, mc::decodeInteger<ArrayLength>(&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));

View File

@ -13,13 +13,13 @@
#include <ox/std/bit.hpp>
#include <ox/std/buffer.hpp>
#include <ox/std/byteswap.hpp>
#include <ox/std/hashmap.hpp>
#include <ox/std/string.hpp>
#include <ox/std/types.hpp>
#include <ox/std/units.hpp>
#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<String, T> *val);
template<std::size_t L>
Error field(const char*, ox::BString<L> *val) noexcept;
Error field(const char*, BString<L> *val) noexcept;
Error field(const char*, SerStr val) noexcept;
@ -86,7 +86,7 @@ class MetalClawWriter {
};
template<std::size_t L>
Error MetalClawWriter::field(const char *name, ox::BString<L> *val) noexcept {
Error MetalClawWriter::field(const char *name, BString<L> *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<typename T>
Error MetalClawWriter::field(const char*, HashMap<String, T> *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<String, T> *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));