-
-
Notifications
You must be signed in to change notification settings - Fork 6
fix: use arithmetic HexEncode instead of lookups #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -157,17 +157,18 @@ 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); } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a comment to this function and explain what it does?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that if you express it as I did above, it does not require an explanation anymore.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @anonrig is this still needed with the rewritten function? |
||
|
|
||
| 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<size_t>(slen, 2u) && | ||
| "not enough space provided for hex encode"); | ||
|
|
||
| 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<uint8_t>(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; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.