Merge pull request #315 from fbraem/develop

Solve wrong escaping of unicode characters
This commit is contained in:
Aleksandar Fabijanic 2014-03-11 20:29:18 -05:00
commit 1cd745996c
2 changed files with 35 additions and 35 deletions

View File

@ -92,41 +92,41 @@ void Stringifier::formatString(const std::string& value, std::ostream& out)
out << '"'; out << '"';
for (std::string::const_iterator it = value.begin(); it != value.end(); ++it) for (std::string::const_iterator it = value.begin(); it != value.end(); ++it)
{ {
switch (*it) if (*it == 0x20 ||
{ *it == 0x21 ||
case '"': (*it >= 0x23 && *it <= 0x2E) ||
(*it >= 0x30 && *it <= 0x5B) ||
(*it >= 0x5D && *it <= 0xFF))
out << *it;
else if (*it == '"')
out << "\\\""; out << "\\\"";
break; else if (*it == '\\')
case '\\':
out << "\\\\"; out << "\\\\";
break; else if (*it == '\b')
case '\b':
out << "\\b"; out << "\\b";
break; else if (*it == '\f')
case '\f':
out << "\\f"; out << "\\f";
break; else if (*it == '\n')
case '\n':
out << "\\n"; out << "\\n";
break; else if (*it == '\r')
case '\r':
out << "\\r"; out << "\\r";
break; else if (*it == '\t')
case '\t':
out << "\\t"; out << "\\t";
break; else if ( *it == '\0' )
default: out << "\\u0000";
{
if ( *it > 0 && *it <= 0x1F )
{
out << "\\u" << std::hex << std::uppercase << std::setfill('0') << std::setw(4) << static_cast<int>(*it);
}
else else
{ {
out << *it; const char *hexdigits = "0123456789ABCDEF";
} unsigned long u = (std::min)(static_cast<unsigned long>(static_cast<unsigned char>(*it)), 0xFFFFul);
break; int d1 = u / 4096; u -= d1 * 4096;
} int d2 = u / 256; u -= d2 * 256;
int d3 = u / 16; u -= d3 * 16;
int d4 = u;
out << "\\u";
out << hexdigits[d1];
out << hexdigits[d2];
out << hexdigits[d3];
out << hexdigits[d4];
} }
} }
out << '"'; out << '"';

View File

@ -76,7 +76,7 @@ public:
void write(BinaryWriter& writer); void write(BinaryWriter& writer);
/// Writes the header /// Writes the header
std::size_t getMessageLength() const; Int32 getMessageLength() const;
/// Returns the message length /// Returns the message length
OpCode opCode() const; OpCode opCode() const;
@ -95,10 +95,10 @@ private:
MessageHeader(OpCode opcode); MessageHeader(OpCode opcode);
/// Constructor. /// Constructor.
void setMessageLength(std::size_t length); void setMessageLength(Int32 length);
/// Sets the message length /// Sets the message length
std::size_t _messageLength; Int32 _messageLength;
Int32 _requestID; Int32 _requestID;
Int32 _responseTo; Int32 _responseTo;
OpCode _opCode; OpCode _opCode;
@ -113,13 +113,13 @@ inline MessageHeader::OpCode MessageHeader::opCode() const
} }
inline std::size_t MessageHeader::getMessageLength() const inline Int32 MessageHeader::getMessageLength() const
{ {
return _messageLength; return _messageLength;
} }
inline void MessageHeader::setMessageLength(std::size_t length) inline void MessageHeader::setMessageLength(Int32 length)
{ {
_messageLength = MSG_HEADER_SIZE + length; _messageLength = MSG_HEADER_SIZE + length;
} }