diff --git a/include/dsn/utility/endians.h b/include/dsn/utility/endians.h index 550d3426f5..833fc3a630 100644 --- a/include/dsn/utility/endians.h +++ b/include/dsn/utility/endians.h @@ -13,12 +13,16 @@ namespace dsn { namespace endian { +inline uint8_t hton(uint8_t v) { return v; } + inline uint16_t hton(uint16_t v) { return htobe16(v); } inline uint32_t hton(uint32_t v) { return htobe32(v); } inline uint64_t hton(uint64_t v) { return htobe64(v); } +inline uint8_t ntoh(uint8_t v) { return v; } + inline uint16_t ntoh(uint16_t v) { return be16toh(v); } inline uint32_t ntoh(uint32_t v) { return be32toh(v); } @@ -35,6 +39,8 @@ class data_output explicit data_output(std::string &s) : data_output(&s[0], s.length()) {} + data_output &write_u8(uint8_t val) { return write_unsigned(val); } + data_output &write_u16(uint16_t val) { return write_unsigned(val); } data_output &write_u32(uint32_t val) { return write_unsigned(val); } @@ -71,6 +77,8 @@ class data_input public: explicit data_input(string_view s) : _p(s.data()), _size(s.size()) {} + uint8_t read_u8() { return read_unsigned(); } + uint16_t read_u16() { return read_unsigned(); } uint32_t read_u32() { return read_unsigned(); } diff --git a/src/utils/test/endian_test.cpp b/src/utils/test/endian_test.cpp index c1fe2e1aac..34b50acead 100644 --- a/src/utils/test/endian_test.cpp +++ b/src/utils/test/endian_test.cpp @@ -24,6 +24,13 @@ TEST(endian, write_and_read) ASSERT_EQ(100, data_input(data).read_u32()); } + { + std::string data; + data.resize(1); + data_output(data).write_u8(100); + ASSERT_EQ(100, data_input(data).read_u8()); + } + { std::string data; data.resize(1000 * 8);