15 #ifndef RAPIDJSON_ENCODINGS_H_ 16 #define RAPIDJSON_ENCODINGS_H_ 22 RAPIDJSON_DIAG_OFF(4244)
23 RAPIDJSON_DIAG_OFF(4702)
24 #elif defined(__GNUC__) 26 RAPIDJSON_DIAG_OFF(effc++)
95 template<
typename CharType =
char>
101 template<
typename OutputStream>
104 os.Put(static_cast<Ch>(
codepoint & 0xFF));
106 os.Put(static_cast<Ch>(0xC0 | ((
codepoint >> 6) & 0xFF)));
107 os.Put(static_cast<Ch>(0x80 | ((
codepoint & 0x3F))));
110 os.Put(static_cast<Ch>(0xE0 | ((
codepoint >> 12) & 0xFF)));
111 os.Put(static_cast<Ch>(0x80 | ((
codepoint >> 6) & 0x3F)));
112 os.Put(static_cast<Ch>(0x80 | (
codepoint & 0x3F)));
116 os.Put(static_cast<Ch>(0xF0 | ((
codepoint >> 18) & 0xFF)));
117 os.Put(static_cast<Ch>(0x80 | ((
codepoint >> 12) & 0x3F)));
118 os.Put(static_cast<Ch>(0x80 | ((
codepoint >> 6) & 0x3F)));
119 os.Put(static_cast<Ch>(0x80 | (
codepoint & 0x3F)));
123 template<
typename OutputStream>
145 template <
typename InputStream>
147 #define COPY() c = is.Take(); *codepoint = (*codepoint << 6) | (static_cast<unsigned char>(c) & 0x3Fu) 148 #define TRANS(mask) result &= ((GetRange(static_cast<unsigned char>(c)) & mask) != 0) 149 #define TAIL() COPY(); TRANS(0x70) 150 typename InputStream::Ch
c = is.Take();
156 unsigned char type =
GetRange(static_cast<unsigned char>(
c));
171 default:
return false;
178 template <
typename InputStream,
typename OutputStream>
179 static bool Validate(InputStream& is, OutputStream& os) {
180 #define COPY() os.Put(c = is.Take()) 181 #define TRANS(mask) result &= ((GetRange(static_cast<unsigned char>(c)) & mask) != 0) 182 #define TAIL() COPY(); TRANS(0x70) 189 switch (
GetRange(static_cast<unsigned char>(
c))) {
197 default:
return false;
207 static const unsigned char type[] = {
208 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
209 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
210 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
211 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
212 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,
213 0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,
214 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
215 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
216 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,
217 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,
222 template <
typename InputByteStream>
223 static CharType
TakeBOM(InputByteStream& is) {
225 typename InputByteStream::Ch
c =
Take(is);
226 if (static_cast<unsigned char>(
c) != 0xEFu)
return c;
228 if (static_cast<unsigned char>(
c) != 0xBBu)
return c;
230 if (static_cast<unsigned char>(
c) != 0xBFu)
return c;
235 template <
typename InputByteStream>
238 return static_cast<Ch>(is.Take());
241 template <
typename OutputByteStream>
242 static void PutBOM(OutputByteStream& os) {
244 os.Put(static_cast<typename OutputByteStream::Ch>(0xEFu));
245 os.Put(static_cast<typename OutputByteStream::Ch>(0xBBu));
246 os.Put(static_cast<typename OutputByteStream::Ch>(0xBFu));
249 template <
typename OutputByteStream>
250 static void Put(OutputByteStream& os,
Ch c) {
252 os.Put(static_cast<typename OutputByteStream::Ch>(
c));
268 template<
typename CharType =
wchar_t>
275 template<
typename OutputStream>
280 os.Put(static_cast<typename OutputStream::Ch>(
codepoint));
285 os.Put(static_cast<typename OutputStream::Ch>((
v >> 10) | 0xD800));
286 os.Put((
v & 0x3FF) | 0xDC00);
291 template<
typename OutputStream>
301 PutUnsafe(os, static_cast<typename OutputStream::Ch>((
v >> 10) | 0xD800));
306 template <
typename InputStream>
309 typename InputStream::Ch
c = is.Take();
314 else if (
c <= 0xDBFF) {
315 *
codepoint = (
static_cast<unsigned>(
c) & 0x3FF) << 10;
317 *
codepoint |= (
static_cast<unsigned>(
c) & 0x3FF);
319 return c >= 0xDC00 &&
c <= 0xDFFF;
324 template <
typename InputStream,
typename OutputStream>
325 static bool Validate(InputStream& is, OutputStream& os) {
328 typename InputStream::Ch
c;
329 os.Put(static_cast<typename OutputStream::Ch>(
c = is.Take()));
332 else if (
c <= 0xDBFF) {
333 os.Put(
c = is.Take());
334 return c >= 0xDC00 &&
c <= 0xDFFF;
341 template<
typename CharType =
wchar_t>
343 template <
typename InputByteStream>
344 static CharType
TakeBOM(InputByteStream& is) {
346 CharType
c =
Take(is);
350 template <
typename InputByteStream>
351 static CharType
Take(InputByteStream& is) {
353 unsigned c =
static_cast<uint8_t>(is.Take());
354 c |=
static_cast<unsigned>(
static_cast<uint8_t>(is.Take())) << 8;
355 return static_cast<CharType
>(
c);
358 template <
typename OutputByteStream>
359 static void PutBOM(OutputByteStream& os) {
361 os.Put(static_cast<typename OutputByteStream::Ch>(0xFFu));
362 os.Put(static_cast<typename OutputByteStream::Ch>(0xFEu));
365 template <
typename OutputByteStream>
366 static void Put(OutputByteStream& os, CharType
c) {
368 os.Put(static_cast<typename OutputByteStream::Ch>(static_cast<unsigned>(
c) & 0xFFu));
369 os.Put(static_cast<typename OutputByteStream::Ch>((static_cast<unsigned>(
c) >> 8) & 0xFFu));
374 template<
typename CharType =
wchar_t>
376 template <
typename InputByteStream>
377 static CharType
TakeBOM(InputByteStream& is) {
379 CharType
c =
Take(is);
383 template <
typename InputByteStream>
384 static CharType
Take(InputByteStream& is) {
386 unsigned c =
static_cast<unsigned>(
static_cast<uint8_t>(is.Take())) << 8;
387 c |=
static_cast<uint8_t>(is.Take());
388 return static_cast<CharType
>(
c);
391 template <
typename OutputByteStream>
392 static void PutBOM(OutputByteStream& os) {
394 os.Put(static_cast<typename OutputByteStream::Ch>(0xFEu));
395 os.Put(static_cast<typename OutputByteStream::Ch>(0xFFu));
398 template <
typename OutputByteStream>
399 static void Put(OutputByteStream& os, CharType
c) {
401 os.Put(static_cast<typename OutputByteStream::Ch>((static_cast<unsigned>(
c) >> 8) & 0xFFu));
402 os.Put(static_cast<typename OutputByteStream::Ch>(static_cast<unsigned>(
c) & 0xFFu));
417 template<
typename CharType =
unsigned>
424 template<
typename OutputStream>
431 template<
typename OutputStream>
438 template <
typename InputStream>
443 return c <= 0x10FFFF;
446 template <
typename InputStream,
typename OutputStream>
447 static bool Validate(InputStream& is, OutputStream& os) {
450 os.Put(
c = is.Take());
451 return c <= 0x10FFFF;
456 template<
typename CharType =
unsigned>
458 template <
typename InputByteStream>
459 static CharType
TakeBOM(InputByteStream& is) {
461 CharType
c =
Take(is);
465 template <
typename InputByteStream>
466 static CharType
Take(InputByteStream& is) {
468 unsigned c =
static_cast<uint8_t>(is.Take());
469 c |=
static_cast<unsigned>(
static_cast<uint8_t>(is.Take())) << 8;
470 c |=
static_cast<unsigned>(
static_cast<uint8_t>(is.Take())) << 16;
471 c |=
static_cast<unsigned>(
static_cast<uint8_t>(is.Take())) << 24;
472 return static_cast<CharType
>(
c);
475 template <
typename OutputByteStream>
476 static void PutBOM(OutputByteStream& os) {
478 os.Put(static_cast<typename OutputByteStream::Ch>(0xFFu));
479 os.Put(static_cast<typename OutputByteStream::Ch>(0xFEu));
480 os.Put(static_cast<typename OutputByteStream::Ch>(0x00u));
481 os.Put(static_cast<typename OutputByteStream::Ch>(0x00u));
484 template <
typename OutputByteStream>
485 static void Put(OutputByteStream& os, CharType
c) {
487 os.Put(static_cast<typename OutputByteStream::Ch>(
c & 0xFFu));
488 os.Put(static_cast<typename OutputByteStream::Ch>((
c >> 8) & 0xFFu));
489 os.Put(static_cast<typename OutputByteStream::Ch>((
c >> 16) & 0xFFu));
490 os.Put(static_cast<typename OutputByteStream::Ch>((
c >> 24) & 0xFFu));
495 template<
typename CharType =
unsigned>
497 template <
typename InputByteStream>
498 static CharType
TakeBOM(InputByteStream& is) {
500 CharType
c =
Take(is);
504 template <
typename InputByteStream>
505 static CharType
Take(InputByteStream& is) {
507 unsigned c =
static_cast<unsigned>(
static_cast<uint8_t>(is.Take())) << 24;
508 c |=
static_cast<unsigned>(
static_cast<uint8_t>(is.Take())) << 16;
509 c |=
static_cast<unsigned>(
static_cast<uint8_t>(is.Take())) << 8;
510 c |=
static_cast<unsigned>(
static_cast<uint8_t>(is.Take()));
511 return static_cast<CharType
>(
c);
514 template <
typename OutputByteStream>
515 static void PutBOM(OutputByteStream& os) {
517 os.Put(static_cast<typename OutputByteStream::Ch>(0x00u));
518 os.Put(static_cast<typename OutputByteStream::Ch>(0x00u));
519 os.Put(static_cast<typename OutputByteStream::Ch>(0xFEu));
520 os.Put(static_cast<typename OutputByteStream::Ch>(0xFFu));
523 template <
typename OutputByteStream>
524 static void Put(OutputByteStream& os, CharType
c) {
526 os.Put(static_cast<typename OutputByteStream::Ch>((
c >> 24) & 0xFFu));
527 os.Put(static_cast<typename OutputByteStream::Ch>((
c >> 16) & 0xFFu));
528 os.Put(static_cast<typename OutputByteStream::Ch>((
c >> 8) & 0xFFu));
529 os.Put(static_cast<typename OutputByteStream::Ch>(
c & 0xFFu));
541 template<
typename CharType =
char>
547 template<
typename OutputStream>
550 os.Put(static_cast<Ch>(
codepoint & 0xFF));
553 template<
typename OutputStream>
559 template <
typename InputStream>
566 template <
typename InputStream,
typename OutputStream>
567 static bool Validate(InputStream& is, OutputStream& os) {
569 os.Put(static_cast<typename OutputStream::Ch>(
c));
573 template <
typename InputByteStream>
574 static CharType
TakeBOM(InputByteStream& is) {
577 return static_cast<Ch>(
c);
580 template <
typename InputByteStream>
583 return static_cast<Ch>(is.Take());
586 template <
typename OutputByteStream>
587 static void PutBOM(OutputByteStream& os) {
592 template <
typename OutputByteStream>
593 static void Put(OutputByteStream& os,
Ch c) {
595 os.Put(static_cast<typename OutputByteStream::Ch>(
c));
614 template<
typename CharType>
620 #define RAPIDJSON_ENCODINGS_FUNC(x) UTF8<Ch>::x, UTF16LE<Ch>::x, UTF16BE<Ch>::x, UTF32LE<Ch>::x, UTF32BE<Ch>::x 622 template<
typename OutputStream>
624 typedef void (*EncodeFunc)(OutputStream&, unsigned);
629 template<
typename OutputStream>
631 typedef void (*EncodeFunc)(OutputStream&, unsigned);
636 template <
typename InputStream>
638 typedef bool (*DecodeFunc)(InputStream&,
unsigned*);
643 template <
typename InputStream,
typename OutputStream>
644 RAPIDJSON_FORCEINLINE
static bool Validate(InputStream& is, OutputStream& os) {
645 typedef bool (*ValidateFunc)(InputStream&, OutputStream&);
647 return (*
f[is.GetType()])(is, os);
650 #undef RAPIDJSON_ENCODINGS_FUNC 657 template<
typename SourceEncoding,
typename TargetEncoding>
660 template<
typename InputStream,
typename OutputStream>
661 RAPIDJSON_FORCEINLINE
static bool Transcode(InputStream& is, OutputStream& os) {
663 if (!SourceEncoding::Decode(is, &
codepoint))
669 template<
typename InputStream,
typename OutputStream>
670 RAPIDJSON_FORCEINLINE
static bool TranscodeUnsafe(InputStream& is, OutputStream& os) {
672 if (!SourceEncoding::Decode(is, &
codepoint))
674 TargetEncoding::EncodeUnsafe(os,
codepoint);
679 template<
typename InputStream,
typename OutputStream>
680 RAPIDJSON_FORCEINLINE
static bool Validate(InputStream& is, OutputStream& os) {
686 template<
typename Stream>
690 template<
typename Encoding>
692 template<
typename InputStream,
typename OutputStream>
693 RAPIDJSON_FORCEINLINE
static bool Transcode(InputStream& is, OutputStream& os) {
698 template<
typename InputStream,
typename OutputStream>
699 RAPIDJSON_FORCEINLINE
static bool TranscodeUnsafe(InputStream& is, OutputStream& os) {
704 template<
typename InputStream,
typename OutputStream>
705 RAPIDJSON_FORCEINLINE
static bool Validate(InputStream& is, OutputStream& os) {
706 return Encoding::Validate(is, os);
712 #if defined(__GNUC__) || defined(_MSC_VER) 716 #endif // RAPIDJSON_ENCODINGS_H_ static RAPIDJSON_FORCEINLINE bool Validate(InputStream &is, OutputStream &os)
Definition: encodings.h:644
static unsigned char GetRange(unsigned char c)
Definition: encodings.h:204
static CharType TakeBOM(InputByteStream &is)
Definition: encodings.h:377
static RAPIDJSON_FORCEINLINE bool TranscodeUnsafe(InputStream &is, OutputStream &os)
Definition: encodings.h:670
Definition: encodings.h:545
static void Put(OutputByteStream &os, CharType c)
Definition: encodings.h:399
static void PutBOM(OutputByteStream &os)
Definition: encodings.h:392
void PutUnsafe(Stream &stream, typename Stream::Ch c)
Write character to a stream, presuming buffer is reserved.
Definition: stream.h:91
static void PutBOM(OutputByteStream &os)
Definition: encodings.h:587
static void PutBOM(OutputByteStream &os)
Definition: encodings.h:242
ASCII encoding.
Definition: encodings.h:542
common definitions and configuration
Definition: encodings.h:99
static void Put(OutputByteStream &os, Ch c)
Definition: encodings.h:593
UTF-32 encoding.
Definition: encodings.h:418
CharType Ch
Definition: encodings.h:543
UTF-16 encoding.
Definition: encodings.h:269
static bool Decode(InputStream &is, unsigned *codepoint)
Definition: encodings.h:560
static int codepoint(lua_State *L)
Definition: lutf8lib.c:100
static void PutBOM(OutputByteStream &os)
Definition: encodings.h:359
Definition: encodings.h:273
UTF-32 big endian.
Definition: encodings.h:608
Encoding conversion.
Definition: encodings.h:658
GLfloat f
Definition: glext.h:8207
static void Put(OutputByteStream &os, CharType c)
Definition: encodings.h:366
static RAPIDJSON_FORCEINLINE void EncodeUnsafe(OutputStream &os, unsigned codepoint)
Definition: encodings.h:630
static void Encode(OutputStream &os, unsigned codepoint)
Definition: encodings.h:102
UTF-32 big endian encoding.
Definition: encodings.h:496
static INLINE float overflow(void)
Definition: vector_2.h:52
static bool Validate(InputStream &is, OutputStream &os)
Definition: encodings.h:567
CharType Ch
Definition: encodings.h:419
Definition: encodings.h:618
static void PutBOM(OutputByteStream &os)
Definition: encodings.h:515
typedef void(__stdcall *PFN_DESTRUCTION_CALLBACK)(void *pData)
static void EncodeUnsafe(OutputStream &os, unsigned codepoint)
Definition: encodings.h:554
CharType Ch
Definition: encodings.h:270
UTF-16 little endian.
Definition: encodings.h:605
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:119
UTF-32 little endian.
Definition: encodings.h:607
const GLubyte * c
Definition: glext.h:9812
static void EncodeUnsafe(OutputStream &os, unsigned codepoint)
Definition: encodings.h:432
typedef bool(RETRO_CALLCONV *retro_replace_image_index_t)(unsigned index
static bool Decode(InputStream &is, unsigned *codepoint)
Definition: encodings.h:307
static bool Validate(InputStream &is, OutputStream &os)
Definition: encodings.h:179
static CharType TakeBOM(InputByteStream &is)
Definition: encodings.h:498
static bool Decode(InputStream &is, unsigned *codepoint)
Definition: encodings.h:439
GLenum type
Definition: glext.h:6233
RAPIDJSON_STATIC_ASSERT(sizeof(Ch) >=4)
static void Encode(OutputStream &os, unsigned codepoint)
Definition: encodings.h:425
static RAPIDJSON_FORCEINLINE bool Transcode(InputStream &is, OutputStream &os)
Take one Unicode codepoint from source encoding, convert it to target encoding and put it to the outp...
Definition: encodings.h:661
static void Put(OutputByteStream &os, CharType c)
Definition: encodings.h:485
static CharType Take(InputByteStream &is)
Definition: encodings.h:384
static void EncodeUnsafe(OutputStream &os, unsigned codepoint)
Definition: encodings.h:292
static void Put(OutputByteStream &os, CharType c)
Definition: encodings.h:524
static void Encode(OutputStream &os, unsigned codepoint)
Definition: encodings.h:276
static bool Decode(InputStream &is, unsigned *codepoint)
Definition: encodings.h:146
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:402
GLuint64EXT * result
Definition: glext.h:12211
static CharType TakeBOM(InputByteStream &is)
Definition: encodings.h:459
static Ch Take(InputByteStream &is)
Definition: encodings.h:236
static CharType Take(InputByteStream &is)
Definition: encodings.h:351
static void EncodeUnsafe(OutputStream &os, unsigned codepoint)
Definition: encodings.h:124
static CharType TakeBOM(InputByteStream &is)
Definition: encodings.h:223
static CharType Take(InputByteStream &is)
Definition: encodings.h:466
RAPIDJSON_STATIC_ASSERT(sizeof(Ch) >=2)
static void Encode(OutputStream &os, unsigned codepoint)
Definition: encodings.h:548
UTFType
Runtime-specified UTF encoding type of a stream.
Definition: encodings.h:603
CharType Ch
Definition: encodings.h:616
const GLdouble * v
Definition: glext.h:6391
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:116
UTF-16 big endian encoding.
Definition: encodings.h:375
UTF-8.
Definition: encodings.h:604
GLuint GLuint stream
Definition: glext.h:8189
static CharType Take(InputByteStream &is)
Definition: encodings.h:505
static RAPIDJSON_FORCEINLINE bool TranscodeUnsafe(InputStream &is, OutputStream &os)
Definition: encodings.h:699
static RAPIDJSON_FORCEINLINE bool Decode(InputStream &is, unsigned *codepoint)
Definition: encodings.h:637
Definition: encodings.h:422
Dynamically select encoding according to stream's runtime-specified UTF encoding type.
Definition: encodings.h:615
CharType Ch
Definition: encodings.h:97
static bool Validate(InputStream &is, OutputStream &os)
Definition: encodings.h:325
#define RAPIDJSON_STATIC_ASSERT(x)
(Internal) macro to check for conditions at compile-time
Definition: rapidjson.h:437
byte Encoding
Definition: jsonsax_full.c:106
static RAPIDJSON_FORCEINLINE bool Transcode(InputStream &is, OutputStream &os)
Definition: encodings.h:693
#define RAPIDJSON_ENCODINGS_FUNC(x)
Definition: encodings.h:620
UTF-16 big endian.
Definition: encodings.h:606
static void PutBOM(OutputByteStream &os)
Definition: encodings.h:476
static CharType TakeBOM(InputByteStream &is)
Definition: encodings.h:344
UTF-8 encoding.
Definition: encodings.h:96
static RAPIDJSON_FORCEINLINE bool Validate(InputStream &is, OutputStream &os)
Validate one Unicode codepoint from an encoded stream.
Definition: encodings.h:680
static Ch Take(InputByteStream &is)
Definition: encodings.h:581
static RAPIDJSON_FORCEINLINE void Encode(OutputStream &os, unsigned codepoint)
Definition: encodings.h:623
static void Put(OutputByteStream &os, Ch c)
Definition: encodings.h:250
unsigned short uint16_t
Definition: stdint.h:125
unsigned char uint8_t
Definition: stdint.h:124
unsigned int uint32_t
Definition: stdint.h:126
static CharType TakeBOM(InputByteStream &is)
Definition: encodings.h:574
UTF-32 little endian enocoding.
Definition: encodings.h:457
static RAPIDJSON_FORCEINLINE bool Validate(InputStream &is, OutputStream &os)
Definition: encodings.h:705
static bool Validate(InputStream &is, OutputStream &os)
Definition: encodings.h:447
UTF-16 little endian encoding.
Definition: encodings.h:342