]> granicus.if.org Git - graphviz/commitdiff
fig plugin: replace 'fig_string' with 'gvputs_nonascii'
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 27 Nov 2021 22:03:47 +0000 (14:03 -0800)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 4 Dec 2021 06:11:33 +0000 (22:11 -0800)
This partially de-dupes some code (`mp_string` and `pic_string` are duplicates
of `fig_string`), makes this plugin more thread safe (no static buffer is no
longer used), and improves its efficiency (bytes are now written directly into
the output instead of a temporary buffer, requiring dynamic allocation and two
copies).

Related to #2051.

plugin/core/gvrender_core_fig.c

index 1df4db22429129181b315364734579f6f89cca7f..d6ee97f308df67f87ff71ba6074a88233b764bfc 100644 (file)
@@ -13,7 +13,6 @@
 #include <stdarg.h>
 #include <stdlib.h>
 #include <string.h>
-#include <ctype.h>
 
 #ifdef _WIN32
 #include <io.h>
@@ -52,44 +51,6 @@ static void figptarray(GVJ_t *job, pointf * A, int n, int close)
     gvputs(job, "\n");
 }
 
-static char *fig_string(char *s)
-{
-    static char *buf = NULL;
-    static size_t bufsize = 0;
-    size_t pos = 0;
-    char *p;
-    char c;
-
-    if (!buf) {
-        bufsize = 64;
-        buf = malloc(bufsize * sizeof(char));
-    }
-
-    p = buf;
-    while ((c = *s++)) {
-        if (pos > (bufsize - 8)) {
-            bufsize *= 2;
-            buf = realloc(buf, bufsize * sizeof(char));
-            p = buf + pos;
-        }
-        if (isascii(c)) {
-            if (c == '\\') {
-                *p++ = '\\';
-                pos++;
-            }
-            *p++ = c;
-            pos++;
-        } else {
-            *p++ = '\\';
-            sprintf(p, "%03o", (unsigned)c);
-            p += 3;
-            pos += 4;
-        }
-    }
-    *p = '\0';
-    return buf;
-}
-
 static int figColorResolve(int *new, unsigned char r, unsigned char g,
   unsigned char b)
 {
@@ -292,10 +253,11 @@ static void fig_textspan(GVJ_t * job, pointf p, textspan_t * span)
        $A      \\in    M_0$\001
 */
     gvprintf(job,
-            "%d %d %d %d %d %d %.1f %.4f %d %.1f %.1f %d %d %s\\001\n",
+            "%d %d %d %d %d %d %.1f %.4f %d %.1f %.1f %d %d ",
             object_code, sub_type, color, depth, pen_style, font,
-            font_size, angle, font_flags, height, length, ROUND(p.x), ROUND((p.y-72.0)),
-            fig_string(span->str));
+            font_size, angle, font_flags, height, length, ROUND(p.x), ROUND((p.y-72.0)));
+    gvputs_nonascii(job, span->str);
+    gvputs(job, "\\001\n");
 }
 
 static void fig_ellipse(GVJ_t * job, pointf * A, int filled)