MCPcopy Create free account
hub / github.com/altairwei/WizNotePlus / valueToQuotedStringN

Function valueToQuotedStringN

src/share/jsoncpp/jsoncpp.cpp:4335–4396  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4333 return NULL;
4334}
4335static JSONCPP_STRING valueToQuotedStringN(const char* value, unsigned length) {
4336 if (value == NULL)
4337 return "";
4338 // Not sure how to handle unicode...
4339 if (strnpbrk(value, "\"\\\b\f\n\r\t", length) == NULL &&
4340 !containsControlCharacter0(value, length))
4341 return JSONCPP_STRING("\"") + value + "\"";
4342 // We have to walk value and escape any special characters.
4343 // Appending to JSONCPP_STRING is not efficient, but this should be rare.
4344 // (Note: forward slashes are *not* rare, but I am not escaping them.)
4345 JSONCPP_STRING::size_type maxsize =
4346 length * 2 + 3; // allescaped+quotes+NULL
4347 JSONCPP_STRING result;
4348 result.reserve(maxsize); // to avoid lots of mallocs
4349 result += "\"";
4350 char const* end = value + length;
4351 for (const char* c = value; c != end; ++c) {
4352 switch (*c) {
4353 case '\"':
4354 result += "\\\"";
4355 break;
4356 case '\\':
4357 result += "\\\\";
4358 break;
4359 case '\b':
4360 result += "\\b";
4361 break;
4362 case '\f':
4363 result += "\\f";
4364 break;
4365 case '\n':
4366 result += "\\n";
4367 break;
4368 case '\r':
4369 result += "\\r";
4370 break;
4371 case '\t':
4372 result += "\\t";
4373 break;
4374 // case '/':
4375 // Even though \/ is considered a legal escape in JSON, a bare
4376 // slash is also legal, so I see no reason to escape it.
4377 // (I hope I am not misunderstanding something.)
4378 // blep notes: actually escaping \/ may be useful in javascript to avoid </
4379 // sequence.
4380 // Should add a flag to allow this compatibility mode and prevent this
4381 // sequence from occurring.
4382 default:
4383 if ((isControlCharacter(*c)) || (*c == 0)) {
4384 JSONCPP_OSTRINGSTREAM oss;
4385 oss << "\\u" << std::hex << std::uppercase << std::setfill('0')
4386 << std::setw(4) << static_cast<int>(*c);
4387 result += oss.str();
4388 } else {
4389 result += *c;
4390 }
4391 break;
4392 }

Callers 1

writeValueMethod · 0.85

Calls 3

strnpbrkFunction · 0.85
isControlCharacterFunction · 0.85

Tested by

no test coverage detected