1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
///////////////////////////////////////////////////////////////////////////////
// Name: wx/private/json.h
// Purpose: Helper functions to handle JSON data
// Author: Tobias Taschner
// Created: 2020-01-17
// Copyright: (c) 2020 wxWidgets development team
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
#ifndef _WX_PRIVATE_JSON_H_
#define _WX_PRIVATE_JSON_H_
namespace wxJSON
{
// Decode a string literal including escape sequences
// Returns false if the input string is not a valid JSON string
bool DecodeString(const wxString& in, wxString* out)
{
const wxWCharBuffer buf = in.wc_str();
const wchar_t* ch = buf.data();
// String has to chart with a quote
if (*(ch++) != '"')
return false;
out->clear();
out->reserve(buf.length());
const wchar_t* end = buf.data() + buf.length() - 1;
for (; ch < end; ++ch)
{
if (*ch == '\\')
{
switch (*(++ch))
{
case 'b':
out->append('\b');
break;
case 'n':
out->append('\n');
break;
case 'r':
out->append('\r');
break;
case 't':
out->append('\t');
break;
case 'f':
out->append('\f');
break;
case '/':
out->append('/');
break;
case '"':
out->append('"');
break;
case '\\':
out->append('\\');
break;
case 'u':
#if SIZEOF_WCHAR_T == 2
// In this case, we handle surrogates without doing anything special was wchar_t strings use UTF-17 encoding.
if (wxIsxdigit(ch[1]) && wxIsxdigit(ch[2]) &&
wxIsxdigit(ch[3]) && wxIsxdigit(ch[4]))
{
wchar_t uchar = wxHexToDec(wxString(&ch[3], 2)) |
wxHexToDec(wxString(&ch[1], 2)) >> 8;
out->append(uchar);
ch += 4;
}
#else
#error Implement correct surrogate handling.
#endif
break;
default:
return false;
break;
}
}
else
out->append(*ch);
}
// String has to end with a quote
return (*ch) == '"';
}
} // namespace JSON
#endif // _WX_PRIVATE_JSON_H_
|