From 9852956293f67812889e01fb244b8115871006ad Mon Sep 17 00:00:00 2001 From: "Zezheng.Li" Date: Tue, 21 Nov 2023 14:38:52 +0800 Subject: [PATCH] fix --- include/ylt/struct_pack/endian_wrapper.hpp | 45 +++++++++++++--------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/include/ylt/struct_pack/endian_wrapper.hpp b/include/ylt/struct_pack/endian_wrapper.hpp index 6bf96ca6f..72766ed3f 100644 --- a/include/ylt/struct_pack/endian_wrapper.hpp +++ b/include/ylt/struct_pack/endian_wrapper.hpp @@ -174,7 +174,10 @@ void low_bytes_write_wrapper(writer_t& writer, const T& elem) { } else { const char* SP_RESTRICT data = sizeof(T) - block_size + (const char*)&elem; - if constexpr (block_size == 2) { + if constexpr (block_size == 1) { + writer.write((char*)&data, block_size); + } + else if constexpr (block_size == 2) { auto tmp = bswap16(*(uint16_t*)data); writer.write((char*)&tmp, block_size); } @@ -188,7 +191,7 @@ void low_bytes_write_wrapper(writer_t& writer, const T& elem) { } else { static_assert(!sizeof(writer), - "illegal block size(should be 1,2,4,8,16)"); + "illegal block size(should be 1,2,4,8)"); } } } @@ -234,31 +237,35 @@ bool read_bytes_array(reader_t& reader, char* SP_RESTRICT data, template bool low_bytes_read_wrapper(reader_t& reader, T& elem) { static_assert(sizeof(T) >= block_size); - if constexpr (is_system_little_endian || block_size == sizeof(T) || - block_size == 1) { + if constexpr (is_system_little_endian || block_size == sizeof(T)) { char* SP_RESTRICT data = (char*)&elem; return static_cast(reader.read(data, block_size)); } else { - char tmp[block_size]; char* SP_RESTRICT data = (char*)&elem + sizeof(T) - block_size; - bool res = static_cast(reader.read(tmp, block_size)); - if SP_UNLIKELY (!res) { - return res; - } - if constexpr (block_size == 2) { - *(uint16_t*)data = bswap16(*(uint16_t*)tmp); - } - else if constexpr (block_size == 4) { - *(uint32_t*)data = bswap32(*(uint32_t*)tmp); - } - else if constexpr (block_size == 8) { - *(uint64_t*)data = bswap64(*(uint64_t*)tmp); + if constexpr (block_size > 1) { + char tmp[block_size]; + bool res = static_cast(reader.read(tmp, block_size)); + if SP_UNLIKELY (!res) { + return res; + } + if constexpr (block_size == 2) { + *(uint16_t*)data = bswap16(*(uint16_t*)tmp); + } + else if constexpr (block_size == 4) { + *(uint32_t*)data = bswap32(*(uint32_t*)tmp); + } + else if constexpr (block_size == 8) { + *(uint64_t*)data = bswap64(*(uint64_t*)tmp); + } + else { + static_assert(!sizeof(reader), "illegal block size(should be 1,2,4,8)"); + } + return true; } else { - static_assert(!sizeof(reader), "illegal block size(should be 1,2,4,8)"); + reader.read(data, block_size); } - return true; } } }; // namespace struct_pack::detail \ No newline at end of file