src/ccodegen.c

changeset 29
ec6e97454e64
parent 28
1be8ea902ef4
child 31
50ae611a785c
--- a/src/ccodegen.c	Sat Apr 25 19:01:16 2015 +0200
+++ b/src/ccodegen.c	Sat Apr 25 19:14:57 2015 +0200
@@ -47,6 +47,9 @@
     return (word[0] == '#');
 }
 
+#define memcpy_const(darr,doff,str) memcpy(&(darr[doff]), str, sizeof(str)-1); \
+                                    dp += sizeof(str)-1
+
 void cparseline(char *src, char *dest, highlighter_t *hltr) {
     size_t sp = 0, dp = 0;
     /* indent */
@@ -62,8 +65,7 @@
 
     if (hltr->iscommentml) {
         iscomment = 1;
-        memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29);
-        dp += 29;
+        memcpy_const(dest, dp, "<span class=\"c2html-comment\">");
     }
 
     for (char c = src[sp] ; c ; c=src[++sp]) {
@@ -72,27 +74,23 @@
             if (hltr->iscommentml && sp > 0 && src[sp-1] == '*') {
                 iscomment = 0;
                 hltr->iscommentml = 0;
-                memcpy(&(dest[dp]), "/</span>", 8);
-                dp += 8;
+                memcpy_const(dest, dp, "/</span>");
                 continue;
             } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
                 iscomment = 1;
                 hltr->iscommentml = (src[sp+1] == '*');
-                memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29);
-                dp += 29;
+                memcpy_const(dest, dp, "<span class=\"c2html-comment\">");
             }
         }
 
         if (iscomment) {
             if (c == '\n') {
-                memcpy(&(dest[dp]), "</span>", 7);
-                dp += 7;
+                memcpy_const(dest, dp,  "</span>");
             }
             dp = writeescapedchar(dest, dp, c);
         } else if (isinclude) {
             if (c == '<') {
-                memcpy(&(dest[dp]), "<span class=\"c2html-stdinclude\">", 32);
-                dp += 32;
+                memcpy_const(dest, dp, "<span class=\"c2html-stdinclude\">");
                 dp = writeescapedchar(dest, dp, c);
             } else if (c == '\"') {
                 if (parseinclude) {
@@ -102,13 +100,11 @@
                     dp += ifp;
 
                     dp = writeescapedchar(dest, dp, c);
-                    memcpy(&(dest[dp]), "</a>", 4);
-                    dp += 4;
+                    memcpy_const(dest, dp, "</a>");
                     parseinclude = 0;
                 } else {
-                    memcpy(&(dest[dp]),
-                        "<a class=\"c2html-userinclude\" href=", 35);
-                    dp += 35;
+                    memcpy_const(dest, dp,
+                        "<a class=\"c2html-userinclude\" href=");
                     dp = writeescapedchar(dest, dp, c);
                     ifp = 0;
                     hltr->includefile[ifp++] = '\"';
@@ -116,8 +112,7 @@
                 }
             } else if (c == '>') {
                 dp = writeescapedchar(dest, dp, c);
-                memcpy(&(dest[dp]), "</span>", 7);
-                dp += 7;
+                memcpy_const(dest, dp, "</span>");
             } else {
                 if (parseinclude) {
                     hltr->includefile[ifp++] = c;
@@ -128,20 +123,19 @@
             /* strings */
             if (!isescaping && (c == '\'' || c == '\"')) {
                 if (isstring) {
+                    dp = writeescapedchar(dest, dp, c);
                     if (c == quote) {
                         isstring = 0;
-                        memcpy(&(dest[dp]), "<span class=\"c2html-string\">", 28);
-                        dp += 28;
-                        dp = writeescapedchar(dest, dp, c);
+                        memcpy_const(dest, dp, "</span>");
                     } else {
                         dp = writeescapedchar(dest, dp, c);
                     }
                 } else {
                     isstring = 1;
                     quote = c;
+                    memcpy_const(dest, dp,
+                        "<span class=\"c2html-string\">");
                     dp = writeescapedchar(dest, dp, c);
-                    memcpy(&(dest[dp]), "</span>", 7);
-                    dp += 7;
                 }
             } else {
                 if (isstring) {
@@ -151,23 +145,19 @@
                     if (wp > 0 && wp < WORDBUF_SIZE) {
                         int closespan = 1;
                         if (iskeyword(hltr->word, hltr->keywords)) {
-                            memcpy(&(dest[dp]),
-                                "<span class=\"c2html-keyword\">", 29);
-                            dp += 29;
+                            memcpy_const(dest, dp, 
+                                "<span class=\"c2html-keyword\">");
                         } else if (hltr->istype(hltr->word, wp)) {
-                            memcpy(&(dest[dp]),
-                                "<span class=\"c2html-type\">", 26);
-                            dp += 26;
+                            memcpy_const(dest, dp, 
+                                "<span class=\"c2html-type\">");
                         } else if (hltr->isdirective(hltr->word)) {
                             isinclude = !strncmp(
                                 "#include", hltr->word, WORDBUF_SIZE);
-                            memcpy(&(dest[dp]),
-                                "<span class=\"c2html-directive\">", 31);
-                            dp += 31;
+                            memcpy_const(dest, dp, 
+                                "<span class=\"c2html-directive\">");
                         } else if (iscapsonly(hltr->word, wp)) {
-                            memcpy(&(dest[dp]),
-                                "<span class=\"c2html-macroconst\">", 32);
-                            dp += 32;
+                            memcpy_const(dest, dp, 
+                                "<span class=\"c2html-macroconst\">");
                         } else {
                             closespan = 0;
                         }
@@ -175,8 +165,7 @@
                             dp = writeescapedchar(dest, dp, hltr->word[i]);
                         }
                         if (closespan) {
-                            memcpy(&(dest[dp]), "</span>", 7);
-                            dp += 7;
+                            memcpy_const(dest, dp, "</span>");
                         }
                     }
                     memset(hltr->word, 0, WORDBUF_SIZE);

mercurial