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 // =======================================================================
17 // function : strGetAdvance
18 // purpose : Compute advance for specified string.
19 // =======================================================================
20 template<typename TypeTo> template<typename TypeFrom> inline
21 void NCollection_UtfString<TypeTo>::strGetAdvance (const TypeFrom* theStringUtf,
22 const Standard_Integer theLengthMax,
23 Standard_Integer& theSizeBytes,
24 Standard_Integer& theLength)
28 NCollection_UtfIterator<TypeFrom> anIter (theStringUtf);
29 const Standard_Integer aLengthMax = (theLengthMax > 0) ? theLengthMax : IntegerLast();
30 switch (sizeof(TypeTo))
32 case sizeof(Standard_Utf8Char):
34 for (; *anIter != 0 && anIter.Index() < aLengthMax; ++anIter)
36 theSizeBytes += anIter.AdvanceBytesUtf8();
38 theLength = anIter.Index();
41 case sizeof(Standard_Utf16Char):
43 for (; *anIter != 0 && anIter.Index() < aLengthMax; ++anIter)
45 theSizeBytes += anIter.AdvanceBytesUtf16();
47 theLength = anIter.Index();
50 case sizeof(Standard_Utf32Char):
52 for (; *anIter != 0 && anIter.Index() < aLengthMax; ++anIter)
54 theSizeBytes += anIter.AdvanceBytesUtf32();
56 theLength = anIter.Index();
63 // =======================================================================
66 // =======================================================================
67 template<typename Type>
68 Standard_Utf32Char NCollection_UtfString<Type>::GetChar (const Standard_Integer theCharIndex) const
70 //Standard_ASSERT_SKIP (theCharIndex < myLength, "Out of range");
71 NCollection_UtfIterator<Type> anIter (myString);
72 for (; *anIter != 0; ++anIter)
74 if (anIter.Index() == theCharIndex)
82 // =======================================================================
83 // function : GetCharBuffer
85 // =======================================================================
86 template<typename Type>
87 const Type* NCollection_UtfString<Type>::GetCharBuffer (const Standard_Integer theCharIndex) const
89 //Standard_ASSERT_SKIP(theCharIndex < myLength);
90 NCollection_UtfIterator<Type> anIter (myString);
91 for (; *anIter != 0; ++anIter)
93 if (anIter.Index() == theCharIndex)
95 return anIter.BufferHere();
101 // =======================================================================
104 // =======================================================================
105 template<typename Type> inline
106 void NCollection_UtfString<Type>::Clear()
111 myString = strAlloc (mySize);
114 // =======================================================================
115 // function : NCollection_UtfString
117 // =======================================================================
118 template<typename Type> inline
119 NCollection_UtfString<Type>::NCollection_UtfString()
120 : myString (strAlloc(0)),
127 // =======================================================================
128 // function : NCollection_UtfString
130 // =======================================================================
131 template<typename Type> inline
132 NCollection_UtfString<Type>::NCollection_UtfString (const NCollection_UtfString& theCopy)
133 : myString (strAlloc (theCopy.mySize)),
134 mySize (theCopy.mySize),
135 myLength (theCopy.myLength)
137 strCopy ((Standard_Byte* )myString, (const Standard_Byte* )theCopy.myString, mySize);
140 // =======================================================================
141 // function : NCollection_UtfString
143 // =======================================================================
144 template<typename Type> inline
145 NCollection_UtfString<Type>::NCollection_UtfString (const char* theCopyUtf8,
146 const Standard_Integer theLength)
151 FromUnicode (theCopyUtf8, theLength);
154 // =======================================================================
155 // function : NCollection_UtfString
157 // =======================================================================
158 template<typename Type> inline
159 NCollection_UtfString<Type>::NCollection_UtfString (const Standard_Utf16Char* theCopyUtf16,
160 const Standard_Integer theLength)
165 FromUnicode (theCopyUtf16, theLength);
168 // =======================================================================
169 // function : NCollection_UtfString
171 // =======================================================================
172 template<typename Type> inline
173 NCollection_UtfString<Type>::NCollection_UtfString (const Standard_Utf32Char* theCopyUtf32,
174 const Standard_Integer theLength)
179 FromUnicode (theCopyUtf32, theLength);
182 // =======================================================================
183 // function : NCollection_UtfString
185 // =======================================================================
186 template<typename Type> inline
187 NCollection_UtfString<Type>::NCollection_UtfString (const Standard_WideChar* theCopyUtfWide,
188 const Standard_Integer theLength)
193 FromUnicode (theCopyUtfWide, theLength);
196 // =======================================================================
197 // function : ~NCollection_UtfString
199 // =======================================================================
200 template<typename Type> inline
201 NCollection_UtfString<Type>::~NCollection_UtfString()
206 // =======================================================================
207 // function : operator=
209 // =======================================================================
210 template<typename Type> inline
211 const NCollection_UtfString<Type>& NCollection_UtfString<Type>::operator= (const NCollection_UtfString<Type>& theOther)
213 if (this == &theOther)
219 mySize = theOther.mySize;
220 myLength = theOther.myLength;
221 myString = strAlloc (mySize);
222 strCopy ((Standard_Byte* )myString, (const Standard_Byte* )theOther.myString, mySize);
226 // =======================================================================
227 // function : FromUnicode
229 // =======================================================================
230 template<typename Type> template<typename TypeFrom>
231 void NCollection_UtfString<Type>::FromUnicode (const TypeFrom* theStringUtf,
232 const Standard_Integer theLength)
234 Type* anOldBuffer = myString; // necessary in case of self-copying
235 NCollection_UtfIterator<TypeFrom> anIterRead (theStringUtf);
236 if (*anIterRead == 0)
243 switch (sizeof(TypeFrom)) // use switch() rather than if() to shut up msvc compiler
250 for(; *anIterRead != 0 && anIterRead.Index() < theLength; ++anIterRead) {}
252 mySize = Standard_Integer((Standard_Byte* )anIterRead.BufferHere() - (Standard_Byte* )theStringUtf);
253 myLength = anIterRead.Index();
254 myString = strAlloc (mySize);
255 strCopy ((Standard_Byte* )myString, (const Standard_Byte* )theStringUtf, mySize);
256 strFree (anOldBuffer);
263 strGetAdvance (theStringUtf, theLength, mySize, myLength);
264 myString = strAlloc (mySize);
266 anIterRead.Init (theStringUtf);
267 Type* anIterWrite = myString;
268 for (; *anIterRead != 0 && anIterRead.Index() < myLength; ++anIterRead)
270 anIterWrite = anIterRead.GetUtf (anIterWrite);
272 strFree (anOldBuffer);
275 // =======================================================================
276 // function : FromLocale
278 // =======================================================================
279 template<typename Type> inline
280 void NCollection_UtfString<Type>::FromLocale (const char* theString,
281 const Standard_Integer theLength)
283 #if(defined(_WIN32) || defined(__WIN32__))
285 int aWideSize = MultiByteToWideChar (CP_ACP, MB_PRECOMPOSED, theString, -1, NULL, 0);
291 wchar_t* aWideBuffer = new wchar_t[aWideSize + 1];
292 MultiByteToWideChar (CP_ACP, MB_PRECOMPOSED, theString, -1, aWideBuffer, aWideSize);
293 aWideBuffer[aWideSize] = L'\0';
294 FromUnicode (aWideBuffer, theLength);
295 delete[] aWideBuffer;
297 // this is size in bytes but should probably be enough to store string in wide chars
298 // notice that these functions are sensitive to locale set by application!
299 int aMbLen = mblen (theString, MB_CUR_MAX);
305 wchar_t* aWideBuffer = new wchar_t[aMbLen + 1];
306 mbstowcs (aWideBuffer, theString, aMbLen);
307 aWideBuffer[aMbLen] = L'\0';
308 FromUnicode (aWideBuffer, theLength);
309 delete[] aWideBuffer;
313 // =======================================================================
314 // function : ToLocale
316 // =======================================================================
317 template<typename Type> inline
318 bool NCollection_UtfString<Type>::ToLocale (char* theBuffer,
319 const Standard_Integer theSizeBytes) const
321 NCollection_UtfString<wchar_t> aWideCopy (myString, myLength);
322 #if(defined(_WIN32) || defined(__WIN32__))
323 int aMbBytes = WideCharToMultiByte (CP_ACP, 0, aWideCopy.ToCString(), -1, theBuffer, theSizeBytes, NULL, NULL);
325 std::size_t aMbBytes = std::wcstombs (theBuffer, aWideCopy.ToCString(), theSizeBytes);
335 // =======================================================================
336 // function : operator=
338 // =======================================================================
339 template<typename Type> inline
340 const NCollection_UtfString<Type>& NCollection_UtfString<Type>::operator= (const char* theStringUtf8)
342 FromUnicode (theStringUtf8);
346 // =======================================================================
347 // function : operator=
349 // =======================================================================
350 template<typename Type> inline
351 const NCollection_UtfString<Type>& NCollection_UtfString<Type>::operator= (const Standard_WideChar* theStringUtfWide)
353 FromUnicode (theStringUtfWide);
357 // =======================================================================
358 // function : IsEqual
360 // =======================================================================
361 template<typename Type> inline
362 bool NCollection_UtfString<Type>::IsEqual (const NCollection_UtfString& theCompare) const
364 return this == &theCompare
365 || strAreEqual (myString, mySize, theCompare.myString, theCompare.mySize);
368 // =======================================================================
369 // function : operator!=
371 // =======================================================================
372 template<typename Type> inline
373 bool NCollection_UtfString<Type>::operator!= (const NCollection_UtfString& theCompare) const
375 return (!NCollection_UtfString::operator== (theCompare));
378 // =======================================================================
379 // function : operator+=
381 // =======================================================================
382 template<typename Type> inline
383 NCollection_UtfString<Type>& NCollection_UtfString<Type>::operator+= (const NCollection_UtfString<Type>& theAppend)
385 if (theAppend.IsEmpty())
391 Standard_Integer aSize = mySize + theAppend.mySize;
392 Type* aString = strAlloc (aSize);
393 strCopy ((Standard_Byte* )aString, (const Standard_Byte* )myString, mySize);
394 strCopy ((Standard_Byte* )aString + mySize, (const Standard_Byte* )theAppend.myString, theAppend.mySize);
399 myLength += theAppend.myLength;
403 // =======================================================================
404 // function : SubString
406 // =======================================================================
407 template<typename Type> inline
408 NCollection_UtfString<Type> NCollection_UtfString<Type>::SubString (const Standard_Integer theStart,
409 const Standard_Integer theEnd) const
411 if (theStart >= theEnd)
413 return NCollection_UtfString<Type>();
415 for (NCollection_UtfIterator<Type> anIter(myString); *anIter != 0; ++anIter)
417 if (anIter.Index() >= theStart)
419 return NCollection_UtfString<Type> (anIter.BufferHere(), theEnd - theStart);
422 return NCollection_UtfString<Type>();
425 // =======================================================================
428 // =======================================================================
429 template<typename Type> inline
430 const NCollection_UtfString<Standard_Utf8Char> NCollection_UtfString<Type>::ToUtf8() const
432 NCollection_UtfString<Standard_Utf8Char> aCopy;
433 aCopy.FromUnicode (myString);
437 // =======================================================================
438 // function : ToUtf16
440 // =======================================================================
441 template<typename Type> inline
442 const NCollection_UtfString<Standard_Utf16Char> NCollection_UtfString<Type>::ToUtf16() const
444 NCollection_UtfString<Standard_Utf16Char> aCopy;
445 aCopy.FromUnicode (myString);
449 // =======================================================================
450 // function : ToUtf32
452 // =======================================================================
453 template<typename Type> inline
454 const NCollection_UtfString<Standard_Utf32Char> NCollection_UtfString<Type>::ToUtf32() const
456 NCollection_UtfString<Standard_Utf32Char> aCopy;
457 aCopy.FromUnicode (myString);
461 // =======================================================================
462 // function : ToUtfWide
464 // =======================================================================
465 template<typename Type> inline
466 const NCollection_UtfString<Standard_WideChar> NCollection_UtfString<Type>::ToUtfWide() const
468 NCollection_UtfString<Standard_WideChar> aCopy;
469 aCopy.FromUnicode (myString);