MCPcopy Create free account
hub / github.com/deathkiller/jazz2-native / valueToQuotedStringN

Function valueToQuotedStringN

Sources/Dependencies/jsoncpp/writer.cpp:219–293  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

217 }
218
219 static StringContainer valueToQuotedStringN(const char* value, size_t length, bool emitUTF8 = false) {
220 if (value == nullptr)
221 return "";
222
223 if (!doesAnyCharRequireEscaping(value, length))
224 return StringContainer("\"") + value + "\"";
225 // We have to walk value and escape any special characters.
226 // Appending to StringContainer is not efficient, but this should be rare.
227 // (Note: forward slashes are *not* rare, but I am not escaping them.)
228 StringContainer::size_type maxsize = length * 2 + 3; // allescaped+quotes+NULL
229 StringContainer result;
230 result.reserve(maxsize); // to avoid lots of mallocs
231 result += "\"";
232 char const* end = value + length;
233 for (const char* c = value; c != end; ++c) {
234 switch (*c) {
235 case '\"':
236 result += "\\\"";
237 break;
238 case '\\':
239 result += "\\\\";
240 break;
241 case '\b':
242 result += "\\b";
243 break;
244 case '\f':
245 result += "\\f";
246 break;
247 case '\n':
248 result += "\\n";
249 break;
250 case '\r':
251 result += "\\r";
252 break;
253 case '\t':
254 result += "\\t";
255 break;
256 // case '/':
257 // Even though \/ is considered a legal escape in JSON, a bare
258 // slash is also legal, so I see no reason to escape it.
259 // (I hope I am not misunderstanding something.)
260 // blep notes: actually escaping \/ may be useful in javascript to avoid </
261 // sequence.
262 // Should add a flag to allow this compatibility mode and prevent this
263 // sequence from occurring.
264 default: {
265 if (emitUTF8) {
266 unsigned codepoint = static_cast<unsigned char>(*c);
267 if (codepoint < 0x20) {
268 appendHex(result, codepoint);
269 } else {
270 appendRaw(result, codepoint);
271 }
272 } else {
273 unsigned codepoint = utf8ToCodepoint(c, end); // modifies `c`
274 if (codepoint < 0x20) {
275 appendHex(result, codepoint);
276 } else if (codepoint < 0x80) {

Callers 2

valueToQuotedStringFunction · 0.85
writeValueMethod · 0.85

Calls 5

appendHexFunction · 0.85
appendRawFunction · 0.85
utf8ToCodepointFunction · 0.85
reserveMethod · 0.45

Tested by

no test coverage detected