]> granicus.if.org Git - graphviz/commitdiff
remove intermediate string construction altogether in dot_polygon
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 6 Mar 2021 02:16:17 +0000 (18:16 -0800)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Tue, 16 Mar 2021 02:25:47 +0000 (19:25 -0700)
There's no need for this and we can achieve it in one (well two) shot.

The better way to do this would be with an in-memory expanding buffer (like
open_memstream), but this change is at least an improvement over the previous
code.

cmd/gvmap/make_map.c

index 4e2ed2631ab4f85c0c345c31971650d59f58845d..4382cb9a667910e28120bd4d82bb2dabaafb9cfb 100644 (file)
@@ -313,8 +313,7 @@ static void dot_polygon(char **sbuff, int *len, int *len_max, int np, float *xp,
                        int fill, int close, char *cstring){
   int i;
   int ret = 0;
-  char swidth[10000];
-  size_t len_swidth;
+  size_t len_swidth = 0;
 
   if (np > 0){
     /* figure out the size needed */
@@ -324,8 +323,8 @@ static void dot_polygon(char **sbuff, int *len, int *len_max, int np, float *xp,
       assert(line_width >= 0);
       if (line_width > 0){
        len_swidth = (size_t)snprintf(NULL, 0, "%f", line_width);
-       sprintf(swidth,"S %zu -setlinewidth(%f)",len_swidth+14, line_width);
-       ret += snprintf(NULL, 0, " c %zu -%s %s L %d ", strlen(cstring), cstring, swidth, np);
+       ret += snprintf(NULL, 0, " c %zu -%s S %zu -setlinewidth(%f) L %d ",
+                       strlen(cstring), cstring, len_swidth + 14, line_width, np);
       } else {
        ret += snprintf(NULL, 0, " c %zu -%s L %d ", strlen(cstring), cstring, np);
       }
@@ -345,7 +344,8 @@ static void dot_polygon(char **sbuff, int *len, int *len_max, int np, float *xp,
       ret = sprintf(&((*sbuff)[*len]), " c %zu -%s C %zu -%s P %d ", strlen(cstring), cstring, strlen(cstring), cstring, np);
     } else {
       if (line_width > 0){
-       ret = sprintf(&((*sbuff)[*len]), " c %zu -%s %s L %d ", strlen(cstring), cstring, swidth, np);
+       ret = sprintf(&((*sbuff)[*len]), " c %zu -%s S %zu -setlinewidth(%f) L %d ",
+                     strlen(cstring), cstring, len_swidth + 14, line_width, np);
       } else {
        ret = sprintf(&((*sbuff)[*len]), " c %zu -%s L %d ", strlen(cstring), cstring, np);
       }