1.1 --- a/src/ccodegen.c Thu Aug 25 11:30:30 2016 +0200 1.2 +++ b/src/ccodegen.c Thu Aug 25 12:16:57 2016 +0200 1.3 @@ -37,24 +37,23 @@ 1.4 "while", NULL 1.5 }; 1.6 1.7 -void c_highlighter(char *src, UcxBuffer *dest, int *multiline_comment) { 1.8 - /* TODO: try to replace these buffers */ 1.9 - char wordbuf[WORDBUF_SIZE]; 1.10 - sstr_t word; 1.11 - word.ptr = wordbuf; word.length = 0; 1.12 +void c_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd) { 1.13 + /* reset buffers without clearing them */ 1.14 + hd->primary_buffer->size = hd->primary_buffer->pos = 0; 1.15 + hd->secondary_buffer->size = hd->secondary_buffer->pos = 0; 1.16 1.17 - char includefilebuf[512]; 1.18 - sstr_t includefile; 1.19 - includefile.ptr = includefilebuf; 1.20 - includefile.length = 0; 1.21 + /* alias the buffers for better handling */ 1.22 + UcxBuffer *wbuf = hd->primary_buffer; 1.23 + UcxBuffer *ifilebuf = hd->secondary_buffer; 1.24 1.25 + /* local information */ 1.26 size_t sp = (size_t)-1; 1.27 int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0; 1.28 char quote = '\0'; 1.29 int isescaping = 0; 1.30 1.31 /* continue a multi line comment highlighting */ 1.32 - if (*multiline_comment) { 1.33 + if (hd->multiline_comment) { 1.34 iscomment = 1; 1.35 ucx_buffer_puts(dest, "<span class=\"c2html-comment\">"); 1.36 } 1.37 @@ -66,14 +65,14 @@ 1.38 1.39 /* comments */ 1.40 if (!isstring && c == '/') { 1.41 - if (*multiline_comment && sp > 0 && src[sp-1] == '*') { 1.42 + if (hd->multiline_comment && sp > 0 && src[sp-1] == '*') { 1.43 iscomment = 0; 1.44 - *multiline_comment = 0; 1.45 + hd->multiline_comment = 0; 1.46 ucx_buffer_puts(dest, "/</span>"); 1.47 continue; 1.48 } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { 1.49 iscomment = 1; 1.50 - *multiline_comment = (src[sp+1] == '*'); 1.51 + hd->multiline_comment = (src[sp+1] == '*'); 1.52 ucx_buffer_puts(dest, "<span class=\"c2html-comment\">"); 1.53 } 1.54 } 1.55 @@ -90,21 +89,21 @@ 1.56 "<span class=\"c2html-stdinclude\"><"); 1.57 } else if (c == '\"') { 1.58 if (parseinclude) { 1.59 - ucx_bprintf(dest, "\">%.*s\"</a>", 1.60 - includefile.length, includefile.ptr); 1.61 + ucx_buffer_puts(dest, "\">"); 1.62 + ucx_buffer_write(ifilebuf->space, 1, ifilebuf->size, dest); 1.63 + ucx_buffer_puts(dest, "\"</a>"); 1.64 parseinclude = 0; 1.65 } else { 1.66 ucx_buffer_puts(dest, 1.67 "<a class=\"c2html-userinclude\" href=\""); 1.68 - includefile.length = 0; 1.69 - includefile.ptr[includefile.length++] = '\"'; 1.70 + ucx_buffer_putc(ifilebuf, '\"'); 1.71 parseinclude = 1; 1.72 } 1.73 } else if (c == '>') { 1.74 ucx_buffer_puts(dest, "></span>"); 1.75 } else { 1.76 if (parseinclude) { 1.77 - includefile.ptr[includefile.length++] = c; 1.78 + ucx_buffer_putc(ifilebuf, c); 1.79 } 1.80 put_htmlescaped(dest, c); 1.81 } 1.82 @@ -128,8 +127,10 @@ 1.83 } else { 1.84 if (isstring) { 1.85 put_htmlescaped(dest, c); 1.86 - } else if (!isalnum(c) && c!='_' && c!='#' && c!='@') { 1.87 - if (word.length > 0 && word.length < WORDBUF_SIZE) { 1.88 + } else if (!isalnum(c) && c!='_' && c!='#') { 1.89 + /* write buffered word, if any */ 1.90 + if (wbuf->size > 0) { 1.91 + sstr_t word = sstrn(wbuf->space, wbuf->size); 1.92 int closespan = 1; 1.93 sstr_t typesuffix = ST("_t"); 1.94 if (check_keyword(word, ckeywords)) { 1.95 @@ -153,20 +154,13 @@ 1.96 ucx_buffer_puts(dest, "</span>"); 1.97 } 1.98 } 1.99 - word.length = 0; 1.100 + wbuf->pos = wbuf->size = 0; /* reset word buffer */ 1.101 + 1.102 + /* write current character */ 1.103 put_htmlescaped(dest, c); 1.104 } else { 1.105 - /* read word */ 1.106 - if (word.length < WORDBUF_SIZE) { 1.107 - word.ptr[word.length++] = c; 1.108 - } else if (word.length == WORDBUF_SIZE) { 1.109 - /* TODO: this will be removed */ 1.110 - ucx_buffer_puts(dest, 1.111 - "!!! WARNING - WORD TOO LONG TO PARSE !!!"); 1.112 - word.length = 0; 1.113 - } else { 1.114 - put_htmlescaped(dest, c); 1.115 - } 1.116 + /* buffer the current word */ 1.117 + ucx_buffer_putc(wbuf, c); 1.118 } 1.119 } 1.120