1 // Created on: 2013-01-28
2 // Created by: Kirill GAVRILOV
3 // Copyright (c) 2013-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 // =======================================================================
19 // =======================================================================
20 template<typename Type>
21 Standard_Utf32Char NCollection_UtfString<Type>::GetChar (const Standard_Integer theCharIndex) const
23 //Standard_ASSERT_SKIP (theCharIndex < myLength, "Out of range");
24 NCollection_UtfIterator<Type> anIter (myString);
25 for (; *anIter != 0; ++anIter)
27 if (anIter.Index() == theCharIndex)
35 // =======================================================================
36 // function : GetCharBuffer
38 // =======================================================================
39 template<typename Type>
40 const Type* NCollection_UtfString<Type>::GetCharBuffer (const Standard_Integer theCharIndex) const
42 //Standard_ASSERT_SKIP(theCharIndex < myLength);
43 NCollection_UtfIterator<Type> anIter (myString);
44 for (; *anIter != 0; ++anIter)
46 if (anIter.Index() == theCharIndex)
48 return anIter.BufferHere();
54 // =======================================================================
57 // =======================================================================
58 template<typename Type> inline
59 void NCollection_UtfString<Type>::Clear()
64 myString = strAlloc (mySize);
67 // =======================================================================
68 // function : NCollection_UtfString
70 // =======================================================================
71 template<typename Type> inline
72 NCollection_UtfString<Type>::NCollection_UtfString()
73 : myString (strAlloc(0)),
80 // =======================================================================
81 // function : NCollection_UtfString
83 // =======================================================================
84 template<typename Type> inline
85 NCollection_UtfString<Type>::NCollection_UtfString (const NCollection_UtfString& theCopy)
86 : myString (strAlloc (theCopy.mySize)),
87 mySize (theCopy.mySize),
88 myLength (theCopy.myLength)
90 strCopy ((Standard_Byte* )myString, (const Standard_Byte* )theCopy.myString, mySize);
93 #ifndef OCCT_NO_RVALUE_REFERENCE
94 // =======================================================================
95 // function : NCollection_UtfString
97 // =======================================================================
98 template<typename Type> inline
99 NCollection_UtfString<Type>::NCollection_UtfString (NCollection_UtfString&& theOther)
100 : myString(theOther.myString),
101 mySize (theOther.mySize),
102 myLength(theOther.myLength)
104 theOther.myString = NULL;
106 theOther.myLength = 0;
110 // =======================================================================
111 // function : NCollection_UtfString
113 // =======================================================================
114 template<typename Type> inline
115 NCollection_UtfString<Type>::NCollection_UtfString (const char* theCopyUtf8,
116 const Standard_Integer theLength)
121 FromUnicode (theCopyUtf8, theLength);
124 // =======================================================================
125 // function : NCollection_UtfString
127 // =======================================================================
128 template<typename Type> inline
129 NCollection_UtfString<Type>::NCollection_UtfString (const Standard_Utf16Char* theCopyUtf16,
130 const Standard_Integer theLength)
135 FromUnicode (theCopyUtf16, theLength);
138 // =======================================================================
139 // function : NCollection_UtfString
141 // =======================================================================
142 template<typename Type> inline
143 NCollection_UtfString<Type>::NCollection_UtfString (const Standard_Utf32Char* theCopyUtf32,
144 const Standard_Integer theLength)
149 FromUnicode (theCopyUtf32, theLength);
152 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED) || (defined(_MSC_VER) && _MSC_VER >= 1900)
153 // =======================================================================
154 // function : NCollection_UtfString
156 // =======================================================================
157 template<typename Type> inline
158 NCollection_UtfString<Type>::NCollection_UtfString (const Standard_WideChar* theCopyUtfWide,
159 const Standard_Integer theLength)
164 FromUnicode (theCopyUtfWide, theLength);
168 // =======================================================================
169 // function : ~NCollection_UtfString
171 // =======================================================================
172 template<typename Type> inline
173 NCollection_UtfString<Type>::~NCollection_UtfString()
178 // =======================================================================
181 // =======================================================================
182 template<typename Type> inline
183 const NCollection_UtfString<Type>& NCollection_UtfString<Type>::Assign (const NCollection_UtfString<Type>& theOther)
185 if (this == &theOther)
191 mySize = theOther.mySize;
192 myLength = theOther.myLength;
193 myString = strAlloc (mySize);
194 strCopy ((Standard_Byte* )myString, (const Standard_Byte* )theOther.myString, mySize);
198 // =======================================================================
201 // =======================================================================
202 template<typename Type> inline
203 void NCollection_UtfString<Type>::Swap (NCollection_UtfString<Type>& theOther)
205 // Note: we could use std::swap() here, but prefer to not
206 // have dependency on <algorithm> header at that level
207 Type* aString = myString;
208 const Standard_Integer aSize = mySize;
209 const Standard_Integer aLength = myLength;
210 myString = theOther.myString;
211 mySize = theOther.mySize;
212 myLength = theOther.myLength;
213 theOther.myString = aString;
214 theOther.mySize = aSize;
215 theOther.myLength = aLength;
218 #if !defined(__ANDROID__)
219 //! Auxiliary convertion tool.
220 class NCollection_UtfStringTool
223 //! Empty constructor.
224 NCollection_UtfStringTool() : myWideBuffer (NULL) {}
226 //! Destructor for temporary resources.
227 Standard_EXPORT ~NCollection_UtfStringTool();
229 //! Convert the string from current locale into UNICODE (wide characters) using system APIs.
230 //! Returned pointer will be released by this tool.
231 Standard_EXPORT wchar_t* FromLocale (const char* theString);
233 //! Convert the UNICODE (wide characters) string into locale using system APIs.
234 Standard_EXPORT static bool ToLocale (const wchar_t* theWideString,
236 const Standard_Integer theSizeBytes);
238 wchar_t* myWideBuffer; //!< temporary variable
242 // =======================================================================
243 // function : FromLocale
245 // =======================================================================
246 template<typename Type> inline
247 void NCollection_UtfString<Type>::FromLocale (const char* theString,
248 const Standard_Integer theLength)
250 #if defined(__ANDROID__)
251 // no locales on Android
252 FromUnicode (theString, theLength);
254 NCollection_UtfStringTool aConvertor;
255 wchar_t* aWideBuffer = aConvertor.FromLocale (theString);
256 if (aWideBuffer == NULL)
261 FromUnicode (aWideBuffer, theLength);
265 // =======================================================================
266 // function : ToLocale
268 // =======================================================================
269 template<typename Type> inline
270 bool NCollection_UtfString<Type>::ToLocale (char* theBuffer,
271 const Standard_Integer theSizeBytes) const
273 #if defined(__ANDROID__)
274 // no locales on Android
275 NCollection_UtfString<Standard_Utf8Char> anUtf8Copy (myString, myLength);
276 const Standard_Integer aSize = anUtf8Copy.Size() + 1;
277 if (theSizeBytes < aSize)
281 std::memcpy (theBuffer, anUtf8Copy.ToCString(), (Standard_Size )aSize);
284 NCollection_UtfString<wchar_t> aWideCopy (myString, myLength);
285 return NCollection_UtfStringTool::ToLocale (aWideCopy.ToCString(), theBuffer, theSizeBytes);
289 // =======================================================================
290 // function : operator=
292 // =======================================================================
293 template<typename Type> inline
294 const NCollection_UtfString<Type>& NCollection_UtfString<Type>::operator= (const char* theStringUtf8)
296 FromUnicode (theStringUtf8);
300 // =======================================================================
301 // function : operator=
303 // =======================================================================
304 template<typename Type> inline
305 const NCollection_UtfString<Type>& NCollection_UtfString<Type>::operator= (const Standard_WideChar* theStringUtfWide)
307 FromUnicode (theStringUtfWide);
311 // =======================================================================
312 // function : IsEqual
314 // =======================================================================
315 template<typename Type> inline
316 bool NCollection_UtfString<Type>::IsEqual (const NCollection_UtfString& theCompare) const
318 return this == &theCompare
319 || strAreEqual (myString, mySize, theCompare.myString, theCompare.mySize);
322 // =======================================================================
323 // function : operator!=
325 // =======================================================================
326 template<typename Type> inline
327 bool NCollection_UtfString<Type>::operator!= (const NCollection_UtfString& theCompare) const
329 return (!NCollection_UtfString::operator== (theCompare));
332 // =======================================================================
333 // function : operator+=
335 // =======================================================================
336 template<typename Type> inline
337 NCollection_UtfString<Type>& NCollection_UtfString<Type>::operator+= (const NCollection_UtfString<Type>& theAppend)
339 if (theAppend.IsEmpty())
345 Standard_Integer aSize = mySize + theAppend.mySize;
346 Type* aString = strAlloc (aSize);
347 strCopy ((Standard_Byte* )aString, (const Standard_Byte* )myString, mySize);
348 strCopy ((Standard_Byte* )aString + mySize, (const Standard_Byte* )theAppend.myString, theAppend.mySize);
353 myLength += theAppend.myLength;
357 // =======================================================================
358 // function : SubString
360 // =======================================================================
361 template<typename Type> inline
362 NCollection_UtfString<Type> NCollection_UtfString<Type>::SubString (const Standard_Integer theStart,
363 const Standard_Integer theEnd) const
365 if (theStart >= theEnd)
367 return NCollection_UtfString<Type>();
369 for (NCollection_UtfIterator<Type> anIter(myString); *anIter != 0; ++anIter)
371 if (anIter.Index() >= theStart)
373 return NCollection_UtfString<Type> (anIter.BufferHere(), theEnd - theStart);
376 return NCollection_UtfString<Type>();
379 // =======================================================================
382 // =======================================================================
383 template<typename Type> inline
384 const NCollection_UtfString<Standard_Utf8Char> NCollection_UtfString<Type>::ToUtf8() const
386 NCollection_UtfString<Standard_Utf8Char> aCopy;
387 aCopy.FromUnicode (myString);
391 // =======================================================================
392 // function : ToUtf16
394 // =======================================================================
395 template<typename Type> inline
396 const NCollection_UtfString<Standard_Utf16Char> NCollection_UtfString<Type>::ToUtf16() const
398 NCollection_UtfString<Standard_Utf16Char> aCopy;
399 aCopy.FromUnicode (myString);
403 // =======================================================================
404 // function : ToUtf32
406 // =======================================================================
407 template<typename Type> inline
408 const NCollection_UtfString<Standard_Utf32Char> NCollection_UtfString<Type>::ToUtf32() const
410 NCollection_UtfString<Standard_Utf32Char> aCopy;
411 aCopy.FromUnicode (myString);
415 // =======================================================================
416 // function : ToUtfWide
418 // =======================================================================
419 template<typename Type> inline
420 const NCollection_UtfString<Standard_WideChar> NCollection_UtfString<Type>::ToUtfWide() const
422 NCollection_UtfString<Standard_WideChar> aCopy;
423 aCopy.FromUnicode (myString);