Skip to content

Commit

Permalink
update.
Browse files Browse the repository at this point in the history
  • Loading branch information
caozhanhao committed Mar 22, 2024
1 parent 856c6b2 commit 9afffd0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 35 deletions.
48 changes: 15 additions & 33 deletions include/packme/packme.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include <set>
#include <queue>
#include <iterator>
#include <bitset>
#include <tuple>
#include <variant>
#include <array>
Expand Down Expand Up @@ -336,27 +335,15 @@ namespace packme
}
else
{
std::string data;
data.resize(8);
std::string data(((sizeof(T) * 8) / 7) * 8 + 1, '\0');
size_t pos = 0;
auto num = item;
while (num >= 128)
{
char a = num % 128;
num = (num - a) / 128;
std::bitset<8> byte(a);
byte.flip(7);
char tmp = byte.to_ulong();
if (pos + 8 >= data.size())
data.resize(data.size() + 2);
std::memcpy(data.data() + pos, &tmp, 1);
pos += 1;
data[pos++] = 0x80 | static_cast<char>(num & 0x7f);
num >>= 7;
}
char a = num % 128;
std::bitset<8> byte(a);
char tmp = byte.to_ulong();
std::memcpy(data.data() + pos, &tmp, 1);
pos += 1;
data[pos++] = static_cast<char>(num);
return data.substr(0, pos);
}
}
Expand All @@ -378,15 +365,14 @@ namespace packme
}
else
{
std::bitset<sizeof(T) * 8> result;
size_t pos = 0;
for (auto &c: str)
T result = 0;
size_t shift = 0;
for (auto &c : str)
{
std::bitset<8> byte(c);
for (size_t i = 0; i < 7 && pos < result.size(); ++i, ++pos)
result[pos] = byte[i];
result |= static_cast<T>(c & 0x7f) << shift;
shift += 7;
}
return static_cast<T>(result.to_ullong());
return result;
}
}
else
Expand All @@ -410,8 +396,7 @@ namespace packme
template<typename T>
std::string internal_pack(trivially_copy_tag, const T &item)
{
std::string data;
data.resize(sizeof(T));
std::string data(sizeof(T), '\0');
std::memcpy(data.data(), &item, sizeof(T));
return data;
}
Expand Down Expand Up @@ -501,7 +486,7 @@ namespace packme
size_t int_size = 1;
for (size_t i = pos; i < str.size(); ++i, ++int_size)
{
if (std::bitset<8>(str[i])[7] == 0) break;
if ((str[i] & 0x80) == 0) break;
}
std::string buf = str.substr(pos, int_size);
pos += int_size;
Expand All @@ -523,8 +508,7 @@ namespace packme
template<typename T>
std::string internal_pack(container_tag, const T &item)
{
std::string buf;
buf.resize(64);
std::string buf(64, '\0');
size_t pos = 0;
for (const auto& r : item)
item_pack_helper(buf, pos, r);
Expand All @@ -547,8 +531,7 @@ namespace packme
template<typename T>
std::string internal_pack(struct_tag, const T &item)
{
std::string buf;
buf.resize(64);
std::string buf(64, '\0');
size_t pos = 0;
field_for_each(item, [&buf, &pos](auto &&r) { item_pack_helper(buf, pos, r); });
return buf.substr(0, pos);
Expand All @@ -568,8 +551,7 @@ namespace packme
std::string internal_pack(array_tag, const T &item)
{
using value_type = std::remove_extent_t<T>;
std::string buf;
buf.resize(64);
std::string buf(64, '\0');
size_t pos = 0;
for (size_t i = 0; i < sizeof(T) / sizeof(value_type); ++i)
item_pack_helper(buf, pos, item[i]);
Expand Down
27 changes: 25 additions & 2 deletions tests/packme_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace packme::test
PACKME_TEST(czh)
{
// Number
auto s1 = pack(63);
PACKME_EXPECT_EQ(63, unpack<int>(s1))
auto s1 = pack<uint32_t>(64);
PACKME_EXPECT_EQ(64, unpack<uint32_t>(s1))
PACKME_EXPECT_EQ(s1.size(), 1)
auto s2 = pack(-128);
PACKME_EXPECT_EQ(-128, unpack<int>(s2))
Expand All @@ -19,6 +19,27 @@ namespace packme::test
auto s4 = pack((std::numeric_limits<long long>::min)());
PACKME_EXPECT_EQ((std::numeric_limits<long long>::min)(), unpack<long long>(s4))


auto num_test = []<typename T>()
{
std::random_device rd;
std::mt19937 gen(rd());
for(int i = 0; i < 1024; ++i)
{
std::uniform_int_distribution<T> u((std::numeric_limits<T>::min)(),(std::numeric_limits<T>::max)());
auto num = u(gen);
auto s = pack(num);
PACKME_EXPECT_EQ(unpack<T>(s), num);
}
};
num_test.operator()<int16_t>();
num_test.operator()<uint16_t>();
num_test.operator()<int32_t>();
num_test.operator()<uint32_t>();
num_test.operator()<int64_t>();
num_test.operator()<uint64_t>();


// Array and String
auto s5 = pack<const char[20]>("PACK ME TEST STRING");
PACKME_EXPECT_EQ(s5.size(), 20)
Expand All @@ -29,6 +50,8 @@ namespace packme::test
s5 = pack(std::string{"PACK ME TEST STRING"});
PACKME_EXPECT_EQ(s5.size(), 19)
PACKME_EXPECT_EQ("PACK ME TEST STRING", unpack<std::string>(s5))



// Trivially Copyable
struct A {int a; double b;
Expand Down

0 comments on commit 9afffd0

Please sign in to comment.