21 #ifndef RAPIDJSON_ENCODINGS_H_ 22 #define RAPIDJSON_ENCODINGS_H_ 28 RAPIDJSON_DIAG_OFF(4244)
29 RAPIDJSON_DIAG_OFF(4702)
30 #elif defined(__GNUC__) 32 RAPIDJSON_DIAG_OFF(effc++)
100 template<
typename CharType =
char>
106 template<
typename OutputStream>
107 static void Encode(OutputStream& os,
unsigned codepoint) {
108 if (codepoint <= 0x7F)
109 os.Put(static_cast<Ch>(codepoint & 0xFF));
110 else if (codepoint <= 0x7FF) {
111 os.Put(static_cast<Ch>(0xC0 | ((codepoint >> 6) & 0xFF)));
112 os.Put(static_cast<Ch>(0x80 | ((codepoint & 0x3F))));
114 else if (codepoint <= 0xFFFF) {
115 os.Put(static_cast<Ch>(0xE0 | ((codepoint >> 12) & 0xFF)));
116 os.Put(static_cast<Ch>(0x80 | ((codepoint >> 6) & 0x3F)));
117 os.Put(static_cast<Ch>(0x80 | (codepoint & 0x3F)));
121 os.Put(static_cast<Ch>(0xF0 | ((codepoint >> 18) & 0xFF)));
122 os.Put(static_cast<Ch>(0x80 | ((codepoint >> 12) & 0x3F)));
123 os.Put(static_cast<Ch>(0x80 | ((codepoint >> 6) & 0x3F)));
124 os.Put(static_cast<Ch>(0x80 | (codepoint & 0x3F)));
128 template <
typename InputStream>
129 static bool Decode(InputStream&
is,
unsigned* codepoint) {
130 #define COPY() c = is.Take(); *codepoint = (*codepoint << 6) | ((unsigned char)c & 0x3Fu) 131 #define TRANS(mask) result &= ((GetRange((unsigned char)c) & mask) != 0) 132 #define TAIL() COPY(); TRANS(0x70) 135 *codepoint = (
unsigned char)c;
140 *codepoint = (0xFF >>
type) & (
unsigned char)
c;
150 default:
return false;
157 template <
typename InputStream,
typename OutputStream>
159 #define COPY() os.Put(c = is.Take()) 160 #define TRANS(mask) result &= ((GetRange((unsigned char)c) & mask) != 0) 161 #define TAIL() COPY(); TRANS(0x70) 168 switch (
GetRange((
unsigned char)c)) {
176 default:
return false;
186 static const unsigned char type[] = {
187 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,
188 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,
189 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,
190 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,
191 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,
192 0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,
193 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
194 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
195 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,
196 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,
201 template <
typename InputByteStream>
205 if ((
unsigned char)c != 0xEFu)
return c;
207 if ((
unsigned char)c != 0xBBu)
return c;
209 if ((
unsigned char)c != 0xBFu)
return c;
214 template <
typename InputByteStream>
220 template <
typename OutputByteStream>
221 static void PutBOM(OutputByteStream& os) {
223 os.Put(0xEFu); os.Put(0xBBu); os.Put(0xBFu);
226 template <
typename OutputByteStream>
227 static void Put(OutputByteStream& os, Ch
c) {
229 os.Put(static_cast<typename OutputByteStream::Ch>(c));
245 template<
typename CharType =
wchar_t>
252 template<
typename OutputStream>
253 static void Encode(OutputStream& os,
unsigned codepoint) {
255 if (codepoint <= 0xFFFF) {
257 os.Put(static_cast<typename OutputStream::Ch>(codepoint));
261 unsigned v = codepoint - 0x10000;
262 os.Put(static_cast<typename OutputStream::Ch>((v >> 10) | 0xD800));
263 os.Put((v & 0x3FF) | 0xDC00);
267 template <
typename InputStream>
268 static bool Decode(InputStream&
is,
unsigned* codepoint) {
271 if (c < 0xD800 || c > 0xDFFF) {
275 else if (c <= 0xDBFF) {
276 *codepoint = (c & 0x3FF) << 10;
278 *codepoint |= (c & 0x3FF);
279 *codepoint += 0x10000;
280 return c >= 0xDC00 && c <= 0xDFFF;
285 template <
typename InputStream,
typename OutputStream>
290 os.Put(c = is.Take());
291 if (c < 0xD800 || c > 0xDFFF)
293 else if (c <= 0xDBFF) {
294 os.Put(c = is.Take());
295 return c >= 0xDC00 && c <= 0xDFFF;
302 template<
typename CharType =
wchar_t>
304 template <
typename InputByteStream>
307 CharType
c =
Take(is);
308 return (
unsigned short)c == 0xFEFF
u ?
Take(is) :
c;
311 template <
typename InputByteStream>
312 static CharType
Take(InputByteStream&
is) {
314 CharType
c = (
unsigned char)is.Take();
315 c |= (
unsigned char)is.Take() << 8;
319 template <
typename OutputByteStream>
320 static void PutBOM(OutputByteStream& os) {
322 os.Put(0xFFu); os.Put(0xFEu);
325 template <
typename OutputByteStream>
326 static void Put(OutputByteStream& os, CharType
c) {
329 os.Put((c >> 8) & 0xFFu);
334 template<
typename CharType =
wchar_t>
336 template <
typename InputByteStream>
339 CharType
c =
Take(is);
340 return (
unsigned short)c == 0xFEFF
u ?
Take(is) :
c;
343 template <
typename InputByteStream>
344 static CharType
Take(InputByteStream&
is) {
346 CharType
c = (
unsigned char)is.Take() << 8;
347 c |= (
unsigned char)is.Take();
351 template <
typename OutputByteStream>
352 static void PutBOM(OutputByteStream& os) {
354 os.Put(0xFEu); os.Put(0xFFu);
357 template <
typename OutputByteStream>
358 static void Put(OutputByteStream& os, CharType
c) {
360 os.Put((c >> 8) & 0xFFu);
376 template<
typename CharType =
unsigned>
383 template<
typename OutputStream>
384 static void Encode(OutputStream& os,
unsigned codepoint) {
390 template <
typename InputStream>
391 static bool Decode(InputStream&
is,
unsigned* codepoint) {
395 return c <= 0x10FFFF;
398 template <
typename InputStream,
typename OutputStream>
402 os.Put(c = is.Take());
403 return c <= 0x10FFFF;
408 template<
typename CharType =
unsigned>
410 template <
typename InputByteStream>
413 CharType
c =
Take(is);
414 return (
unsigned)c == 0x0000FEFF
u ?
Take(is) :
c;
417 template <
typename InputByteStream>
418 static CharType
Take(InputByteStream&
is) {
420 CharType
c = (
unsigned char)is.Take();
421 c |= (
unsigned char)is.Take() << 8;
422 c |= (
unsigned char)is.Take() << 16;
423 c |= (
unsigned char)is.Take() << 24;
427 template <
typename OutputByteStream>
428 static void PutBOM(OutputByteStream& os) {
430 os.Put(0xFFu); os.Put(0xFEu); os.Put(0x00u); os.Put(0x00u);
433 template <
typename OutputByteStream>
434 static void Put(OutputByteStream& os, CharType
c) {
437 os.Put((c >> 8) & 0xFFu);
438 os.Put((c >> 16) & 0xFFu);
439 os.Put((c >> 24) & 0xFFu);
444 template<
typename CharType =
unsigned>
446 template <
typename InputByteStream>
449 CharType
c =
Take(is);
450 return (
unsigned)c == 0x0000FEFF
u ?
Take(is) :
c;
453 template <
typename InputByteStream>
454 static CharType
Take(InputByteStream&
is) {
456 CharType
c = (
unsigned char)is.Take() << 24;
457 c |= (
unsigned char)is.Take() << 16;
458 c |= (
unsigned char)is.Take() << 8;
459 c |= (
unsigned char)is.Take();
463 template <
typename OutputByteStream>
464 static void PutBOM(OutputByteStream& os) {
466 os.Put(0x00u); os.Put(0x00u); os.Put(0xFEu); os.Put(0xFFu);
469 template <
typename OutputByteStream>
470 static void Put(OutputByteStream& os, CharType
c) {
472 os.Put((c >> 24) & 0xFFu);
473 os.Put((c >> 16) & 0xFFu);
474 os.Put((c >> 8) & 0xFFu);
487 template<
typename CharType =
char>
493 template<
typename OutputStream>
494 static void Encode(OutputStream& os,
unsigned codepoint) {
496 os.Put(static_cast<Ch>(codepoint & 0xFF));
499 template <
typename InputStream>
500 static bool Decode(InputStream&
is,
unsigned* codepoint) {
501 unsigned char c =
static_cast<unsigned char>(is.Take());
506 template <
typename InputStream,
typename OutputStream>
508 unsigned char c = is.Take();
513 template <
typename InputByteStream>
520 template <
typename InputByteStream>
526 template <
typename OutputByteStream>
527 static void PutBOM(OutputByteStream& os) {
532 template <
typename OutputByteStream>
533 static void Put(OutputByteStream& os, Ch
c) {
535 os.Put(static_cast<typename OutputByteStream::Ch>(c));
554 template<
typename CharType>
560 #define RAPIDJSON_ENCODINGS_FUNC(x) UTF8<Ch>::x, UTF16LE<Ch>::x, UTF16BE<Ch>::x, UTF32LE<Ch>::x, UTF32BE<Ch>::x 562 template<
typename OutputStream>
563 RAPIDJSON_FORCEINLINE
static void Encode(OutputStream& os,
unsigned codepoint) {
564 typedef void (*EncodeFunc)(OutputStream&, unsigned);
566 (*f[os.GetType()])(os, codepoint);
569 template <
typename InputStream>
570 RAPIDJSON_FORCEINLINE
static bool Decode(InputStream&
is,
unsigned* codepoint) {
571 typedef bool (*DecodeFunc)(InputStream&,
unsigned*);
573 return (*f[is.GetType()])(is, codepoint);
576 template <
typename InputStream,
typename OutputStream>
577 RAPIDJSON_FORCEINLINE
static bool Validate(InputStream&
is, OutputStream& os) {
578 typedef bool (*ValidateFunc)(InputStream&, OutputStream&);
580 return (*f[is.GetType()])(is, os);
583 #undef RAPIDJSON_ENCODINGS_FUNC 590 template<
typename SourceEncoding,
typename TargetEncoding>
593 template<
typename InputStream,
typename OutputStream>
594 RAPIDJSON_FORCEINLINE
static bool Transcode(InputStream&
is, OutputStream& os) {
596 if (!SourceEncoding::Decode(is, &codepoint))
598 TargetEncoding::Encode(os, codepoint);
603 template<
typename InputStream,
typename OutputStream>
604 RAPIDJSON_FORCEINLINE
static bool Validate(InputStream&
is, OutputStream& os) {
605 return Transcode(is, os);
610 template<
typename Encoding>
612 template<
typename InputStream,
typename OutputStream>
613 RAPIDJSON_FORCEINLINE
static bool Transcode(InputStream&
is, OutputStream& os) {
618 template<
typename InputStream,
typename OutputStream>
619 RAPIDJSON_FORCEINLINE
static bool Validate(InputStream&
is, OutputStream& os) {
620 return Encoding::Validate(is, os);
626 #if defined(__GNUC__) || defined(_MSV_VER) 630 #endif // RAPIDJSON_ENCODINGS_H_ CharType Ch
Definition: encodings.h:102
static void Put(OutputByteStream &os, Ch c)
Definition: encodings.h:533
static void Encode(OutputStream &os, unsigned codepoint)
Definition: encodings.h:253
static void Encode(OutputStream &os, unsigned codepoint)
Definition: encodings.h:494
static bool Decode(InputStream &is, unsigned *codepoint)
Definition: encodings.h:129
UTFType
Runtime-specified UTF encoding type of a stream.
Definition: encodings.h:543
static Ch Take(InputByteStream &is)
Definition: encodings.h:215
static bool Validate(InputStream &is, OutputStream &os)
Definition: encodings.h:286
int c
Definition: cpp_unittests.cpp:275
static unsigned char GetRange(unsigned char c)
Definition: encodings.h:183
static void PutBOM(OutputByteStream &os)
Definition: encodings.h:527
static bool Validate(InputStream &is, OutputStream &os)
Definition: encodings.h:399
static void PutBOM(OutputByteStream &os)
Definition: encodings.h:221
OPENSSL_EXPORT pem_password_cb void * u
Definition: pem.h:398
#define Ch(x, y, z)
Definition: sha256.c:217
UTF-32 big endian.
Definition: encodings.h:548
UTF-16 little endian.
Definition: encodings.h:545
static CharType TakeBOM(InputByteStream &is)
Definition: encodings.h:411
UTF-8.
Definition: encodings.h:544
static CharType Take(InputByteStream &is)
Definition: encodings.h:454
#define RAPIDJSON_STATIC_ASSERT(x)
(Internal) macro to check for conditions at compile-time
Definition: rapidjson.h:346
static void Put(OutputByteStream &os, CharType c)
Definition: encodings.h:434
static Ch Take(InputByteStream &is)
Definition: encodings.h:521
static CharType TakeBOM(InputByteStream &is)
Definition: encodings.h:305
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:91
static bool Validate(InputStream &is, OutputStream &os)
Definition: encodings.h:158
UTF-16 big endian.
Definition: encodings.h:546
static CharType TakeBOM(InputByteStream &is)
Definition: encodings.h:447
static void PutBOM(OutputByteStream &os)
Definition: encodings.h:352
static CharType Take(InputByteStream &is)
Definition: encodings.h:418
Encoding conversion.
Definition: encodings.h:591
#define RAPIDJSON_ENCODINGS_FUNC(x)
Definition: encodings.h:560
static CharType TakeBOM(InputByteStream &is)
Definition: encodings.h:514
Definition: encodings.h:104
CharType Ch
Definition: encodings.h:556
static void Encode(OutputStream &os, unsigned codepoint)
Definition: encodings.h:107
void
Definition: AVFoundationCFSoftLinking.h:81
Dynamically select encoding according to stream's runtime-specified UTF encoding type.
Definition: encodings.h:555
static void Encode(OutputStream &os, unsigned codepoint)
Definition: encodings.h:384
static RAPIDJSON_FORCEINLINE void Encode(OutputStream &os, unsigned codepoint)
Definition: encodings.h:563
static CharType Take(InputByteStream &is)
Definition: encodings.h:344
ASCII encoding.
Definition: encodings.h:488
static RAPIDJSON_FORCEINLINE bool Validate(InputStream &is, OutputStream &os)
Definition: encodings.h:619
static void PutBOM(OutputByteStream &os)
Definition: encodings.h:428
static void Put(OutputByteStream &os, CharType c)
Definition: encodings.h:358
static CharType TakeBOM(InputByteStream &is)
Definition: encodings.h:337
static void Put(OutputByteStream &os, CharType c)
Definition: encodings.h:326
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:94
static CharType Take(InputByteStream &is)
Definition: encodings.h:312
CharType Ch
Definition: encodings.h:489
UTF-32 big endian encoding.
Definition: encodings.h:445
GLfloat f
Definition: gl2.h:417
UTF-32 little endian enocoding.
Definition: encodings.h:409
const GLfloat * v
Definition: gl2.h:514
EGLenum type
Definition: eglext.h:63
static void Put(OutputByteStream &os, Ch c)
Definition: encodings.h:227
result
Definition: target-blank-opener-post-window.php:5
static void PutBOM(OutputByteStream &os)
Definition: encodings.h:464
bool is(Ref< ArgType > &source)
Definition: Ref.h:220
UTF-32 encoding.
Definition: encodings.h:377
CharType Ch
Definition: encodings.h:247
static CharType TakeBOM(InputByteStream &is)
Definition: encodings.h:202
common definitions and configuration
static bool Validate(InputStream &is, OutputStream &os)
Definition: encodings.h:507
static RAPIDJSON_FORCEINLINE bool Transcode(InputStream &is, OutputStream &os)
Definition: encodings.h:613
static bool Decode(InputStream &is, unsigned *codepoint)
Definition: encodings.h:500
static void PutBOM(OutputByteStream &os)
Definition: encodings.h:320
static RAPIDJSON_FORCEINLINE bool Decode(InputStream &is, unsigned *codepoint)
Definition: encodings.h:570
UTF-16 encoding.
Definition: encodings.h:246
CharType Ch
Definition: encodings.h:378
UTF-32 little endian.
Definition: encodings.h:547
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:594
static bool Decode(InputStream &is, unsigned *codepoint)
Definition: encodings.h:268
static void Put(OutputByteStream &os, CharType c)
Definition: encodings.h:470
UTF-16 big endian encoding.
Definition: encodings.h:335
UTF-16 little endian encoding.
Definition: encodings.h:303
static bool Decode(InputStream &is, unsigned *codepoint)
Definition: encodings.h:391
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:315
static RAPIDJSON_FORCEINLINE bool Validate(InputStream &is, OutputStream &os)
Definition: encodings.h:577
UTF-8 encoding.
Definition: encodings.h:101
static RAPIDJSON_FORCEINLINE bool Validate(InputStream &is, OutputStream &os)
Validate one Unicode codepoint from an encoded stream.
Definition: encodings.h:604