# HG changeset patch # User Mike Becker # Date 1429982097 -7200 # Node ID ec6e97454e646352128c109fefeddf9dc8e72109 # Parent 1be8ea902ef44e61181905c25ccc5e72b186dfbb introduced macro for constant string memcpy + fixed string highlight fix diff -r 1be8ea902ef4 -r ec6e97454e64 src/ccodegen.c --- 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]), "", 29); - dp += 29; + memcpy_const(dest, dp, ""); } 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]), "/", 8); - dp += 8; + memcpy_const(dest, dp, "/"); continue; } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { iscomment = 1; hltr->iscommentml = (src[sp+1] == '*'); - memcpy(&(dest[dp]), "", 29); - dp += 29; + memcpy_const(dest, dp, ""); } } if (iscomment) { if (c == '\n') { - memcpy(&(dest[dp]), "", 7); - dp += 7; + memcpy_const(dest, dp, ""); } dp = writeescapedchar(dest, dp, c); } else if (isinclude) { if (c == '<') { - memcpy(&(dest[dp]), "", 32); - dp += 32; + memcpy_const(dest, dp, ""); dp = writeescapedchar(dest, dp, c); } else if (c == '\"') { if (parseinclude) { @@ -102,13 +100,11 @@ dp += ifp; dp = writeescapedchar(dest, dp, c); - memcpy(&(dest[dp]), "", 4); - dp += 4; + memcpy_const(dest, dp, ""); parseinclude = 0; } else { - memcpy(&(dest[dp]), - "') { dp = writeescapedchar(dest, dp, c); - memcpy(&(dest[dp]), "", 7); - dp += 7; + memcpy_const(dest, dp, ""); } 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]), "", 28); - dp += 28; - dp = writeescapedchar(dest, dp, c); + memcpy_const(dest, dp, ""); } else { dp = writeescapedchar(dest, dp, c); } } else { isstring = 1; quote = c; + memcpy_const(dest, dp, + ""); dp = writeescapedchar(dest, dp, c); - memcpy(&(dest[dp]), "", 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]), - "", 29); - dp += 29; + memcpy_const(dest, dp, + ""); } else if (hltr->istype(hltr->word, wp)) { - memcpy(&(dest[dp]), - "", 26); - dp += 26; + memcpy_const(dest, dp, + ""); } else if (hltr->isdirective(hltr->word)) { isinclude = !strncmp( "#include", hltr->word, WORDBUF_SIZE); - memcpy(&(dest[dp]), - "", 31); - dp += 31; + memcpy_const(dest, dp, + ""); } else if (iscapsonly(hltr->word, wp)) { - memcpy(&(dest[dp]), - "", 32); - dp += 32; + memcpy_const(dest, dp, + ""); } else { closespan = 0; } @@ -175,8 +165,7 @@ dp = writeescapedchar(dest, dp, hltr->word[i]); } if (closespan) { - memcpy(&(dest[dp]), "", 7); - dp += 7; + memcpy_const(dest, dp, ""); } } memset(hltr->word, 0, WORDBUF_SIZE); diff -r 1be8ea902ef4 -r ec6e97454e64 src/javacodegen.c --- a/src/javacodegen.c Sat Apr 25 19:01:16 2015 +0200 +++ b/src/javacodegen.c Sat Apr 25 19:14:57 2015 +0200 @@ -49,6 +49,9 @@ return word[0] == '@'; } +#define memcpy_const(darr,doff,str) memcpy(&(darr[doff]), str, sizeof(str)-1); \ + dp += sizeof(str)-1 + void jparseline(char *src, char *dest, highlighter_t *hltr) { size_t sp = 0, dp = 0; /* indent */ @@ -64,8 +67,7 @@ if (hltr->iscommentml) { iscomment = 1; - memcpy(&(dest[dp]), "", 29); - dp += 29; + memcpy_const(dest, dp, ""); } for (char c = src[sp] ; c ; c=src[++sp]) { @@ -74,14 +76,12 @@ if (hltr->iscommentml && sp > 0 && src[sp-1] == '*') { iscomment = 0; hltr->iscommentml = 0; - memcpy(&(dest[dp]), "/", 8); - dp += 8; + memcpy_const(dest, dp, "/"); continue; } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { iscomment = 1; hltr->iscommentml = (src[sp+1] == '*'); - memcpy(&(dest[dp]), "", 29); - dp += 29; + memcpy_const(dest, dp, ""); } } @@ -97,20 +97,19 @@ /* strings */ if (!isescaping && (c == '\'' || c == '\"')) { if (isstring) { + dp = writeescapedchar(dest, dp, c); if (c == quote) { isstring = 0; - memcpy(&(dest[dp]), "", 28); - dp += 28; - dp = writeescapedchar(dest, dp, c); + memcpy_const(dest, dp, ""); } else { dp = writeescapedchar(dest, dp, c); } } else { isstring = 1; quote = c; + memcpy_const(dest, dp, + ""); dp = writeescapedchar(dest, dp, c); - memcpy(&(dest[dp]), "", 7); - dp += 7; } } else { if (isstring) { @@ -120,21 +119,17 @@ if (wp > 0 && wp < WORDBUF_SIZE) { int closespan = 1; if (iskeyword(hltr->word, hltr->keywords)) { - memcpy(&(dest[dp]), - "", 29); - dp += 29; + memcpy_const(dest, dp, + ""); } else if (hltr->istype(hltr->word, wp)) { - memcpy(&(dest[dp]), - "", 26); - dp += 26; + memcpy_const(dest, dp, + ""); } else if (hltr->isdirective(hltr->word)) { - memcpy(&(dest[dp]), - "", 31); - dp += 31; + memcpy_const(dest, dp, + ""); } else if (iscapsonly(hltr->word, wp)) { - memcpy(&(dest[dp]), - "", 32); - dp += 32; + memcpy_const(dest, dp, + ""); } else { closespan = 0; } @@ -142,8 +137,7 @@ dp = writeescapedchar(dest, dp, hltr->word[i]); } if (closespan) { - memcpy(&(dest[dp]), "", 7); - dp += 7; + memcpy_const(dest, dp, ""); } } memset(hltr->word, 0, WORDBUF_SIZE);