encodings.h (rapidjson-1.0.2) | : | encodings.h (rapidjson-1.1.0) | ||
---|---|---|---|---|
skipping to change at line 123 | skipping to change at line 123 | |||
} | } | |||
else { | else { | |||
RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); | RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); | |||
os.Put(static_cast<Ch>(0xF0 | ((codepoint >> 18) & 0xFF))); | os.Put(static_cast<Ch>(0xF0 | ((codepoint >> 18) & 0xFF))); | |||
os.Put(static_cast<Ch>(0x80 | ((codepoint >> 12) & 0x3F))); | os.Put(static_cast<Ch>(0x80 | ((codepoint >> 12) & 0x3F))); | |||
os.Put(static_cast<Ch>(0x80 | ((codepoint >> 6) & 0x3F))); | os.Put(static_cast<Ch>(0x80 | ((codepoint >> 6) & 0x3F))); | |||
os.Put(static_cast<Ch>(0x80 | (codepoint & 0x3F))); | os.Put(static_cast<Ch>(0x80 | (codepoint & 0x3F))); | |||
} | } | |||
} | } | |||
template<typename OutputStream> | ||||
static void EncodeUnsafe(OutputStream& os, unsigned codepoint) { | ||||
if (codepoint <= 0x7F) | ||||
PutUnsafe(os, static_cast<Ch>(codepoint & 0xFF)); | ||||
else if (codepoint <= 0x7FF) { | ||||
PutUnsafe(os, static_cast<Ch>(0xC0 | ((codepoint >> 6) & 0xFF))); | ||||
PutUnsafe(os, static_cast<Ch>(0x80 | ((codepoint & 0x3F)))); | ||||
} | ||||
else if (codepoint <= 0xFFFF) { | ||||
PutUnsafe(os, static_cast<Ch>(0xE0 | ((codepoint >> 12) & 0xFF))); | ||||
PutUnsafe(os, static_cast<Ch>(0x80 | ((codepoint >> 6) & 0x3F))); | ||||
PutUnsafe(os, static_cast<Ch>(0x80 | (codepoint & 0x3F))); | ||||
} | ||||
else { | ||||
RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); | ||||
PutUnsafe(os, static_cast<Ch>(0xF0 | ((codepoint >> 18) & 0xFF))); | ||||
PutUnsafe(os, static_cast<Ch>(0x80 | ((codepoint >> 12) & 0x3F))); | ||||
PutUnsafe(os, static_cast<Ch>(0x80 | ((codepoint >> 6) & 0x3F))); | ||||
PutUnsafe(os, static_cast<Ch>(0x80 | (codepoint & 0x3F))); | ||||
} | ||||
} | ||||
template <typename InputStream> | template <typename InputStream> | |||
static bool Decode(InputStream& is, unsigned* codepoint) { | static bool Decode(InputStream& is, unsigned* codepoint) { | |||
#define COPY() c = is.Take(); *codepoint = (*codepoint << 6) | ((unsigned char)c | #define COPY() c = is.Take(); *codepoint = (*codepoint << 6) | (static_cast<unsi | |||
& 0x3Fu) | gned char>(c) & 0x3Fu) | |||
#define TRANS(mask) result &= ((GetRange((unsigned char)c) & mask) != 0) | #define TRANS(mask) result &= ((GetRange(static_cast<unsigned char>(c)) & mask) | |||
!= 0) | ||||
#define TAIL() COPY(); TRANS(0x70) | #define TAIL() COPY(); TRANS(0x70) | |||
Ch c = is.Take(); | typename InputStream::Ch c = is.Take(); | |||
if (!(c & 0x80)) { | if (!(c & 0x80)) { | |||
*codepoint = (unsigned char)c; | *codepoint = static_cast<unsigned char>(c); | |||
return true; | return true; | |||
} | } | |||
unsigned char type = GetRange((unsigned char)c); | unsigned char type = GetRange(static_cast<unsigned char>(c)); | |||
*codepoint = (0xFF >> type) & (unsigned char)c; | if (type >= 32) { | |||
*codepoint = 0; | ||||
} else { | ||||
*codepoint = (0xFF >> type) & static_cast<unsigned char>(c); | ||||
} | ||||
bool result = true; | bool result = true; | |||
switch (type) { | switch (type) { | |||
case 2: TAIL(); return result; | case 2: TAIL(); return result; | |||
case 3: TAIL(); TAIL(); return result; | case 3: TAIL(); TAIL(); return result; | |||
case 4: COPY(); TRANS(0x50); TAIL(); return result; | case 4: COPY(); TRANS(0x50); TAIL(); return result; | |||
case 5: COPY(); TRANS(0x10); TAIL(); TAIL(); return result; | case 5: COPY(); TRANS(0x10); TAIL(); TAIL(); return result; | |||
case 6: TAIL(); TAIL(); TAIL(); return result; | case 6: TAIL(); TAIL(); TAIL(); return result; | |||
case 10: COPY(); TRANS(0x20); TAIL(); return result; | case 10: COPY(); TRANS(0x20); TAIL(); return result; | |||
case 11: COPY(); TRANS(0x60); TAIL(); TAIL(); return result; | case 11: COPY(); TRANS(0x60); TAIL(); TAIL(); return result; | |||
default: return false; | default: return false; | |||
} | } | |||
#undef COPY | #undef COPY | |||
#undef TRANS | #undef TRANS | |||
#undef TAIL | #undef TAIL | |||
} | } | |||
template <typename InputStream, typename OutputStream> | template <typename InputStream, typename OutputStream> | |||
static bool Validate(InputStream& is, OutputStream& os) { | static bool Validate(InputStream& is, OutputStream& os) { | |||
#define COPY() os.Put(c = is.Take()) | #define COPY() os.Put(c = is.Take()) | |||
#define TRANS(mask) result &= ((GetRange((unsigned char)c) & mask) != 0) | #define TRANS(mask) result &= ((GetRange(static_cast<unsigned char>(c)) & mask) != 0) | |||
#define TAIL() COPY(); TRANS(0x70) | #define TAIL() COPY(); TRANS(0x70) | |||
Ch c; | Ch c; | |||
COPY(); | COPY(); | |||
if (!(c & 0x80)) | if (!(c & 0x80)) | |||
return true; | return true; | |||
bool result = true; | bool result = true; | |||
switch (GetRange((unsigned char)c)) { | switch (GetRange(static_cast<unsigned char>(c))) { | |||
case 2: TAIL(); return result; | case 2: TAIL(); return result; | |||
case 3: TAIL(); TAIL(); return result; | case 3: TAIL(); TAIL(); return result; | |||
case 4: COPY(); TRANS(0x50); TAIL(); return result; | case 4: COPY(); TRANS(0x50); TAIL(); return result; | |||
case 5: COPY(); TRANS(0x10); TAIL(); TAIL(); return result; | case 5: COPY(); TRANS(0x10); TAIL(); TAIL(); return result; | |||
case 6: TAIL(); TAIL(); TAIL(); return result; | case 6: TAIL(); TAIL(); TAIL(); return result; | |||
case 10: COPY(); TRANS(0x20); TAIL(); return result; | case 10: COPY(); TRANS(0x20); TAIL(); return result; | |||
case 11: COPY(); TRANS(0x60); TAIL(); TAIL(); return result; | case 11: COPY(); TRANS(0x60); TAIL(); TAIL(); return result; | |||
default: return false; | default: return false; | |||
} | } | |||
#undef COPY | #undef COPY | |||
skipping to change at line 199 | skipping to change at line 225 | |||
0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x2 0,0x20,0x20, | 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x2 0,0x20,0x20, | |||
8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, | 8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, | |||
10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3, 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8, | 10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3, 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8, | |||
}; | }; | |||
return type[c]; | return type[c]; | |||
} | } | |||
template <typename InputByteStream> | template <typename InputByteStream> | |||
static CharType TakeBOM(InputByteStream& is) { | static CharType TakeBOM(InputByteStream& is) { | |||
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | |||
Ch c = Take(is); | typename InputByteStream::Ch c = Take(is); | |||
if ((unsigned char)c != 0xEFu) return c; | if (static_cast<unsigned char>(c) != 0xEFu) return c; | |||
c = is.Take(); | c = is.Take(); | |||
if ((unsigned char)c != 0xBBu) return c; | if (static_cast<unsigned char>(c) != 0xBBu) return c; | |||
c = is.Take(); | c = is.Take(); | |||
if ((unsigned char)c != 0xBFu) return c; | if (static_cast<unsigned char>(c) != 0xBFu) return c; | |||
c = is.Take(); | c = is.Take(); | |||
return c; | return c; | |||
} | } | |||
template <typename InputByteStream> | template <typename InputByteStream> | |||
static Ch Take(InputByteStream& is) { | static Ch Take(InputByteStream& is) { | |||
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | |||
return is.Take(); | return static_cast<Ch>(is.Take()); | |||
} | } | |||
template <typename OutputByteStream> | template <typename OutputByteStream> | |||
static void PutBOM(OutputByteStream& os) { | static void PutBOM(OutputByteStream& os) { | |||
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); | |||
os.Put(0xEFu); os.Put(0xBBu); os.Put(0xBFu); | os.Put(static_cast<typename OutputByteStream::Ch>(0xEFu)); | |||
os.Put(static_cast<typename OutputByteStream::Ch>(0xBBu)); | ||||
os.Put(static_cast<typename OutputByteStream::Ch>(0xBFu)); | ||||
} | } | |||
template <typename OutputByteStream> | template <typename OutputByteStream> | |||
static void Put(OutputByteStream& os, Ch c) { | static void Put(OutputByteStream& os, Ch c) { | |||
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); | |||
os.Put(static_cast<typename OutputByteStream::Ch>(c)); | os.Put(static_cast<typename OutputByteStream::Ch>(c)); | |||
} | } | |||
}; | }; | |||
/////////////////////////////////////////////////////////////////////////////// | /////////////////////////////////////////////////////////////////////////////// | |||
skipping to change at line 262 | skipping to change at line 290 | |||
os.Put(static_cast<typename OutputStream::Ch>(codepoint)); | os.Put(static_cast<typename OutputStream::Ch>(codepoint)); | |||
} | } | |||
else { | else { | |||
RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); | RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); | |||
unsigned v = codepoint - 0x10000; | unsigned v = codepoint - 0x10000; | |||
os.Put(static_cast<typename OutputStream::Ch>((v >> 10) | 0xD800)); | os.Put(static_cast<typename OutputStream::Ch>((v >> 10) | 0xD800)); | |||
os.Put((v & 0x3FF) | 0xDC00); | os.Put((v & 0x3FF) | 0xDC00); | |||
} | } | |||
} | } | |||
template<typename OutputStream> | ||||
static void EncodeUnsafe(OutputStream& os, unsigned codepoint) { | ||||
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 2); | ||||
if (codepoint <= 0xFFFF) { | ||||
RAPIDJSON_ASSERT(codepoint < 0xD800 || codepoint > 0xDFFF); // Code | ||||
point itself cannot be surrogate pair | ||||
PutUnsafe(os, static_cast<typename OutputStream::Ch>(codepoint)); | ||||
} | ||||
else { | ||||
RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); | ||||
unsigned v = codepoint - 0x10000; | ||||
PutUnsafe(os, static_cast<typename OutputStream::Ch>((v >> 10) | 0xD | ||||
800)); | ||||
PutUnsafe(os, (v & 0x3FF) | 0xDC00); | ||||
} | ||||
} | ||||
template <typename InputStream> | template <typename InputStream> | |||
static bool Decode(InputStream& is, unsigned* codepoint) { | static bool Decode(InputStream& is, unsigned* codepoint) { | |||
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 2); | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 2); | |||
Ch c = is.Take(); | typename InputStream::Ch c = is.Take(); | |||
if (c < 0xD800 || c > 0xDFFF) { | if (c < 0xD800 || c > 0xDFFF) { | |||
*codepoint = c; | *codepoint = static_cast<unsigned>(c); | |||
return true; | return true; | |||
} | } | |||
else if (c <= 0xDBFF) { | else if (c <= 0xDBFF) { | |||
*codepoint = (c & 0x3FF) << 10; | *codepoint = (static_cast<unsigned>(c) & 0x3FF) << 10; | |||
c = is.Take(); | c = is.Take(); | |||
*codepoint |= (c & 0x3FF); | *codepoint |= (static_cast<unsigned>(c) & 0x3FF); | |||
*codepoint += 0x10000; | *codepoint += 0x10000; | |||
return c >= 0xDC00 && c <= 0xDFFF; | return c >= 0xDC00 && c <= 0xDFFF; | |||
} | } | |||
return false; | return false; | |||
} | } | |||
template <typename InputStream, typename OutputStream> | template <typename InputStream, typename OutputStream> | |||
static bool Validate(InputStream& is, OutputStream& os) { | static bool Validate(InputStream& is, OutputStream& os) { | |||
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 2); | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 2); | |||
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 2); | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 2); | |||
Ch c; | typename InputStream::Ch c; | |||
os.Put(c = is.Take()); | os.Put(static_cast<typename OutputStream::Ch>(c = is.Take())); | |||
if (c < 0xD800 || c > 0xDFFF) | if (c < 0xD800 || c > 0xDFFF) | |||
return true; | return true; | |||
else if (c <= 0xDBFF) { | else if (c <= 0xDBFF) { | |||
os.Put(c = is.Take()); | os.Put(c = is.Take()); | |||
return c >= 0xDC00 && c <= 0xDFFF; | return c >= 0xDC00 && c <= 0xDFFF; | |||
} | } | |||
return false; | return false; | |||
} | } | |||
}; | }; | |||
//! UTF-16 little endian encoding. | //! UTF-16 little endian encoding. | |||
template<typename CharType = wchar_t> | template<typename CharType = wchar_t> | |||
struct UTF16LE : UTF16<CharType> { | struct UTF16LE : UTF16<CharType> { | |||
template <typename InputByteStream> | template <typename InputByteStream> | |||
static CharType TakeBOM(InputByteStream& is) { | static CharType TakeBOM(InputByteStream& is) { | |||
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | |||
CharType c = Take(is); | CharType c = Take(is); | |||
return (unsigned short)c == 0xFEFFu ? Take(is) : c; | return static_cast<uint16_t>(c) == 0xFEFFu ? Take(is) : c; | |||
} | } | |||
template <typename InputByteStream> | template <typename InputByteStream> | |||
static CharType Take(InputByteStream& is) { | static CharType Take(InputByteStream& is) { | |||
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | |||
CharType c = (unsigned char)is.Take(); | unsigned c = static_cast<uint8_t>(is.Take()); | |||
c |= (unsigned char)is.Take() << 8; | c |= static_cast<unsigned>(static_cast<uint8_t>(is.Take())) << 8; | |||
return c; | return static_cast<CharType>(c); | |||
} | } | |||
template <typename OutputByteStream> | template <typename OutputByteStream> | |||
static void PutBOM(OutputByteStream& os) { | static void PutBOM(OutputByteStream& os) { | |||
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); | |||
os.Put(0xFFu); os.Put(0xFEu); | os.Put(static_cast<typename OutputByteStream::Ch>(0xFFu)); | |||
os.Put(static_cast<typename OutputByteStream::Ch>(0xFEu)); | ||||
} | } | |||
template <typename OutputByteStream> | template <typename OutputByteStream> | |||
static void Put(OutputByteStream& os, CharType c) { | static void Put(OutputByteStream& os, CharType c) { | |||
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); | |||
os.Put(c & 0xFFu); | os.Put(static_cast<typename OutputByteStream::Ch>(static_cast<unsigned>( | |||
os.Put((c >> 8) & 0xFFu); | c) & 0xFFu)); | |||
os.Put(static_cast<typename OutputByteStream::Ch>((static_cast<unsigned> | ||||
(c) >> 8) & 0xFFu)); | ||||
} | } | |||
}; | }; | |||
//! UTF-16 big endian encoding. | //! UTF-16 big endian encoding. | |||
template<typename CharType = wchar_t> | template<typename CharType = wchar_t> | |||
struct UTF16BE : UTF16<CharType> { | struct UTF16BE : UTF16<CharType> { | |||
template <typename InputByteStream> | template <typename InputByteStream> | |||
static CharType TakeBOM(InputByteStream& is) { | static CharType TakeBOM(InputByteStream& is) { | |||
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | |||
CharType c = Take(is); | CharType c = Take(is); | |||
return (unsigned short)c == 0xFEFFu ? Take(is) : c; | return static_cast<uint16_t>(c) == 0xFEFFu ? Take(is) : c; | |||
} | } | |||
template <typename InputByteStream> | template <typename InputByteStream> | |||
static CharType Take(InputByteStream& is) { | static CharType Take(InputByteStream& is) { | |||
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | |||
CharType c = (unsigned char)is.Take() << 8; | unsigned c = static_cast<unsigned>(static_cast<uint8_t>(is.Take())) << 8 | |||
c |= (unsigned char)is.Take(); | ; | |||
return c; | c |= static_cast<uint8_t>(is.Take()); | |||
return static_cast<CharType>(c); | ||||
} | } | |||
template <typename OutputByteStream> | template <typename OutputByteStream> | |||
static void PutBOM(OutputByteStream& os) { | static void PutBOM(OutputByteStream& os) { | |||
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); | |||
os.Put(0xFEu); os.Put(0xFFu); | os.Put(static_cast<typename OutputByteStream::Ch>(0xFEu)); | |||
os.Put(static_cast<typename OutputByteStream::Ch>(0xFFu)); | ||||
} | } | |||
template <typename OutputByteStream> | template <typename OutputByteStream> | |||
static void Put(OutputByteStream& os, CharType c) { | static void Put(OutputByteStream& os, CharType c) { | |||
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); | |||
os.Put((c >> 8) & 0xFFu); | os.Put(static_cast<typename OutputByteStream::Ch>((static_cast<unsigned> | |||
os.Put(c & 0xFFu); | (c) >> 8) & 0xFFu)); | |||
os.Put(static_cast<typename OutputByteStream::Ch>(static_cast<unsigned>( | ||||
c) & 0xFFu)); | ||||
} | } | |||
}; | }; | |||
/////////////////////////////////////////////////////////////////////////////// | /////////////////////////////////////////////////////////////////////////////// | |||
// UTF32 | // UTF32 | |||
//! UTF-32 encoding. | //! UTF-32 encoding. | |||
/*! http://en.wikipedia.org/wiki/UTF-32 | /*! http://en.wikipedia.org/wiki/UTF-32 | |||
\tparam CharType Type for storing 32-bit UTF-32 data. Default is unsigned. C ++11 may use char32_t instead. | \tparam CharType Type for storing 32-bit UTF-32 data. Default is unsigned. C ++11 may use char32_t instead. | |||
\note implements Encoding concept | \note implements Encoding concept | |||
skipping to change at line 385 | skipping to change at line 430 | |||
enum { supportUnicode = 1 }; | enum { supportUnicode = 1 }; | |||
template<typename OutputStream> | template<typename OutputStream> | |||
static void Encode(OutputStream& os, unsigned codepoint) { | static void Encode(OutputStream& os, unsigned codepoint) { | |||
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 4); | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 4); | |||
RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); | RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); | |||
os.Put(codepoint); | os.Put(codepoint); | |||
} | } | |||
template<typename OutputStream> | ||||
static void EncodeUnsafe(OutputStream& os, unsigned codepoint) { | ||||
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 4); | ||||
RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); | ||||
PutUnsafe(os, codepoint); | ||||
} | ||||
template <typename InputStream> | template <typename InputStream> | |||
static bool Decode(InputStream& is, unsigned* codepoint) { | static bool Decode(InputStream& is, unsigned* codepoint) { | |||
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 4); | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 4); | |||
Ch c = is.Take(); | Ch c = is.Take(); | |||
*codepoint = c; | *codepoint = c; | |||
return c <= 0x10FFFF; | return c <= 0x10FFFF; | |||
} | } | |||
template <typename InputStream, typename OutputStream> | template <typename InputStream, typename OutputStream> | |||
static bool Validate(InputStream& is, OutputStream& os) { | static bool Validate(InputStream& is, OutputStream& os) { | |||
skipping to change at line 409 | skipping to change at line 461 | |||
} | } | |||
}; | }; | |||
//! UTF-32 little endian enocoding. | //! UTF-32 little endian enocoding. | |||
template<typename CharType = unsigned> | template<typename CharType = unsigned> | |||
struct UTF32LE : UTF32<CharType> { | struct UTF32LE : UTF32<CharType> { | |||
template <typename InputByteStream> | template <typename InputByteStream> | |||
static CharType TakeBOM(InputByteStream& is) { | static CharType TakeBOM(InputByteStream& is) { | |||
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | |||
CharType c = Take(is); | CharType c = Take(is); | |||
return (unsigned)c == 0x0000FEFFu ? Take(is) : c; | return static_cast<uint32_t>(c) == 0x0000FEFFu ? Take(is) : c; | |||
} | } | |||
template <typename InputByteStream> | template <typename InputByteStream> | |||
static CharType Take(InputByteStream& is) { | static CharType Take(InputByteStream& is) { | |||
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | |||
CharType c = (unsigned char)is.Take(); | unsigned c = static_cast<uint8_t>(is.Take()); | |||
c |= (unsigned char)is.Take() << 8; | c |= static_cast<unsigned>(static_cast<uint8_t>(is.Take())) << 8; | |||
c |= (unsigned char)is.Take() << 16; | c |= static_cast<unsigned>(static_cast<uint8_t>(is.Take())) << 16; | |||
c |= (unsigned char)is.Take() << 24; | c |= static_cast<unsigned>(static_cast<uint8_t>(is.Take())) << 24; | |||
return c; | return static_cast<CharType>(c); | |||
} | } | |||
template <typename OutputByteStream> | template <typename OutputByteStream> | |||
static void PutBOM(OutputByteStream& os) { | static void PutBOM(OutputByteStream& os) { | |||
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); | |||
os.Put(0xFFu); os.Put(0xFEu); os.Put(0x00u); os.Put(0x00u); | os.Put(static_cast<typename OutputByteStream::Ch>(0xFFu)); | |||
os.Put(static_cast<typename OutputByteStream::Ch>(0xFEu)); | ||||
os.Put(static_cast<typename OutputByteStream::Ch>(0x00u)); | ||||
os.Put(static_cast<typename OutputByteStream::Ch>(0x00u)); | ||||
} | } | |||
template <typename OutputByteStream> | template <typename OutputByteStream> | |||
static void Put(OutputByteStream& os, CharType c) { | static void Put(OutputByteStream& os, CharType c) { | |||
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); | |||
os.Put(c & 0xFFu); | os.Put(static_cast<typename OutputByteStream::Ch>(c & 0xFFu)); | |||
os.Put((c >> 8) & 0xFFu); | os.Put(static_cast<typename OutputByteStream::Ch>((c >> 8) & 0xFFu)); | |||
os.Put((c >> 16) & 0xFFu); | os.Put(static_cast<typename OutputByteStream::Ch>((c >> 16) & 0xFFu)); | |||
os.Put((c >> 24) & 0xFFu); | os.Put(static_cast<typename OutputByteStream::Ch>((c >> 24) & 0xFFu)); | |||
} | } | |||
}; | }; | |||
//! UTF-32 big endian encoding. | //! UTF-32 big endian encoding. | |||
template<typename CharType = unsigned> | template<typename CharType = unsigned> | |||
struct UTF32BE : UTF32<CharType> { | struct UTF32BE : UTF32<CharType> { | |||
template <typename InputByteStream> | template <typename InputByteStream> | |||
static CharType TakeBOM(InputByteStream& is) { | static CharType TakeBOM(InputByteStream& is) { | |||
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | |||
CharType c = Take(is); | CharType c = Take(is); | |||
return (unsigned)c == 0x0000FEFFu ? Take(is) : c; | return static_cast<uint32_t>(c) == 0x0000FEFFu ? Take(is) : c; | |||
} | } | |||
template <typename InputByteStream> | template <typename InputByteStream> | |||
static CharType Take(InputByteStream& is) { | static CharType Take(InputByteStream& is) { | |||
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | |||
CharType c = (unsigned char)is.Take() << 24; | unsigned c = static_cast<unsigned>(static_cast<uint8_t>(is.Take())) << 2 | |||
c |= (unsigned char)is.Take() << 16; | 4; | |||
c |= (unsigned char)is.Take() << 8; | c |= static_cast<unsigned>(static_cast<uint8_t>(is.Take())) << 16; | |||
c |= (unsigned char)is.Take(); | c |= static_cast<unsigned>(static_cast<uint8_t>(is.Take())) << 8; | |||
return c; | c |= static_cast<unsigned>(static_cast<uint8_t>(is.Take())); | |||
return static_cast<CharType>(c); | ||||
} | } | |||
template <typename OutputByteStream> | template <typename OutputByteStream> | |||
static void PutBOM(OutputByteStream& os) { | static void PutBOM(OutputByteStream& os) { | |||
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); | |||
os.Put(0x00u); os.Put(0x00u); os.Put(0xFEu); os.Put(0xFFu); | os.Put(static_cast<typename OutputByteStream::Ch>(0x00u)); | |||
os.Put(static_cast<typename OutputByteStream::Ch>(0x00u)); | ||||
os.Put(static_cast<typename OutputByteStream::Ch>(0xFEu)); | ||||
os.Put(static_cast<typename OutputByteStream::Ch>(0xFFu)); | ||||
} | } | |||
template <typename OutputByteStream> | template <typename OutputByteStream> | |||
static void Put(OutputByteStream& os, CharType c) { | static void Put(OutputByteStream& os, CharType c) { | |||
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); | |||
os.Put((c >> 24) & 0xFFu); | os.Put(static_cast<typename OutputByteStream::Ch>((c >> 24) & 0xFFu)); | |||
os.Put((c >> 16) & 0xFFu); | os.Put(static_cast<typename OutputByteStream::Ch>((c >> 16) & 0xFFu)); | |||
os.Put((c >> 8) & 0xFFu); | os.Put(static_cast<typename OutputByteStream::Ch>((c >> 8) & 0xFFu)); | |||
os.Put(c & 0xFFu); | os.Put(static_cast<typename OutputByteStream::Ch>(c & 0xFFu)); | |||
} | } | |||
}; | }; | |||
/////////////////////////////////////////////////////////////////////////////// | /////////////////////////////////////////////////////////////////////////////// | |||
// ASCII | // ASCII | |||
//! ASCII encoding. | //! ASCII encoding. | |||
/*! http://en.wikipedia.org/wiki/ASCII | /*! http://en.wikipedia.org/wiki/ASCII | |||
\tparam CharType Code unit for storing 7-bit ASCII data. Default is char. | \tparam CharType Code unit for storing 7-bit ASCII data. Default is char. | |||
\note implements Encoding concept | \note implements Encoding concept | |||
skipping to change at line 494 | skipping to change at line 552 | |||
typedef CharType Ch; | typedef CharType Ch; | |||
enum { supportUnicode = 0 }; | enum { supportUnicode = 0 }; | |||
template<typename OutputStream> | template<typename OutputStream> | |||
static void Encode(OutputStream& os, unsigned codepoint) { | static void Encode(OutputStream& os, unsigned codepoint) { | |||
RAPIDJSON_ASSERT(codepoint <= 0x7F); | RAPIDJSON_ASSERT(codepoint <= 0x7F); | |||
os.Put(static_cast<Ch>(codepoint & 0xFF)); | os.Put(static_cast<Ch>(codepoint & 0xFF)); | |||
} | } | |||
template<typename OutputStream> | ||||
static void EncodeUnsafe(OutputStream& os, unsigned codepoint) { | ||||
RAPIDJSON_ASSERT(codepoint <= 0x7F); | ||||
PutUnsafe(os, static_cast<Ch>(codepoint & 0xFF)); | ||||
} | ||||
template <typename InputStream> | template <typename InputStream> | |||
static bool Decode(InputStream& is, unsigned* codepoint) { | static bool Decode(InputStream& is, unsigned* codepoint) { | |||
unsigned char c = static_cast<unsigned char>(is.Take()); | uint8_t c = static_cast<uint8_t>(is.Take()); | |||
*codepoint = c; | *codepoint = c; | |||
return c <= 0X7F; | return c <= 0X7F; | |||
} | } | |||
template <typename InputStream, typename OutputStream> | template <typename InputStream, typename OutputStream> | |||
static bool Validate(InputStream& is, OutputStream& os) { | static bool Validate(InputStream& is, OutputStream& os) { | |||
unsigned char c = is.Take(); | uint8_t c = static_cast<uint8_t>(is.Take()); | |||
os.Put(c); | os.Put(static_cast<typename OutputStream::Ch>(c)); | |||
return c <= 0x7F; | return c <= 0x7F; | |||
} | } | |||
template <typename InputByteStream> | template <typename InputByteStream> | |||
static CharType TakeBOM(InputByteStream& is) { | static CharType TakeBOM(InputByteStream& is) { | |||
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | |||
Ch c = Take(is); | uint8_t c = static_cast<uint8_t>(Take(is)); | |||
return c; | return static_cast<Ch>(c); | |||
} | } | |||
template <typename InputByteStream> | template <typename InputByteStream> | |||
static Ch Take(InputByteStream& is) { | static Ch Take(InputByteStream& is) { | |||
RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | |||
return is.Take(); | return static_cast<Ch>(is.Take()); | |||
} | } | |||
template <typename OutputByteStream> | template <typename OutputByteStream> | |||
static void PutBOM(OutputByteStream& os) { | static void PutBOM(OutputByteStream& os) { | |||
RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); | |||
(void)os; | (void)os; | |||
} | } | |||
template <typename OutputByteStream> | template <typename OutputByteStream> | |||
static void Put(OutputByteStream& os, Ch c) { | static void Put(OutputByteStream& os, Ch c) { | |||
skipping to change at line 564 | skipping to change at line 628 | |||
#define RAPIDJSON_ENCODINGS_FUNC(x) UTF8<Ch>::x, UTF16LE<Ch>::x, UTF16BE<Ch>::x, UTF32LE<Ch>::x, UTF32BE<Ch>::x | #define RAPIDJSON_ENCODINGS_FUNC(x) UTF8<Ch>::x, UTF16LE<Ch>::x, UTF16BE<Ch>::x, UTF32LE<Ch>::x, UTF32BE<Ch>::x | |||
template<typename OutputStream> | template<typename OutputStream> | |||
RAPIDJSON_FORCEINLINE static void Encode(OutputStream& os, unsigned codepoin t) { | RAPIDJSON_FORCEINLINE static void Encode(OutputStream& os, unsigned codepoin t) { | |||
typedef void (*EncodeFunc)(OutputStream&, unsigned); | typedef void (*EncodeFunc)(OutputStream&, unsigned); | |||
static const EncodeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Encode) }; | static const EncodeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Encode) }; | |||
(*f[os.GetType()])(os, codepoint); | (*f[os.GetType()])(os, codepoint); | |||
} | } | |||
template<typename OutputStream> | ||||
RAPIDJSON_FORCEINLINE static void EncodeUnsafe(OutputStream& os, unsigned co | ||||
depoint) { | ||||
typedef void (*EncodeFunc)(OutputStream&, unsigned); | ||||
static const EncodeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(EncodeUnsafe) } | ||||
; | ||||
(*f[os.GetType()])(os, codepoint); | ||||
} | ||||
template <typename InputStream> | template <typename InputStream> | |||
RAPIDJSON_FORCEINLINE static bool Decode(InputStream& is, unsigned* codepoin t) { | RAPIDJSON_FORCEINLINE static bool Decode(InputStream& is, unsigned* codepoin t) { | |||
typedef bool (*DecodeFunc)(InputStream&, unsigned*); | typedef bool (*DecodeFunc)(InputStream&, unsigned*); | |||
static const DecodeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Decode) }; | static const DecodeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Decode) }; | |||
return (*f[is.GetType()])(is, codepoint); | return (*f[is.GetType()])(is, codepoint); | |||
} | } | |||
template <typename InputStream, typename OutputStream> | template <typename InputStream, typename OutputStream> | |||
RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os ) { | RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os ) { | |||
typedef bool (*ValidateFunc)(InputStream&, OutputStream&); | typedef bool (*ValidateFunc)(InputStream&, OutputStream&); | |||
skipping to change at line 597 | skipping to change at line 668 | |||
//! Take one Unicode codepoint from source encoding, convert it to target en coding and put it to the output stream. | //! Take one Unicode codepoint from source encoding, convert it to target en coding and put it to the output stream. | |||
template<typename InputStream, typename OutputStream> | template<typename InputStream, typename OutputStream> | |||
RAPIDJSON_FORCEINLINE static bool Transcode(InputStream& is, OutputStream& o s) { | RAPIDJSON_FORCEINLINE static bool Transcode(InputStream& is, OutputStream& o s) { | |||
unsigned codepoint; | unsigned codepoint; | |||
if (!SourceEncoding::Decode(is, &codepoint)) | if (!SourceEncoding::Decode(is, &codepoint)) | |||
return false; | return false; | |||
TargetEncoding::Encode(os, codepoint); | TargetEncoding::Encode(os, codepoint); | |||
return true; | return true; | |||
} | } | |||
template<typename InputStream, typename OutputStream> | ||||
RAPIDJSON_FORCEINLINE static bool TranscodeUnsafe(InputStream& is, OutputStr | ||||
eam& os) { | ||||
unsigned codepoint; | ||||
if (!SourceEncoding::Decode(is, &codepoint)) | ||||
return false; | ||||
TargetEncoding::EncodeUnsafe(os, codepoint); | ||||
return true; | ||||
} | ||||
//! Validate one Unicode codepoint from an encoded stream. | //! Validate one Unicode codepoint from an encoded stream. | |||
template<typename InputStream, typename OutputStream> | template<typename InputStream, typename OutputStream> | |||
RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os ) { | RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os ) { | |||
return Transcode(is, os); // Since source/target encoding is different , must transcode. | return Transcode(is, os); // Since source/target encoding is different , must transcode. | |||
} | } | |||
}; | }; | |||
// Forward declaration. | ||||
template<typename Stream> | ||||
inline void PutUnsafe(Stream& stream, typename Stream::Ch c); | ||||
//! Specialization of Transcoder with same source and target encoding. | //! Specialization of Transcoder with same source and target encoding. | |||
template<typename Encoding> | template<typename Encoding> | |||
struct Transcoder<Encoding, Encoding> { | struct Transcoder<Encoding, Encoding> { | |||
template<typename InputStream, typename OutputStream> | template<typename InputStream, typename OutputStream> | |||
RAPIDJSON_FORCEINLINE static bool Transcode(InputStream& is, OutputStream& o s) { | RAPIDJSON_FORCEINLINE static bool Transcode(InputStream& is, OutputStream& o s) { | |||
os.Put(is.Take()); // Just copy one code unit. This semantic is differe nt from primary template class. | os.Put(is.Take()); // Just copy one code unit. This semantic is differe nt from primary template class. | |||
return true; | return true; | |||
} | } | |||
template<typename InputStream, typename OutputStream> | template<typename InputStream, typename OutputStream> | |||
RAPIDJSON_FORCEINLINE static bool TranscodeUnsafe(InputStream& is, OutputStr | ||||
eam& os) { | ||||
PutUnsafe(os, is.Take()); // Just copy one code unit. This semantic is | ||||
different from primary template class. | ||||
return true; | ||||
} | ||||
template<typename InputStream, typename OutputStream> | ||||
RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os ) { | RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os ) { | |||
return Encoding::Validate(is, os); // source/target encoding are the sa me | return Encoding::Validate(is, os); // source/target encoding are the sa me | |||
} | } | |||
}; | }; | |||
RAPIDJSON_NAMESPACE_END | RAPIDJSON_NAMESPACE_END | |||
#if defined(__GNUC__) || defined(_MSV_VER) | #if defined(__GNUC__) || defined(_MSC_VER) | |||
RAPIDJSON_DIAG_POP | RAPIDJSON_DIAG_POP | |||
#endif | #endif | |||
#endif // RAPIDJSON_ENCODINGS_H_ | #endif // RAPIDJSON_ENCODINGS_H_ | |||
End of changes. 45 change blocks. | ||||
64 lines changed or deleted | 168 lines changed or added |