Warning, /include/opencascade/NCollection_UtfIterator.lxx is written in an unsupported language. File is not indexed.
0001 // Created on: 2013-01-28
0002 // Created by: Kirill GAVRILOV
0003 // Copyright (c) 2013-2014 OPEN CASCADE SAS
0004 //
0005 // This file is part of Open CASCADE Technology software library.
0006 //
0007 // This library is free software; you can redistribute it and/or modify it under
0008 // the terms of the GNU Lesser General Public License version 2.1 as published
0009 // by the Free Software Foundation, with special exception defined in the file
0010 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0011 // distribution for complete text of the license and disclaimer of any warranty.
0012 //
0013 // Alternatively, this file may be used under the terms of Open CASCADE
0014 // commercial license or contractual agreement.
0015
0016 // Portions of code are copyrighted by Unicode, Inc.
0017 //
0018 // Copyright (c) 2001-2004 Unicode, Inc.
0019 //
0020 // Disclaimer
0021 //
0022 // This source code is provided as is by Unicode, Inc. No claims are
0023 // made as to fitness for any particular purpose. No warranties of any
0024 // kind are expressed or implied. The recipient agrees to determine
0025 // applicability of information provided. If this file has been
0026 // purchased on magnetic or optical media from Unicode, Inc., the
0027 // sole remedy for any claim will be exchange of defective media
0028 // within 90 days of receipt.
0029 //
0030 // Limitations on Rights to Redistribute This Code
0031 //
0032 // Unicode, Inc. hereby grants the right to freely use the information
0033 // supplied in this file in the creation of products supporting the
0034 // Unicode Standard, and to make copies of this file in any form
0035 // for internal or external distribution as long as this notice
0036 // remains attached.
0037
0038 // =======================================================================
0039 // function : readUTF8
0040 // purpose : Get a UTF-8 character; leave the tracking pointer at the start of the next character.
0041 // Not protected against invalid UTF-8.
0042 // =======================================================================
0043 template <typename Type>
0044 inline void NCollection_UtfIterator<Type>::readUTF8()
0045 {
0046 // unsigned arithmetic used
0047 unsigned char* aPos = (unsigned char*)myPosNext;
0048 const unsigned char aBytesToRead = UTF8_BYTES_MINUS_ONE[*aPos];
0049 myCharUtf32 = 0;
0050 switch (aBytesToRead)
0051 {
0052 case 5:
0053 myCharUtf32 += *aPos++;
0054 myCharUtf32 <<= 6; // remember, illegal UTF-8
0055 [[fallthrough]];
0056 case 4:
0057 myCharUtf32 += *aPos++;
0058 myCharUtf32 <<= 6; // remember, illegal UTF-8
0059 [[fallthrough]];
0060 case 3:
0061 myCharUtf32 += *aPos++;
0062 myCharUtf32 <<= 6;
0063 [[fallthrough]];
0064 case 2:
0065 myCharUtf32 += *aPos++;
0066 myCharUtf32 <<= 6;
0067 [[fallthrough]];
0068 case 1:
0069 myCharUtf32 += *aPos++;
0070 myCharUtf32 <<= 6;
0071 [[fallthrough]];
0072 case 0:
0073 myCharUtf32 += *aPos++;
0074 }
0075 myCharUtf32 -= offsetsFromUTF8[aBytesToRead];
0076 myPosNext = (Type*)aPos;
0077 }
0078
0079 //=================================================================================================
0080
0081 template <typename Type>
0082 inline void NCollection_UtfIterator<Type>::readUTF16()
0083 {
0084 char32_t aChar = *myPosNext++;
0085 // if we have the first half of the surrogate pair
0086 if (aChar >= UTF16_SURROGATE_HIGH_START && aChar <= UTF16_SURROGATE_HIGH_END)
0087 {
0088 const char32_t aChar2 = *myPosNext;
0089 // complete the surrogate pair
0090 if (aChar2 >= UTF16_SURROGATE_LOW_START && aChar2 <= UTF16_SURROGATE_LOW_END)
0091 {
0092 aChar = ((aChar - UTF16_SURROGATE_HIGH_START) << UTF16_SURROGATE_HIGH_SHIFT)
0093 + (aChar2 - UTF16_SURROGATE_LOW_START) + UTF16_SURROGATE_LOW_BASE;
0094 ++myPosNext;
0095 }
0096 }
0097 myCharUtf32 = aChar;
0098 }
0099
0100 //=================================================================================================
0101
0102 template <typename Type>
0103 inline int NCollection_UtfIterator<Type>::AdvanceBytesUtf8() const
0104 {
0105 if (myCharUtf32 >= UTF16_SURROGATE_HIGH_START && myCharUtf32 <= UTF16_SURROGATE_LOW_END)
0106 {
0107 // UTF-16 surrogate values are illegal in UTF-32
0108 return 0;
0109 }
0110 else if (myCharUtf32 < char32_t(0x80))
0111 {
0112 return 1;
0113 }
0114 else if (myCharUtf32 < char32_t(0x800))
0115 {
0116 return 2;
0117 }
0118 else if (myCharUtf32 < char32_t(0x10000))
0119 {
0120 return 3;
0121 }
0122 else if (myCharUtf32 <= UTF32_MAX_LEGAL)
0123 {
0124 return 4;
0125 }
0126 else
0127 {
0128 // illegal
0129 return 0;
0130 }
0131 }
0132
0133 //=================================================================================================
0134
0135 template <typename Type>
0136 inline char* NCollection_UtfIterator<Type>::GetUtf8(char* theBuffer) const
0137 {
0138 // unsigned arithmetic used
0139 return (char*)GetUtf8((unsigned char*)theBuffer);
0140 }
0141
0142 //=================================================================================================
0143
0144 template <typename Type>
0145 inline unsigned char* NCollection_UtfIterator<Type>::GetUtf8(unsigned char* theBuffer) const
0146 {
0147 char32_t aChar = myCharUtf32;
0148 if (myCharUtf32 >= UTF16_SURROGATE_HIGH_START && myCharUtf32 <= UTF16_SURROGATE_LOW_END)
0149 {
0150 // UTF-16 surrogate values are illegal in UTF-32
0151 return theBuffer;
0152 }
0153 else if (myCharUtf32 < char32_t(0x80))
0154 {
0155 *theBuffer++ = static_cast<unsigned char>(aChar | UTF8_FIRST_BYTE_MARK[1]);
0156 return theBuffer;
0157 }
0158 else if (myCharUtf32 < char32_t(0x800))
0159 {
0160 *++theBuffer = static_cast<unsigned char>((aChar | UTF8_BYTE_MARK) & UTF8_BYTE_MASK);
0161 aChar >>= 6;
0162 *--theBuffer = static_cast<unsigned char>(aChar | UTF8_FIRST_BYTE_MARK[2]);
0163 return theBuffer + 2;
0164 }
0165 else if (myCharUtf32 < char32_t(0x10000))
0166 {
0167 theBuffer += 3;
0168 *--theBuffer = static_cast<unsigned char>((aChar | UTF8_BYTE_MARK) & UTF8_BYTE_MASK);
0169 aChar >>= 6;
0170 *--theBuffer = static_cast<unsigned char>((aChar | UTF8_BYTE_MARK) & UTF8_BYTE_MASK);
0171 aChar >>= 6;
0172 *--theBuffer = static_cast<unsigned char>(aChar | UTF8_FIRST_BYTE_MARK[3]);
0173 return theBuffer + 3;
0174 }
0175 else if (myCharUtf32 <= UTF32_MAX_LEGAL)
0176 {
0177 theBuffer += 4;
0178 *--theBuffer = static_cast<unsigned char>((aChar | UTF8_BYTE_MARK) & UTF8_BYTE_MASK);
0179 aChar >>= 6;
0180 *--theBuffer = static_cast<unsigned char>((aChar | UTF8_BYTE_MARK) & UTF8_BYTE_MASK);
0181 aChar >>= 6;
0182 *--theBuffer = static_cast<unsigned char>((aChar | UTF8_BYTE_MARK) & UTF8_BYTE_MASK);
0183 aChar >>= 6;
0184 *--theBuffer = static_cast<unsigned char>(aChar | UTF8_FIRST_BYTE_MARK[4]);
0185 return theBuffer + 4;
0186 }
0187 else
0188 {
0189 // illegal
0190 return theBuffer;
0191 }
0192 }
0193
0194 //=================================================================================================
0195
0196 template <typename Type>
0197 inline int NCollection_UtfIterator<Type>::AdvanceBytesUtf16() const
0198 {
0199 return AdvanceCodeUnitsUtf16() * sizeof(char16_t);
0200 }
0201
0202 //=================================================================================================
0203
0204 template <typename Type>
0205 inline int NCollection_UtfIterator<Type>::AdvanceCodeUnitsUtf16() const
0206 {
0207 if (myCharUtf32 <= UTF32_MAX_BMP) // target is a character <= 0xFFFF
0208 {
0209 // UTF-16 surrogate values are illegal in UTF-32; 0xffff or 0xfffe are both reserved values
0210 if (myCharUtf32 >= UTF16_SURROGATE_HIGH_START && myCharUtf32 <= UTF16_SURROGATE_LOW_END)
0211 {
0212 return 0;
0213 }
0214 else
0215 {
0216 return 1;
0217 }
0218 }
0219 else if (myCharUtf32 > UTF32_MAX_LEGAL)
0220 {
0221 // illegal
0222 return 0;
0223 }
0224 else
0225 {
0226 // target is a character in range 0xFFFF - 0x10FFFF
0227 // surrogate pair
0228 return 2;
0229 }
0230 }
0231
0232 //=================================================================================================
0233
0234 template <typename Type>
0235 inline char16_t* NCollection_UtfIterator<Type>::GetUtf16(char16_t* theBuffer) const
0236 {
0237 if (myCharUtf32 <= UTF32_MAX_BMP) // target is a character <= 0xFFFF
0238 {
0239 // UTF-16 surrogate values are illegal in UTF-32; 0xffff or 0xfffe are both reserved values
0240 if (myCharUtf32 >= UTF16_SURROGATE_HIGH_START && myCharUtf32 <= UTF16_SURROGATE_LOW_END)
0241 {
0242 return theBuffer;
0243 }
0244 else
0245 {
0246 *theBuffer++ = char16_t(myCharUtf32);
0247 return theBuffer;
0248 }
0249 }
0250 else if (myCharUtf32 > UTF32_MAX_LEGAL)
0251 {
0252 // illegal
0253 return theBuffer;
0254 }
0255 else
0256 {
0257 // surrogate pair
0258 char32_t aChar = myCharUtf32 - UTF16_SURROGATE_LOW_BASE;
0259 *theBuffer++ = char16_t((aChar >> UTF16_SURROGATE_HIGH_SHIFT) + UTF16_SURROGATE_HIGH_START);
0260 *theBuffer++ = char16_t((aChar & UTF16_SURROGATE_LOW_MASK) + UTF16_SURROGATE_LOW_START);
0261 return theBuffer;
0262 }
0263 }
0264
0265 //=================================================================================================
0266
0267 template <typename Type>
0268 inline char32_t* NCollection_UtfIterator<Type>::GetUtf32(char32_t* theBuffer) const
0269 {
0270 *theBuffer++ = myCharUtf32;
0271 return theBuffer;
0272 }