From e72494ab114623dc6ea263d9a75ac04a67fe25c1 Mon Sep 17 00:00:00 2001 From: Nikita Skovoroda Date: Mon, 2 Feb 2026 10:20:41 +0300 Subject: [PATCH 1/3] src: use arithmetic HexEncode instead of lookups --- src/nbytes.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/nbytes.cpp b/src/nbytes.cpp index a827809..4556ee7 100644 --- a/src/nbytes.cpp +++ b/src/nbytes.cpp @@ -157,6 +157,10 @@ const int8_t unhex_table[256] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; +inline char nibble(uint8_t x) { + return x + '0' + ((x > 9) * 39); +} + size_t HexEncode(const char *src, size_t slen, char *dst, size_t dlen) { // We know how much we'll write, just make sure that there's space. NBYTES_ASSERT_TRUE(dlen >= MultiplyWithOverflowCheck(slen, 2u) && @@ -164,10 +168,9 @@ size_t HexEncode(const char *src, size_t slen, char *dst, size_t dlen) { dlen = slen * 2; for (size_t i = 0, k = 0; k < dlen; i += 1, k += 2) { - static const char hex[] = "0123456789abcdef"; uint8_t val = static_cast(src[i]); - dst[k + 0] = hex[val >> 4]; - dst[k + 1] = hex[val & 15]; + dst[k + 0] = nibble(val >> 4); + dst[k + 1] = nibble(val & 15); } return dlen; From 8cb0b72a6f454259bd4b278a7bf1222076ef8702 Mon Sep 17 00:00:00 2001 From: Nikita Skovoroda Date: Mon, 2 Feb 2026 10:25:46 +0300 Subject: [PATCH 2/3] fixup!: lint --- src/nbytes.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/nbytes.cpp b/src/nbytes.cpp index 4556ee7..44bd8e5 100644 --- a/src/nbytes.cpp +++ b/src/nbytes.cpp @@ -157,9 +157,7 @@ const int8_t unhex_table[256] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; -inline char nibble(uint8_t x) { - return x + '0' + ((x > 9) * 39); -} +inline char nibble(uint8_t x) { return x + '0' + ((x > 9) * 39); } size_t HexEncode(const char *src, size_t slen, char *dst, size_t dlen) { // We know how much we'll write, just make sure that there's space. From e65cb9e486e721d4b0387c1e18ff8f511f7c5643 Mon Sep 17 00:00:00 2001 From: Nikita Skovoroda Date: Mon, 2 Feb 2026 19:54:06 +0300 Subject: [PATCH 3/3] fixup!: constexpr Co-authored-by: Yagiz Nizipli --- src/nbytes.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/nbytes.cpp b/src/nbytes.cpp index 44bd8e5..3f2b662 100644 --- a/src/nbytes.cpp +++ b/src/nbytes.cpp @@ -157,7 +157,9 @@ const int8_t unhex_table[256] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; -inline char nibble(uint8_t x) { return x + '0' + ((x > 9) * 39); } +inline constexpr char nibble(uint8_t x) { + return x + '0' + ((x > 9) * 39); +} size_t HexEncode(const char *src, size_t slen, char *dst, size_t dlen) { // We know how much we'll write, just make sure that there's space.