src/highlighter.c

changeset 66
1b12cf799fee
parent 57
eba880c1705c
child 67
5da2cb5aea6b
--- a/src/highlighter.c	Thu Nov 10 18:44:48 2016 +0100
+++ b/src/highlighter.c	Mon Apr 24 20:54:38 2023 +0200
@@ -33,35 +33,35 @@
 #include <stdio.h>
 #include <string.h>
 #include <ctype.h>
-#include "ucx/string.h"
-#include "ucx/utils.h"
 
-static void put_htmlescaped(UcxBuffer *dest, char c) {
+#include <cx/string.h>
+
+static void put_htmlescaped(CxBuffer *dest, char c) {
     if (c == '>') {
-        ucx_buffer_puts(dest, "&gt;");
+        cxBufferPutString(dest, "&gt;");
     } else if (c == '<') {
-        ucx_buffer_puts(dest, "&lt;");
+        cxBufferPutString(dest, "&lt;");
     } else if (c) {
-        ucx_buffer_putc(dest, c);
+        cxBufferPut(dest, c);
     }
 }
 
-static void put_htmlescapedstr(UcxBuffer *dest, sstr_t s) {
+static void put_htmlescapedstr(CxBuffer *dest, cxstring s) {
     for (int i = 0 ; i < s.length ; i++) {
         put_htmlescaped(dest, s.ptr[i]);
     }
 }
 
-static int check_keyword(sstr_t word, const char** keywords) {
+static int check_keyword(cxstring word, const char** keywords) {
     for (int i = 0 ; keywords[i] ; i++) {
-        if (sstrcmp(word, sstr((char*)keywords[i])) == 0) {
+        if (cx_strcmp(word, cx_str(keywords[i])) == 0) {
             return 1;
         }
     }
     return 0;
 }
 
-static int check_capsonly(sstr_t word) {
+static int check_capsonly(cxstring word) {
     for (size_t i = 0 ; i < word.length ; i++) {
         if (!isupper(word.ptr[i]) && !isdigit(word.ptr[i])
                 && word.ptr[i] != '_') {
@@ -73,7 +73,7 @@
 
 /* Plaintext Highlighter */
 
-void c2html_plain_highlighter(char *src, UcxBuffer *dest,
+void c2html_plain_highlighter(char const *src, CxBuffer *dest,
         c2html_highlighter_data *hd) {
     while (*src && *src != '\n') {
         if (*src != '\r') {
@@ -81,7 +81,7 @@
         }
         src++;
     }
-    ucx_buffer_putc(dest, '\n');
+    cxBufferPut(dest, '\n');
 }
 
 /* C Highlighter */
@@ -94,15 +94,15 @@
     "while", NULL
 };
 
-void c2html_c_highlighter(char *src, UcxBuffer *dest,
+void c2html_c_highlighter(char const *src, CxBuffer *dest,
         c2html_highlighter_data *hd) {
     /* reset buffers without clearing them */
-    hd->primary_buffer->size = hd->primary_buffer->pos = 0;
-    hd->secondary_buffer->size = hd->secondary_buffer->pos = 0;
+    hd->primary_buffer.size = hd->primary_buffer.pos = 0;
+    hd->secondary_buffer.size = hd->secondary_buffer.pos = 0;
     
     /* alias the buffers for better handling */
-    UcxBuffer *wbuf = hd->primary_buffer;
-    UcxBuffer *ifilebuf = hd->secondary_buffer;
+    CxBuffer *wbuf = &hd->primary_buffer;
+    CxBuffer *ifilebuf = &hd->secondary_buffer;
     
     /* local information */
     size_t sp = (size_t)-1;
@@ -113,7 +113,7 @@
     /* continue a multi line comment highlighting */
     if (hd->multiline_comment) {
         iscomment = 1;
-        ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
+        cxBufferPutString(dest, "<span class=\"c2html-comment\">");
     }
 
     char c;
@@ -126,42 +126,42 @@
             if (hd->multiline_comment && sp > 0 && src[sp-1] == '*') {
                 iscomment = 0;
                 hd->multiline_comment = 0;
-                ucx_buffer_puts(dest, "/</span>");
+                cxBufferPutString(dest, "/</span>");
                 continue;
             } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
                 iscomment = 1;
                 hd->multiline_comment = (src[sp+1] == '*');
-                ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
+                cxBufferPutString(dest, "<span class=\"c2html-comment\">");
             }
         }
 
         if (iscomment) {
             if (c == '\n') {
-                ucx_buffer_puts(dest, "</span>\n");
+                cxBufferPutString(dest, "</span>\n");
             } else {
                 put_htmlescaped(dest, c);
             }
         } else if (isinclude) {
             if (c == '<') {
-                ucx_buffer_puts(dest,
+                cxBufferPutString(dest,
                         "<span class=\"c2html-stdinclude\">&lt;");
             } else if (c == '\"') {
                 if (parseinclude) {
-                    ucx_buffer_puts(dest, "\">");
-                    ucx_buffer_write(ifilebuf->space, 1, ifilebuf->size, dest);
-                    ucx_buffer_puts(dest, "\"</a>");
+                    cxBufferPutString(dest, "\">");
+                    cxBufferWrite(ifilebuf->space, 1, ifilebuf->size, dest);
+                    cxBufferPutString(dest, "\"</a>");
                     parseinclude = 0;
                 } else {
-                    ucx_buffer_puts(dest,
+                    cxBufferPutString(dest,
                             "<a class=\"c2html-userinclude\" href=\"");
-                    ucx_buffer_putc(ifilebuf, '\"');
+                    cxBufferPut(ifilebuf, '\"');
                     parseinclude = 1;
                 }
             } else if (c == '>') {
-                ucx_buffer_puts(dest,  "&gt;</span>");
+                cxBufferPutString(dest,  "&gt;</span>");
             } else {
                 if (parseinclude) {
-                    ucx_buffer_putc(ifilebuf, c);
+                    cxBufferPut(ifilebuf, c);
                 }
                 put_htmlescaped(dest, c);
             }
@@ -172,14 +172,14 @@
                     put_htmlescaped(dest, c);
                     if (c == quote) {
                         isstring = 0;
-                        ucx_buffer_puts(dest, "</span>");
+                        cxBufferPutString(dest, "</span>");
                     } else {
                         put_htmlescaped(dest, c);
                     }
                 } else {
                     isstring = 1;
                     quote = c;
-                    ucx_buffer_puts(dest, "<span class=\"c2html-string\">");
+                    cxBufferPutString(dest, "<span class=\"c2html-string\">");
                     put_htmlescaped(dest, c);
                 }
             } else {
@@ -187,32 +187,32 @@
                     put_htmlescaped(dest, c);
                 } else if (isalnum(c) ||  c == '_' || c == '#') {
                     /* buffer the current word */
-                    ucx_buffer_putc(wbuf, c);
+                    cxBufferPut(wbuf, c);
                 } else {
                     /* write buffered word, if any */
                     if (wbuf->size > 0) {
-                        sstr_t word = sstrn(wbuf->space, wbuf->size);
+                        cxstring word = cx_strn(wbuf->space, wbuf->size);
                         int closespan = 1;
-                        sstr_t typesuffix = ST("_t");
+                        cxstring typesuffix = CX_STR("_t");
                         if (check_keyword(word, ckeywords)) {
-                            ucx_buffer_puts(dest,
+                            cxBufferPutString(dest,
                                     "<span class=\"c2html-keyword\">");
-                        } else if (sstrsuffix(word, typesuffix)) {
-                            ucx_buffer_puts(dest,
+                        } else if (cx_strsuffix(word, typesuffix)) {
+                            cxBufferPutString(dest,
                                 "<span class=\"c2html-type\">");
                         } else if (word.ptr[0] == '#') {
-                            isinclude = !sstrcmp(word, S("#include"));
-                            ucx_buffer_puts(dest,
+                            isinclude = !cx_strcmp(word, CX_STR("#include"));
+                            cxBufferPutString(dest,
                                 "<span class=\"c2html-directive\">");
                         } else if (check_capsonly(word)) {
-                            ucx_buffer_puts(dest,
+                            cxBufferPutString(dest,
                                 "<span class=\"c2html-macroconst\">");
                         } else {
                             closespan = 0;
                         }
                         put_htmlescapedstr(dest, word);
                         if (closespan) {
-                            ucx_buffer_puts(dest, "</span>");
+                            cxBufferPutString(dest, "</span>");
                         }
                     }
                     wbuf->pos = wbuf->size = 0; /* reset word buffer */
@@ -239,14 +239,14 @@
     "volatile", "const", "float", "native", "super", "while", NULL
 };
 
-void c2html_java_highlighter(char *src, UcxBuffer *dest,
+void c2html_java_highlighter(char const *src, CxBuffer *dest,
         c2html_highlighter_data *hd) {
     /* reset buffers without clearing them */
-    hd->primary_buffer->size = hd->primary_buffer->pos = 0;
-    hd->secondary_buffer->size = hd->secondary_buffer->pos = 0;
+    hd->primary_buffer.size = hd->primary_buffer.pos = 0;
+    hd->secondary_buffer.size = hd->secondary_buffer.pos = 0;
 
     /* alias the buffers for better handling */
-    UcxBuffer *wbuf = hd->primary_buffer;
+    CxBuffer *wbuf = &hd->primary_buffer;
     
     /* local information */
     size_t sp = (size_t)-1;
@@ -256,7 +256,7 @@
 
     if (hd->multiline_comment) {
         iscomment = 1;
-        ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
+        cxBufferPutString(dest, "<span class=\"c2html-comment\">");
     }
 
     char c;
@@ -269,18 +269,18 @@
             if (hd->multiline_comment && sp > 0 && src[sp-1] == '*') {
                 iscomment = 0;
                 hd->multiline_comment = 0;
-                ucx_buffer_puts(dest, "/</span>");
+                cxBufferPutString(dest, "/</span>");
                 continue;
             } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
                 iscomment = 1;
                 hd->multiline_comment = (src[sp+1] == '*');
-                ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
+                cxBufferPutString(dest, "<span class=\"c2html-comment\">");
             }
         }
 
         if (iscomment) {
             if (c == '\n') {
-                ucx_buffer_puts(dest, "</span>\n");
+                cxBufferPutString(dest, "</span>\n");
             } else {
                 put_htmlescaped(dest, c);
             }
@@ -293,14 +293,14 @@
                     put_htmlescaped(dest, c);
                     if (c == quote) {
                         isstring = 0;
-                        ucx_buffer_puts(dest, "</span>");
+                        cxBufferPutString(dest, "</span>");
                     } else {
                         put_htmlescaped(dest, c);
                     }
                 } else {
                     isstring = 1;
                     quote = c;
-                    ucx_buffer_puts(dest,
+                    cxBufferPutString(dest,
                         "<span class=\"c2html-string\">");
                     put_htmlescaped(dest, c);
                 }
@@ -309,23 +309,23 @@
                     put_htmlescaped(dest, c);
                 } else if (isalnum(c) || c == '_' || c == '@') {
                     /* buffer the current word */
-                    ucx_buffer_putc(wbuf, c);
+                    cxBufferPut(wbuf, c);
                 } else {
                     /* write buffered word, if any */
                     if (wbuf->size > 0) {
-                        sstr_t word = sstrn(wbuf->space, wbuf->size);
+                        cxstring word = cx_strn(wbuf->space, wbuf->size);
                         int closespan = 1;
                         if (check_keyword(word, jkeywords)) {
-                            ucx_buffer_puts(dest,
+                            cxBufferPutString(dest,
                                 "<span class=\"c2html-keyword\">");
                         } else if (isupper(word.ptr[0])) {
-                            ucx_buffer_puts(dest,
+                            cxBufferPutString(dest,
                                 "<span class=\"c2html-type\">");
                         } else if (word.ptr[0] == '@') {
-                            ucx_buffer_puts(dest,
+                            cxBufferPutString(dest,
                                 "<span class=\"c2html-directive\">");
                         } else if (check_capsonly(word)) {
-                            ucx_buffer_puts(dest,
+                            cxBufferPutString(dest,
                                 "<span class=\"c2html-macroconst\">");
                         } else {
                             closespan = 0;
@@ -333,7 +333,7 @@
                         put_htmlescapedstr(dest, word);
                         
                         if (closespan) {
-                            ucx_buffer_puts(dest, "</span>");
+                            cxBufferPutString(dest, "</span>");
                         }
                     }
                     wbuf->pos = wbuf->size = 0; /* reset buffer */

mercurial