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