Skip to content

Commit

Permalink
Fixing byte array construction (#9)
Browse files Browse the repository at this point in the history
* Fixing byte array construction, and adding option to left align the bytes.

* Show test failure output.

* Fixing bad memcpy.

* Properly fixed the bug, major cleanup.

* Remove ambiguity

* Testing...

* Account for imperfect size.
  • Loading branch information
GhostofCookie committed Sep 6, 2023
1 parent 1158c91 commit 4373d9d
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 234 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
working-directory: ${{github.workspace}}/build
# Execute tests defined by the CMake configuration.
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: ctest -C ${{env.BUILD_TYPE}}
run: ctest -C ${{env.BUILD_TYPE}} --output-on-failure

- name: Benchmark
run: ${{github.workspace}}/build/benchmark/qname_benchmark
Expand Down
37 changes: 19 additions & 18 deletions include/quicr/hex_endec.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ class HexEndec
template<Unsigned... UInt_ts>
static inline std::string Encode(std::span<uint16_t> distribution, UInt_ts... values)
{
static_assert(Size >= (Dist + ...), "Total bits cannot exceed specified size");
if(Size < std::accumulate(distribution.begin(), distribution.end(), 0))
throw std::invalid_argument("Total bits cannot exceed specified size");

std::array<uint64_t, sizeof...(UInt_ts)> vals{ values... };
return Encode(distribution, std::span<uint64_t>(vals));
Expand Down Expand Up @@ -94,59 +95,59 @@ class HexEndec
* @brief Decodes a hex string that has size in bits of Size into a list of
* values sized according to Dist in order.
*
* @tparam Uint_t The unsigned integer type to return.
* @tparam UInt_t The unsigned integer type to return.
* @param hex The hex string to decode. Must have a length in bytes
* corresponding to the size in bits of Size.
*
* @returns Structured binding of values decoded from hex string
* corresponding in order to the size of Dist.
*/
template<Unsigned Uint_t = uint64_t>
static constexpr std::array<Uint_t, sizeof...(Dist)> Decode(std::string_view hex)
template<Unsigned UInt_t = uint64_t>
static constexpr std::array<UInt_t, sizeof...(Dist)> Decode(std::string_view hex)
{
return Decode<Uint_t>(quicr::Name(hex));
return Decode<UInt_t>(quicr::Name(hex));
}

template<Unsigned Uint_t = uint64_t>
static constexpr std::array<Uint_t, sizeof...(Dist)> Decode(quicr::Name name)
template<Unsigned UInt_t = uint64_t>
static constexpr std::array<UInt_t, sizeof...(Dist)> Decode(quicr::Name name)
{
static_assert(Size >= (Dist + ...), "Total bits cannot exceed specified size");

std::array<uint16_t, sizeof...(Dist)> distribution{ Dist... };
return Decode<sizeof...(Dist), Uint_t>(distribution, name);
return Decode<sizeof...(Dist), UInt_t>(distribution, name);
}

template<size_t N, Unsigned Uint_t = uint64_t>
static constexpr std::array<Uint_t, N> Decode(std::span<uint16_t> distribution, quicr::Name name)
template<size_t N, Unsigned UInt_t = uint64_t>
static constexpr std::array<UInt_t, N> Decode(std::span<uint16_t> distribution, quicr::Name name)
{
const auto dist_size = distribution.size();
std::array<Uint_t, N> result;
std::array<UInt_t, N> result;

for (size_t i = 0; i < dist_size; ++i)
{
const auto dist = distribution[i];
result[i] = name.bits<Uint_t>(Size - dist, dist);
result[i] = name.bits<UInt_t>(Size - dist, dist);
name <<= dist;
}

return result;
}

template<Unsigned Uint_t = uint64_t>
static inline std::vector<Uint_t> Decode(std::span<uint16_t> distribution, std::string_view hex)
template<Unsigned UInt_t = uint64_t>
static inline std::vector<UInt_t> Decode(std::span<uint16_t> distribution, std::string_view hex)
{
return Decode(distribution, quicr::Name(hex));
}

template<Unsigned Uint_t = uint64_t>
static inline std::vector<Uint_t> Decode(std::span<uint16_t> distribution, quicr::Name name)
template<Unsigned UInt_t = uint64_t>
static inline std::vector<UInt_t> Decode(std::span<uint16_t> distribution, quicr::Name name)
{
const auto dist_size = distribution.size();
std::vector<Uint_t> result(dist_size);
std::vector<UInt_t> result(dist_size);
for (size_t i = 0; i < dist_size; ++i)
{
const auto dist = distribution[i];
result[i] = name.bits<Uint_t>(Size - dist, dist);
result[i] = name.bits<UInt_t>(Size - dist, dist);
name <<= dist;
}

Expand Down
Loading

0 comments on commit 4373d9d

Please sign in to comment.