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,14 +33,35 @@ internal
Conv_Res str_to_u64(String8 s) Conv_Res str_to_u64(String8 s)
{ {
Conv_Res res {}; Conv_Res res {};
for (u64 i = 0; i < s.size; ++i) { if (s.size > 2 && s[0] == '0' && s[1] == 'x') {
u8 c = s.str[i]; // hexadecimal
if (c >= '0' && c <= '9') { for (u64 i = 2; i < s.size; ++i) {
res.num *= 10; u8 c = s.str[i];
res.num += c - '0'; if (c >= '0' && c <= '9') {
} else { res.num *= 16;
res.error = true; res.num += c - '0';
break; } 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') {
res.num *= 10;
res.num += c - '0';
} else {
res.error = true;
break;
}
} }
} }
return res; 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; u64 off = start + i;
/// Select color /// 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. // 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. // 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); Ansi_Color acol = viewer_to_ansi_color(app.viewer, viewer, byte_col);

View file

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