src/highlighter.c

changeset 66
1b12cf799fee
parent 57
eba880c1705c
child 67
5da2cb5aea6b
     1.1 --- a/src/highlighter.c	Thu Nov 10 18:44:48 2016 +0100
     1.2 +++ b/src/highlighter.c	Mon Apr 24 20:54:38 2023 +0200
     1.3 @@ -33,35 +33,35 @@
     1.4  #include <stdio.h>
     1.5  #include <string.h>
     1.6  #include <ctype.h>
     1.7 -#include "ucx/string.h"
     1.8 -#include "ucx/utils.h"
     1.9  
    1.10 -static void put_htmlescaped(UcxBuffer *dest, char c) {
    1.11 +#include <cx/string.h>
    1.12 +
    1.13 +static void put_htmlescaped(CxBuffer *dest, char c) {
    1.14      if (c == '>') {
    1.15 -        ucx_buffer_puts(dest, "&gt;");
    1.16 +        cxBufferPutString(dest, "&gt;");
    1.17      } else if (c == '<') {
    1.18 -        ucx_buffer_puts(dest, "&lt;");
    1.19 +        cxBufferPutString(dest, "&lt;");
    1.20      } else if (c) {
    1.21 -        ucx_buffer_putc(dest, c);
    1.22 +        cxBufferPut(dest, c);
    1.23      }
    1.24  }
    1.25  
    1.26 -static void put_htmlescapedstr(UcxBuffer *dest, sstr_t s) {
    1.27 +static void put_htmlescapedstr(CxBuffer *dest, cxstring s) {
    1.28      for (int i = 0 ; i < s.length ; i++) {
    1.29          put_htmlescaped(dest, s.ptr[i]);
    1.30      }
    1.31  }
    1.32  
    1.33 -static int check_keyword(sstr_t word, const char** keywords) {
    1.34 +static int check_keyword(cxstring word, const char** keywords) {
    1.35      for (int i = 0 ; keywords[i] ; i++) {
    1.36 -        if (sstrcmp(word, sstr((char*)keywords[i])) == 0) {
    1.37 +        if (cx_strcmp(word, cx_str(keywords[i])) == 0) {
    1.38              return 1;
    1.39          }
    1.40      }
    1.41      return 0;
    1.42  }
    1.43  
    1.44 -static int check_capsonly(sstr_t word) {
    1.45 +static int check_capsonly(cxstring word) {
    1.46      for (size_t i = 0 ; i < word.length ; i++) {
    1.47          if (!isupper(word.ptr[i]) && !isdigit(word.ptr[i])
    1.48                  && word.ptr[i] != '_') {
    1.49 @@ -73,7 +73,7 @@
    1.50  
    1.51  /* Plaintext Highlighter */
    1.52  
    1.53 -void c2html_plain_highlighter(char *src, UcxBuffer *dest,
    1.54 +void c2html_plain_highlighter(char const *src, CxBuffer *dest,
    1.55          c2html_highlighter_data *hd) {
    1.56      while (*src && *src != '\n') {
    1.57          if (*src != '\r') {
    1.58 @@ -81,7 +81,7 @@
    1.59          }
    1.60          src++;
    1.61      }
    1.62 -    ucx_buffer_putc(dest, '\n');
    1.63 +    cxBufferPut(dest, '\n');
    1.64  }
    1.65  
    1.66  /* C Highlighter */
    1.67 @@ -94,15 +94,15 @@
    1.68      "while", NULL
    1.69  };
    1.70  
    1.71 -void c2html_c_highlighter(char *src, UcxBuffer *dest,
    1.72 +void c2html_c_highlighter(char const *src, CxBuffer *dest,
    1.73          c2html_highlighter_data *hd) {
    1.74      /* reset buffers without clearing them */
    1.75 -    hd->primary_buffer->size = hd->primary_buffer->pos = 0;
    1.76 -    hd->secondary_buffer->size = hd->secondary_buffer->pos = 0;
    1.77 +    hd->primary_buffer.size = hd->primary_buffer.pos = 0;
    1.78 +    hd->secondary_buffer.size = hd->secondary_buffer.pos = 0;
    1.79      
    1.80      /* alias the buffers for better handling */
    1.81 -    UcxBuffer *wbuf = hd->primary_buffer;
    1.82 -    UcxBuffer *ifilebuf = hd->secondary_buffer;
    1.83 +    CxBuffer *wbuf = &hd->primary_buffer;
    1.84 +    CxBuffer *ifilebuf = &hd->secondary_buffer;
    1.85      
    1.86      /* local information */
    1.87      size_t sp = (size_t)-1;
    1.88 @@ -113,7 +113,7 @@
    1.89      /* continue a multi line comment highlighting */
    1.90      if (hd->multiline_comment) {
    1.91          iscomment = 1;
    1.92 -        ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
    1.93 +        cxBufferPutString(dest, "<span class=\"c2html-comment\">");
    1.94      }
    1.95  
    1.96      char c;
    1.97 @@ -126,42 +126,42 @@
    1.98              if (hd->multiline_comment && sp > 0 && src[sp-1] == '*') {
    1.99                  iscomment = 0;
   1.100                  hd->multiline_comment = 0;
   1.101 -                ucx_buffer_puts(dest, "/</span>");
   1.102 +                cxBufferPutString(dest, "/</span>");
   1.103                  continue;
   1.104              } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
   1.105                  iscomment = 1;
   1.106                  hd->multiline_comment = (src[sp+1] == '*');
   1.107 -                ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
   1.108 +                cxBufferPutString(dest, "<span class=\"c2html-comment\">");
   1.109              }
   1.110          }
   1.111  
   1.112          if (iscomment) {
   1.113              if (c == '\n') {
   1.114 -                ucx_buffer_puts(dest, "</span>\n");
   1.115 +                cxBufferPutString(dest, "</span>\n");
   1.116              } else {
   1.117                  put_htmlescaped(dest, c);
   1.118              }
   1.119          } else if (isinclude) {
   1.120              if (c == '<') {
   1.121 -                ucx_buffer_puts(dest,
   1.122 +                cxBufferPutString(dest,
   1.123                          "<span class=\"c2html-stdinclude\">&lt;");
   1.124              } else if (c == '\"') {
   1.125                  if (parseinclude) {
   1.126 -                    ucx_buffer_puts(dest, "\">");
   1.127 -                    ucx_buffer_write(ifilebuf->space, 1, ifilebuf->size, dest);
   1.128 -                    ucx_buffer_puts(dest, "\"</a>");
   1.129 +                    cxBufferPutString(dest, "\">");
   1.130 +                    cxBufferWrite(ifilebuf->space, 1, ifilebuf->size, dest);
   1.131 +                    cxBufferPutString(dest, "\"</a>");
   1.132                      parseinclude = 0;
   1.133                  } else {
   1.134 -                    ucx_buffer_puts(dest,
   1.135 +                    cxBufferPutString(dest,
   1.136                              "<a class=\"c2html-userinclude\" href=\"");
   1.137 -                    ucx_buffer_putc(ifilebuf, '\"');
   1.138 +                    cxBufferPut(ifilebuf, '\"');
   1.139                      parseinclude = 1;
   1.140                  }
   1.141              } else if (c == '>') {
   1.142 -                ucx_buffer_puts(dest,  "&gt;</span>");
   1.143 +                cxBufferPutString(dest,  "&gt;</span>");
   1.144              } else {
   1.145                  if (parseinclude) {
   1.146 -                    ucx_buffer_putc(ifilebuf, c);
   1.147 +                    cxBufferPut(ifilebuf, c);
   1.148                  }
   1.149                  put_htmlescaped(dest, c);
   1.150              }
   1.151 @@ -172,14 +172,14 @@
   1.152                      put_htmlescaped(dest, c);
   1.153                      if (c == quote) {
   1.154                          isstring = 0;
   1.155 -                        ucx_buffer_puts(dest, "</span>");
   1.156 +                        cxBufferPutString(dest, "</span>");
   1.157                      } else {
   1.158                          put_htmlescaped(dest, c);
   1.159                      }
   1.160                  } else {
   1.161                      isstring = 1;
   1.162                      quote = c;
   1.163 -                    ucx_buffer_puts(dest, "<span class=\"c2html-string\">");
   1.164 +                    cxBufferPutString(dest, "<span class=\"c2html-string\">");
   1.165                      put_htmlescaped(dest, c);
   1.166                  }
   1.167              } else {
   1.168 @@ -187,32 +187,32 @@
   1.169                      put_htmlescaped(dest, c);
   1.170                  } else if (isalnum(c) ||  c == '_' || c == '#') {
   1.171                      /* buffer the current word */
   1.172 -                    ucx_buffer_putc(wbuf, c);
   1.173 +                    cxBufferPut(wbuf, c);
   1.174                  } else {
   1.175                      /* write buffered word, if any */
   1.176                      if (wbuf->size > 0) {
   1.177 -                        sstr_t word = sstrn(wbuf->space, wbuf->size);
   1.178 +                        cxstring word = cx_strn(wbuf->space, wbuf->size);
   1.179                          int closespan = 1;
   1.180 -                        sstr_t typesuffix = ST("_t");
   1.181 +                        cxstring typesuffix = CX_STR("_t");
   1.182                          if (check_keyword(word, ckeywords)) {
   1.183 -                            ucx_buffer_puts(dest,
   1.184 +                            cxBufferPutString(dest,
   1.185                                      "<span class=\"c2html-keyword\">");
   1.186 -                        } else if (sstrsuffix(word, typesuffix)) {
   1.187 -                            ucx_buffer_puts(dest,
   1.188 +                        } else if (cx_strsuffix(word, typesuffix)) {
   1.189 +                            cxBufferPutString(dest,
   1.190                                  "<span class=\"c2html-type\">");
   1.191                          } else if (word.ptr[0] == '#') {
   1.192 -                            isinclude = !sstrcmp(word, S("#include"));
   1.193 -                            ucx_buffer_puts(dest,
   1.194 +                            isinclude = !cx_strcmp(word, CX_STR("#include"));
   1.195 +                            cxBufferPutString(dest,
   1.196                                  "<span class=\"c2html-directive\">");
   1.197                          } else if (check_capsonly(word)) {
   1.198 -                            ucx_buffer_puts(dest,
   1.199 +                            cxBufferPutString(dest,
   1.200                                  "<span class=\"c2html-macroconst\">");
   1.201                          } else {
   1.202                              closespan = 0;
   1.203                          }
   1.204                          put_htmlescapedstr(dest, word);
   1.205                          if (closespan) {
   1.206 -                            ucx_buffer_puts(dest, "</span>");
   1.207 +                            cxBufferPutString(dest, "</span>");
   1.208                          }
   1.209                      }
   1.210                      wbuf->pos = wbuf->size = 0; /* reset word buffer */
   1.211 @@ -239,14 +239,14 @@
   1.212      "volatile", "const", "float", "native", "super", "while", NULL
   1.213  };
   1.214  
   1.215 -void c2html_java_highlighter(char *src, UcxBuffer *dest,
   1.216 +void c2html_java_highlighter(char const *src, CxBuffer *dest,
   1.217          c2html_highlighter_data *hd) {
   1.218      /* reset buffers without clearing them */
   1.219 -    hd->primary_buffer->size = hd->primary_buffer->pos = 0;
   1.220 -    hd->secondary_buffer->size = hd->secondary_buffer->pos = 0;
   1.221 +    hd->primary_buffer.size = hd->primary_buffer.pos = 0;
   1.222 +    hd->secondary_buffer.size = hd->secondary_buffer.pos = 0;
   1.223  
   1.224      /* alias the buffers for better handling */
   1.225 -    UcxBuffer *wbuf = hd->primary_buffer;
   1.226 +    CxBuffer *wbuf = &hd->primary_buffer;
   1.227      
   1.228      /* local information */
   1.229      size_t sp = (size_t)-1;
   1.230 @@ -256,7 +256,7 @@
   1.231  
   1.232      if (hd->multiline_comment) {
   1.233          iscomment = 1;
   1.234 -        ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
   1.235 +        cxBufferPutString(dest, "<span class=\"c2html-comment\">");
   1.236      }
   1.237  
   1.238      char c;
   1.239 @@ -269,18 +269,18 @@
   1.240              if (hd->multiline_comment && sp > 0 && src[sp-1] == '*') {
   1.241                  iscomment = 0;
   1.242                  hd->multiline_comment = 0;
   1.243 -                ucx_buffer_puts(dest, "/</span>");
   1.244 +                cxBufferPutString(dest, "/</span>");
   1.245                  continue;
   1.246              } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
   1.247                  iscomment = 1;
   1.248                  hd->multiline_comment = (src[sp+1] == '*');
   1.249 -                ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
   1.250 +                cxBufferPutString(dest, "<span class=\"c2html-comment\">");
   1.251              }
   1.252          }
   1.253  
   1.254          if (iscomment) {
   1.255              if (c == '\n') {
   1.256 -                ucx_buffer_puts(dest, "</span>\n");
   1.257 +                cxBufferPutString(dest, "</span>\n");
   1.258              } else {
   1.259                  put_htmlescaped(dest, c);
   1.260              }
   1.261 @@ -293,14 +293,14 @@
   1.262                      put_htmlescaped(dest, c);
   1.263                      if (c == quote) {
   1.264                          isstring = 0;
   1.265 -                        ucx_buffer_puts(dest, "</span>");
   1.266 +                        cxBufferPutString(dest, "</span>");
   1.267                      } else {
   1.268                          put_htmlescaped(dest, c);
   1.269                      }
   1.270                  } else {
   1.271                      isstring = 1;
   1.272                      quote = c;
   1.273 -                    ucx_buffer_puts(dest,
   1.274 +                    cxBufferPutString(dest,
   1.275                          "<span class=\"c2html-string\">");
   1.276                      put_htmlescaped(dest, c);
   1.277                  }
   1.278 @@ -309,23 +309,23 @@
   1.279                      put_htmlescaped(dest, c);
   1.280                  } else if (isalnum(c) || c == '_' || c == '@') {
   1.281                      /* buffer the current word */
   1.282 -                    ucx_buffer_putc(wbuf, c);
   1.283 +                    cxBufferPut(wbuf, c);
   1.284                  } else {
   1.285                      /* write buffered word, if any */
   1.286                      if (wbuf->size > 0) {
   1.287 -                        sstr_t word = sstrn(wbuf->space, wbuf->size);
   1.288 +                        cxstring word = cx_strn(wbuf->space, wbuf->size);
   1.289                          int closespan = 1;
   1.290                          if (check_keyword(word, jkeywords)) {
   1.291 -                            ucx_buffer_puts(dest,
   1.292 +                            cxBufferPutString(dest,
   1.293                                  "<span class=\"c2html-keyword\">");
   1.294                          } else if (isupper(word.ptr[0])) {
   1.295 -                            ucx_buffer_puts(dest,
   1.296 +                            cxBufferPutString(dest,
   1.297                                  "<span class=\"c2html-type\">");
   1.298                          } else if (word.ptr[0] == '@') {
   1.299 -                            ucx_buffer_puts(dest,
   1.300 +                            cxBufferPutString(dest,
   1.301                                  "<span class=\"c2html-directive\">");
   1.302                          } else if (check_capsonly(word)) {
   1.303 -                            ucx_buffer_puts(dest,
   1.304 +                            cxBufferPutString(dest,
   1.305                                  "<span class=\"c2html-macroconst\">");
   1.306                          } else {
   1.307                              closespan = 0;
   1.308 @@ -333,7 +333,7 @@
   1.309                          put_htmlescapedstr(dest, word);
   1.310                          
   1.311                          if (closespan) {
   1.312 -                            ucx_buffer_puts(dest, "</span>");
   1.313 +                            cxBufferPutString(dest, "</span>");
   1.314                          }
   1.315                      }
   1.316                      wbuf->pos = wbuf->size = 0; /* reset buffer */

mercurial