MCPcopy Create free account
hub / github.com/ValveSoftware/Proton / valueToQuotedStringN

Function valueToQuotedStringN

vrclient_x64/jsoncpp.cpp:4289–4350  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4287 return NULL;
4288}
4289static std::string valueToQuotedStringN(const char* value, unsigned length) {
4290 if (value == NULL)
4291 return "";
4292 // Not sure how to handle unicode...
4293 if (strnpbrk(value, "\"\\\b\f\n\r\t", length) == NULL &&
4294 !containsControlCharacter0(value, length))
4295 return std::string("\"") + value + "\"";
4296 // We have to walk value and escape any special characters.
4297 // Appending to std::string is not efficient, but this should be rare.
4298 // (Note: forward slashes are *not* rare, but I am not escaping them.)
4299 std::string::size_type maxsize =
4300 length * 2 + 3; // allescaped+quotes+NULL
4301 std::string result;
4302 result.reserve(maxsize); // to avoid lots of mallocs
4303 result += "\"";
4304 char const* end = value + length;
4305 for (const char* c = value; c != end; ++c) {
4306 switch (*c) {
4307 case '\"':
4308 result += "\\\"";
4309 break;
4310 case '\\':
4311 result += "\\\\";
4312 break;
4313 case '\b':
4314 result += "\\b";
4315 break;
4316 case '\f':
4317 result += "\\f";
4318 break;
4319 case '\n':
4320 result += "\\n";
4321 break;
4322 case '\r':
4323 result += "\\r";
4324 break;
4325 case '\t':
4326 result += "\\t";
4327 break;
4328 // case '/':
4329 // Even though \/ is considered a legal escape in JSON, a bare
4330 // slash is also legal, so I see no reason to escape it.
4331 // (I hope I am not misunderstanding something.)
4332 // blep notes: actually escaping \/ may be useful in javascript to avoid </
4333 // sequence.
4334 // Should add a flag to allow this compatibility mode and prevent this
4335 // sequence from occurring.
4336 default:
4337 if ((isControlCharacter(*c)) || (*c == 0)) {
4338 std::ostringstream oss;
4339 oss << "\\u" << std::hex << std::uppercase << std::setfill('0')
4340 << std::setw(4) << static_cast<int>(*c);
4341 result += oss.str();
4342 } else {
4343 result += *c;
4344 }
4345 break;
4346 }

Callers 1

writeValueMethod · 0.85

Calls 3

strnpbrkFunction · 0.85
isControlCharacterFunction · 0.85

Tested by

no test coverage detected