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

Function valueToQuotedStringN

src/jsoncpp.cpp:4290–4351  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 1

writeValueMethod · 0.85

Calls 3

strnpbrkFunction · 0.85
isControlCharacterFunction · 0.85

Tested by

no test coverage detected