Skip to content

Commit

Permalink
Issue #75: Fix json string escaping.
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Kaufmann committed Mar 14, 2022
1 parent 5cd3a2e commit 10d8f27
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/clasp_output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -529,15 +529,16 @@ void JsonOutput::printString(const char* v, const char* sep) {
assert(v);
const uint32 BUF_SIZE = 1024;
char buf[BUF_SIZE];
uint32 n = 0;
buf[n++] = '"';
while (*v) {
if (*v != '\\' && *v != '"') { buf[n++] = *v++; }
else if (*v == '"' || !strchr("\"\\/\b\f\n\r\t", v[1])) { buf[n++] = '\\'; buf[n++] = *v++; }
else { buf[n++] = v[0]; buf[n++] = v[1]; v += 2; }
if (n > BUF_SIZE - 2) { buf[n] = 0; printf("%s%s", sep, buf); n = 0; sep = ""; }
}
buf[n] = 0;
const char* special = "\b\f\n\r\t\"\\";
const char* replace = "bfnrt\"\\";
buf[0] = '"';
for (uint32 n = 1; (buf[n] = *v) != 0; ++v) {
if (const char* esc = strchr(special, buf[n])) {
buf[n] = '\\';
buf[++n] = replace[esc - special];
}
if (++n > BUF_SIZE - 2) { buf[n] = 0; printf("%s%s", sep, buf); n = 0; sep = ""; }
}
printf("%s%s\"", sep, buf);
}

Expand Down

0 comments on commit 10d8f27

Please sign in to comment.