Skip to content

Commit 1cb2823

Browse files
committed
Solve wrong escaping of unicode characters
1 parent 7f2bea8 commit 1cb2823

1 file changed

Lines changed: 27 additions & 27 deletions

File tree

JSON/src/Stringifier.cpp

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -92,41 +92,41 @@ void Stringifier::formatString(const std::string& value, std::ostream& out)
9292
out << '"';
9393
for (std::string::const_iterator it = value.begin(); it != value.end(); ++it)
9494
{
95-
switch (*it)
96-
{
97-
case '"':
95+
if (*it == 0x20 ||
96+
*it == 0x21 ||
97+
(*it >= 0x23 && *it <= 0x2E) ||
98+
(*it >= 0x30 && *it <= 0x5B) ||
99+
(*it >= 0x5D && *it <= 0xFF))
100+
out << *it;
101+
else if (*it == '"')
98102
out << "\\\"";
99-
break;
100-
case '\\':
103+
else if (*it == '\\')
101104
out << "\\\\";
102-
break;
103-
case '\b':
105+
else if (*it == '\b')
104106
out << "\\b";
105-
break;
106-
case '\f':
107+
else if (*it == '\f')
107108
out << "\\f";
108-
break;
109-
case '\n':
109+
else if (*it == '\n')
110110
out << "\\n";
111-
break;
112-
case '\r':
111+
else if (*it == '\r')
113112
out << "\\r";
114-
break;
115-
case '\t':
113+
else if (*it == '\t')
116114
out << "\\t";
117-
break;
118-
default:
115+
else if ( *it == '\0' )
116+
out << "\\u0000";
117+
else
119118
{
120-
if ( *it > 0 && *it <= 0x1F )
121-
{
122-
out << "\\u" << std::hex << std::uppercase << std::setfill('0') << std::setw(4) << static_cast<int>(*it);
123-
}
124-
else
125-
{
126-
out << *it;
127-
}
128-
break;
129-
}
119+
const char *hexdigits = "0123456789ABCDEF";
120+
unsigned long u = (std::min)(static_cast<unsigned long>(static_cast<unsigned char>(*it)), 0xFFFFul);
121+
int d1 = u / 4096; u -= d1 * 4096;
122+
int d2 = u / 256; u -= d2 * 256;
123+
int d3 = u / 16; u -= d3 * 16;
124+
int d4 = u;
125+
out << "\\u";
126+
out << hexdigits[d1];
127+
out << hexdigits[d2];
128+
out << hexdigits[d3];
129+
out << hexdigits[d4];
130130
}
131131
}
132132
out << '"';

0 commit comments

Comments
 (0)