parse hexadecimals as int args

This commit is contained in:
silverweed 2024-07-25 17:58:32 +02:00
parent 1e2f73ee18
commit 2c5471e05f
3 changed files with 31 additions and 9 deletions

View file

@ -33,6 +33,26 @@ internal
Conv_Res str_to_u64(String8 s)
{
Conv_Res res {};
if (s.size > 2 && s[0] == '0' && s[1] == 'x') {
// hexadecimal
for (u64 i = 2; i < s.size; ++i) {
u8 c = s.str[i];
if (c >= '0' && c <= '9') {
res.num *= 16;
res.num += c - '0';
} else if (c >= 'A' && c <= 'F') {
res.num *= 16;
res.num += 10 + c - 'A';
} else if (c >= 'a' && c <= 'f') {
res.num *= 16;
res.num += 10 + c - 'a';
} else {
res.error = true;
break;
}
}
} else {
// decimal
for (u64 i = 0; i < s.size; ++i) {
u8 c = s.str[i];
if (c >= '0' && c <= '9') {
@ -43,6 +63,7 @@ Conv_Res str_to_u64(String8 s)
break;
}
}
}
return res;
}

View file

@ -553,7 +553,7 @@ String8 render_range_to_string(Arena *arena, App_State &app, u64 len, u64 n_cols
u64 off = start + i;
/// Select color
u32 byte_col = mem_edit_bg_color_fn(data, off, &app);
u32 byte_col = mem_edit_bg_color_fn(data + start, i, &app);
// piggyback off mem_edit_bg_color_fn instead of rewriting the color-choosing logic.
// Kinda dumb code but we don't need speed here since we're printing this one-off.
Ansi_Color acol = viewer_to_ansi_color(app.viewer, viewer, byte_col);

View file

@ -3,6 +3,7 @@ struct String8 {
u64 size;
const char *c() const { return reinterpret_cast<const char *>(str); }
u8 operator[](u64 idx) const { return str[idx]; }
};
#define str8(s) String8 { (u8*)(s), sizeof(s) - 1 }