MCPcopy Create free account
hub / github.com/IBM/Project_CodeNet / JSON_escape

Function JSON_escape

tools/tokenizer/antlr4tojson.c:167–192  ·  view source on GitHub ↗

Escape token for output as JSON string.

Source from the content-addressed store, hash-verified

165
166// Escape token for output as JSON string.
167static void JSON_escape(FILE *out, const char *p, unsigned len)
168{
169 for (; len; len--, p++) {
170 char c = *p;
171 // A " which is not yet escaped, will be escaped:
172 if (c == '"')
173 // Insert a backslash before the double quote:
174 fputc('\\', out);
175 else
176 // A \ which is not followed by the JSON expected, is escaped
177 if (c == '\\') {
178 const char anything_but_valid_escape = '0'; // [^\\\"bfnrt]
179 const char peek = len ? *(p+1) : anything_but_valid_escape; // look ahead
180 fputc('\\', out);
181 if (strchr("\\\"bfnrt", peek)) {
182 // An valid JSON escape. Output it and skip peek:
183 c = peek;
184 p++;
185 len--;
186 }
187 //else Not a correct JSON escape, a standalone backslash; double it.
188 }
189 //else Not a " or \; default action.
190 fputc(c, out);
191 }
192}
193
194// Escape token for output as CSV string.
195static void CSV_escape(FILE *out, const char *p, unsigned len)

Callers 1

getFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected