MCPcopy Create free account
hub / github.com/SOUI2/soui / valueToQuotedStringN

Function valueToQuotedStringN

third-part/jsoncpp/src/lib_json/json_writer.cpp:253–323  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

251}
252
253static JSONCPP_STRING valueToQuotedStringN(const char* value, unsigned length) {
254 if (value == NULL)
255 return "";
256
257 if (!isAnyCharRequiredQuoting(value, length))
258 return JSONCPP_STRING("\"") + value + "\"";
259 // We have to walk value and escape any special characters.
260 // Appending to JSONCPP_STRING is not efficient, but this should be rare.
261 // (Note: forward slashes are *not* rare, but I am not escaping them.)
262 JSONCPP_STRING::size_type maxsize =
263 length * 2 + 3; // allescaped+quotes+NULL
264 JSONCPP_STRING result;
265 result.reserve(maxsize); // to avoid lots of mallocs
266 result += "\"";
267 char const* end = value + length;
268 for (const char* c = value; c != end; ++c) {
269 switch (*c) {
270 case '\"':
271 result += "\\\"";
272 break;
273 case '\\':
274 result += "\\\\";
275 break;
276 case '\b':
277 result += "\\b";
278 break;
279 case '\f':
280 result += "\\f";
281 break;
282 case '\n':
283 result += "\\n";
284 break;
285 case '\r':
286 result += "\\r";
287 break;
288 case '\t':
289 result += "\\t";
290 break;
291 // case '/':
292 // Even though \/ is considered a legal escape in JSON, a bare
293 // slash is also legal, so I see no reason to escape it.
294 // (I hope I am not misunderstanding something.)
295 // blep notes: actually escaping \/ may be useful in javascript to avoid </
296 // sequence.
297 // Should add a flag to allow this compatibility mode and prevent this
298 // sequence from occurring.
299 default: {
300 unsigned int cp = utf8ToCodepoint(c, end);
301 // don't escape non-control characters
302 // (short escape sequence are applied above)
303 if (cp < 0x80 && cp >= 0x20)
304 result += static_cast<char>(cp);
305 else if (cp < 0x10000) { // codepoint is in Basic Multilingual Plane
306 result += "\\u";
307 result += toHex16Bit(cp);
308 }
309 else { // codepoint is not in Basic Multilingual Plane
310 // convert to surrogate pair first

Callers 2

valueToQuotedStringFunction · 0.85
writeValueMethod · 0.85

Calls 4

isAnyCharRequiredQuotingFunction · 0.85
utf8ToCodepointFunction · 0.85
toHex16BitFunction · 0.85
reserveMethod · 0.45

Tested by

no test coverage detected