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