VR-Forces 5.0.3 Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DtUnicodeChar.h
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright (c) 2010 MAK Technologies, Inc.
3 ** All rights reserved.
4 ******************************************************************************/
5 
8 
9 #ifndef DtUnicodeChar_H
10 #define DtUnicodeChar_H
11 
12 #include <stdexcept>
13 
14 namespace makVrv
15 {
18  {
19  public:
20 
21  //A Unicode Code Point, is a unique Unicode Character.
22  typedef unsigned int CodePoint;
23 
24  //A Unicode Surrogate code point, is a 16bit code point, used in UTF-16 encoding.
25  typedef unsigned short Surrogate;
26 
28  typedef unsigned char Octet;
29 
30  inline DtUnicodeChar()
31  : myCodePoint(0)
32  {
33  }
34 
37  inline explicit DtUnicodeChar(const char c)
38  : myCodePoint(c)
39  {
40  }
41 
42 
46  inline explicit DtUnicodeChar(const wchar_t c)
47  : myCodePoint(c)
48  {
49  }
50 
53  inline explicit DtUnicodeChar(const unsigned short s,const unsigned short c)
55  {
56  if(!(isSurrogate(s) && isSurrogate(c)))
57  {
58  throw std::invalid_argument("UTF-16 characters are not a surrogate pair.");
59  }
60  }
61 
62  inline DtUnicodeChar(const DtUnicodeChar& other)
63  : myCodePoint(other.myCodePoint)
64  {
65  }
66 
67  template <int sizeof_wchar_t>
68  inline static CodePoint widechar(const wchar_t c);
69 
70  inline DtUnicodeChar& operator=(const DtUnicodeChar& other)
71  {
72  //No need to check for equality.
73  myCodePoint = other.myCodePoint;
74  return *this;
75  }
76 
77  inline bool operator==(const DtUnicodeChar& other) const
78  {
79  return myCodePoint == other.myCodePoint;
80  }
81 
82  inline bool operator!=(const DtUnicodeChar& other) const
83  {
84  return myCodePoint != other.myCodePoint;
85  }
86 
87  // Maximum valid value for a Unicode code point
88  static const CodePoint CodePointMax = 0x0010ffff;
89  static const Surrogate LeadSurrogateMin = 0xd800;
90  static const Surrogate LeadSurrogateMax = 0xdbff;
91  static const Surrogate TrailSurrogateMin = 0xdc00;
92  static const Surrogate TrailSurrogateMax = 0xdfff;
93 
94  static inline bool isCodePointValid(CodePoint codePoint)
95  {
96  return (codePoint <= CodePointMax && !isSurrogate((Surrogate)codePoint) &&
97  codePoint != 0xfffe && codePoint != 0xffff);
98  }
99 
100  static inline bool isSurrogateSubstringValid(const Surrogate* start,
101  const Surrogate* end, const Surrogate* substringStart,
102  const Surrogate* substringEnd)
103  {
104  // Test to see if beginning in middle of surrogate pair.
105  if(TrailSurrogateMin == ((*substringStart)&0xfffffc00) &&
106  start != substringStart &&
107  LeadSurrogateMin == ((*(substringStart - 1))&0xfffffc00))
108  {
109 
110  return false;
111  }
112 
113  //Test to see if end is middle of pair.
114  if((LeadSurrogateMin == ((*(substringEnd - 1 ))&0xfffffc00)) &&
115  substringStart != end &&
116  (TrailSurrogateMin == ((*substringEnd)&0xfffffc00)))
117  {
118  return false;
119  }
120 
121  //Not at middle of surrogate pair.
122  return true;
123  }
124 
125 
126  static inline bool isSurrogate(Surrogate cp)
127  {
128  return (cp >= LeadSurrogateMin && cp <= TrailSurrogateMax);
129  }
130 
132  static inline DtUnicodeChar fromUtf32(unsigned int character)
133  {
134  if(!isCodePointValid(character)) throw std::invalid_argument("Not a valid UTF 32 code point");
135  return DtUnicodeChar(character);
136  }
137 
139  inline unsigned int utf8Length() const
140  {
141  if (myCodePoint < 0x80) // one octet
142  {
143  return 1;
144  }
145  else if (myCodePoint < 0x800) // two octets
146  {
147  return 2;
148  }
149  else if (myCodePoint < 0x10000) // three octets
150  {
151  return 3;
152  }
153  else // four octets
154  {
155  return 4;
156  }
157  }
158 
159  template <int sizeofWChar_t>
160  inline unsigned int wchartLength() const;
161 
162  inline unsigned int wideStringLength() const;
163 
164  inline unsigned int utf16Length() const
165  {
166  if(myCodePoint > 0xFFFF) return 4; else return 2;
167  }
168 
169  inline unsigned int utf32Length() const
170  {
171  return 4;
172  }
173 
175  {
176  //Get bits 10 - 15 bits from hi, and get bits 0-9 bits from lo
177  CodePoint X = (hi & 0x3F) << 10 | (lo & 0x3FF);
178  //Get bits 16-21 from hi, and offset.
179  CodePoint U = ((hi >> 6) & (0x1F)) + 1;
180  //Recombine.
181  return (U << 16) | X;
182  }
183 
184  static inline void encodeSurrogatePair(CodePoint cp, Surrogate& hi,Surrogate& lo)
185  {
186  // TAG Top 17-22 bits minus 1,
187  hi = LeadSurrogateMin | ((unsigned short)((cp >> 16) & 0x1F) - 1)
188  // bits 10-16 bits.
189  | (unsigned short)((cp >> 10) & 0x3F);
190  //Has bottom 10 bits + Trail Tag.
191  lo = TrailSurrogateMin | (unsigned short)(cp & 0x3FF);
192  }
193 
194  inline unsigned int utf16CodeUnits() const
195  {
196  if(myCodePoint >= 0x10000)
197  {
198  return 2;
199  }
200  else
201  {
202  return 1;
203  }
204  }
205 
208  inline unsigned int readUtf16(const Surrogate* memory)
209  {
210  if(isSurrogate(*memory))
211  {
212  Surrogate hiPair = *memory;
213  Surrogate loPair = *(memory + 1);
214  myCodePoint = decodeSurrogatePair(hiPair,loPair);
215  return 2;
216  }
217  else
218  {
219  myCodePoint = *memory;
220  return 1;
221  }
222  }
223 
224  inline void writeUtf16(Surrogate* memory) const
225  {
226  if(myCodePoint >= 0x10000)
227  {
228  encodeSurrogatePair(myCodePoint,*memory,*(memory + 1));
229  }
230  else
231  {
232  *memory = static_cast<Surrogate>(myCodePoint);
233  }
234  }
235 
236  inline void readUtf32(const unsigned int* memory)
237  {
238  myCodePoint = *memory;
239  }
240 
241  inline void writeUtf32(unsigned int* memory) const
242  {
243  *memory = myCodePoint;
244  }
245 
247  static inline unsigned int utf8Length(const unsigned char c)
248  {
249  if(c < 0x80)
250  {
251  return 1;
252  }
253  else if(c < 0xE0)
254  {
255  return 2;
256  }
257  else if(c < 0xF0)
258  {
259  return 3;
260  }
261  else if(c < 0xF8)
262  {
263  return 4;
264  }
265  else
266  {
267  throw std::invalid_argument("Not a valid UTF8 string.");
268  }
269  }
270 
271  inline unsigned int readUtf8(const unsigned char* memory)
272  {
273  unsigned char c = *memory;
274  if(c < 0x80)
275  {
276  myCodePoint = c;
277  return 1;
278  }
279  else if(c < 0xE0)
280  {
281  myCodePoint = ((c & 0x1F) << 6) |
282  (*(memory + 1) & 0x3F);
283  return 2;
284  }
285  else if(c < 0xF0)
286  {
287  myCodePoint = ((c & 0xF) << 12) |
288  ((*(memory + 1) & 0x3F) << 6) |
289  (*(memory + 2) & 0x3F);
290  return 3;
291  }
292  else if(c < 0xF8)
293  {
294  myCodePoint = ((c & 0x7) << 18) |
295  ((*(memory + 1) & 0x3F) << 12) |
296  ((*(memory + 2) & 0x3F) << 6) |
297  (*(memory + 3) & 0x3F);
298  return 4;
299  }
300  else
301  {
302  throw std::invalid_argument("Not a valid UTF8 string.");
303  }
304  }
305 
306  inline void writeUtf8(char* memory) const
307  {
308  if (myCodePoint < 0x80) // one octet
309  {
310  *memory = (unsigned char)(myCodePoint & 0x7F);
311  }
312  else if (myCodePoint < 0x800) // two octets
313  {
314  *memory = (unsigned char)((myCodePoint >> 6) & 0x1F) | 0xC0;
315  *(memory+1) = (unsigned char)((myCodePoint ) & 0x3F) | 0x80;
316 
317  }
318  else if (myCodePoint < 0x10000) // three octets
319  {
320  *memory = (unsigned char)((myCodePoint >> 12) & 0xF ) | 0xE0;
321  *(memory+1) = (unsigned char)((myCodePoint >> 6) & 0x3F) | 0x80;
322  *(memory+2) = (unsigned char)((myCodePoint ) & 0x3F) | 0x80;
323  }
324  else // four octets
325  {
326  *memory = (unsigned char)((myCodePoint >> 18) & 0x7 ) | 0xF0;
327  *(memory+1) = (unsigned char)((myCodePoint >> 12) & 0x3F) | 0x80;
328  *(memory+2) = (unsigned char)((myCodePoint >> 6 ) & 0x3F) | 0x80;
329  *(memory+3) = (unsigned char)((myCodePoint ) & 0x3F) | 0x80;
330  }
331  }
332 
333  template <int sizeofWchart>
334  inline void writeWCharT(wchar_t* memory) const;
335 
336 
337  inline void writeWChar(wchar_t* memory) const;
338 
339  inline CodePoint codePoint() const
340  {
341  return myCodePoint;
342  }
343 
344  protected:
345 
346  inline DtUnicodeChar(unsigned int utf32c)
347  : myCodePoint(utf32c)
348  {
349  }
350 
351  private:
353  };
354 
355  template <>
356  inline unsigned int DtUnicodeChar::wchartLength<2>() const
357  {
358  return utf16CodeUnits() << 1;
359  }
360 
361  template <>
362  inline unsigned int DtUnicodeChar::wchartLength<4>() const
363  {
364  return utf32Length();
365  }
366 
367 
368 
369  template <int sizeof_wchar_t>
371  {
372  return c;
373  }
374 
375  template <>
376  inline DtUnicodeChar::CodePoint DtUnicodeChar::widechar<2>(const wchar_t c)
377  {
378  if(isSurrogate(c))
379  {
380  throw std::invalid_argument("Invalid UTF-16 character");
381  }
382  return c;
383  }
384 
385  template <>
386  inline void DtUnicodeChar::writeWCharT<2>(wchar_t* memory) const
387  {
388  writeUtf16(reinterpret_cast<Surrogate*>(memory));
389  }
390 
391  template <>
392  inline void DtUnicodeChar::writeWCharT<4>(wchar_t* memory) const
393  {
394  writeUtf32(reinterpret_cast<unsigned int*>(memory));
395  }
396 
397  inline unsigned int DtUnicodeChar::wideStringLength() const
398  {
399  return wchartLength<sizeof(wchar_t)>();
400  }
401 
402  inline void DtUnicodeChar::writeWChar(wchar_t* memory) const
403  {
404  writeWCharT<sizeof(wchar_t)>(memory);
405  }
406 }
407 
408 #endif
void writeWChar(wchar_t *memory) const
Definition: DtUnicodeChar.h:402
unsigned int wchartLength() const
static const Surrogate LeadSurrogateMin
Definition: DtUnicodeChar.h:89
void writeUtf16(Surrogate *memory) const
Definition: DtUnicodeChar.h:224
static const Surrogate TrailSurrogateMin
Definition: DtUnicodeChar.h:91
void readUtf32(const unsigned int *memory)
Definition: DtUnicodeChar.h:236
static void encodeSurrogatePair(CodePoint cp, Surrogate &hi, Surrogate &lo)
Definition: DtUnicodeChar.h:184
static bool isCodePointValid(CodePoint codePoint)
Definition: DtUnicodeChar.h:94
CodePoint myCodePoint
Definition: DtUnicodeChar.h:352
static unsigned int utf8Length(const unsigned char c)
Get the length based upon the first character.
Definition: DtUnicodeChar.h:247
unsigned char Octet
A Unicode Octet is 8bit byte used in UTF-8 encoding.
Definition: DtUnicodeChar.h:28
static const CodePoint CodePointMax
Definition: DtUnicodeChar.h:88
unsigned int utf16CodeUnits() const
Definition: DtUnicodeChar.h:194
DtUnicodeChar(const unsigned short s, const unsigned short c)
Construct a pair of UTF-16 code units, where the first character must be a surrogate pair...
Definition: DtUnicodeChar.h:53
unsigned int utf16Length() const
Definition: DtUnicodeChar.h:164
DtUnicodeChar(const char c)
Construct a unicode character for a character c. C is assumed to be either ASCII or Latin-1...
Definition: DtUnicodeChar.h:37
unsigned int wideStringLength() const
Definition: DtUnicodeChar.h:397
DtUnicodeChar(unsigned int utf32c)
Definition: DtUnicodeChar.h:346
unsigned int readUtf8(const unsigned char *memory)
Definition: DtUnicodeChar.h:271
A single unicode character.
Definition: DtUnicodeChar.h:17
bool operator==(const DtUnicodeChar &other) const
Definition: DtUnicodeChar.h:77
void writeUtf8(char *memory) const
Definition: DtUnicodeChar.h:306
DtUnicodeChar(const wchar_t c)
Construct a unicode character for a character c. C is assumed to be either UTF-16 or UTF-32 depending...
Definition: DtUnicodeChar.h:46
bool operator!=(const DtUnicodeChar &other) const
Definition: DtUnicodeChar.h:82
static DtUnicodeChar fromUtf32(unsigned int character)
Definition: DtUnicodeChar.h:132
unsigned int utf8Length() const
Calculate the utf-8 length of a code point.
Definition: DtUnicodeChar.h:139
void writeWCharT(wchar_t *memory) const
DtUnicodeChar()
Definition: DtUnicodeChar.h:30
DtUnicodeChar(const DtUnicodeChar &other)
Definition: DtUnicodeChar.h:62
unsigned int readUtf16(const Surrogate *memory)
Read from UTF-16 encoded memory, Returns number of code units read, NOT number of bytes...
Definition: DtUnicodeChar.h:208
DtUnicodeChar & operator=(const DtUnicodeChar &other)
Definition: DtUnicodeChar.h:70
unsigned short Surrogate
Definition: DtUnicodeChar.h:25
unsigned int CodePoint
Definition: DtUnicodeChar.h:22
static const Surrogate LeadSurrogateMax
Definition: DtUnicodeChar.h:90
static bool isSurrogateSubstringValid(const Surrogate *start, const Surrogate *end, const Surrogate *substringStart, const Surrogate *substringEnd)
Definition: DtUnicodeChar.h:100
CodePoint codePoint() const
Definition: DtUnicodeChar.h:339
static const Surrogate TrailSurrogateMax
Definition: DtUnicodeChar.h:92
void writeUtf32(unsigned int *memory) const
Definition: DtUnicodeChar.h:241
#define X(a)
Definition: DtSharedSimulationSettings.h:46
static CodePoint widechar(const wchar_t c)
Definition: DtUnicodeChar.h:370
unsigned int utf32Length() const
Definition: DtUnicodeChar.h:169
static bool isSurrogate(Surrogate cp)
Definition: DtUnicodeChar.h:126
static CodePoint decodeSurrogatePair(Surrogate hi, Surrogate lo)
Definition: DtUnicodeChar.h:174

Document ID: Generated on Thu Jun 1 17:58:13 EDT 2023 from SVN revision 255404
Copyright © 2005-2021 MAK Technologies. All Rights Reserved (www.mak.com)