Sat, 25 Apr 2015 19:14:57 +0200
introduced macro for constant string memcpy + fixed string highlight fix
src/ccodegen.c | file | annotate | diff | comparison | revisions | |
src/javacodegen.c | file | annotate | diff | comparison | revisions |
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);
2.1 --- a/src/javacodegen.c Sat Apr 25 19:01:16 2015 +0200 2.2 +++ b/src/javacodegen.c Sat Apr 25 19:14:57 2015 +0200 2.3 @@ -49,6 +49,9 @@ 2.4 return word[0] == '@'; 2.5 } 2.6 2.7 +#define memcpy_const(darr,doff,str) memcpy(&(darr[doff]), str, sizeof(str)-1); \ 2.8 + dp += sizeof(str)-1 2.9 + 2.10 void jparseline(char *src, char *dest, highlighter_t *hltr) { 2.11 size_t sp = 0, dp = 0; 2.12 /* indent */ 2.13 @@ -64,8 +67,7 @@ 2.14 2.15 if (hltr->iscommentml) { 2.16 iscomment = 1; 2.17 - memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29); 2.18 - dp += 29; 2.19 + memcpy_const(dest, dp, "<span class=\"c2html-comment\">"); 2.20 } 2.21 2.22 for (char c = src[sp] ; c ; c=src[++sp]) { 2.23 @@ -74,14 +76,12 @@ 2.24 if (hltr->iscommentml && sp > 0 && src[sp-1] == '*') { 2.25 iscomment = 0; 2.26 hltr->iscommentml = 0; 2.27 - memcpy(&(dest[dp]), "/</span>", 8); 2.28 - dp += 8; 2.29 + memcpy_const(dest, dp, "/</span>"); 2.30 continue; 2.31 } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { 2.32 iscomment = 1; 2.33 hltr->iscommentml = (src[sp+1] == '*'); 2.34 - memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29); 2.35 - dp += 29; 2.36 + memcpy_const(dest, dp, "<span class=\"c2html-comment\">"); 2.37 } 2.38 } 2.39 2.40 @@ -97,20 +97,19 @@ 2.41 /* strings */ 2.42 if (!isescaping && (c == '\'' || c == '\"')) { 2.43 if (isstring) { 2.44 + dp = writeescapedchar(dest, dp, c); 2.45 if (c == quote) { 2.46 isstring = 0; 2.47 - memcpy(&(dest[dp]), "<span class=\"c2html-string\">", 28); 2.48 - dp += 28; 2.49 - dp = writeescapedchar(dest, dp, c); 2.50 + memcpy_const(dest, dp, "</span>"); 2.51 } else { 2.52 dp = writeescapedchar(dest, dp, c); 2.53 } 2.54 } else { 2.55 isstring = 1; 2.56 quote = c; 2.57 + memcpy_const(dest, dp, 2.58 + "<span class=\"c2html-string\">"); 2.59 dp = writeescapedchar(dest, dp, c); 2.60 - memcpy(&(dest[dp]), "</span>", 7); 2.61 - dp += 7; 2.62 } 2.63 } else { 2.64 if (isstring) { 2.65 @@ -120,21 +119,17 @@ 2.66 if (wp > 0 && wp < WORDBUF_SIZE) { 2.67 int closespan = 1; 2.68 if (iskeyword(hltr->word, hltr->keywords)) { 2.69 - memcpy(&(dest[dp]), 2.70 - "<span class=\"c2html-keyword\">", 29); 2.71 - dp += 29; 2.72 + memcpy_const(dest, dp, 2.73 + "<span class=\"c2html-keyword\">"); 2.74 } else if (hltr->istype(hltr->word, wp)) { 2.75 - memcpy(&(dest[dp]), 2.76 - "<span class=\"c2html-type\">", 26); 2.77 - dp += 26; 2.78 + memcpy_const(dest, dp, 2.79 + "<span class=\"c2html-type\">"); 2.80 } else if (hltr->isdirective(hltr->word)) { 2.81 - memcpy(&(dest[dp]), 2.82 - "<span class=\"c2html-directive\">", 31); 2.83 - dp += 31; 2.84 + memcpy_const(dest, dp, 2.85 + "<span class=\"c2html-directive\">"); 2.86 } else if (iscapsonly(hltr->word, wp)) { 2.87 - memcpy(&(dest[dp]), 2.88 - "<span class=\"c2html-macroconst\">", 32); 2.89 - dp += 32; 2.90 + memcpy_const(dest, dp, 2.91 + "<span class=\"c2html-macroconst\">"); 2.92 } else { 2.93 closespan = 0; 2.94 } 2.95 @@ -142,8 +137,7 @@ 2.96 dp = writeescapedchar(dest, dp, hltr->word[i]); 2.97 } 2.98 if (closespan) { 2.99 - memcpy(&(dest[dp]), "</span>", 7); 2.100 - dp += 7; 2.101 + memcpy_const(dest, dp, "</span>"); 2.102 } 2.103 } 2.104 memset(hltr->word, 0, WORDBUF_SIZE);