VR-Vantage
Loading...
Searching...
No Matches
DtUnicode.inl
Go to the documentation of this file.
1/****************************************************************************
2 * Copyright (c) 2025 MAK Technologies, Inc.
3 * All rights reserved.
4 ****************************************************************************/
5
9
10#pragma once
11#include <algorithm>
12#include <string.h>
13
14#include <cassert>
15
16#if _WIN32
17#include <boost/detail/interlocked.hpp>
18#endif
19
21
22#ifdef min
23#undef min
24#endif
25
26#if DT_USE_VRLINK
27#include <vlutil/vlExceptions.h>
28#if _WIN32
29#define AtomicIncrement(a) BOOST_133_INTERLOCKED_INCREMENT(a)
30#define AtomicDecrement(a) BOOST_133_INTERLOCKED_DECREMENT(a)
31#else
32#define AtomicIncrement(a) boost::detail::atomic_increment(a)
33#define AtomicDecrement(a) boost::detail::atomic_decrement(a)
34#endif
35#else
36
37#include <boost/smart_ptr/detail/sp_counted_base.hpp>
38
39
40#if defined(__rhentws8__) || defined(__rhentws9__)
41#define AtomicIncrement(a) boost::detail::atomic_increment((unsigned int*)a)
42#define AtomicDecrement(a) boost::detail::atomic_decrement((unsigned int*)a)
43#else
44#define AtomicIncrement(a) boost::detail::atomic_increment((std::atomic_int*)a)
45#define AtomicDecrement(a) boost::detail::atomic_decrement((std::atomic_int*)a)
46#endif
47
48#endif
49
50#if _WIN32
51#define MallocAligned(sz,align) _aligned_malloc(sz,align)
52#define ReallocAligned(ptr,sz,oldsz,align) _aligned_realloc(ptr,sz,align)
53#define FreeAligned(ptr) _aligned_free(ptr)
54#else
55
56#include <stdlib.h>
57
58inline void* mallocAligned(size_t sz, size_t align)
59{
60 void* ptr;
61 posix_memalign(&ptr,align,sz);
62 return ptr;
63}
64
65#define MallocAligned(sz,align) mallocAligned(sz,align)
66inline void* reallocAligned(void* ptr, size_t sz, size_t oldsz, size_t align)
67{
68 void* newPtr = 0;
69 if(ptr)
70 {
71 newPtr = MallocAligned(sz,align);
72 if(newPtr)
73 {
74 memcpy(newPtr,ptr,oldsz);
75 free(ptr);
76 }
77 }
78 return newPtr;
79}
80
81#define ReallocAligned(ptr,sz,oldsz, align) reallocAligned(ptr,sz,oldsz,align)
82#define FreeAligned(ptr) free(ptr)
83#endif
84
85
86
87namespace makVrv
88{
89
90inline size_t utf16len(const unsigned short* str)
91{
92 if( str == NULL)
93 {
94 return 0;
95 }
96 const unsigned short* checker = str;
97 while(*checker != 0)
98 {
99 ++checker;
100 }
101 return checker - str;
102}
103
104
105inline const unsigned short* wcsnstr(const unsigned short* haystack, int hayLength,
106 const unsigned short* needle, int needleLength)
107{
108 //Validate pointers and lengths.
109 //Do not remove, allows for faster code.
110 if(needle == NULL || needleLength < -1)
111 {
112 return haystack;
113 }
114 if(haystack == NULL || hayLength < -1 )
115 {
116 return NULL;
117 }
118 //Read the first character int faster read, and treat the needle as one
119 //character shorter.
120 unsigned short charToFind = *needle++;
121 --needleLength;
122 // The end of the needle string.
123 const unsigned short* needleEnd = needle + needleLength;
124 // The end of the haystack string.
125 const unsigned short* haystackEnd = haystack + hayLength;
126 // Search end is the pointer where the search must stop, as there are not
127 // enough character left to find the needle.
128 const unsigned short* searchEnd = haystackEnd - needleLength;
129 while(haystack != searchEnd)
130 {
131 //Read the current character.
132 unsigned short currentChar = *haystack++;
133 if(currentChar == charToFind)
134 {
135 //Search remaining needle.
136 const unsigned short* hayRead = haystack;
137 const unsigned short* needleRead = needle;
138 for(;;)
139 {
140 if(needleRead == needleEnd) //End of needle.
141 {
143 haystackEnd, haystack - 1, hayRead ))
144 {
145 return haystack - 1;
146 }
147 else
148 {
149 break;
150 }
151 }
152 if(*hayRead != *needleRead)
153 {
154 break;
155 }
156 ++hayRead;
157 ++needleRead;
158 }
159 }
160 }
161 return 0;
162}
163
164
169
171: myCodeUnits()
172{
173 while(n--)
174 {
175 push_back(c);
176 }
177}
178
182
183
185: myCodeUnits(str.myCodeUnits) // Share the units
186{
187}
188
190{
191 if(&str != this)
192 {
193 //Share the units
195 }
196 return *this;
197}
198
200{
201 //Verify we are not doing trivial operation.
202 if(str.length())
203 {
204 //Concat, supports passing itself. itself.
205 myCodeUnits.concat(str.myCodeUnits);
206 }
207 return *this;
208}
209
211{
212 this->push_back(c);
213 return *this;
214}
215
216
218{
219 return DtUnicode(*this,str);
220}
221
223{
224 DtUnicode result(*this);
225 result.push_back(character);
226 return result;
227}
228
229template <int wcharSize>
230inline DtUnicode decodeWCHAR(const wchar_t utf16[], int len /*= -1*/);
231
232template <>
233inline DtUnicode decodeWCHAR<2>(const wchar_t utf16[], int len /*= -1*/)
234{
235 return DtUnicode::fromUtf16((unsigned short*)utf16,len);
236}
237
238template <>
239inline DtUnicode decodeWCHAR<4>(const wchar_t utf16[], int len /*= -1*/)
240{
241 return DtUnicode::fromUtf32((unsigned int*)utf16,len);
242}
243
244
245DtUnicode DtUnicode::fromWCharArray( const wchar_t utf16[], int len /*= -1*/ )
246{
247 return decodeWCHAR<sizeof(wchar_t)>(utf16,len);
248}
249
250
251DtUnicode DtUnicode::fromWideString( const std::wstring& str )
252{
253 return fromWCharArray(str.c_str(),str.length());
254}
255
256
257DtUnicode DtUnicode::fromUtf8( const unsigned char* utf8, int len /*= -1*/ )
258{
259 return DtUnicode((const char*)utf8,len,DtUnicode::Utf8);
260}
261
262DtUnicode DtUnicode::fromUtf8(const std::string& utf8)
263{
264 return DtUnicode((const char*)utf8.c_str(),utf8.size(),DtUnicode::Utf8);
265}
266
267
268DtUnicode DtUnicode::fromUtf16(const unsigned short* utf16, int len )
269{
270 return DtUnicode(utf16,len);
271}
272
273
274DtUnicode DtUnicode::fromUtf32( const unsigned int* utf32, int len )
275{
276 return DtUnicode(utf32,len);
277}
278
279
280DtUnicode DtUnicode::fromAscii(const char* ascii, int len )
281{
282 return DtUnicode(ascii,len);
283}
284
285DtUnicode DtUnicode::fromAscii(const std::string& ascii)
286{
287 return DtUnicode(ascii);
288}
289
290void DtUnicode::setFromAscii(const std::string& ascii)
291{
292 myCodeUnits.resize(ascii.size() << 1);
293
294 CodeUnit* writePtr = myCodeUnits.begin;
295 std::string::const_iterator i = ascii.begin();
296 std::string::const_iterator e = ascii.end();
297 while(i != e)
298 {
299 *writePtr++ = *i++;
300 }
301}
302
303void DtUnicode::setFromAscii(const char* ascii, int len )
304{
305 if(len < 0)
306 {
307 len = (int)strlen(ascii);
308 }
309 myCodeUnits.resize(len << 1);
310 CodeUnit* writePtr = myCodeUnits.begin;
311 const char* i = ascii;
312 const char* e = ascii + len;
313 while(i != e)
314 {
315 *writePtr++ = *i++;
316 }
317}
318
319void DtUnicode::setFromUtf8( const unsigned char* utf8, int len /*= -1*/ )
320{
321 if(len < 0)
322 {
323 len = (int)strlen((const char*)utf8);
324 }
325
326 myCodeUnits.reserve(len);
327 const unsigned char* i = utf8;
328 const unsigned char* e = utf8 + len;
330 while(i != e)
331 {
332 //If the utf8 string is corrupt, throw
333 if(i + DtUnicodeChar::utf8Length(*i) > e)
334 {
335 return;
336 }
337 i += c.readUtf8((unsigned char*)i);
338 push_back(c);
339 }
340}
341
342void DtUnicode::setFromUtf8(const std::string& utf8)
343{
344 setFromUtf8((const unsigned char*)utf8.c_str(),utf8.size());
345}
346
347void DtUnicode::setFromLatin1( const char* latin, int len )
348{
349 if(len < 0)
350 {
351 len = (int)strlen(latin);
352 }
353 myCodeUnits.resize(len << 1);
354 CodeUnit* writePtr = myCodeUnits.begin;
355 const char* i = latin;
356 const char* e = latin + len;
357 while(i != e)
358 {
359 *writePtr++ = *i++;
360 }
361}
362
363
364void DtUnicode::toAscii(std::string& str) const
365{
366 str.clear();
367 str.reserve(length());
368 CodeUnit* i = myCodeUnits.begin;
369 CodeUnit* e = myCodeUnits.end;
370 for(;i != e;++i)
371 {
372 CodeUnit c = *i;
374 {
375 ++i;
376 if(i == e) //Error unicode string is not properly formed.
377 {return;}
378 }
379 else if(c <= 127)
380 {
381 str.push_back(static_cast<unsigned char>(c));
382 }
383 }
384}
385
386void DtUnicode::toLatin1(std::string& str) const
387{
388 str.clear();
389 str.reserve(length());
390 CodeUnit* i = myCodeUnits.begin;
391 CodeUnit* e = myCodeUnits.end;
392 for(;i != e;++i)
393 {
394 CodeUnit c = *i;
396 {
397 ++i;
398 if(i == e) //Error unicode string is not properly formed.
399 {return;}
400 }
401 else if(c <= 255)
402 {
403 str.push_back(static_cast<unsigned char>(c));
404 }
405 }
406}
407
408
409void DtUnicode::toUtf8( std::string& str ) const
410{
411 str.clear();
412 str.reserve(calculateCodePoints());
413 CodeUnit* i = myCodeUnits.begin;
414 CodeUnit* e = myCodeUnits.end;
416 for(; i != e;)
417 {
418 //Read character, move pointer based on number of units read.
419 i += c.readUtf16(i);
420
421 //Get the UTF8 length.
422 int len = c.utf8Length();
423
424 //Resize the string for the length.
425 str.resize(str.size() + len);
426 //Get the newly created memory.
427 const char* memory = str.c_str() + (str.size() - len);
428 //Write the character as UTF-8 into memory.
429 c.writeUtf8(const_cast<char*>(memory));
430 }
431}
432
433
434void DtUnicode::toWideString( std::wstring& str ) const
435{
436 str.clear();
437 str.reserve(calculateCodePoints());
438 CodeUnit* i = myCodeUnits.begin;
439 CodeUnit* e = myCodeUnits.end;
441 for(; i != e;)
442 {
443 //Read character, move pointer based on number of units read.
444 i += c.readUtf16(i);
445
446 //Get the length length.
447 int len = c.wideStringLength() / sizeof(wchar_t);
448
449 //Resize the string for the length.
450 str.resize(str.size() + len);
451
452 //Get the newly created memory.
453 const wchar_t* memory = str.c_str() + (str.size() - len);
454
455 //Write the character as UTF-8 into memory.
456 c.writeWChar(const_cast<wchar_t*>(memory));
457 }
458}
459
460
461template <int wcharSize>
462inline void setFromWCHAR(DtUnicode& ref, const wchar_t utf16[], int len /*= -1*/);
463
464template <>
465inline void setFromWCHAR<2>(DtUnicode& str, const wchar_t utf16[], int len /*= -1*/)
466{
467 str.setFromUtf16((unsigned short*)utf16,len);
468}
469
470template <>
471inline void setFromWCHAR<4>(DtUnicode& str, const wchar_t utf32[], int len /*= -1*/)
472{
473 str.setFromUtf32((unsigned int*)utf32,len);
474}
475
476
477void DtUnicode::setFromWCharArray( const wchar_t utf16[], int len )
478{
479 setFromWCHAR<sizeof(wchar_t)>(*this,utf16,len);
480}
481
482void DtUnicode::setFromUtf16(const unsigned short* utf16, int len )
483{
484 if(len < 0)
485 {
486 len = utf16len(utf16);
487 }
488
489 myCodeUnits.resize(len << 1);
490 memcpy(myCodeUnits.begin,utf16,len << 1);
491}
492
493
494void DtUnicode::setFromUtf32( const unsigned int* utf32, int len)
495{
496 myCodeUnits.resize(0);
497 if(len < 0)
498 {
499 unsigned int c = *utf32;
500 while(c)
501 {
503 ++utf32;
504 c = *utf32;
505 }
506 }
507 else
508 {
509 while(len--)
510 {
511 push_back(*((DtUnicodeChar*)utf32));
512 ++utf32;
513 }
514 }
515}
516
517bool DtUnicode::operator == ( const DtUnicode& other ) const
518{
519 // If different sizes, not equal.
520 if ( length() != other.length() )
521 {
522 return false;
523 }
524
525 // Fast ptr check.
526 if ( myCodeUnits.begin == other.myCodeUnits.begin )
527 {
528 return true;
529 }
530
531 // If either pointer is invalid, they're not the same.
532 if ( ! myCodeUnits.begin || ! other.myCodeUnits.begin )
533 {
534 return false;
535 }
536
537 // Both are non-zero
538 return memcmp(myCodeUnits.begin,other.myCodeUnits.begin,myCodeUnits.size()) == 0;
539}
540
541bool DtUnicode::operator != ( const DtUnicode& other ) const
542{
543 // If different sizes, not equal.
544 if ( length() != other.length() )
545 {
546 return true;
547 }
548
549 // Fast ptr check.
550 if ( myCodeUnits.begin == other.myCodeUnits.begin )
551 {
552 return false;
553 }
554
555 // If either pointer is invalid, they're not the same.
556 if ( ! myCodeUnits.begin || ! other.myCodeUnits.begin )
557 {
558 return true;
559 }
560
561 // Both are non-zero
562 return memcmp(myCodeUnits.begin,other.myCodeUnits.begin,myCodeUnits.size()) != 0;
563}
564
565bool DtUnicode::operator<(const DtUnicode& other) const
566{
567 //If I am empty,
568 if(!myCodeUnits.begin)
569 {
570 //and the other is not, then I am less
571 if(other.myCodeUnits.begin)
572 {
573 return true;
574 }
575 else //else we are equal.
576 {
577 return false;
578 }
579 }
580
581 //Both are non-zero
582 int result = memcmp(myCodeUnits.begin,other.myCodeUnits.begin,
583 std::min(myCodeUnits.size(),other.myCodeUnits.size()));
584
585 //If we are equal, then I am less, if I am smaller.
586 if(result == 0)
587 {
588 return myCodeUnits.size() < other.myCodeUnits.size();
589 }
590 else
591 {
592 return result < 0;
593 }
594}
595
596
597size_t DtUnicode::length() const
598{
599 return myCodeUnits.size() >> 1;
600}
601
602
604{
605 size_t codePoints = 0;
606 CodeUnit* i = myCodeUnits.begin;
607 CodeUnit* e = myCodeUnits.end;
608 for(;i!=e;++i)
609 {
611 {
612 ++codePoints;
613 }
614 }
615 return codePoints;
616}
617
618inline size_t DtUnicode::count(const DtUnicodeChar& x) const
619{
620 size_t currentPos = 0;
621 size_t countResult = 0;
622
623 while (currentPos < length())
624 {
625 currentPos = indexOf(x, currentPos);
626 if (currentPos == -1)
627 {
628 break;
629 }
630 ++countResult;
631 ++currentPos;
632 }
633
634 return countResult;
635}
636
637int DtUnicode::indexOf( const DtUnicodeChar& x, size_t pos) const
638{
639 return indexOf(DtUnicode(1,x),pos);
640}
641
642int DtUnicode::indexOf( const DtUnicode& x, size_t pos) const
643{
644 //Handle both zero length and out of bounds.
645 if(pos >= length())
646 {
647 return -1;
648 }
649
650 if(x.length() + pos > length())
651 {
652 return -1;
653 }
654
655 const CodeUnit* ptr = (const CodeUnit*)wcsnstr(myCodeUnits.begin + pos,
656 (myCodeUnits.size() >> 1) - pos , x.myCodeUnits.begin, (x.myCodeUnits.size() >> 1));
657
658 if(ptr)
659 {
660 return static_cast<int>(ptr - myCodeUnits.begin);
661 }
662 else
663 {
664 return -1;
665 }
666}
667
668DtUnicode DtUnicode::subString( size_t pos, int len ) const
669{
670 //Handle both zero length and out of bounds.
671 if(pos > length())
672 {
673 return DtUnicode();
674 }
675 else
676 {
677 //Crop the size.
678 int maxLen = length() - pos;
679 if(len < 0)
680 {
681 len = maxLen;
682 }
683 else
684 {
685 len = std::min(maxLen,len);
686 }
687
688 return DtUnicode(myCodeUnits.begin + pos,len);
689 }
690}
691
693{
694 //Add N units
695 myCodeUnits.resize(myCodeUnits.size() + (x.utf16CodeUnits() << 1));
696 //Write N units to before end.
697 x.writeUtf16(myCodeUnits.end - x.utf16CodeUnits());
698}
699
700
701void DtUnicode::resize( int size )
702{
703 if(size < 0)
704 myCodeUnits.clear();
705 else
706 myCodeUnits.resize(size << 1);
707}
708
709
710DtUnicode::DtUnicode(const char* ascii, int len,Encoding encoding)
711: myCodeUnits()
712{
713 switch(encoding)
714 {
715 case Ascii:
716 default:
717 setFromAscii(ascii,len);
718 break;
719 case Latin1:
720 setFromLatin1(ascii,len);
721 break;
722 case Utf8:
723 setFromUtf8((const unsigned char*)ascii,len);
724 break;
725 }
726}
727
728DtUnicode::DtUnicode(const unsigned short* utf16, int len )
729: myCodeUnits()
730{
731 if(len < 0)
732 {
733 len = utf16len(utf16);
734 }
735
736 myCodeUnits.resize(len << 1);
737 memcpy(myCodeUnits.begin,utf16,len << 1);
738}
739
740
741DtUnicode::DtUnicode( const unsigned int* utf32, int len)
742: myCodeUnits()
743{
744 setFromUtf32(utf32,len);
745}
746
747DtUnicode::DtUnicode(const std::string& ascii)
748: myCodeUnits()
749{
750 setFromAscii(ascii);
751}
752
753//Concat constructor
755: myCodeUnits()
756{
757 //Pre-resize.
758 myCodeUnits.reserve(a.myCodeUnits.size() + b.myCodeUnits.size());
759 *this += a;
760 *this += b;
761}
762
764: count(0)
765, begin(0)
766, end(0)
767, last(0)
768{
769}
770
775
777: count(other.count)
778, begin(other.begin)
779, end(other.end)
780, last(other.last)
781{
782 add_ref();
783}
784
786{
787 if(this != &other)
788 {
789 dec_ref();
790 count = other.count;
791 begin = other.begin;
792 end = other.end;
793 last = other.last;
794 add_ref();
795 }
796 return *this;
797}
798
800{
801 size_t oldSize = size();
802 resize(newSize);
803 end = reinterpret_cast<CodeUnit*>( reinterpret_cast<char*>(begin) + oldSize);
804}
805
806
808{
809 if(unique())
810 {
811 end = begin;
812 }
813 else
814 {
815 resize(0);
816 }
817}
818
819#ifdef __rhentws7__
820void DtUnicode::CodeUnitArray::resize(size_t newSize)
821{
822 //If new size is zero, clear out my memory.
823 if(newSize == 0)
824 {
825 dec_ref();
826 count = 0;
827 begin = 0;
828 end = 0;
829 last = 0;
830 return;
831 }
832
833 //If I nave no string, or I'm sharing a string, create a new string.
834 if(empty())
835 {
836 count = (volatile long*)MallocAligned(newSize + sizeof(long),sizeof(long*));
837 assert(count != 0);
838 begin = reinterpret_cast<CodeUnit*>(const_cast<long*>(count) + 1);
839 end = last = reinterpret_cast<CodeUnit*>( reinterpret_cast<char*>(begin) + newSize);
840 *count = 1;
841 return;
842 }
843 else if(!unique())
844 {
845 CodeUnit* oldBegin = begin;
846 size_t oldSize = size();
847 dec_ref();
848 count = (volatile long*)MallocAligned(newSize + sizeof(long),sizeof(long*));
849 begin = reinterpret_cast<CodeUnit*>(const_cast<long*>(count) + 1);
850 end = last = reinterpret_cast<CodeUnit*>( reinterpret_cast<char*>(begin) + newSize);
851 *count = 1;
852 memcpy(begin,oldBegin,oldSize);
853 return;
854 }
855 else //Else I will just try to resize my buffer.
856 {
857 //Is the new size bigger.
858 if(newSize > capacity())
859 {
860 //Resize to their capacity.
861 size_t newCapacity = calculateCapacity(newSize + sizeof(long)) - sizeof(long);
862 count = (volatile long*)ReallocAligned(const_cast<long*>(count),newCapacity + sizeof(long),size() + sizeof(long),sizeof(long*));
863 begin = reinterpret_cast<CodeUnit*>(const_cast<long*>(count) + 1);
864 last = reinterpret_cast<CodeUnit*>( reinterpret_cast<char*>(begin) + newCapacity);
865 }
866 end = reinterpret_cast<CodeUnit*>( reinterpret_cast<char*>(begin) + newSize);
867 }
868}
869
870#else
872{
873 //If new size is zero, clear out my memory.
874 if(newSize == 0)
875 {
876 dec_ref();
877 count = 0;
878 begin = 0;
879 end = 0;
880 last = 0;
881 return;
882 }
883
884 //If I nave no string, or I'm sharing a string, create a new string.
885 if(empty())
886 {
887 count = (volatile std::atomic_long*)MallocAligned(newSize + sizeof(std::atomic_long),sizeof(std::atomic_long*));
888 assert(count != 0);
889 begin = reinterpret_cast<CodeUnit*>(const_cast<std::atomic_long*>(count) + 1);
890 end = last = reinterpret_cast<CodeUnit*>( reinterpret_cast<char*>(begin) + newSize);
891 *count = 1;
892 return;
893 }
894 else if(!unique())
895 {
896 CodeUnit* oldBegin = begin;
897 size_t oldSize = size();
898 dec_ref();
899 count = (volatile std::atomic_long*)MallocAligned(newSize + sizeof(std::atomic_long),sizeof(std::atomic_long*));
900 begin = reinterpret_cast<CodeUnit*>(const_cast<std::atomic_long*>(count) + 1);
901 end = last = reinterpret_cast<CodeUnit*>( reinterpret_cast<char*>(begin) + newSize);
902 *count = 1;
903 memcpy(begin,oldBegin,oldSize);
904 return;
905 }
906 else //Else I will just try to resize my buffer.
907 {
908 //Is the new size bigger.
909 if(newSize > capacity())
910 {
911 //Resize to their capacity.
912 size_t newCapacity = calculateCapacity(newSize + sizeof(std::atomic_long)) - sizeof(std::atomic_long);
913 count = (volatile std::atomic_long*)ReallocAligned(const_cast<std::atomic_long*>(count),newCapacity + sizeof(std::atomic_long),size() + sizeof(std::atomic_long),sizeof(std::atomic_long*));
914 begin = reinterpret_cast<CodeUnit*>(const_cast<std::atomic_long*>(count) + 1);
915 last = reinterpret_cast<CodeUnit*>( reinterpret_cast<char*>(begin) + newCapacity);
916 }
917 end = reinterpret_cast<CodeUnit*>( reinterpret_cast<char*>(begin) + newSize);
918 }
919}
920#endif
921
922//Calculate the capacity
924{
925 //If smaller than 1/2 MB due nearest pow 2.
926 if (necessarySize < 0x80000)
927 {
928 //Else allocate next power of 2.
929 float f = static_cast<float>(necessarySize);
930 unsigned int bits(0);
931 memcpy(&bits,&f,sizeof(float));
932 const unsigned int t = 1U << ((bits >> 23) - 0x7f);
933 return t << ((t < necessarySize) ? 1 : 0);
934 }
935 else if (necessarySize != 0)
936 {
937 //Create the necessary size + a little extra.
938 return necessarySize + 32;
939 }
940 else
941 {
942 return 1;
943 }
944}
945
946//Return size in bytes
948{
949 return reinterpret_cast<const char*>(end) -
950 reinterpret_cast<const char*>(begin);
951}
952
953//Return capacity in bytes
955{
956 return reinterpret_cast<const char*>(last) -
957 reinterpret_cast<const char*>(begin);
958}
959
960
961//Concat another array, supports concatting itself.
963{
964 size_t oldSize = size();
965 size_t otherSize = other.size();
966 resize(oldSize + otherSize);
967 memcpy(reinterpret_cast<char*>(begin) + oldSize,other.begin,otherSize);
968}
969
970
973{
974 return !count || (*count == 1);
975}
976
979{
980 return !count;
981}
982
987
989{
990 if(count)
991 {
992 // Check the count, not the value returned by dec
994 if(*count == 0)
995 {
996#ifndef __rhentws7__
997 FreeAligned(const_cast<std::atomic_long*>(count));
998#else
999 FreeAligned(const_cast<long*>(count));
1000#endif
1001 //Zero out memory.
1002 count = 0;
1003 begin = 0;
1004 end = 0;
1005 last = 0;
1006 }
1007 }
1008}
1009
1011{
1012 const DtUnicode::CodeUnit* i = this->codeUnits();
1013 const DtUnicode::CodeUnit* e = i + this->length();
1014
1015 while( i != e )
1016 {
1017 if (DtIsRightToLeft(*i))
1018 {
1019 return true;
1020 }
1021
1022 ++i;
1023 }
1024
1025 return false;
1026}
1027
1028}
#define FreeAligned(ptr)
Definition DtUnicode.inl:82
void * reallocAligned(void *ptr, size_t sz, size_t oldsz, size_t align)
Definition DtUnicode.inl:66
#define AtomicIncrement(a)
Definition DtUnicode.inl:44
#define ReallocAligned(ptr, sz, oldsz, align)
Definition DtUnicode.inl:81
#define AtomicDecrement(a)
Definition DtUnicode.inl:45
void * mallocAligned(size_t sz, size_t align)
Definition DtUnicode.inl:58
#define MallocAligned(sz, align)
Definition DtUnicode.inl:65
A single unicode character.
Definition DtUnicodeChar.h:18
unsigned int readUtf8(const unsigned char *memory)
Definition DtUnicodeChar.h:271
unsigned int readUtf16(const Surrogate *memory)
Definition DtUnicodeChar.h:208
unsigned int utf8Length() const
Calculate the utf-8 length of a code point.
Definition DtUnicodeChar.h:139
void writeUtf8(char *memory) const
Definition DtUnicodeChar.h:306
static bool isSurrogateSubstringValid(const Surrogate *start, const Surrogate *end, const Surrogate *substringStart, const Surrogate *substringEnd)
Definition DtUnicodeChar.h:100
unsigned int wideStringLength() const
Definition DtUnicodeChar.h:397
void writeWChar(wchar_t *memory) const
Definition DtUnicodeChar.h:402
static bool isSurrogate(Surrogate cp)
Definition DtUnicodeChar.h:126
Definition DtUnicode.h:34
bool operator<(const DtUnicode &other) const
Definition DtUnicode.inl:565
void setFromUtf16(const unsigned short *utf16, int len=-1)
Definition DtUnicode.inl:482
std::wstring toWideString() const
Definition DtUnicode.h:167
DtUnicode operator+(const DtUnicode &str) const
Append two strings together.
Definition DtUnicode.inl:217
bool operator==(const DtUnicode &other) const
Test if two strings are identical.
Definition DtUnicode.inl:517
virtual bool containsRightToLeft() const
Returns true if this text contains any right to left characters.
Definition DtUnicode.inl:1010
DtUnicode()
CTOR creates an empty string.
Definition DtUnicode.inl:165
void setFromLatin1(const char *latin, int len=-1)
Definition DtUnicode.inl:347
static DtUnicode fromWideString(const std::wstring &str)
Set from a std::wstring.
Definition DtUnicode.inl:251
void push_back(const DtUnicodeChar &x)
Append a unicode character to the string.
Definition DtUnicode.inl:692
unsigned short CodeUnit
Definition DtUnicode.h:36
void resize(int size)
Definition DtUnicode.inl:701
DtUnicode & operator+=(const DtUnicode &str)
Append a string to the end.
Definition DtUnicode.inl:199
size_t count(const DtUnicodeChar &x) const
Definition DtUnicode.inl:618
std::string toLatin1() const
Definition DtUnicode.h:162
CodeUnitArray myCodeUnits
Definition DtUnicode.h:318
void setFromAscii(const std::string &ascii)
Definition DtUnicode.inl:290
static DtUnicode fromAscii(const std::string &ascii)
Definition DtUnicode.inl:285
Encoding
Definition DtUnicode.h:244
@ Ascii
Definition DtUnicode.h:245
@ Utf8
Definition DtUnicode.h:247
@ Latin1
Definition DtUnicode.h:246
int indexOf(const DtUnicodeChar &x, size_t pos=0) const
Search for a character starting a pos, returns index, or -1 if not found.
Definition DtUnicode.inl:637
std::string toUtf8() const
Definition DtUnicode.h:165
void setFromUtf8(const unsigned char *utf8, int len=-1)
Definition DtUnicode.inl:319
std::string toAscii() const
Definition DtUnicode.h:157
virtual ~DtUnicode()
DTOR may NOT deallocate memory as this string may share data.
Definition DtUnicode.inl:179
DtUnicode subString(size_t pos=0, int len=-1) const
Definition DtUnicode.inl:668
void setFromUtf32(const unsigned int *utf32, int len=-1)
Definition DtUnicode.inl:494
static DtUnicode fromWCharArray(const wchar_t utf16[], int len=-1)
Static create via specifying encoding.
Definition DtUnicode.inl:245
size_t calculateCodePoints() const
Calculate the number of Code Points.
Definition DtUnicode.inl:603
static DtUnicode fromUtf8(const unsigned char *utf8, int len=-1)
Definition DtUnicode.inl:257
static DtUnicode fromUtf16(const unsigned short *utf16, int len=-1)
Definition DtUnicode.inl:268
void setFromWCharArray(const wchar_t utf16[], int len=-1)
Initialize via specifying encoding.
Definition DtUnicode.inl:477
bool operator!=(const DtUnicode &other) const
Test if two strings are not identical.
Definition DtUnicode.inl:541
DtUnicode & operator=(const DtUnicode &str)
Definition DtUnicode.inl:189
static DtUnicode fromUtf32(const unsigned int *utf32, int len=-1)
Definition DtUnicode.inl:274
size_t length() const
Definition DtUnicode.inl:597
const CodeUnit * codeUnits() const
Get an array of code units.
Definition DtUnicode.h:211
Contains DtCreateSensorFieldOfViewCommand and DtCreateSensorFieldOfViewCommandCreator class declarati...
Definition DtGridLinesSettingsRecord.h:16
DtUnicode decodeWCHAR< 4 >(const wchar_t utf16[], int len)
Definition DtUnicode.inl:239
bool DT_DLL_VRVUTIL DtIsRightToLeft(unsigned int c)
Returns true if the given character is right to left according to unicode.
size_t utf16len(const unsigned short *str)
Definition DtUnicode.inl:90
DtUnicode decodeWCHAR(const wchar_t utf16[], int len)
DtUnicode decodeWCHAR< 2 >(const wchar_t utf16[], int len)
Definition DtUnicode.inl:233
const unsigned short * wcsnstr(const unsigned short *haystack, int hayLength, const unsigned short *needle, int needleLength)
Definition DtUnicode.inl:105
void setFromWCHAR< 2 >(DtUnicode &str, const wchar_t utf16[], int len)
Definition DtUnicode.inl:465
void setFromWCHAR(DtUnicode &ref, const wchar_t utf16[], int len)
void setFromWCHAR< 4 >(DtUnicode &str, const wchar_t utf32[], int len)
Definition DtUnicode.inl:471
Internal structure used to store the code units.
Definition DtUnicode.h:269
volatile std::atomic_long * count
Definition DtUnicode.h:312
size_t calculateCapacity(size_t necessarySize)
Definition DtUnicode.inl:923
CodeUnitArray & operator=(const CodeUnitArray &)
Definition DtUnicode.inl:785
void reserve(size_t capacity)
Ensure the array's capacity is at least capacity, do not change size.
Definition DtUnicode.inl:799
void concat(const CodeUnitArray &other)
Definition DtUnicode.inl:962
CodeUnit * end
Definition DtUnicode.h:315
void dec_ref()
Decrement the reference count.
Definition DtUnicode.inl:988
size_t capacity() const
Definition DtUnicode.inl:954
CodeUnit * last
Definition DtUnicode.h:316
void clear()
Definition DtUnicode.inl:807
void add_ref()
Increment the reference count.
Definition DtUnicode.inl:983
bool unique() const
Am I unique.
Definition DtUnicode.inl:972
~CodeUnitArray()
Definition DtUnicode.inl:771
bool empty() const
Am I empty.
Definition DtUnicode.inl:978
size_t size() const
Definition DtUnicode.inl:947
void resize(size_t newSize)
Resize the array.
Definition DtUnicode.inl:871
CodeUnit * begin
Definition DtUnicode.h:314
CodeUnitArray()
Definition DtUnicode.inl:763