forked from 0ad/0ad
Provide JSON-compatible string escaping
This was SVN commit r10464.
This commit is contained in:
parent
9cbf587e59
commit
4ef66a6950
@ -32,7 +32,7 @@ static inline CStr NetMessageStringConvert(const CStr8& arg)
|
||||
|
||||
static inline CStr NetMessageStringConvert(const CStrW& arg)
|
||||
{
|
||||
return arg.ToUTF8().EscapeToPrintableASCII();
|
||||
return arg.EscapeToPrintableASCII();
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -356,25 +356,26 @@ CStr CStr::UnescapeBackslashes() const
|
||||
return NewString;
|
||||
}
|
||||
|
||||
CStr CStr::EscapeToPrintableASCII() const
|
||||
CStr8 CStr::EscapeToPrintableASCII() const
|
||||
{
|
||||
CStr NewString;
|
||||
CStr8 NewString;
|
||||
for (size_t i = 0; i < length(); i++)
|
||||
{
|
||||
tchar ch = (*this)[i];
|
||||
|
||||
if (ch == '"')
|
||||
NewString += _T("\\\"");
|
||||
else if (ch == '\\')
|
||||
NewString += _T("\\\\");
|
||||
else if (ch == '\n')
|
||||
NewString += _T("\\n");
|
||||
if (ch == '"') NewString += "\\\"";
|
||||
else if (ch == '\\') NewString += "\\\\";
|
||||
else if (ch == '\b') NewString += "\\b";
|
||||
else if (ch == '\f') NewString += "\\f";
|
||||
else if (ch == '\n') NewString += "\\n";
|
||||
else if (ch == '\r') NewString += "\\r";
|
||||
else if (ch == '\t') NewString += "\\t";
|
||||
else if (ch >= 32 && ch <= 126)
|
||||
NewString += ch;
|
||||
else
|
||||
{
|
||||
std::tstringstream ss;
|
||||
ss << _T("\\x") << std::hex << std::setfill(_T('0')) << std::setw(2) << (int)(unsigned char)ch;
|
||||
std::stringstream ss;
|
||||
ss << "\\u" << std::hex << std::setfill('0') << std::setw(4) << (int)(unsigned char)ch;
|
||||
NewString += ss.str();
|
||||
}
|
||||
}
|
||||
|
@ -311,9 +311,9 @@ public:
|
||||
CStr UnescapeBackslashes() const;
|
||||
|
||||
/**
|
||||
* Convert strings to printable ASCII characters with C-style escapes
|
||||
* Convert strings to printable ASCII characters with JSON-style escapes.
|
||||
*/
|
||||
CStr EscapeToPrintableASCII() const;
|
||||
CStr8 EscapeToPrintableASCII() const;
|
||||
|
||||
/**
|
||||
* Return a trimmed copy of the CStr.
|
||||
|
Loading…
Reference in New Issue
Block a user