VR-Forces 4.8 Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DtUnicode.h
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright (c) 2014 MAK Technologies, Inc.
3 ** All rights reserved.
4 ******************************************************************************/
5 
8 
9 #pragma once
10 
11 #include <wchar.h>
12 #include <vrvUtil/DtUnicodeChar.h>
13 #include <boost/functional/hash.hpp>
14 #include <boost/serialization/split_free.hpp>
15 
16 #ifndef __rhentws7__
17 #include <atomic>
18 #endif
19 
20 namespace makVrv
21 {
22 
33  class DtUnicode
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;
189 
191  inline bool operator==(const DtUnicode& other) const;
193  inline bool operator!=(const DtUnicode& other) const;
196  inline bool operator<(const DtUnicode& other) const;
197 
200  inline size_t length() const;
201 
203  inline size_t sizeInBytes() const { return length() << 1; }
204 
206  inline size_t calculateCodePoints() const;
207 
209  inline const CodeUnit* codeUnits() const { return myCodeUnits.begin; }
210 
212  inline int indexOf( const DtUnicodeChar& x, size_t pos = 0) const;
213 
215  inline int indexOf( const DtUnicode& x, size_t pos = 0) const;
216 
220  inline DtUnicode subString( size_t pos = 0, int len = -1 ) const;
221 
223  inline void push_back ( const DtUnicodeChar& x );
224 
232  inline void resize( int size );
233 
235  virtual bool containsRightToLeft() const;
236 
237  protected: // THESE are the protected functions not to be used directly.
238 
239  enum Encoding
240  {
244  };
245 
246  //Ascii CTOR
247  inline explicit DtUnicode(const char* ascii, int len = -1,Encoding encoding = Ascii);
248 
249  //UTF16 constructor
250  inline explicit DtUnicode(const unsigned short* utf16, int len = -1);
251 
252  //UTF32 constructor.
253  inline explicit DtUnicode(const unsigned int* utf32, int len = -1);
254 
255  //Std string constructor.
256  inline explicit DtUnicode(const std::string& ascii);
257 
258  //Concat constructor.
259  inline explicit DtUnicode(const DtUnicode& a, const DtUnicode& b);
260 
261  protected:
262 
265  {
266  inline CodeUnitArray();
267  inline CodeUnitArray(const CodeUnitArray&);
268  inline CodeUnitArray& operator=(const CodeUnitArray&);
269  inline ~CodeUnitArray();
270 
272  inline void resize(size_t newSize);
273 
276  inline void clear();
277 
279  inline void reserve(size_t capacity);
280 
281  //Calculate the capacity
282  inline size_t calculateCapacity(size_t necessarySize);
283 
284  //Return size in bytes
285  inline size_t size() const;
286 
287  //Return capacity in bytes
288  inline size_t capacity() const;
289 
290  //Concat another array, supports concat operations on itself.
291  inline void concat(const CodeUnitArray& other);
292 
294  inline bool unique() const;
295 
297  inline bool empty() const;
298 
300  inline void add_ref();
301 
303  inline void dec_ref();
304 
305 #ifdef __rhentws7__
306  volatile long* count;
307 #else
308  volatile std::atomic_long* count;
309 #endif
313  };
315  };
316 }
317 
318 //Allows DtUnicode to be used in boost hash containers.
319 namespace makVrv
320 {
321  inline std::size_t hash_value(DtUnicode const& val)
322  {
323  //Copied from TR1 implementation.
324 
325  size_t seed = 2166136261U;
326  size_t first = 0;
327  size_t last = val.length();
328  size_t stride = 1 + last / 10;
329 
330  if (stride < last)
331  {
332  last -= stride;
333  }
334  for(; first < last ; first += stride )
335  {
336  seed = 16777619U * seed ^ (size_t)val.codeUnits()[first];
337  }
338  return seed;
339  }
340 }
341 
342 #include "DtUnicode.inl"
static DtUnicode fromUtf16(const unsigned short *utf16, int len=-1)
Create a unicode string from an UTF-16 encoded string, the string MUST be zero terminated if the leng...
Definition: DtUnicode.h:242
CodeUnit * begin
Definition: DtUnicode.h:310
DtUnicode()
CTOR creates an empty string.
A unicode string.
Definition: DtUnicode.h:33
DtUnicode subString(size_t pos=0, int len=-1) const
Create a substring starting at position, for n characters.
void setFromLatin1(const char *latin, int len=-1)
Set from an UTF-8 encoded string, the string MUST be zero terminated if the length is not passed...
void concat(const CodeUnitArray &other)
bool operator!=(const DtUnicode &other) const
Test if two strings are not identical.
static DtUnicode fromWideString(const std::wstring &str)
Set from a std::wstring.
DtUnicode & operator=(const DtUnicode &str)
Assignment operator copies the string, this function will NOT allocate memory for the string...
virtual bool containsRightToLeft() const
Returns true if this text contains any right to left characters.
bool unique() const
Am I unique.
voidpf void uLong size
Definition: ioapi.h:39
CodeUnit * end
Definition: DtUnicode.h:311
void clear()
Resize the array to 0, does not change the memory allocation if the array is unique.
unsigned short CodeUnit
Definition: DtUnicode.h:36
static DtUnicode fromWCharArray(const wchar_t utf16[], int len=-1)
Static create via specifying encoding.
void setFromUtf8(const unsigned char *utf8, int len=-1)
Set from an UTF-8 encoded string, the string MUST be zero terminated if the length is not passed...
size_t calculateCodePoints() const
Calculate the number of Code Points.
void reserve(size_t capacity)
Ensure the array&#39;s capacity is at least capacity, do not change size.
void push_back(const DtUnicodeChar &x)
Append a unicode character to the string.
Internal structure used to store the code units.
Definition: DtUnicode.h:264
std::string toUtf8() const
Definition: DtUnicode.h:165
size_t length() const
Get the length in Code Units.
void setFromUtf16(const unsigned short *utf16, int len=-1)
Set from an UTF-16 encoded string, the string MUST be zero terminated if the length is not passed...
DtUnicode operator+(const DtUnicode &str) const
Append two strings together.
static DtUnicode fromUtf32(const unsigned int *utf32, int len=-1)
Create a unicode string from an UTF-32 encoded string, the string MUST be zero terminated if the leng...
bool empty() const
Am I empty.
static DtUnicode fromUtf8(const unsigned char *utf8, int len=-1)
Create a unicode string from an UTF-16 encoded string, the string MUST be zero terminated if the leng...
size_t sizeInBytes() const
Get the size in 8bit bytes.
Definition: DtUnicode.h:203
void setFromAscii(const std::string &ascii)
Set from an ascii string, the string MUST be zero terminated if the length is not passed...
volatile std::atomic_long * count
Definition: DtUnicode.h:308
Definition: DtUnicode.h:243
A single unicode character.
Definition: DtUnicodeChar.h:20
static DtUnicode tr(const char *ascii)
Ascii translation function, used to be compatible with Qt&#39;s translation tools.
Definition: DtUnicode.h:92
void setFromUtf32(const unsigned int *utf32, int len=-1)
Set from an UTF-32 encoded string, the string MUST be zero terminated if the length is not passed...
QStringType translate() const
Utility function for translating a DtUnicode variable initialized using the static DtUnicode::tr func...
Definition: DtUnicode.h:100
CodeUnitArray myCodeUnits
Definition: DtUnicode.h:314
virtual ~DtUnicode()
DTOR may NOT deallocate memory as this string may share data.
std::string toAscii() const
Definition: DtUnicode.h:157
void resize(int size)
Resize a string to a new size, if the size &lt; 0 the string will be empty.
Definition: DtUnicode.h:241
int indexOf(const DtUnicodeChar &x, size_t pos=0) const
Search for a character starting a pos, returns index, or -1 if not found.
void setFromQString(QStringType qstr)
Template function for setting from Qt 4 style string.
Definition: DtUnicode.h:147
const CodeUnit * codeUnits() const
Get an array of code units.
Definition: DtUnicode.h:209
bool operator==(const DtUnicode &other) const
Test if two strings are identical.
CodeUnit * last
Definition: DtUnicode.h:312
static DtUnicode fromQString(QStringType qstr)
Template function for setting from Qt 4 style string.
Definition: DtUnicode.h:107
DtUnicode & operator+=(const DtUnicode &str)
Append a string to the end.
void setFromWCharArray(const wchar_t utf16[], int len=-1)
Initialize via specifying encoding.
void toQString(QStringType &qstr) const
Template function for initializing a Qt 4 style string.
Definition: DtUnicode.h:178
void resize(size_t newSize)
Resize the array.
size_t calculateCapacity(size_t necessarySize)
QStringType toQString() const
Template function for outputting a Qt 4 style string.
Definition: DtUnicode.h:171
static DtUnicode fromAscii(const std::string &ascii)
Create a unicode string from an ascii string, the string MUST be zero terminated if the length is not...
std::size_t hash_value(DtUnicode const &val)
Definition: DtUnicode.h:321
bool operator<(const DtUnicode &other) const
Test if a string is less than another based on their first different character or the length...
void dec_ref()
Decrement the reference count.
std::wstring toWideString() const
Definition: DtUnicode.h:167
std::string toLatin1() const
Definition: DtUnicode.h:162
CodeUnitArray & operator=(const CodeUnitArray &)
void add_ref()
Increment the reference count.
Encoding
Definition: DtUnicode.h:239

Document ID: Generated on Thu Aug 27 10:56:05 EDT 2020 from SVN revision 217100
Copyright © 2005-2020 MAK Technologies. All Rights Reserved (www.mak.com)