static void EscapeString(const String &s, std::string *_text) { std::string &text = *_text; text += "\""; for (uoffset_t i = 0; i < s.size(); i++) { char c = s[i]; switch (c) { case '\n': text += "\\n"; break; case '\t': text += "\\t"; break; case '\r': text += "\\r"; break; case '\b': text += "\\b"; break; case '\f': text += "\\f"; break; case '\"': text += "\\\""; break; case '\\': text += "\\\\"; break; default: if (c >= ' ' && c <= '~') { text += c; } else { // Not printable ASCII data. Let's see if it's valid UTF-8 first: const char *utf8 = s.c_str() + i; int ucc = FromUTF8(&utf8); if (ucc >= 0x80 && ucc <= 0xFFFF) { // Parses as Unicode within JSON's \uXXXX range, so use that. text += "\\u"; text += IntToStringHex(ucc, 4); // Skip past characters recognized. i = static_cast<uoffset_t>(utf8 - s.c_str() - 1); } else { // It's either unprintable ASCII, arbitrary binary, or Unicode data // that doesn't fit \uXXXX, so use \xXX escape code instead. text += "\\x"; text += IntToStringHex(static_cast<uint8_t>(c), 2); } } break; } } text += "\""; }
static void EscapeString(const String &s, std::string *_text) { std::string &text = *_text; text += "\""; for (uoffset_t i = 0; i < s.Length(); i++) { char c = s.Get(i); switch (c) { case '\n': text += "\\n"; break; case '\t': text += "\\t"; break; case '\r': text += "\\r"; break; case '\"': text += "\\\""; break; case '\\': text += "\\\\"; break; default: if (c >= ' ' && c <= '~') { text += c; } else { auto u = static_cast<unsigned char>(c); text += "\\x" + IntToStringHex(u); } break; } } text += "\""; }