File indexing completed on 2026-09-28 09:21:02
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #ifndef NCollection_UtfString_HeaderFile
0017 #define NCollection_UtfString_HeaderFile
0018
0019 #include <NCollection_UtfIterator.hxx>
0020
0021 #include <cstring>
0022 #include <cstdlib>
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033 template <typename Type>
0034 class NCollection_UtfString
0035 {
0036
0037 public:
0038 NCollection_UtfIterator<Type> Iterator() const { return NCollection_UtfIterator<Type>(myString); }
0039
0040
0041 int Size() const noexcept { return mySize; }
0042
0043
0044 int Length() const noexcept { return myLength; }
0045
0046
0047
0048
0049
0050 char32_t GetChar(const int theCharIndex) const;
0051
0052
0053
0054
0055
0056
0057 const Type* GetCharBuffer(const int theCharIndex) const;
0058
0059
0060
0061 char32_t operator[](const int theCharIndex) const { return GetChar(theCharIndex); }
0062
0063
0064 NCollection_UtfString();
0065
0066
0067
0068 NCollection_UtfString(const NCollection_UtfString& theCopy);
0069
0070
0071 NCollection_UtfString(NCollection_UtfString&& theOther) noexcept;
0072
0073
0074
0075
0076
0077
0078 NCollection_UtfString(const char* theCopyUtf8, const int theLength = -1);
0079
0080
0081
0082
0083
0084
0085 NCollection_UtfString(const char16_t* theCopyUtf16, const int theLength = -1);
0086
0087
0088
0089
0090
0091
0092 NCollection_UtfString(const char32_t* theCopyUtf32, const int theLength = -1);
0093
0094 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED) \
0095 || (defined(_MSC_VER) && _MSC_VER >= 1900)
0096
0097
0098
0099
0100
0101
0102
0103 NCollection_UtfString(const wchar_t* theCopyUtfWide, const int theLength = -1);
0104 #endif
0105
0106
0107
0108
0109
0110
0111
0112 template <typename TypeFrom>
0113 inline void FromUnicode(const TypeFrom* theStringUtf, const int theLength = -1)
0114 {
0115 NCollection_UtfIterator<TypeFrom> anIterRead(theStringUtf);
0116 if (*anIterRead == 0)
0117 {
0118
0119 Clear();
0120 return;
0121 }
0122 fromUnicodeImpl(theStringUtf, theLength, anIterRead);
0123 }
0124
0125
0126
0127
0128
0129
0130 void FromLocale(const char* theString, const int theLength = -1);
0131
0132
0133 ~NCollection_UtfString();
0134
0135
0136 bool IsEqual(const NCollection_UtfString& theCompare) const noexcept;
0137
0138
0139
0140
0141
0142 NCollection_UtfString SubString(const int theStart, const int theEnd) const;
0143
0144
0145
0146
0147 const Type* ToCString() const noexcept { return myString; }
0148
0149
0150 const NCollection_UtfString<char> ToUtf8() const;
0151
0152
0153 const NCollection_UtfString<char16_t> ToUtf16() const;
0154
0155
0156 const NCollection_UtfString<char32_t> ToUtf32() const;
0157
0158
0159 const NCollection_UtfString<wchar_t> ToUtfWide() const;
0160
0161
0162
0163
0164
0165 bool ToLocale(char* theBuffer, const int theSizeBytes) const;
0166
0167
0168 bool IsEmpty() const noexcept { return myString[0] == Type(0); }
0169
0170
0171 void Clear();
0172
0173 public:
0174
0175 const NCollection_UtfString& Assign(const NCollection_UtfString& theOther);
0176
0177
0178 void Swap(NCollection_UtfString& theOther) noexcept;
0179
0180
0181 const NCollection_UtfString& operator=(const NCollection_UtfString& theOther)
0182 {
0183 return Assign(theOther);
0184 }
0185
0186
0187 NCollection_UtfString& operator=(NCollection_UtfString&& theOther)
0188 {
0189 Swap(theOther);
0190 return *this;
0191 }
0192
0193
0194 const NCollection_UtfString& operator=(const char* theStringUtf8);
0195
0196
0197 const NCollection_UtfString& operator=(const wchar_t* theStringUtfWide);
0198
0199
0200 NCollection_UtfString& operator+=(const NCollection_UtfString& theAppend);
0201
0202
0203 friend NCollection_UtfString operator+(const NCollection_UtfString& theLeft,
0204 const NCollection_UtfString& theRight)
0205 {
0206 NCollection_UtfString aSumm;
0207 strFree(aSumm.myString);
0208 aSumm.mySize = theLeft.mySize + theRight.mySize;
0209 aSumm.myLength = theLeft.myLength + theRight.myLength;
0210 aSumm.myString = strAlloc(aSumm.mySize);
0211
0212
0213 strCopy((uint8_t*)aSumm.myString, (const uint8_t*)theLeft.myString, theLeft.mySize);
0214 strCopy((uint8_t*)aSumm.myString + theLeft.mySize,
0215 (const uint8_t*)theRight.myString,
0216 theRight.mySize);
0217 return aSumm;
0218 }
0219
0220 public:
0221 bool operator==(const NCollection_UtfString& theCompare) const noexcept
0222 {
0223 return IsEqual(theCompare);
0224 }
0225
0226 bool operator!=(const NCollection_UtfString& theCompare) const noexcept;
0227
0228 private:
0229
0230 void fromUnicodeImpl(const Type* theStringUtf,
0231 const int theLength,
0232 NCollection_UtfIterator<Type>& theIterator)
0233 {
0234 Type* anOldBuffer = myString;
0235
0236
0237 const int aLengthMax = (theLength > 0) ? theLength : IntegerLast();
0238 for (; *theIterator != 0 && theIterator.Index() < aLengthMax; ++theIterator)
0239 {
0240 }
0241
0242 mySize = int((uint8_t*)theIterator.BufferHere() - (uint8_t*)theStringUtf);
0243 myLength = theIterator.Index();
0244 myString = strAlloc(mySize);
0245 strCopy((uint8_t*)myString, (const uint8_t*)theStringUtf, mySize);
0246
0247 strFree(anOldBuffer);
0248 }
0249
0250
0251 template <typename TypeFrom>
0252 void fromUnicodeImpl(
0253 typename opencascade::std::enable_if<!opencascade::std::is_same<Type, TypeFrom>::value,
0254 const TypeFrom*>::type theStringUtf,
0255 const int theLength,
0256 NCollection_UtfIterator<TypeFrom>& theIterator)
0257 {
0258 Type* anOldBuffer = myString;
0259
0260 mySize = 0;
0261 const int aLengthMax = (theLength > 0) ? theLength : IntegerLast();
0262 for (; *theIterator != 0 && theIterator.Index() < aLengthMax; ++theIterator)
0263 {
0264 mySize += theIterator.template AdvanceBytesUtf<Type>();
0265 }
0266 myLength = theIterator.Index();
0267
0268 myString = strAlloc(mySize);
0269
0270
0271 theIterator.Init(theStringUtf);
0272 Type* anIterWrite = myString;
0273 for (; *theIterator != 0 && theIterator.Index() < myLength; ++theIterator)
0274 {
0275 anIterWrite = theIterator.GetUtf(anIterWrite);
0276 }
0277
0278 strFree(anOldBuffer);
0279 }
0280
0281
0282 static Type* strAlloc(const size_t theSizeBytes)
0283 {
0284 Type* aPtr = (Type*)Standard::Allocate(theSizeBytes + sizeof(Type));
0285 if (aPtr != nullptr)
0286 {
0287
0288 aPtr[theSizeBytes / sizeof(Type)] = Type(0);
0289 }
0290 return aPtr;
0291 }
0292
0293
0294 static void strFree(Type*& thePtr) { Standard::Free(thePtr); }
0295
0296
0297 static void strCopy(uint8_t* theStrDst, const uint8_t* theStrSrc, const int theSizeBytes) noexcept
0298 {
0299 std::memcpy(theStrDst, theStrSrc, (size_t)theSizeBytes);
0300 }
0301
0302
0303 static bool strAreEqual(const Type* theString1,
0304 const int theSizeBytes1,
0305 const Type* theString2,
0306 const int theSizeBytes2) noexcept
0307 {
0308 return (theSizeBytes1 == theSizeBytes2)
0309 && (std::memcmp(theString1, theString2, (size_t)theSizeBytes1) == 0);
0310 }
0311
0312 private:
0313 Type* myString;
0314 int mySize;
0315
0316 int myLength;
0317
0318 };
0319
0320
0321 #include <NCollection_UtfString.lxx>
0322
0323 #endif