From: Matthew Fernandez Date: Sun, 29 Jan 2023 01:39:17 +0000 (-0800) Subject: common colorxlate: replace a long lived buffer with an agxbuf X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=68da76f477c2329712cf09a68aac5e5d9a4633a9;p=graphviz common colorxlate: replace a long lived buffer with an agxbuf This is not performance sensitive code. Using an agxbuf makes this logic clearer and less error prone. --- diff --git a/lib/common/colxlate.c b/lib/common/colxlate.c index c4e3d5b28..59132ef52 100644 --- a/lib/common/colxlate.c +++ b/lib/common/colxlate.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -240,15 +241,12 @@ static char* resolveColor (char* str) int colorxlate(char *str, gvcolor_t * color, color_type_t target_type) { static hsvrgbacolor_t *last; - static char *canon; - static size_t allocated; - char *p, *q; + char *p; hsvrgbacolor_t fake; char c; double H, S, V, A, R, G, B; double C, M, Y, K; unsigned int r, g, b, a; - size_t len; int rc; color->type = target_type; @@ -313,21 +311,13 @@ int colorxlate(char *str, gvcolor_t * color, color_type_t target_type) /* test for hsv value such as: ".6,.5,.3" */ if ((c = *p) == '.' || isdigit(c)) { - len = strlen(p); - if (len >= allocated) { - allocated = len + 1 + 10; - canon = grealloc(canon, allocated); - } - q = canon; + agxbuf canon = {0}; while ((c = *p++)) { - if (c == ',') - c = ' '; - *q++ = c; + agxbputc(&canon, c == ',' ? ' ' : c); } - *q = '\0'; A = 1.0; // default - if (sscanf(canon, "%lf%lf%lf%lf", &H, &S, &V, &A) >= 3) { + if (sscanf(agxbuse(&canon), "%lf%lf%lf%lf", &H, &S, &V, &A) >= 3) { /* clip to reasonable values */ H = fmax(fmin(H, 1.0), 0.0); S = fmax(fmin(S, 1.0), 0.0); @@ -376,8 +366,10 @@ int colorxlate(char *str, gvcolor_t * color, color_type_t target_type) default: UNREACHABLE(); } + agxbfree(&canon); return rc; } + agxbfree(&canon); } /* test for known color name (generic, not renderer specific known names) */