Skip to content

Commit

Permalink
Add ioBase::i64_dyn_v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Sainan committed Nov 25, 2024
1 parent b59360f commit 4ce704f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
17 changes: 17 additions & 0 deletions soup/Reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,21 @@ NAMESPACE_SOUP

return true;
}

bool Reader::i64_dyn_v2(int64_t& v) noexcept
{
uint64_t u;
SOUP_RETHROW_FALSE(u64_dyn_v2(u));
const bool neg = (u >> 6) & 1; // check bit 6
u = ((u >> 1) & ~0x3f) | (u & 0x3f); // remove bit 6
if (neg)
{
v = (u * -1) - 1;
}
else
{
v = u;
}
return true;
}
}
3 changes: 3 additions & 0 deletions soup/Reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ NAMESPACE_SOUP
// An unsigned 64-bit integer encoded in 1..9 bytes. This is a slightly more efficient version of u64_dyn, e.g. 0x4000..0x407f are encoded in 2 bytes instead of 3.
bool u64_dyn_v2(uint64_t& v) noexcept;

// A signed 64-bit integer encoded in 1..9 bytes. (Specialisation of u64_dyn_v2. This revision also simplifies how negative integers are handled.)
bool i64_dyn_v2(int64_t& v) noexcept;

// An integer where every byte's most significant bit is used to indicate if another byte follows, most significant byte first.
template <typename Int>
bool om(Int& v) noexcept
Expand Down
15 changes: 15 additions & 0 deletions soup/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ NAMESPACE_SOUP
return ret;
}

bool Writer::i64_dyn_v2(const int64_t& v) noexcept
{
uint64_t u;
bool neg = (v < 0);
if (neg)
{
u = (v * -1) - 1;
}
else
{
u = v;
}
return u64_dyn_v2(((uint64_t)neg << 6) | ((u & ~0x3f) << 1) | (u & 0x3f));
}

bool Writer::mysql_lenenc(const uint64_t& v) noexcept
{
if (v < 0xFB)
Expand Down
3 changes: 3 additions & 0 deletions soup/Writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ NAMESPACE_SOUP
// An unsigned 64-bit integer encoded in 1..9 bytes. This is a slightly more efficient version of u64_dyn, e.g. 0x4000..0x407f are encoded in 2 bytes instead of 3.
bool u64_dyn_v2(const uint64_t& v) noexcept;

// A signed 64-bit integer encoded in 1..9 bytes. (Specialisation of u64_dyn_v2. This revision also simplifies how negative integers are handled.)
bool i64_dyn_v2(const int64_t& v) noexcept;

// An integer where every byte's most significant bit is used to indicate if another byte follows, most significant byte first.
template <typename Int>
bool om(const Int& v) noexcept
Expand Down

0 comments on commit 4ce704f

Please sign in to comment.