File indexing completed on 2026-09-15 09:16:30
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #ifndef NCollection_UtfIterator_HeaderFile
0017 #define NCollection_UtfIterator_HeaderFile
0018
0019 #include <Standard_Handle.hxx>
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 template <typename Type>
0030 class NCollection_UtfIterator
0031 {
0032
0033 public:
0034
0035
0036 NCollection_UtfIterator(const Type* theString)
0037 : myPosition(theString),
0038 myPosNext(theString),
0039 myCharIndex(0),
0040 myCharUtf32(0)
0041 {
0042 if (theString != nullptr)
0043 {
0044 ++(*this);
0045 myCharIndex = 0;
0046 }
0047 }
0048
0049
0050 void Init(const Type* theString)
0051 {
0052 myPosition = theString;
0053 myPosNext = theString;
0054 myCharUtf32 = 0;
0055 if (theString != nullptr)
0056 {
0057 ++(*this);
0058 }
0059 myCharIndex = 0;
0060 }
0061
0062
0063
0064 NCollection_UtfIterator& operator++()
0065 {
0066 myPosition = myPosNext;
0067 ++myCharIndex;
0068 readNext(static_cast<const typename CharTypeChooser<Type>::type*>(nullptr));
0069 return *this;
0070 }
0071
0072
0073
0074 NCollection_UtfIterator operator++(int)
0075 {
0076 NCollection_UtfIterator aCopy = *this;
0077 ++*this;
0078 return aCopy;
0079 }
0080
0081
0082 constexpr bool operator==(const NCollection_UtfIterator& theRight) const noexcept
0083 {
0084 return myPosition == theRight.myPosition;
0085 }
0086
0087
0088 constexpr bool IsValid() const noexcept { return myCharUtf32 <= UTF32_MAX_LEGAL; }
0089
0090
0091
0092 constexpr char32_t operator*() const noexcept { return myCharUtf32; }
0093
0094
0095 constexpr const Type* BufferHere() const noexcept { return myPosition; }
0096
0097
0098 Type* ChangeBufferHere() noexcept { return (Type*)myPosition; }
0099
0100
0101 constexpr const Type* BufferNext() const noexcept { return myPosNext; }
0102
0103
0104
0105 constexpr int Index() const noexcept { return myCharIndex; }
0106
0107
0108
0109
0110 int AdvanceBytesUtf8() const;
0111
0112
0113
0114
0115
0116 int AdvanceBytesUtf16() const;
0117
0118
0119
0120
0121
0122 int AdvanceCodeUnitsUtf16() const;
0123
0124
0125
0126 constexpr int AdvanceBytesUtf32() const noexcept { return int(sizeof(char32_t)); }
0127
0128
0129
0130
0131
0132 char* GetUtf8(char* theBuffer) const;
0133 unsigned char* GetUtf8(unsigned char* theBuffer) const;
0134
0135
0136
0137
0138
0139 char16_t* GetUtf16(char16_t* theBuffer) const;
0140
0141
0142
0143
0144
0145 char32_t* GetUtf32(char32_t* theBuffer) const;
0146
0147
0148 template <typename TypeWrite>
0149 inline int AdvanceBytesUtf() const
0150 {
0151 return advanceBytes(static_cast<const typename CharTypeChooser<TypeWrite>::type*>(nullptr));
0152 }
0153
0154
0155
0156
0157
0158 template <typename TypeWrite>
0159 inline TypeWrite* GetUtf(TypeWrite* theBuffer) const
0160 {
0161 return (
0162 TypeWrite*)(getUtf(reinterpret_cast<typename CharTypeChooser<TypeWrite>::type*>(theBuffer)));
0163 }
0164
0165 private:
0166
0167
0168
0169
0170
0171
0172
0173 template <typename TypeChar>
0174 class CharTypeChooser
0175 : public std::conditional<
0176 sizeof(TypeChar) == 1,
0177 char,
0178 typename std::conditional<
0179 sizeof(TypeChar) == 2,
0180 char16_t,
0181 typename std::conditional<sizeof(TypeChar) == 4, char32_t, void>::type>::type>
0182 {
0183 };
0184
0185
0186
0187 void readUTF8();
0188
0189
0190
0191 void readUTF16();
0192
0193
0194 void readNext(const char*) { readUTF8(); }
0195
0196 void readNext(const char16_t*) { readUTF16(); }
0197
0198 void readNext(const char32_t*) noexcept { myCharUtf32 = *myPosNext++; }
0199
0200
0201 int advanceBytes(const char*) const { return AdvanceBytesUtf8(); }
0202
0203 int advanceBytes(const char16_t*) const { return AdvanceBytesUtf16(); }
0204
0205 constexpr int advanceBytes(const char32_t*) const noexcept { return AdvanceBytesUtf32(); }
0206
0207
0208 char* getUtf(char* theBuffer) const { return GetUtf8(theBuffer); }
0209
0210 char16_t* getUtf(char16_t* theBuffer) const { return GetUtf16(theBuffer); }
0211
0212 char32_t* getUtf(char32_t* theBuffer) const { return GetUtf32(theBuffer); }
0213
0214 private:
0215
0216 static constexpr unsigned char UTF8_BYTES_MINUS_ONE[256] = {
0217 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,
0218 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,
0219 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,
0220 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,
0221 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,
0222 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,
0223 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,
0224 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};
0225
0226
0227
0228 static constexpr char32_t offsetsFromUTF8[6] =
0229 {0x00000000UL, 0x00003080UL, 0x000E2080UL, 0x03C82080UL, 0xFA082080UL, 0x82082080UL};
0230
0231
0232 static constexpr unsigned char UTF8_FIRST_BYTE_MARK[7] =
0233 {0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC};
0234
0235
0236 static constexpr char32_t UTF8_BYTE_MASK = 0xBF;
0237 static constexpr char32_t UTF8_BYTE_MARK = 0x80;
0238 static constexpr char32_t UTF16_SURROGATE_HIGH_START = 0xD800;
0239 static constexpr char32_t UTF16_SURROGATE_HIGH_END = 0xDBFF;
0240 static constexpr char32_t UTF16_SURROGATE_LOW_START = 0xDC00;
0241 static constexpr char32_t UTF16_SURROGATE_LOW_END = 0xDFFF;
0242 static constexpr char32_t UTF16_SURROGATE_HIGH_SHIFT = 10;
0243 static constexpr char32_t UTF16_SURROGATE_LOW_BASE = 0x0010000UL;
0244 static constexpr char32_t UTF16_SURROGATE_LOW_MASK = 0x3FFUL;
0245 static constexpr char32_t UTF32_MAX_BMP = 0x0000FFFFUL;
0246 static constexpr char32_t UTF32_MAX_LEGAL = 0x0010FFFFUL;
0247
0248 private:
0249 const Type* myPosition;
0250 const Type* myPosNext;
0251 int myCharIndex;
0252 char32_t myCharUtf32;
0253 };
0254
0255
0256 #include <NCollection_UtfIterator.lxx>
0257
0258 #endif