1 // Created on: 2013-01-28
2 // Created by: Kirill GAVRILOV
3 // Copyright (c) 2013-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 // Portions of code are copyrighted by Unicode, Inc.
18 // Copyright © 2001-2004 Unicode, Inc.
22 // This source code is provided as is by Unicode, Inc. No claims are
23 // made as to fitness for any particular purpose. No warranties of any
24 // kind are expressed or implied. The recipient agrees to determine
25 // applicability of information provided. If this file has been
26 // purchased on magnetic or optical media from Unicode, Inc., the
27 // sole remedy for any claim will be exchange of defective media
28 // within 90 days of receipt.
30 // Limitations on Rights to Redistribute This Code
32 // Unicode, Inc. hereby grants the right to freely use the information
33 // supplied in this file in the creation of products supporting the
34 // Unicode Standard, and to make copies of this file in any form
35 // for internal or external distribution as long as this notice
38 //! The first character in a UTF-8 sequence indicates how many bytes
39 //! to read (among other things).
40 template<typename Type>
41 const unsigned char NCollection_UtfIterator<Type>::UTF8_BYTES_MINUS_ONE[256] =
43 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,
44 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,
45 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,
46 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,
47 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,
48 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,
49 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
50 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5
53 //! Magic values subtracted from a buffer value during UTF-8 conversion.
54 //! This table contains as many values as there might be trailing bytes
55 //! in a UTF-8 sequence.
56 template<typename Type>
57 const unsigned long NCollection_UtfIterator<Type>::offsetsFromUTF8[6] =
59 0x00000000UL, 0x00003080UL, 0x000E2080UL,
60 0x03C82080UL, 0xFA082080UL, 0x82082080UL
63 //! The first character in a UTF-8 sequence indicates how many bytes to read.
64 template<typename Type>
65 const unsigned char NCollection_UtfIterator<Type>::UTF8_FIRST_BYTE_MARK[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
67 // =======================================================================
68 // function : readUTF8
69 // purpose : Get a UTF-8 character; leave the tracking pointer at the start of the next character.
70 // Not protected against invalid UTF-8.
71 // =======================================================================
72 template<typename Type>
73 inline void NCollection_UtfIterator<Type>::readUTF8()
75 // unsigned arithmetic used
76 Standard_Utf8UChar* aPos = (Standard_Utf8UChar* )myPosNext;
77 const unsigned char aBytesToRead = UTF8_BYTES_MINUS_ONE[*aPos];
81 case 5: myCharUtf32 += *aPos++; myCharUtf32 <<= 6; // remember, illegal UTF-8
82 case 4: myCharUtf32 += *aPos++; myCharUtf32 <<= 6; // remember, illegal UTF-8
83 case 3: myCharUtf32 += *aPos++; myCharUtf32 <<= 6;
84 case 2: myCharUtf32 += *aPos++; myCharUtf32 <<= 6;
85 case 1: myCharUtf32 += *aPos++; myCharUtf32 <<= 6;
86 case 0: myCharUtf32 += *aPos++;
88 myCharUtf32 -= offsetsFromUTF8[aBytesToRead];
89 myPosNext = (Type* )aPos;
93 template<typename Type> const unsigned long NCollection_UtfIterator<Type>::UTF8_BYTE_MASK = 0xBF;
94 template<typename Type> const unsigned long NCollection_UtfIterator<Type>::UTF8_BYTE_MARK = 0x80;
95 template<typename Type> const unsigned long NCollection_UtfIterator<Type>::UTF16_SURROGATE_HIGH_START = 0xD800;
96 template<typename Type> const unsigned long NCollection_UtfIterator<Type>::UTF16_SURROGATE_HIGH_END = 0xDBFF;
97 template<typename Type> const unsigned long NCollection_UtfIterator<Type>::UTF16_SURROGATE_LOW_START = 0xDC00;
98 template<typename Type> const unsigned long NCollection_UtfIterator<Type>::UTF16_SURROGATE_LOW_END = 0xDFFF;
99 template<typename Type> const unsigned long NCollection_UtfIterator<Type>::UTF16_SURROGATE_HIGH_SHIFT = 10;
100 template<typename Type> const unsigned long NCollection_UtfIterator<Type>::UTF16_SURROGATE_LOW_BASE = 0x0010000UL;
101 template<typename Type> const unsigned long NCollection_UtfIterator<Type>::UTF16_SURROGATE_LOW_MASK = 0x3FFUL;
102 template<typename Type> const unsigned long NCollection_UtfIterator<Type>::UTF32_MAX_BMP = 0x0000FFFFUL;
103 template<typename Type> const unsigned long NCollection_UtfIterator<Type>::UTF32_MAX_LEGAL = 0x0010FFFFUL;
105 // =======================================================================
106 // function : readUTF16
108 // =======================================================================
109 template<typename Type> inline
110 void NCollection_UtfIterator<Type>::readUTF16()
112 Standard_Utf32Char aChar = *myPosNext++;
113 // if we have the first half of the surrogate pair
114 if (aChar >= UTF16_SURROGATE_HIGH_START
115 && aChar <= UTF16_SURROGATE_HIGH_END)
117 const Standard_Utf32Char aChar2 = *myPosNext;
118 // complete the surrogate pair
119 if (aChar2 >= UTF16_SURROGATE_LOW_START
120 && aChar2 <= UTF16_SURROGATE_LOW_END)
122 aChar = ((aChar - UTF16_SURROGATE_HIGH_START) << UTF16_SURROGATE_HIGH_SHIFT)
123 + (aChar2 - UTF16_SURROGATE_LOW_START) + UTF16_SURROGATE_LOW_BASE;
130 // =======================================================================
131 // function : AdvanceBytesUtf8
133 // =======================================================================
134 template<typename Type> inline
135 Standard_Integer NCollection_UtfIterator<Type>::AdvanceBytesUtf8() const
137 if (myCharUtf32 >= UTF16_SURROGATE_HIGH_START
138 && myCharUtf32 <= UTF16_SURROGATE_LOW_END)
140 // UTF-16 surrogate values are illegal in UTF-32
143 else if (myCharUtf32 < Standard_Utf32Char(0x80))
147 else if (myCharUtf32 < Standard_Utf32Char(0x800))
151 else if (myCharUtf32 < Standard_Utf32Char(0x10000))
155 else if (myCharUtf32 <= UTF32_MAX_LEGAL)
166 // =======================================================================
167 // function : GetUtf8
169 // =======================================================================
170 template<typename Type> inline
171 Standard_Utf8Char* NCollection_UtfIterator<Type>::GetUtf8 (Standard_Utf8Char* theBuffer) const
173 // unsigned arithmetic used
174 return (Standard_Utf8Char* )GetUtf8 ((Standard_Utf8UChar* )theBuffer);
177 // =======================================================================
178 // function : GetUtf8
180 // =======================================================================
181 template<typename Type> inline
182 Standard_Utf8UChar* NCollection_UtfIterator<Type>::GetUtf8 (Standard_Utf8UChar* theBuffer) const
184 Standard_Utf32Char aChar = myCharUtf32;
185 if (myCharUtf32 >= UTF16_SURROGATE_HIGH_START
186 && myCharUtf32 <= UTF16_SURROGATE_LOW_END)
188 // UTF-16 surrogate values are illegal in UTF-32
191 else if (myCharUtf32 < Standard_Utf32Char(0x80))
193 *theBuffer++ = Standard_Utf8UChar (aChar | UTF8_FIRST_BYTE_MARK[1]);
196 else if (myCharUtf32 < Standard_Utf32Char(0x800))
198 *++theBuffer = Standard_Utf8UChar((aChar | UTF8_BYTE_MARK) & UTF8_BYTE_MASK); aChar >>= 6;
199 *--theBuffer = Standard_Utf8UChar (aChar | UTF8_FIRST_BYTE_MARK[2]);
200 return theBuffer + 2;
202 else if (myCharUtf32 < Standard_Utf32Char(0x10000))
205 *--theBuffer = Standard_Utf8UChar((aChar | UTF8_BYTE_MARK) & UTF8_BYTE_MASK); aChar >>= 6;
206 *--theBuffer = Standard_Utf8UChar((aChar | UTF8_BYTE_MARK) & UTF8_BYTE_MASK); aChar >>= 6;
207 *--theBuffer = Standard_Utf8UChar (aChar | UTF8_FIRST_BYTE_MARK[3]);
208 return theBuffer + 3;
210 else if (myCharUtf32 <= UTF32_MAX_LEGAL)
213 *--theBuffer = Standard_Utf8UChar((aChar | UTF8_BYTE_MARK) & UTF8_BYTE_MASK); aChar >>= 6;
214 *--theBuffer = Standard_Utf8UChar((aChar | UTF8_BYTE_MARK) & UTF8_BYTE_MASK); aChar >>= 6;
215 *--theBuffer = Standard_Utf8UChar((aChar | UTF8_BYTE_MARK) & UTF8_BYTE_MASK); aChar >>= 6;
216 *--theBuffer = Standard_Utf8UChar (aChar | UTF8_FIRST_BYTE_MARK[4]);
217 return theBuffer + 4;
226 // =======================================================================
227 // function : AdvanceBytesUtf16
229 // =======================================================================
230 template<typename Type> inline
231 Standard_Integer NCollection_UtfIterator<Type>::AdvanceBytesUtf16() const
233 if (myCharUtf32 <= UTF32_MAX_BMP) // target is a character <= 0xFFFF
235 // UTF-16 surrogate values are illegal in UTF-32; 0xffff or 0xfffe are both reserved values
236 if (myCharUtf32 >= UTF16_SURROGATE_HIGH_START
237 && myCharUtf32 <= UTF16_SURROGATE_LOW_END)
243 return Standard_Integer(sizeof(Standard_Utf16Char));
246 else if (myCharUtf32 > UTF32_MAX_LEGAL)
253 // target is a character in range 0xFFFF - 0x10FFFF
255 return Standard_Integer(sizeof(Standard_Utf16Char) * 2);
259 // =======================================================================
260 // function : GetUtf16
262 // =======================================================================
263 template<typename Type> inline
264 Standard_Utf16Char* NCollection_UtfIterator<Type>::GetUtf16 (Standard_Utf16Char* theBuffer) const
266 if (myCharUtf32 <= UTF32_MAX_BMP) // target is a character <= 0xFFFF
268 // UTF-16 surrogate values are illegal in UTF-32; 0xffff or 0xfffe are both reserved values
269 if (myCharUtf32 >= UTF16_SURROGATE_HIGH_START
270 && myCharUtf32 <= UTF16_SURROGATE_LOW_END)
276 *theBuffer++ = Standard_Utf16Char(myCharUtf32);
280 else if (myCharUtf32 > UTF32_MAX_LEGAL)
288 Standard_Utf32Char aChar = myCharUtf32 - UTF16_SURROGATE_LOW_BASE;
289 *theBuffer++ = Standard_Utf16Char((aChar >> UTF16_SURROGATE_HIGH_SHIFT) + UTF16_SURROGATE_HIGH_START);
290 *theBuffer++ = Standard_Utf16Char((aChar & UTF16_SURROGATE_LOW_MASK) + UTF16_SURROGATE_LOW_START);
295 // =======================================================================
296 // function : GetUtf32
298 // =======================================================================
299 template<typename Type> inline
300 Standard_Utf32Char* NCollection_UtfIterator<Type>::GetUtf32 (Standard_Utf32Char* theBuffer) const
302 *theBuffer++ = myCharUtf32;
306 // =======================================================================
307 // function : AdvanceBytesUtf
309 // =======================================================================
310 template<typename Type> template<typename TypeWrite> inline
311 Standard_Integer NCollection_UtfIterator<Type>::AdvanceBytesUtf() const
313 switch (sizeof(TypeWrite))
315 case sizeof(Standard_Utf8Char): return AdvanceBytesUtf8();
316 case sizeof(Standard_Utf16Char): return AdvanceBytesUtf16();
317 case sizeof(Standard_Utf32Char): return AdvanceBytesUtf32();
318 default: return 0; // invalid case
322 // =======================================================================
325 // =======================================================================
326 template<typename Type> template<typename TypeWrite> inline
327 TypeWrite* NCollection_UtfIterator<Type>::GetUtf (TypeWrite* theBuffer) const
329 switch (sizeof(TypeWrite))
331 case sizeof(Standard_Utf8Char): return (TypeWrite* )GetUtf8 ((Standard_Utf8UChar* )theBuffer);
332 case sizeof(Standard_Utf16Char): return (TypeWrite* )GetUtf16((Standard_Utf16Char* )theBuffer);
333 case sizeof(Standard_Utf32Char): return (TypeWrite* )GetUtf32((Standard_Utf32Char* )theBuffer);
334 default: return NULL; // invalid case