MCPcopy Create free account
hub / github.com/Kitware/CMake / valueToQuotedStringN

Function valueToQuotedStringN

Utilities/cmjsoncpp/src/lib_json/json_writer.cpp:276–351  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

274}
275
276static String valueToQuotedStringN(const char* value, size_t length,
277 bool emitUTF8 = false) {
278 if (value == nullptr)
279 return "";
280
281 if (!doesAnyCharRequireEscaping(value, length))
282 return String("\"") + value + "\"";
283 // We have to walk value and escape any special characters.
284 // Appending to String is not efficient, but this should be rare.
285 // (Note: forward slashes are *not* rare, but I am not escaping them.)
286 String::size_type maxsize = length * 2 + 3; // allescaped+quotes+NULL
287 String result;
288 result.reserve(maxsize); // to avoid lots of mallocs
289 result += "\"";
290 char const* end = value + length;
291 for (const char* c = value; c != end; ++c) {
292 switch (*c) {
293 case '\"':
294 result += "\\\"";
295 break;
296 case '\\':
297 result += "\\\\";
298 break;
299 case '\b':
300 result += "\\b";
301 break;
302 case '\f':
303 result += "\\f";
304 break;
305 case '\n':
306 result += "\\n";
307 break;
308 case '\r':
309 result += "\\r";
310 break;
311 case '\t':
312 result += "\\t";
313 break;
314 // case '/':
315 // Even though \/ is considered a legal escape in JSON, a bare
316 // slash is also legal, so I see no reason to escape it.
317 // (I hope I am not misunderstanding something.)
318 // blep notes: actually escaping \/ may be useful in javascript to avoid </
319 // sequence.
320 // Should add a flag to allow this compatibility mode and prevent this
321 // sequence from occurring.
322 default: {
323 if (emitUTF8) {
324 unsigned codepoint = static_cast<unsigned char>(*c);
325 if (codepoint < 0x20) {
326 appendHex(result, codepoint);
327 } else {
328 appendRaw(result, codepoint);
329 }
330 } else {
331 unsigned codepoint = utf8ToCodepoint(c, end); // modifies `c`
332 if (codepoint < 0x20) {
333 appendHex(result, codepoint);

Callers 2

valueToQuotedStringFunction · 0.85
writeValueMethod · 0.85

Calls 6

StringFunction · 0.85
appendHexFunction · 0.85
appendRawFunction · 0.85
utf8ToCodepointFunction · 0.85
reserveMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…