Add hex encoding/decoding

This commit is contained in:
2026-08-11 23:36:48 +02:00
parent 141220aecb
commit 86d2648cca
3 changed files with 113 additions and 1 deletions
+28
View File
@@ -533,6 +533,33 @@ void string_test_utf8(void)
TRACEF("str4:\n"); TRACEF("str4:\n");
print_runes(str4); // this should be a valid code point print_runes(str4); // this should be a valid code point
}
void string_test_hex(void)
{
MArena arena;
U8 data[4096];
marena_init(&arena, data, sizeof data);
Allocator allocator = marena_allocator(&arena);
Str str1 = S("Hello World ☺");
Str str1_encoded = str_hex_encode_alloc(&allocator, str1);
printf("str1_encoded: '%.*s'\n", str1_encoded.len, str1_encoded.str);
Str str1_decoded = str_hex_decode_alloc(&allocator, str1_encoded);
printf("str1_decoded: '%.*s'\n", str1_decoded.len, str1_decoded.str);
Str str2 = S("");
Str str2_encoded = str_hex_encode_alloc(&allocator, str2);
printf("str2_encoded: '%.*s'\n", str2_encoded.len, str2_encoded.str);
Str str2_decoded = str_hex_decode_alloc(&allocator, str2_encoded);
printf("str2_decoded: '%.*s'\n", str2_decoded.len, str2_decoded.str);
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
@@ -552,6 +579,7 @@ int main(int argc, char *argv[])
string_test_misc(); string_test_misc();
string_test_join(); string_test_join();
string_test_utf8(); string_test_utf8();
string_test_hex();
} }
+52
View File
@@ -93,6 +93,58 @@ S64 str_index(Str haystack, Str needle)
return -1; return -1;
} }
static const char hex_chars[] = "0123456789ABCDEF";
Str str_hex_encode(Str source, Str destination)
{
Assert(destination.len >= source.len * 2);
U32 hex_len = source.len * 2;
for (U32 i = 0, t = 0; i < source.len; ++i,t+=2) {
U8 high_nibble;
U8 low_nibble;
high_nibble = (source.str[i] & 0xF0) >> 4 ;
low_nibble = source.str[i] & 0x0F ;
destination.str[t] = hex_chars[high_nibble];
destination.str[t+1] = hex_chars[low_nibble];
}
destination.len = hex_len;
return destination;
}
Str str_hex_decode(Str source, Str destination)
{
Assert(destination.len >= source.len/2);
U32 dest_pos = 0;
U32 source_pos = 0;
while (source_pos + 1 < source.len) {
if (char_is_ascii_whitespace(source.str[source_pos])) {
source_pos++;
continue;
}
S16 high_nibble;
S16 low_nibble;
high_nibble = char_hex_to_binary(source.str[source_pos]);
low_nibble = char_hex_to_binary(source.str[source_pos + 1]);
if (high_nibble == -1 || low_nibble == -1) {
destination.len = 0;
return destination;
}
destination.str[dest_pos] = (high_nibble << 4 ) | low_nibble;
source_pos += 2;
dest_pos++;
}
destination.len = dest_pos;
return destination;
}
// split @str on any byte in @separators, place split strings in @vec // split @str on any byte in @separators, place split strings in @vec
void str_split(Str str, Str separators, StrVec *vec) void str_split(Str str, Str separators, StrVec *vec)
{ {
+33 -1
View File
@@ -91,7 +91,29 @@ static INLINE Str str_skip(Str str, U32 len)
return new_str; return new_str;
} }
// Destination must be at least source.len * 2. Returns destination.
Str str_hex_encode(Str source, Str destination);
// Destination must be at least source.len / 2. Returns destination.
Str str_hex_decode(Str source, Str destination);
static INLINE Str str_hex_encode_alloc(Allocator *allocator, Str str)
{
Assert(str.len <= U32_Max / 2);
U8 hex_len = str.len * 2;
U8 *result = allocator_push_array(allocator, U8, hex_len);
Str destination = str_create(result, hex_len);
return str_hex_encode(str, destination);
}
static INLINE Str str_hex_decode_alloc(Allocator *allocator, Str str)
{
U8 binary_len = str.len / 2;
U8 *result = allocator_push_array(allocator, U8, binary_len);
Str destination = str_create(result, binary_len);
return str_hex_decode(str, destination);
}
// String Vector // String Vector
typedef struct StrVec StrVec; typedef struct StrVec StrVec;
struct StrVec { struct StrVec {
@@ -113,11 +135,11 @@ void str_split(Str str, Str separators, StrVec *vec);
Str str_join(Allocator *allocator, StrVec strings, Str separator); Str str_join(Allocator *allocator, StrVec strings, Str separator);
// Characters
static INLINE bool char_is_ascii_whitespace(U8 byte) static INLINE bool char_is_ascii_whitespace(U8 byte)
{ {
return byte == ' ' || byte == '\t' || byte == '\v' || byte == '\f' || byte == '\r' || byte == '\n'; return byte == ' ' || byte == '\t' || byte == '\v' || byte == '\f' || byte == '\r' || byte == '\n';
} }
static INLINE bool char_is_digit(U8 byte) static INLINE bool char_is_digit(U8 byte)
@@ -125,7 +147,17 @@ static INLINE bool char_is_digit(U8 byte)
return byte >= '0' && byte <= '9'; return byte >= '0' && byte <= '9';
} }
static INLINE S16 char_hex_to_binary(U8 c)
{
if (c >='0' && c <='9')
return c - '0';
else if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
else if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
return -1;
}
// UTF-8/Unicode // UTF-8/Unicode