Commit: 47bf2148cdb71c7fd1ac9bfc15f8b19faf432227
Parent: ae1ae3723092a19514d797b2eaffff77f7c104e7
Author: Randy Palamar
Date: Wed, 9 Oct 2024 20:31:12 -0600
make parse_hex_u32 operate on s8s
Therefore we no longer need to worry about NUL terminating our
input buffer
Diffstat:
3 files changed, 27 insertions(+), 27 deletions(-)
diff --git a/colourpicker.c b/colourpicker.c
@@ -11,7 +11,7 @@ static s8 mode_labels[CM_LAST][4] = {
};
static void
-mem_move(char *src, char *dest, size n)
+mem_move(u8 *src, u8 *dest, size n)
{
if (dest < src) while (n) { *dest++ = *src++; n--; }
else while (n) { n--; dest[n] = src[n]; }
@@ -277,7 +277,7 @@ parse_and_store_text_input(ColourPickerCtx *ctx)
if (ctx->is.idx == -1) {
return;
} else if (ctx->is.idx == INPUT_HEX) {
- new_colour = normalize_colour(parse_hex_u32(ctx->is.buf));
+ new_colour = normalize_colour(parse_hex_u32(input));
new_mode = CM_RGB;
} else {
new_mode = ctx->colour_mode;
@@ -305,7 +305,7 @@ set_text_input_idx(ColourPickerCtx *ctx, enum input_indices idx, Rect r, v2 mous
if (idx == INPUT_HEX) {
Color hc = colour_from_normalized(get_formatted_colour(ctx, CM_RGB));
- ctx->is.buf_len = snprintf(ctx->is.buf, ARRAY_COUNT(ctx->is.buf),
+ ctx->is.buf_len = snprintf((char *)ctx->is.buf, ARRAY_COUNT(ctx->is.buf),
"%02x%02x%02x%02x", hc.r, hc.g, hc.b, hc.a);
} else {
f32 fv = 0;
@@ -316,7 +316,7 @@ set_text_input_idx(ColourPickerCtx *ctx, enum input_indices idx, Rect r, v2 mous
case INPUT_A: fv = ctx->colour.a; break;
default: break;
}
- ctx->is.buf_len = snprintf(ctx->is.buf, ARRAY_COUNT(ctx->is.buf), "%0.02f", fv);
+ ctx->is.buf_len = snprintf((char *)ctx->is.buf, ARRAY_COUNT(ctx->is.buf), "%0.02f", fv);
}
ctx->is.idx = idx;
@@ -338,7 +338,7 @@ do_text_input(ColourPickerCtx *ctx, Rect r, Color colour, i32 max_disp_chars)
i32 buf_delta = ctx->is.buf_len - max_disp_chars;
if (buf_delta < 0) buf_delta = 0;
- s8 buf = {.len = ctx->is.buf_len - buf_delta, .data =ctx->is.buf + buf_delta};
+ s8 buf = {.len = ctx->is.buf_len - buf_delta, .data = ctx->is.buf + buf_delta};
{
/* NOTE: drop a char if the subtext still doesn't fit */
v2 nts = measure_text(ctx->font, buf);
@@ -394,10 +394,8 @@ do_text_input(ColourPickerCtx *ctx, Rect r, Color colour, i32 max_disp_chars)
/* NOTE: handle multiple input keys on a single frame */
i32 key = GetCharPressed();
while (key > 0) {
- if (ctx->is.buf_len == (ARRAY_COUNT(ctx->is.buf) - 1)) {
- ctx->is.buf[ARRAY_COUNT(ctx->is.buf) - 1] = 0;
+ if (ctx->is.buf_len == ARRAY_COUNT(ctx->is.buf))
break;
- }
mem_move(ctx->is.buf + ctx->is.cursor,
ctx->is.buf + ctx->is.cursor + 1,
@@ -553,7 +551,7 @@ do_slider(ColourPickerCtx *ctx, Rect r, i32 label_idx, v2 relative_mouse)
set_text_input_idx(ctx, label_idx + 1, vr, relative_mouse);
if (ctx->is.idx != (label_idx + 1)) {
- s8 value = {.len = 4, .data = (char *)TextFormat("%0.02f", current)};
+ s8 value = {.len = 4, .data = (u8 *)TextFormat("%0.02f", current)};
draw_text(ctx->font, value, left_align_text_in_rect(vr, value, ctx->font), colour_rl);
} else {
do_text_input(ctx, vr, colour_rl, 4);
@@ -587,7 +585,7 @@ do_status_bar(ColourPickerCtx *ctx, Rect r, v2 relative_mouse)
if (mouse_mask & MOUSE_RIGHT) step_colour_mode(ctx, -1);
Color hc = colour_from_normalized(get_formatted_colour(ctx, CM_RGB));
- s8 hex = {.len = 8, .data = (char *)TextFormat("%02x%02x%02x%02x", hc.r, hc.g, hc.b, hc.a)};
+ s8 hex = {.len = 8, .data = (u8 *)TextFormat("%02x%02x%02x%02x", hc.r, hc.g, hc.b, hc.a)};
s8 label = s8("RGB: ");
v2 label_size = measure_text(ctx->font, label);
@@ -605,7 +603,7 @@ do_status_bar(ColourPickerCtx *ctx, Rect r, v2 relative_mouse)
IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
set_text_input_idx(ctx, -1, hex_r, relative_mouse);
hc = colour_from_normalized(get_formatted_colour(ctx, CM_RGB));
- hex.data = (char *)TextFormat("%02x%02x%02x%02x", hc.r, hc.g, hc.b, hc.a);
+ hex.data = (u8 *)TextFormat("%02x%02x%02x%02x", hc.r, hc.g, hc.b, hc.a);
}
if (hex_collides && IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
@@ -1230,8 +1228,8 @@ do_colour_picker(ColourPickerCtx *ctx, f32 dt, Vector2 window_pos, Vector2 mouse
btn_r.pos.y += 0.54 * mb.size.h;
if (do_text_button(ctx, ctx->buttons + 1, ctx->mouse_pos, btn_r, s8("Paste"), fg, bg)) {
- char *txt = (char *)GetClipboardText();
- if (txt) {
+ s8 txt = cstr_to_s8((char *)GetClipboardText());
+ if (txt.len) {
v4 new_colour = normalize_colour(parse_hex_u32(txt));
store_formatted_colour(ctx, new_colour, CM_RGB);
if (ctx->mode == CPM_PICKER) {
diff --git a/main.c b/main.c
@@ -130,7 +130,7 @@ main(i32 argc, char *argv[])
switch (argv[i][1]) {
case 'h':
- rgb = normalize_colour(parse_hex_u32(argv[i + 1]));
+ rgb = normalize_colour(parse_hex_u32(cstr_to_s8(argv[i + 1])));
ctx.colour = rgb_to_hsv(rgb);
break;
case 'r': rgb.r = parse_f64(cstr_to_s8(argv[i + 1])); CLAMP01(rgb.r); break;
diff --git a/util.c b/util.c
@@ -33,8 +33,8 @@ typedef float f32;
typedef double f64;
typedef ptrdiff_t size;
-typedef struct { size len; char *data; } s8;
-#define s8(s) (s8){.len = sizeof(s) - 1, .data = s}
+typedef struct { size len; u8 *data; } s8;
+#define s8(s) (s8){.len = sizeof(s) - 1, .data = (u8 *)s}
typedef union {
struct { u32 w, h; };
@@ -157,7 +157,7 @@ typedef struct {
f32 cursor_t;
f32 cursor_t_target;
i32 buf_len;
- char buf[64];
+ u8 buf[64];
} InputState;
#ifdef _DEBUG
@@ -341,22 +341,24 @@ pack_rl_colour(Color colour)
}
static u32
-parse_hex_u32(char *s)
+parse_hex_u32(s8 s)
{
u32 res = 0;
/* NOTE: skip over '0x' or '0X' */
- if (*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))
- s += 2;
+ if (s.len > 2 && s.data[0] == '0' && (s.data[1] == 'x' || s.data[1] == 'X')) {
+ s.data += 2;
+ s.len -= 2;
+ }
- for (; *s; s++) {
+ for (; s.len > 0; s.len--, s.data++) {
res <<= 4;
- if (ISDIGIT(*s)) {
- res |= *s - '0';
- } else if (ISHEX(*s)) {
+ if (ISDIGIT(*s.data)) {
+ res |= *s.data - '0';
+ } else if (ISHEX(*s.data)) {
/* NOTE: convert to lowercase first then convert to value */
- *s |= 0x20;
- res |= *s - 0x57;
+ *s.data |= 0x20;
+ res |= *s.data - 0x57;
} else {
/* NOTE: do nothing (treat invalid value as 0) */
}
@@ -398,7 +400,7 @@ parse_f64(s8 s)
static s8
cstr_to_s8(char *s)
{
- s8 result = {.data = s};
+ s8 result = {.data = (u8 *)s};
if (s) while (*s) { result.len++; s++; }
return result;
}