Public Types | Public Member Functions | Static Public Member Functions | List of all members
UTF16< CharType > Struct Template Reference

UTF-16 encoding. More...

#include <encodings.h>

Inheritance diagram for UTF16< CharType >:
UTF16BE< CharType > UTF16LE< CharType >

Public Types

enum  { supportUnicode = 1 }
 
typedef CharType Ch
 

Public Member Functions

 RAPIDJSON_STATIC_ASSERT (sizeof(Ch) >=2)
 

Static Public Member Functions

template<typename OutputStream >
static void Encode (OutputStream &os, unsigned codepoint)
 
template<typename OutputStream >
static void EncodeUnsafe (OutputStream &os, unsigned codepoint)
 
template<typename InputStream >
static bool Decode (InputStream &is, unsigned *codepoint)
 
template<typename InputStream , typename OutputStream >
static bool Validate (InputStream &is, OutputStream &os)
 

Detailed Description

template<typename CharType = wchar_t>
struct UTF16< CharType >

UTF-16 encoding.

http://en.wikipedia.org/wiki/UTF-16 http://tools.ietf.org/html/rfc2781

Template Parameters
CharTypeType for storing 16-bit UTF-16 data. Default is wchar_t. C++11 may use char16_t instead.
Note
implements Encoding concept
For in-memory access, no need to concern endianness. The code units and code points are represented by CPU's endianness. For streaming, use UTF16LE and UTF16BE, which handle endianness.

Definition at line 269 of file encodings.h.

Member Typedef Documentation

template<typename CharType = wchar_t>
typedef CharType UTF16< CharType >::Ch

Definition at line 270 of file encodings.h.

Member Enumeration Documentation

template<typename CharType = wchar_t>
anonymous enum
Enumerator
supportUnicode 

Definition at line 273 of file encodings.h.

Member Function Documentation

template<typename CharType = wchar_t>
template<typename InputStream >
static bool UTF16< CharType >::Decode ( InputStream &  is,
unsigned *  codepoint 
)
inlinestatic

Definition at line 307 of file encodings.h.

307  {
308  RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 2);
309  typename InputStream::Ch c = is.Take();
310  if (c < 0xD800 || c > 0xDFFF) {
311  *codepoint = static_cast<unsigned>(c);
312  return true;
313  }
314  else if (c <= 0xDBFF) {
315  *codepoint = (static_cast<unsigned>(c) & 0x3FF) << 10;
316  c = is.Take();
317  *codepoint |= (static_cast<unsigned>(c) & 0x3FF);
318  *codepoint += 0x10000;
319  return c >= 0xDC00 && c <= 0xDFFF;
320  }
321  return false;
322  }
RAPIDJSON_STATIC_ASSERT(sizeof(Ch) >=2)
template<typename CharType = wchar_t>
template<typename OutputStream >
static void UTF16< CharType >::Encode ( OutputStream &  os,
unsigned  codepoint 
)
inlinestatic

Definition at line 276 of file encodings.h.

276  {
277  RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 2);
278  if (codepoint <= 0xFFFF) {
279  RAPIDJSON_ASSERT(codepoint < 0xD800 || codepoint > 0xDFFF); // Code point itself cannot be surrogate pair
280  os.Put(static_cast<typename OutputStream::Ch>(codepoint));
281  }
282  else {
283  RAPIDJSON_ASSERT(codepoint <= 0x10FFFF);
284  unsigned v = codepoint - 0x10000;
285  os.Put(static_cast<typename OutputStream::Ch>((v >> 10) | 0xD800));
286  os.Put(static_cast<typename OutputStream::Ch>((v & 0x3FF) | 0xDC00));
287  }
288  }
RAPIDJSON_STATIC_ASSERT(sizeof(Ch) >=2)
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:406
template<typename CharType = wchar_t>
template<typename OutputStream >
static void UTF16< CharType >::EncodeUnsafe ( OutputStream &  os,
unsigned  codepoint 
)
inlinestatic

Definition at line 292 of file encodings.h.

292  {
293  RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 2);
294  if (codepoint <= 0xFFFF) {
295  RAPIDJSON_ASSERT(codepoint < 0xD800 || codepoint > 0xDFFF); // Code point itself cannot be surrogate pair
296  PutUnsafe(os, static_cast<typename OutputStream::Ch>(codepoint));
297  }
298  else {
299  RAPIDJSON_ASSERT(codepoint <= 0x10FFFF);
300  unsigned v = codepoint - 0x10000;
301  PutUnsafe(os, static_cast<typename OutputStream::Ch>((v >> 10) | 0xD800));
302  PutUnsafe(os, static_cast<typename OutputStream::Ch>((v & 0x3FF) | 0xDC00));
303  }
304  }
RAPIDJSON_STATIC_ASSERT(sizeof(Ch) >=2)
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:406
void PutUnsafe(Stream &stream, typename Stream::Ch c)
Write character to a stream, presuming buffer is reserved.
Definition: stream.h:91
template<typename CharType = wchar_t>
UTF16< CharType >::RAPIDJSON_STATIC_ASSERT ( sizeof(Ch) >=  2)
template<typename CharType = wchar_t>
template<typename InputStream , typename OutputStream >
static bool UTF16< CharType >::Validate ( InputStream &  is,
OutputStream &  os 
)
inlinestatic

Definition at line 325 of file encodings.h.

325  {
326  RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 2);
327  RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 2);
328  typename InputStream::Ch c;
329  os.Put(static_cast<typename OutputStream::Ch>(c = is.Take()));
330  if (c < 0xD800 || c > 0xDFFF)
331  return true;
332  else if (c <= 0xDBFF) {
333  os.Put(c = is.Take());
334  return c >= 0xDC00 && c <= 0xDFFF;
335  }
336  return false;
337  }
RAPIDJSON_STATIC_ASSERT(sizeof(Ch) >=2)

The documentation for this struct was generated from the following file: