MCPcopy Create free account
hub / github.com/Tracktion/choc / writeWithEscapeCharacters

Function writeWithEscapeCharacters

choc/text/choc_JSON.h:110–157  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

108
109template <typename OutputStreamType>
110void writeWithEscapeCharacters (OutputStreamType& out, text::UTF8Pointer source)
111{
112 auto writeUnicode = [] (OutputStreamType& o, auto digit)
113 {
114 auto hexDigit = [] (auto value) -> char { return "0123456789abcdef"[value & 15]; };
115
116 o << "\\u" << hexDigit (digit >> 12) << hexDigit (digit >> 8) << hexDigit (digit >> 4) << hexDigit (digit);
117 };
118
119 for (;;)
120 {
121 auto c = *source;
122
123 switch (c)
124 {
125 case 0: return;
126
127 case '\"': out << "\\\""; break;
128 case '\\': out << "\\\\"; break;
129 case '\n': out << "\\n"; break;
130 case '\r': out << "\\r"; break;
131 case '\t': out << "\\t"; break;
132 case '\a': out << "\\a"; break;
133 case '\b': out << "\\b"; break;
134 case '\f': out << "\\f"; break;
135
136 default:
137 if (c > 31 && c < 127)
138 {
139 out << (char) c;
140 break;
141 }
142
143 if (c >= 0x10000)
144 {
145 auto pair = choc::text::splitCodePointIntoSurrogatePair (c);
146 writeUnicode (out, pair.high);
147 writeUnicode (out, pair.low);
148 break;
149 }
150
151 writeUnicode (out, c);
152 break;
153 }
154
155 ++source;
156 }
157}
158
159inline std::string addEscapeCharacters (text::UTF8Pointer source)
160{

Callers 2

addEscapeCharactersFunction · 0.85
getEscapedQuotedStringFunction · 0.85

Calls 1

Tested by

no test coverage detected