VR-Vantage
Loading...
Searching...
No Matches
DtUnicode.h
Go to the documentation of this file.
1/******************************************************************************
2** Copyright (c) 2020 MAK Technologies, Inc.
3** All rights reserved.
4******************************************************************************/
5
8
9#pragma once
10
11#include <wchar.h>
13#include <boost/functional/hash.hpp>
14#include <boost/serialization/split_free.hpp>
15
16#ifndef __rhentws7__
17#include <atomic>
18#endif
19
20namespace makVrv
21{
22
34 {
35 public:
36 typedef unsigned short CodeUnit;
37
39 inline DtUnicode();
40
42 inline virtual ~DtUnicode();
43
45 explicit inline DtUnicode( size_t len, DtUnicodeChar c = DtUnicodeChar('\0'));
46
50 inline DtUnicode(const DtUnicode& str);
51
55 inline DtUnicode& operator=(const DtUnicode& str);
56
58
61 inline static DtUnicode fromWCharArray(const wchar_t utf16[], int len = -1);
62
64 inline static DtUnicode fromWideString(const std::wstring& str);
65
68 inline static DtUnicode fromUtf8(const unsigned char* utf8, int len = -1);
69 inline static DtUnicode fromUtf8(const std::string& utf8);
70
73 inline static DtUnicode fromUtf16(const unsigned short* utf16, int len = -1);
74
77 inline static DtUnicode fromUtf32(const unsigned int* utf32, int len = -1);
78
81 inline static DtUnicode fromAscii(const std::string& ascii);
82
85 inline static DtUnicode fromAscii(const char* ascii, int len = -1);
86
92 inline static DtUnicode tr(const char* ascii)
93 {
94 return fromAscii(ascii);
95 }
96
99 template <typename QStringType, typename QCoreApp>
100 QStringType translate() const
101 {
102 return QCoreApp::translate("makVrv::DtUnicode",toUtf8().c_str(), 0);
103 }
104
106 template <typename QStringType>
107 inline static DtUnicode fromQString(QStringType qstr)
108 {
109 return fromUtf16(qstr.utf16(),qstr.length());
110 }
111
113
118 inline void setFromWCharArray(const wchar_t utf16[], int len = -1);
119
122 inline void setFromUtf8(const unsigned char* utf8, int len = -1);
123 inline void setFromUtf8(const std::string& utf8);
124
127 inline void setFromLatin1(const char* latin, int len = -1);
128
131 inline void setFromUtf16(const unsigned short* utf16, int len = -1);
132
135 inline void setFromUtf32(const unsigned int* utf32, int len = -1);
136
139 inline void setFromAscii(const std::string& ascii);
140
143 inline void setFromAscii(const char* ascii, int len = -1);
144
146 template <typename QStringType>
147 inline void setFromQString(QStringType qstr)
148 {
149 setFromUtf16(qstr.utf16(),qstr.length());
150 }
151
153
156 inline void toAscii(std::string& str) const;
157 inline std::string toAscii() const { std::string temp; toAscii(temp); return temp;}
158
161 inline void toLatin1(std::string& str) const;
162 inline std::string toLatin1() const { std::string temp; toLatin1(temp); return temp;}
163
164 inline void toUtf8(std::string& str) const;
165 inline std::string toUtf8() const { std::string temp; toUtf8(temp); return temp;}
166 inline void toWideString(std::wstring& str) const;
167 inline std::wstring toWideString() const { std::wstring temp; toWideString(temp); return temp;}
168
170 template <typename QStringType>
171 inline QStringType toQString() const
172 {
173 return QStringType::fromUtf16(codeUnits(),length());
174 }
175
177 template <typename QStringType>
178 inline void toQString(QStringType& qstr) const
179 {
180 qstr.setUtf16(codeUnits(),length());
181 }
182
184 inline DtUnicode& operator+=(const DtUnicode& str);
186 inline DtUnicode& operator+=(const DtUnicodeChar& x);
188 inline DtUnicode operator+(const DtUnicode& str) const;
190 inline DtUnicode operator+(const DtUnicodeChar& character) const;
191
193 inline bool operator==(const DtUnicode& other) const;
195 inline bool operator!=(const DtUnicode& other) const;
198 inline bool operator<(const DtUnicode& other) const;
199
202 inline size_t length() const;
203
205 inline size_t sizeInBytes() const { return length() << 1; }
206
208 inline size_t calculateCodePoints() const;
209
211 inline const CodeUnit* codeUnits() const { return myCodeUnits.begin; }
212
213 inline size_t count(const DtUnicodeChar& x) const;
214
216 inline int indexOf( const DtUnicodeChar& x, size_t pos = 0) const;
217
219 inline int indexOf( const DtUnicode& x, size_t pos = 0) const;
220
224 inline DtUnicode subString( size_t pos = 0, int len = -1 ) const;
225
227 inline void push_back ( const DtUnicodeChar& x );
228
236 inline void resize( int size );
237
239 virtual bool containsRightToLeft() const;
240
241 protected: // THESE are the protected functions not to be used directly.
242
249
250 //Ascii CTOR
251 inline explicit DtUnicode(const char* ascii, int len = -1,Encoding encoding = Ascii);
252
253 //UTF16 constructor
254 inline explicit DtUnicode(const unsigned short* utf16, int len = -1);
255
256 //UTF32 constructor.
257 inline explicit DtUnicode(const unsigned int* utf32, int len = -1);
258
259 //Std string constructor.
260 inline explicit DtUnicode(const std::string& ascii);
261
262 //Concat constructor.
263 inline explicit DtUnicode(const DtUnicode& a, const DtUnicode& b);
264
265 protected:
266
269 {
270 inline CodeUnitArray();
271 inline CodeUnitArray(const CodeUnitArray&);
272 inline CodeUnitArray& operator=(const CodeUnitArray&);
273 inline ~CodeUnitArray();
274
276 inline void resize(size_t newSize);
277
280 inline void clear();
281
283 inline void reserve(size_t capacity);
284
285 //Calculate the capacity
286 inline size_t calculateCapacity(size_t necessarySize);
287
288 //Return size in bytes
289 inline size_t size() const;
290
291 //Return capacity in bytes
292 inline size_t capacity() const;
293
294 //Concat another array, supports concat operations on itself.
295 inline void concat(const CodeUnitArray& other);
296
298 inline bool unique() const;
299
301 inline bool empty() const;
302
304 inline void add_ref();
305
307 inline void dec_ref();
308
309#ifdef __rhentws7__
310 volatile long* count;
311#else
312 volatile std::atomic_long* count;
313#endif
317 };
319 };
320}
321
322//Allows DtUnicode to be used in boost hash containers.
323namespace makVrv
324{
325 inline std::size_t hash_value(DtUnicode const& val)
326 {
327 //Copied from TR1 implementation.
328
329 size_t seed = 2166136261U;
330 size_t first = 0;
331 size_t last = val.length();
332 size_t stride = 1 + last / 10;
333
334 if (stride < last)
335 {
336 last -= stride;
337 }
338 for(; first < last ; first += stride )
339 {
340 seed = 16777619U * seed ^ (size_t)val.codeUnits()[first];
341 }
342 return seed;
343 }
344}
345
346#include "DtUnicode.inl"
Contains the DtUnicode class definition.
A single unicode character.
Definition DtUnicodeChar.h:18
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
static DtUnicode tr(const char *ascii)
Definition DtUnicode.h:92
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
QStringType toQString() const
Template function for outputting a Qt 4 style string.
Definition DtUnicode.h:171
std::string toLatin1() const
Definition DtUnicode.h:162
CodeUnitArray myCodeUnits
Definition DtUnicode.h:318
QStringType translate() const
Definition DtUnicode.h:100
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
size_t sizeInBytes() const
Get the size in 8bit bytes.
Definition DtUnicode.h:205
std::string toUtf8() const
Definition DtUnicode.h:165
void toQString(QStringType &qstr) const
Template function for initializing a Qt 4 style string.
Definition DtUnicode.h:178
void setFromUtf8(const unsigned char *utf8, int len=-1)
Definition DtUnicode.inl:319
void setFromQString(QStringType qstr)
Template function for setting from Qt 4 style string.
Definition DtUnicode.h:147
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
static DtUnicode fromQString(QStringType qstr)
Template function for setting from Qt 4 style string.
Definition DtUnicode.h:107
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
std::size_t hash_value(DtUnicode const &val)
Definition DtUnicode.h:325
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