1.1 --- a/src/ccodegen.c Sat Apr 25 19:01:16 2015 +0200 1.2 +++ b/src/ccodegen.c Sat Apr 25 19:14:57 2015 +0200 1.3 @@ -47,6 +47,9 @@ 1.4 return (word[0] == '#'); 1.5 } 1.6 1.7 +#define memcpy_const(darr,doff,str) memcpy(&(darr[doff]), str, sizeof(str)-1); \ 1.8 + dp += sizeof(str)-1 1.9 + 1.10 void cparseline(char *src, char *dest, highlighter_t *hltr) { 1.11 size_t sp = 0, dp = 0; 1.12 /* indent */ 1.13 @@ -62,8 +65,7 @@ 1.14 1.15 if (hltr->iscommentml) { 1.16 iscomment = 1; 1.17 - memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29); 1.18 - dp += 29; 1.19 + memcpy_const(dest, dp, "<span class=\"c2html-comment\">"); 1.20 } 1.21 1.22 for (char c = src[sp] ; c ; c=src[++sp]) { 1.23 @@ -72,27 +74,23 @@ 1.24 if (hltr->iscommentml && sp > 0 && src[sp-1] == '*') { 1.25 iscomment = 0; 1.26 hltr->iscommentml = 0; 1.27 - memcpy(&(dest[dp]), "/</span>", 8); 1.28 - dp += 8; 1.29 + memcpy_const(dest, dp, "/</span>"); 1.30 continue; 1.31 } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { 1.32 iscomment = 1; 1.33 hltr->iscommentml = (src[sp+1] == '*'); 1.34 - memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29); 1.35 - dp += 29; 1.36 + memcpy_const(dest, dp, "<span class=\"c2html-comment\">"); 1.37 } 1.38 } 1.39 1.40 if (iscomment) { 1.41 if (c == '\n') { 1.42 - memcpy(&(dest[dp]), "</span>", 7); 1.43 - dp += 7; 1.44 + memcpy_const(dest, dp, "</span>"); 1.45 } 1.46 dp = writeescapedchar(dest, dp, c); 1.47 } else if (isinclude) { 1.48 if (c == '<') { 1.49 - memcpy(&(dest[dp]), "<span class=\"c2html-stdinclude\">", 32); 1.50 - dp += 32; 1.51 + memcpy_const(dest, dp, "<span class=\"c2html-stdinclude\">"); 1.52 dp = writeescapedchar(dest, dp, c); 1.53 } else if (c == '\"') { 1.54 if (parseinclude) { 1.55 @@ -102,13 +100,11 @@ 1.56 dp += ifp; 1.57 1.58 dp = writeescapedchar(dest, dp, c); 1.59 - memcpy(&(dest[dp]), "</a>", 4); 1.60 - dp += 4; 1.61 + memcpy_const(dest, dp, "</a>"); 1.62 parseinclude = 0; 1.63 } else { 1.64 - memcpy(&(dest[dp]), 1.65 - "<a class=\"c2html-userinclude\" href=", 35); 1.66 - dp += 35; 1.67 + memcpy_const(dest, dp, 1.68 + "<a class=\"c2html-userinclude\" href="); 1.69 dp = writeescapedchar(dest, dp, c); 1.70 ifp = 0; 1.71 hltr->includefile[ifp++] = '\"'; 1.72 @@ -116,8 +112,7 @@ 1.73 } 1.74 } else if (c == '>') { 1.75 dp = writeescapedchar(dest, dp, c); 1.76 - memcpy(&(dest[dp]), "</span>", 7); 1.77 - dp += 7; 1.78 + memcpy_const(dest, dp, "</span>"); 1.79 } else { 1.80 if (parseinclude) { 1.81 hltr->includefile[ifp++] = c; 1.82 @@ -128,20 +123,19 @@ 1.83 /* strings */ 1.84 if (!isescaping && (c == '\'' || c == '\"')) { 1.85 if (isstring) { 1.86 + dp = writeescapedchar(dest, dp, c); 1.87 if (c == quote) { 1.88 isstring = 0; 1.89 - memcpy(&(dest[dp]), "<span class=\"c2html-string\">", 28); 1.90 - dp += 28; 1.91 - dp = writeescapedchar(dest, dp, c); 1.92 + memcpy_const(dest, dp, "</span>"); 1.93 } else { 1.94 dp = writeescapedchar(dest, dp, c); 1.95 } 1.96 } else { 1.97 isstring = 1; 1.98 quote = c; 1.99 + memcpy_const(dest, dp, 1.100 + "<span class=\"c2html-string\">"); 1.101 dp = writeescapedchar(dest, dp, c); 1.102 - memcpy(&(dest[dp]), "</span>", 7); 1.103 - dp += 7; 1.104 } 1.105 } else { 1.106 if (isstring) { 1.107 @@ -151,23 +145,19 @@ 1.108 if (wp > 0 && wp < WORDBUF_SIZE) { 1.109 int closespan = 1; 1.110 if (iskeyword(hltr->word, hltr->keywords)) { 1.111 - memcpy(&(dest[dp]), 1.112 - "<span class=\"c2html-keyword\">", 29); 1.113 - dp += 29; 1.114 + memcpy_const(dest, dp, 1.115 + "<span class=\"c2html-keyword\">"); 1.116 } else if (hltr->istype(hltr->word, wp)) { 1.117 - memcpy(&(dest[dp]), 1.118 - "<span class=\"c2html-type\">", 26); 1.119 - dp += 26; 1.120 + memcpy_const(dest, dp, 1.121 + "<span class=\"c2html-type\">"); 1.122 } else if (hltr->isdirective(hltr->word)) { 1.123 isinclude = !strncmp( 1.124 "#include", hltr->word, WORDBUF_SIZE); 1.125 - memcpy(&(dest[dp]), 1.126 - "<span class=\"c2html-directive\">", 31); 1.127 - dp += 31; 1.128 + memcpy_const(dest, dp, 1.129 + "<span class=\"c2html-directive\">"); 1.130 } else if (iscapsonly(hltr->word, wp)) { 1.131 - memcpy(&(dest[dp]), 1.132 - "<span class=\"c2html-macroconst\">", 32); 1.133 - dp += 32; 1.134 + memcpy_const(dest, dp, 1.135 + "<span class=\"c2html-macroconst\">"); 1.136 } else { 1.137 closespan = 0; 1.138 } 1.139 @@ -175,8 +165,7 @@ 1.140 dp = writeescapedchar(dest, dp, hltr->word[i]); 1.141 } 1.142 if (closespan) { 1.143 - memcpy(&(dest[dp]), "</span>", 7); 1.144 - dp += 7; 1.145 + memcpy_const(dest, dp, "</span>"); 1.146 } 1.147 } 1.148 memset(hltr->word, 0, WORDBUF_SIZE);