Warning, /include/opencascade/NCollection_UtfString.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 // =======================================================================
0017 // function : GetChar
0018 // purpose :
0019 // =======================================================================
0020 template <typename Type>
0021 Standard_Utf32Char NCollection_UtfString<Type>::GetChar(const Standard_Integer theCharIndex) const
0022 {
0023 // Standard_ASSERT_SKIP (theCharIndex < myLength, "Out of range");
0024 NCollection_UtfIterator<Type> anIter(myString);
0025 for (; *anIter != 0; ++anIter)
0026 {
0027 if (anIter.Index() == theCharIndex)
0028 {
0029 return *anIter;
0030 }
0031 }
0032 return 0;
0033 }
0034
0035 // =======================================================================
0036 // function : GetCharBuffer
0037 // purpose :
0038 // =======================================================================
0039 template <typename Type>
0040 const Type* NCollection_UtfString<Type>::GetCharBuffer(const Standard_Integer theCharIndex) const
0041 {
0042 // Standard_ASSERT_SKIP(theCharIndex < myLength);
0043 NCollection_UtfIterator<Type> anIter(myString);
0044 for (; *anIter != 0; ++anIter)
0045 {
0046 if (anIter.Index() == theCharIndex)
0047 {
0048 return anIter.BufferHere();
0049 }
0050 }
0051 return NULL;
0052 }
0053
0054 // =======================================================================
0055 // function : Clear
0056 // purpose :
0057 // =======================================================================
0058 template <typename Type>
0059 inline void NCollection_UtfString<Type>::Clear()
0060 {
0061 strFree(myString);
0062 mySize = 0;
0063 myLength = 0;
0064 myString = strAlloc(mySize);
0065 }
0066
0067 // =======================================================================
0068 // function : NCollection_UtfString
0069 // purpose :
0070 // =======================================================================
0071 template <typename Type>
0072 inline NCollection_UtfString<Type>::NCollection_UtfString()
0073 : myString(strAlloc(0)),
0074 mySize(0),
0075 myLength(0)
0076 {
0077 //
0078 }
0079
0080 // =======================================================================
0081 // function : NCollection_UtfString
0082 // purpose :
0083 // =======================================================================
0084 template <typename Type>
0085 inline NCollection_UtfString<Type>::NCollection_UtfString(const NCollection_UtfString& theCopy)
0086 : myString(strAlloc(theCopy.mySize)),
0087 mySize(theCopy.mySize),
0088 myLength(theCopy.myLength)
0089 {
0090 strCopy((Standard_Byte*)myString, (const Standard_Byte*)theCopy.myString, mySize);
0091 }
0092
0093 // =======================================================================
0094 // function : NCollection_UtfString
0095 // purpose :
0096 // =======================================================================
0097 template <typename Type>
0098 inline NCollection_UtfString<Type>::NCollection_UtfString(NCollection_UtfString&& theOther) noexcept
0099 : myString(theOther.myString),
0100 mySize(theOther.mySize),
0101 myLength(theOther.myLength)
0102 {
0103 theOther.myString = NULL;
0104 theOther.mySize = 0;
0105 theOther.myLength = 0;
0106 }
0107
0108 // =======================================================================
0109 // function : NCollection_UtfString
0110 // purpose :
0111 // =======================================================================
0112 template <typename Type>
0113 inline NCollection_UtfString<Type>::NCollection_UtfString(const char* theCopyUtf8,
0114 const Standard_Integer theLength)
0115 : myString(NULL),
0116 mySize(0),
0117 myLength(0)
0118 {
0119 FromUnicode(theCopyUtf8, theLength);
0120 }
0121
0122 // =======================================================================
0123 // function : NCollection_UtfString
0124 // purpose :
0125 // =======================================================================
0126 template <typename Type>
0127 inline NCollection_UtfString<Type>::NCollection_UtfString(const Standard_Utf16Char* theCopyUtf16,
0128 const Standard_Integer theLength)
0129 : myString(NULL),
0130 mySize(0),
0131 myLength(0)
0132 {
0133 FromUnicode(theCopyUtf16, theLength);
0134 }
0135
0136 // =======================================================================
0137 // function : NCollection_UtfString
0138 // purpose :
0139 // =======================================================================
0140 template <typename Type>
0141 inline NCollection_UtfString<Type>::NCollection_UtfString(const Standard_Utf32Char* theCopyUtf32,
0142 const Standard_Integer theLength)
0143 : myString(NULL),
0144 mySize(0),
0145 myLength(0)
0146 {
0147 FromUnicode(theCopyUtf32, theLength);
0148 }
0149
0150 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED) \
0151 || (defined(_MSC_VER) && _MSC_VER >= 1900)
0152 // =======================================================================
0153 // function : NCollection_UtfString
0154 // purpose :
0155 // =======================================================================
0156 template <typename Type>
0157 inline NCollection_UtfString<Type>::NCollection_UtfString(const Standard_WideChar* theCopyUtfWide,
0158 const Standard_Integer theLength)
0159 : myString(NULL),
0160 mySize(0),
0161 myLength(0)
0162 {
0163 FromUnicode(theCopyUtfWide, theLength);
0164 }
0165 #endif
0166
0167 // =======================================================================
0168 // function : ~NCollection_UtfString
0169 // purpose :
0170 // =======================================================================
0171 template <typename Type>
0172 inline NCollection_UtfString<Type>::~NCollection_UtfString()
0173 {
0174 strFree(myString);
0175 }
0176
0177 // =======================================================================
0178 // function : Assign
0179 // purpose :
0180 // =======================================================================
0181 template <typename Type>
0182 inline const NCollection_UtfString<Type>& NCollection_UtfString<Type>::Assign(
0183 const NCollection_UtfString<Type>& theOther)
0184 {
0185 if (this == &theOther)
0186 {
0187 return (*this);
0188 }
0189
0190 strFree(myString);
0191 mySize = theOther.mySize;
0192 myLength = theOther.myLength;
0193 myString = strAlloc(mySize);
0194 strCopy((Standard_Byte*)myString, (const Standard_Byte*)theOther.myString, mySize);
0195 return (*this);
0196 }
0197
0198 // =======================================================================
0199 // function : Swap
0200 // purpose :
0201 // =======================================================================
0202 template <typename Type>
0203 inline void NCollection_UtfString<Type>::Swap(NCollection_UtfString<Type>& theOther)
0204 {
0205 // Note: we could use std::swap() here, but prefer to not
0206 // have dependency on <algorithm> header at that level
0207 Type* aString = myString;
0208 const Standard_Integer aSize = mySize;
0209 const Standard_Integer aLength = myLength;
0210 myString = theOther.myString;
0211 mySize = theOther.mySize;
0212 myLength = theOther.myLength;
0213 theOther.myString = aString;
0214 theOther.mySize = aSize;
0215 theOther.myLength = aLength;
0216 }
0217
0218 #if !defined(__ANDROID__)
0219 //! Auxiliary conversion tool.
0220 class NCollection_UtfStringTool
0221 {
0222 public:
0223 //! Empty constructor.
0224 NCollection_UtfStringTool()
0225 : myWideBuffer(NULL)
0226 {
0227 }
0228
0229 //! Destructor for temporary resources.
0230 Standard_EXPORT ~NCollection_UtfStringTool();
0231
0232 //! Convert the string from current locale into UNICODE (wide characters) using system APIs.
0233 //! Returned pointer will be released by this tool.
0234 Standard_EXPORT wchar_t* FromLocale(const char* theString);
0235
0236 //! Convert the UNICODE (wide characters) string into locale using system APIs.
0237 Standard_EXPORT static bool ToLocale(const wchar_t* theWideString,
0238 char* theBuffer,
0239 const Standard_Integer theSizeBytes);
0240
0241 private:
0242 wchar_t* myWideBuffer; //!< temporary variable
0243 };
0244 #endif
0245
0246 // =======================================================================
0247 // function : FromLocale
0248 // purpose :
0249 // =======================================================================
0250 template <typename Type>
0251 inline void NCollection_UtfString<Type>::FromLocale(const char* theString,
0252 const Standard_Integer theLength)
0253 {
0254 #if defined(__ANDROID__)
0255 // no locales on Android
0256 FromUnicode(theString, theLength);
0257 #else
0258 NCollection_UtfStringTool aConvertor;
0259 wchar_t* aWideBuffer = aConvertor.FromLocale(theString);
0260 if (aWideBuffer == NULL)
0261 {
0262 Clear();
0263 return;
0264 }
0265 FromUnicode(aWideBuffer, theLength);
0266 #endif
0267 }
0268
0269 // =======================================================================
0270 // function : ToLocale
0271 // purpose :
0272 // =======================================================================
0273 template <typename Type>
0274 inline bool NCollection_UtfString<Type>::ToLocale(char* theBuffer,
0275 const Standard_Integer theSizeBytes) const
0276 {
0277 #if defined(__ANDROID__)
0278 // no locales on Android
0279 NCollection_UtfString<Standard_Utf8Char> anUtf8Copy(myString, myLength);
0280 const Standard_Integer aSize = anUtf8Copy.Size() + 1;
0281 if (theSizeBytes < aSize)
0282 {
0283 return false;
0284 }
0285 std::memcpy(theBuffer, anUtf8Copy.ToCString(), (Standard_Size)aSize);
0286 return true;
0287 #else
0288 NCollection_UtfString<wchar_t> aWideCopy(myString, myLength);
0289 return NCollection_UtfStringTool::ToLocale(aWideCopy.ToCString(), theBuffer, theSizeBytes);
0290 #endif
0291 }
0292
0293 // =======================================================================
0294 // function : operator=
0295 // purpose :
0296 // =======================================================================
0297 template <typename Type>
0298 inline const NCollection_UtfString<Type>& NCollection_UtfString<Type>::operator=(
0299 const char* theStringUtf8)
0300 {
0301 FromUnicode(theStringUtf8);
0302 return (*this);
0303 }
0304
0305 // =======================================================================
0306 // function : operator=
0307 // purpose :
0308 // =======================================================================
0309 template <typename Type>
0310 inline const NCollection_UtfString<Type>& NCollection_UtfString<Type>::operator=(
0311 const Standard_WideChar* theStringUtfWide)
0312 {
0313 FromUnicode(theStringUtfWide);
0314 return (*this);
0315 }
0316
0317 // =======================================================================
0318 // function : IsEqual
0319 // purpose :
0320 // =======================================================================
0321 template <typename Type>
0322 inline bool NCollection_UtfString<Type>::IsEqual(const NCollection_UtfString& theCompare) const
0323 {
0324 return this == &theCompare
0325 || strAreEqual(myString, mySize, theCompare.myString, theCompare.mySize);
0326 }
0327
0328 // =======================================================================
0329 // function : operator!=
0330 // purpose :
0331 // =======================================================================
0332 template <typename Type>
0333 inline bool NCollection_UtfString<Type>::operator!=(const NCollection_UtfString& theCompare) const
0334 {
0335 return (!NCollection_UtfString::operator==(theCompare));
0336 }
0337
0338 // =======================================================================
0339 // function : operator+=
0340 // purpose :
0341 // =======================================================================
0342 template <typename Type>
0343 inline NCollection_UtfString<Type>& NCollection_UtfString<Type>::operator+=(
0344 const NCollection_UtfString<Type>& theAppend)
0345 {
0346 if (theAppend.IsEmpty())
0347 {
0348 return (*this);
0349 }
0350
0351 // create new string
0352 Standard_Integer aSize = mySize + theAppend.mySize;
0353 Type* aString = strAlloc(aSize);
0354 strCopy((Standard_Byte*)aString, (const Standard_Byte*)myString, mySize);
0355 strCopy((Standard_Byte*)aString + mySize,
0356 (const Standard_Byte*)theAppend.myString,
0357 theAppend.mySize);
0358
0359 strFree(myString);
0360 mySize = aSize;
0361 myString = aString;
0362 myLength += theAppend.myLength;
0363 return (*this);
0364 }
0365
0366 // =======================================================================
0367 // function : SubString
0368 // purpose :
0369 // =======================================================================
0370 template <typename Type>
0371 inline NCollection_UtfString<Type> NCollection_UtfString<Type>::SubString(
0372 const Standard_Integer theStart,
0373 const Standard_Integer theEnd) const
0374 {
0375 if (theStart >= theEnd)
0376 {
0377 return NCollection_UtfString<Type>();
0378 }
0379 for (NCollection_UtfIterator<Type> anIter(myString); *anIter != 0; ++anIter)
0380 {
0381 if (anIter.Index() >= theStart)
0382 {
0383 return NCollection_UtfString<Type>(anIter.BufferHere(), theEnd - theStart);
0384 }
0385 }
0386 return NCollection_UtfString<Type>();
0387 }
0388
0389 // =======================================================================
0390 // function : ToUtf8
0391 // purpose :
0392 // =======================================================================
0393 template <typename Type>
0394 inline const NCollection_UtfString<Standard_Utf8Char> NCollection_UtfString<Type>::ToUtf8() const
0395 {
0396 NCollection_UtfString<Standard_Utf8Char> aCopy;
0397 aCopy.FromUnicode(myString);
0398 return aCopy;
0399 }
0400
0401 // =======================================================================
0402 // function : ToUtf16
0403 // purpose :
0404 // =======================================================================
0405 template <typename Type>
0406 inline const NCollection_UtfString<Standard_Utf16Char> NCollection_UtfString<Type>::ToUtf16() const
0407 {
0408 NCollection_UtfString<Standard_Utf16Char> aCopy;
0409 aCopy.FromUnicode(myString);
0410 return aCopy;
0411 }
0412
0413 // =======================================================================
0414 // function : ToUtf32
0415 // purpose :
0416 // =======================================================================
0417 template <typename Type>
0418 inline const NCollection_UtfString<Standard_Utf32Char> NCollection_UtfString<Type>::ToUtf32() const
0419 {
0420 NCollection_UtfString<Standard_Utf32Char> aCopy;
0421 aCopy.FromUnicode(myString);
0422 return aCopy;
0423 }
0424
0425 // =======================================================================
0426 // function : ToUtfWide
0427 // purpose :
0428 // =======================================================================
0429 template <typename Type>
0430 inline const NCollection_UtfString<Standard_WideChar> NCollection_UtfString<Type>::ToUtfWide() const
0431 {
0432 NCollection_UtfString<Standard_WideChar> aCopy;
0433 aCopy.FromUnicode(myString);
0434 return aCopy;
0435 }