replaces stack buffers with UCX buffers

Thu, 25 Aug 2016 12:16:57 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 25 Aug 2016 12:16:57 +0200
changeset 51
f25ba6fd7a08
parent 50
17408c3607ce
child 52
33ded421c512

replaces stack buffers with UCX buffers

src/c2html.c file | annotate | diff | comparison | revisions
src/ccodegen.c file | annotate | diff | comparison | revisions
src/ccodegen.h file | annotate | diff | comparison | revisions
src/codegens.c file | annotate | diff | comparison | revisions
src/codegens.h file | annotate | diff | comparison | revisions
src/javacodegen.c file | annotate | diff | comparison | revisions
src/javacodegen.h file | annotate | diff | comparison | revisions
--- a/src/c2html.c	Thu Aug 25 11:30:30 2016 +0200
+++ b/src/c2html.c	Thu Aug 25 12:16:57 2016 +0200
@@ -45,7 +45,7 @@
         "\n");
 }
 
-static void plain_highlighter(char *src, UcxBuffer *dest, int* x) {
+static void plain_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd) {
     while (*src && *src != '\n') {
         put_htmlescaped(dest, *src);
         src++;
@@ -65,19 +65,17 @@
         while ((p*=10) < lines) lnw++;
     }
     
-    /* allocate line buffer */
-    UcxBuffer *line = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND);
-    if(!line) {
-        perror("Error allocating line buffer for output");
-        return;
-    }
-    
     /* start monospace formatting */
     out("<pre>\n", 1, 6, stream);
 
     /* process lines */
     size_t lineno = 0;
-    int multiline_comment = 0;
+    HighlighterData *hd = new_highlighter_data();
+    UcxBuffer *line = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND);
+    if(!line || !hd) {
+        perror("Error allocating buffer for output");
+        return;
+    }
     
     UCX_FOREACH(sourceline, in) {
         /* increase line number and clean line buffer */
@@ -92,7 +90,7 @@
         }
         
         /* process code line */
-        highlighter(sourceline->data, line, &multiline_comment);
+        highlighter(sourceline->data, line, hd);
         
         /* write code line */
         out(line->space, 1, line->size, stream);
@@ -102,6 +100,7 @@
     out("</pre>\n", 1, 7, stream);
     
     /* cleanup and return */
+    free_highlighter_data(hd);
     ucx_buffer_free(line);
 }
 
--- a/src/ccodegen.c	Thu Aug 25 11:30:30 2016 +0200
+++ b/src/ccodegen.c	Thu Aug 25 12:16:57 2016 +0200
@@ -37,24 +37,23 @@
     "while", NULL
 };
 
-void c_highlighter(char *src, UcxBuffer *dest, int *multiline_comment) {
-    /* TODO: try to replace these buffers */
-    char wordbuf[WORDBUF_SIZE];
-    sstr_t word;
-    word.ptr = wordbuf; word.length = 0;
+void c_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd) {
+    /* reset buffers without clearing them */
+    hd->primary_buffer->size = hd->primary_buffer->pos = 0;
+    hd->secondary_buffer->size = hd->secondary_buffer->pos = 0;
     
-    char includefilebuf[512];
-    sstr_t includefile;
-    includefile.ptr = includefilebuf;
-    includefile.length = 0;
+    /* alias the buffers for better handling */
+    UcxBuffer *wbuf = hd->primary_buffer;
+    UcxBuffer *ifilebuf = hd->secondary_buffer;
     
+    /* local information */
     size_t sp = (size_t)-1;
     int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0;
     char quote = '\0';
     int isescaping = 0;
     
     /* continue a multi line comment highlighting */
-    if (*multiline_comment) {
+    if (hd->multiline_comment) {
         iscomment = 1;
         ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
     }
@@ -66,14 +65,14 @@
         
         /* comments */
         if (!isstring && c == '/') {
-            if (*multiline_comment && sp > 0 && src[sp-1] == '*') {
+            if (hd->multiline_comment && sp > 0 && src[sp-1] == '*') {
                 iscomment = 0;
-                *multiline_comment = 0;
+                hd->multiline_comment = 0;
                 ucx_buffer_puts(dest, "/</span>");
                 continue;
             } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
                 iscomment = 1;
-                *multiline_comment = (src[sp+1] == '*');
+                hd->multiline_comment = (src[sp+1] == '*');
                 ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
             }
         }
@@ -90,21 +89,21 @@
                         "<span class=\"c2html-stdinclude\">&lt;");
             } else if (c == '\"') {
                 if (parseinclude) {
-                    ucx_bprintf(dest, "\">%.*s\"</a>",
-                            includefile.length, includefile.ptr);
+                    ucx_buffer_puts(dest, "\">");
+                    ucx_buffer_write(ifilebuf->space, 1, ifilebuf->size, dest);
+                    ucx_buffer_puts(dest, "\"</a>");
                     parseinclude = 0;
                 } else {
                     ucx_buffer_puts(dest,
                             "<a class=\"c2html-userinclude\" href=\"");
-                    includefile.length = 0;
-                    includefile.ptr[includefile.length++] = '\"';
+                    ucx_buffer_putc(ifilebuf, '\"');
                     parseinclude = 1;
                 }
             } else if (c == '>') {
                 ucx_buffer_puts(dest,  "&gt;</span>");
             } else {
                 if (parseinclude) {
-                    includefile.ptr[includefile.length++] = c;
+                    ucx_buffer_putc(ifilebuf, c);
                 }
                 put_htmlescaped(dest, c);
             }
@@ -128,8 +127,10 @@
             } else {
                 if (isstring) {
                     put_htmlescaped(dest, c);
-                } else if (!isalnum(c) && c!='_' && c!='#' && c!='@') {
-                    if (word.length > 0 && word.length < WORDBUF_SIZE) {
+                } else if (!isalnum(c) && c!='_' && c!='#') {
+                    /* write buffered word, if any */
+                    if (wbuf->size > 0) {
+                        sstr_t word = sstrn(wbuf->space, wbuf->size);
                         int closespan = 1;
                         sstr_t typesuffix = ST("_t");
                         if (check_keyword(word, ckeywords)) {
@@ -153,20 +154,13 @@
                             ucx_buffer_puts(dest, "</span>");
                         }
                     }
-                    word.length = 0;
+                    wbuf->pos = wbuf->size = 0; /* reset word buffer */
+                    
+                    /* write current character */
                     put_htmlescaped(dest, c);
                 } else {
-                    /* read word */
-                    if (word.length < WORDBUF_SIZE) {
-                        word.ptr[word.length++] = c;
-                    } else if (word.length == WORDBUF_SIZE) {
-                        /* TODO: this will be removed */
-                        ucx_buffer_puts(dest,
-                                "!!! WARNING - WORD TOO LONG TO PARSE !!!");
-                        word.length = 0;
-                    } else {
-                        put_htmlescaped(dest, c);
-                    }
+                    /* buffer the current word */
+                    ucx_buffer_putc(wbuf, c);
                 }
             }
 
--- a/src/ccodegen.h	Thu Aug 25 11:30:30 2016 +0200
+++ b/src/ccodegen.h	Thu Aug 25 12:16:57 2016 +0200
@@ -36,7 +36,7 @@
 extern "C" {
 #endif
 
-void c_highlighter(char *src, UcxBuffer *dest, int *mlc);
+void c_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd);
 
 #ifdef	__cplusplus
 }
--- a/src/codegens.c	Thu Aug 25 11:30:30 2016 +0200
+++ b/src/codegens.c	Thu Aug 25 12:16:57 2016 +0200
@@ -29,6 +29,24 @@
 
 #include "codegens.h"
 
+HighlighterData* new_highlighter_data() {
+    HighlighterData* hd = malloc(sizeof(HighlighterData));
+    if (hd == NULL) {
+        return NULL;
+    } else {
+        hd->multiline_comment = 0;
+        hd->primary_buffer = ucx_buffer_new(NULL, 256, UCX_BUFFER_AUTOEXTEND);
+        hd->secondary_buffer = ucx_buffer_new(NULL, 32, UCX_BUFFER_AUTOEXTEND);
+        return hd;
+    }
+}
+
+void free_highlighter_data(HighlighterData *hd) {
+    ucx_buffer_free(hd->primary_buffer);
+    ucx_buffer_free(hd->secondary_buffer);
+    free(hd);
+}
+
 void put_htmlescaped(UcxBuffer *dest, char c) {
     if (c == '>') {
         ucx_buffer_puts(dest, "&gt;");
--- a/src/codegens.h	Thu Aug 25 11:30:30 2016 +0200
+++ b/src/codegens.h	Thu Aug 25 12:16:57 2016 +0200
@@ -41,10 +41,17 @@
 #ifdef	__cplusplus
 extern "C" {
 #endif
+    
+typedef struct {
+    int multiline_comment;
+    UcxBuffer* primary_buffer;
+    UcxBuffer* secondary_buffer;
+} HighlighterData;
 
-#define WORDBUF_SIZE 256
+HighlighterData* new_highlighter_data();
+void free_highlighter_data(HighlighterData*);
 
-typedef void(*highlighter_func)(char*,UcxBuffer*,int*);
+typedef void(*highlighter_func)(char*,UcxBuffer*,HighlighterData*);
 
 void put_htmlescaped(UcxBuffer *dest, char c);
 void put_htmlescapedstr(UcxBuffer *dest, sstr_t s);
--- a/src/javacodegen.c	Thu Aug 25 11:30:30 2016 +0200
+++ b/src/javacodegen.c	Thu Aug 25 12:16:57 2016 +0200
@@ -39,19 +39,21 @@
     "volatile", "const", "float", "native", "super", "while", NULL
 };
 
-void java_highlighter(char *src, UcxBuffer *dest, int *multiline_comment) {
+void java_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd) {
+    /* reset buffers without clearing them */
+    hd->primary_buffer->size = hd->primary_buffer->pos = 0;
+    hd->secondary_buffer->size = hd->secondary_buffer->pos = 0;
 
-    /* TODO: try to replace this buffer */
-    char wordbuf[WORDBUF_SIZE];
-    sstr_t word;
-    word.ptr = wordbuf; word.length = 0;
+    /* alias the buffers for better handling */
+    UcxBuffer *wbuf = hd->primary_buffer;
     
+    /* local information */
     size_t sp = (size_t)-1;
     int isstring = 0, iscomment = 0, isimport = 0;
     char quote = '\0';
     int isescaping = 0;
 
-    if (*multiline_comment) {
+    if (hd->multiline_comment) {
         iscomment = 1;
         ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
     }
@@ -63,14 +65,14 @@
         
         /* comments */
         if (!isstring && c == '/') {
-            if (*multiline_comment && sp > 0 && src[sp-1] == '*') {
+            if (hd->multiline_comment && sp > 0 && src[sp-1] == '*') {
                 iscomment = 0;
-                *multiline_comment = 0;
+                hd->multiline_comment = 0;
                 ucx_buffer_puts(dest, "/</span>");
                 continue;
             } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
                 iscomment = 1;
-                *multiline_comment = (src[sp+1] == '*');
+                hd->multiline_comment = (src[sp+1] == '*');
                 ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
             }
         }
@@ -104,8 +106,10 @@
             } else {
                 if (isstring) {
                     put_htmlescaped(dest, c);
-                } else if (!isalnum(c) && c!='_' && c!='#' && c!='@') {
-                    if (word.length > 0 && word.length < WORDBUF_SIZE) {
+                } else if (!isalnum(c) && c!='_' && c!='@') {
+                    /* write buffered word, if any */
+                    if (wbuf->size > 0) {
+                        sstr_t word = sstrn(wbuf->space, wbuf->size);
                         int closespan = 1;
                         if (check_keyword(word, jkeywords)) {
                             ucx_buffer_puts(dest,
@@ -128,20 +132,13 @@
                             ucx_buffer_puts(dest, "</span>");
                         }
                     }
-                    word.length = 0;
+                    wbuf->pos = wbuf->size = 0; /* reset buffer */
+                    
+                    /* write current character */
                     put_htmlescaped(dest, c);
                 } else {
-                    /* read word */
-                    if (word.length < WORDBUF_SIZE) {
-                        word.ptr[word.length++] = c;
-                    } else if (word.length == WORDBUF_SIZE) {
-                        /* TODO: this will be removed */
-                        ucx_buffer_puts(dest,
-                                "!!! WARNING - WORD TOO LONG TO PARSE !!!");
-                        word.length = 0;
-                    } else {
-                        put_htmlescaped(dest, c);
-                    }
+                    /* buffer the current word */
+                    ucx_buffer_putc(wbuf, c);
                 }
             }
 
--- a/src/javacodegen.h	Thu Aug 25 11:30:30 2016 +0200
+++ b/src/javacodegen.h	Thu Aug 25 12:16:57 2016 +0200
@@ -36,7 +36,7 @@
 extern "C" {
 #endif
 
-void java_highlighter(char *src, UcxBuffer *dest, int *mlc);
+void java_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd);
 
 #ifdef	__cplusplus
 }

mercurial