]> granicus.if.org Git - graphviz/commitdiff
common colorxlate: replace a long lived buffer with an agxbuf
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Sun, 29 Jan 2023 01:39:17 +0000 (17:39 -0800)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Sun, 29 Jan 2023 02:02:17 +0000 (18:02 -0800)
This is not performance sensitive code. Using an agxbuf makes this logic clearer
and less error prone.

lib/common/colxlate.c

index c4e3d5b28b4d84a9b537952127b4cb209ecc9d67..59132ef52967e4a7376dba9c9b9a9b6f3dc16e75 100644 (file)
@@ -20,6 +20,7 @@
 #include <common/colorprocs.h>
 #include <common/colortbl.h>
 #include <common/memory.h>
+#include <cgraph/agxbuf.h>
 #include <cgraph/strcasecmp.h>
 #include <cgraph/unreachable.h>
 
@@ -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) */