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

Function valueToQuotedString

vrclient_x64/jsoncpp.cpp:4212–4272  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4210std::string valueToString(bool value) { return value ? "true" : "false"; }
4211
4212std::string valueToQuotedString(const char* value) {
4213 if (value == NULL)
4214 return "";
4215 // Not sure how to handle unicode...
4216 if (strpbrk(value, "\"\\\b\f\n\r\t") == NULL &&
4217 !containsControlCharacter(value))
4218 return std::string("\"") + value + "\"";
4219 // We have to walk value and escape any special characters.
4220 // Appending to std::string is not efficient, but this should be rare.
4221 // (Note: forward slashes are *not* rare, but I am not escaping them.)
4222 std::string::size_type maxsize =
4223 strlen(value) * 2 + 3; // allescaped+quotes+NULL
4224 std::string result;
4225 result.reserve(maxsize); // to avoid lots of mallocs
4226 result += "\"";
4227 for (const char* c = value; *c != 0; ++c) {
4228 switch (*c) {
4229 case '\"':
4230 result += "\\\"";
4231 break;
4232 case '\\':
4233 result += "\\\\";
4234 break;
4235 case '\b':
4236 result += "\\b";
4237 break;
4238 case '\f':
4239 result += "\\f";
4240 break;
4241 case '\n':
4242 result += "\\n";
4243 break;
4244 case '\r':
4245 result += "\\r";
4246 break;
4247 case '\t':
4248 result += "\\t";
4249 break;
4250 // case '/':
4251 // Even though \/ is considered a legal escape in JSON, a bare
4252 // slash is also legal, so I see no reason to escape it.
4253 // (I hope I am not misunderstanding something.
4254 // blep notes: actually escaping \/ may be useful in javascript to avoid </
4255 // sequence.
4256 // Should add a flag to allow this compatibility mode and prevent this
4257 // sequence from occurring.
4258 default:
4259 if (isControlCharacter(*c)) {
4260 std::ostringstream oss;
4261 oss << "\\u" << std::hex << std::uppercase << std::setfill('0')
4262 << std::setw(4) << static_cast<int>(*c);
4263 result += oss.str();
4264 } else {
4265 result += *c;
4266 }
4267 break;
4268 }
4269 }

Callers 1

writeValueMethod · 0.85

Calls 2

containsControlCharacterFunction · 0.85
isControlCharacterFunction · 0.85

Tested by

no test coverage detected