From 97e714fba078e8e6b8d8d990468f542e1ad62984 Mon Sep 17 00:00:00 2001 From: SEK1RO Date: Fri, 13 Sep 2024 13:38:26 +0300 Subject: [PATCH] fix(hex): rewrited on string_view --- src/hex.cpp | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/hex.cpp b/src/hex.cpp index cbe0cf3..3e94368 100644 --- a/src/hex.cpp +++ b/src/hex.cpp @@ -36,8 +36,8 @@ namespace hex } void encode(const uint8_t *data, uint64_t data_size, char *str) noexcept { - uint64_t i = 0; - while (str[i] != '\0' && i / 2 < data_size) + std::string_view sv(str); + for(size_t i = 0; i < sv.size() && i / 2 < data_size; i++) { if (i % 2 == 0) { @@ -45,7 +45,6 @@ namespace hex } else { str[i] = hexdigits[data[i / 2] & 0x0F]; } - i++; } } std::string encode(const std::vector &data) noexcept @@ -60,15 +59,14 @@ namespace hex { throw std::logic_error("hex::decode: out of digits map"); } - uint64_t i = 0; - while (i < data_size && str[i * 2] != '\0') + std::string_view sv(str); + if (sv.size() % 2 != 0) + { + throw std::logic_error("hex::decode: isn't hex"); + } + for (size_t i = 0; i < data_size && i * 2 < sv.size(); i++) { - if(str[i + 1] == '\0') - { - throw std::logic_error("hex::decode: isn't hex"); - } data[i] = hexmap[(int8_t)str[i * 2]] << 4 | hexmap[(int8_t)str[i * 2 + 1]]; - i++; } } std::vector decode(const std::string &str)