VR-Link API Documentation for DIS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vlUnicode.h
Go to the documentation of this file.
1 /****************************************************************************
2  * Copyright (c) 2014 MAK Technologies, Inc
3  * All rights reserved.
4  ****************************************************************************/
5 
9 
10 #pragma once
11 
12 #include <vlutil/vlUnicodeChar.h>
13 
14 #include <wchar.h>
15 
28 {
29 public:
30 
31  typedef unsigned short CodeUnit;
32 
33 public:
34 
36  inline DtUcString();
37 
39  explicit inline DtUcString( size_t len, DtUnicodeChar c = DtUnicodeChar('\0') );
40 
44  inline DtUcString( const DtUcString& str );
45 
47  inline ~DtUcString();
48 
52  inline DtUcString& operator = ( const DtUcString& str );
53 
56  inline static DtUcString fromWCharArray( const wchar_t utf16[], int len = -1 );
57 
59  inline static DtUcString fromWideString( const std::wstring& str );
60 
63  inline static DtUcString fromUtf8( const unsigned char* utf8, int len = -1 );
64  inline static DtUcString fromUtf8( const std::string& utf8 );
65 
68  inline static DtUcString fromUtf16( const unsigned short* utf16, int len = -1 );
69 
72  inline static DtUcString fromUtf32( const unsigned int* utf32, int len = -1 );
73 
76  inline static DtUcString fromAscii( const std::string& ascii );
77 
80  inline static DtUcString fromAscii( const char* ascii, int len = -1 );
81 
87  inline static DtUcString tr( const char* ascii );
88 
91  template <typename QStringType, typename QCoreApp>
92  QStringType translate() const;
93 
95  template <typename QStringType>
96  inline static DtUcString fromQString( QStringType qstr );
97 
102  inline void setFromWCharArray( const wchar_t utf16[], int len = -1 );
103 
106  inline void setFromUtf8( const unsigned char* utf8, int len = -1 );
107  inline void setFromUtf8( const std::string& utf8 );
108 
111  inline void setFromLatin1( const char* latin, int len = -1 );
112 
115  inline void setFromUtf16( const unsigned short* utf16, int len = -1 );
116 
119  inline void setFromUtf32( const unsigned int* utf32, int len = -1 );
120 
123  inline void setFromAscii( const std::string& ascii );
124 
127  inline void setFromAscii( const char* ascii, int len = -1 );
128 
130  template <typename QStringType>
131  inline void setFromQString( QStringType qstr );
132 
135  inline void toAscii( std::string& str ) const;
136  inline std::string toAscii() const;
137 
140  inline void toLatin1( std::string& str ) const;
141  inline std::string toLatin1() const;
142 
144  inline void toUtf8( std::string& str ) const;
145  inline std::string toUtf8() const;
146 
148  inline void toWideString( std::wstring& str ) const;
149  inline std::wstring toWideString() const;
150 
152  template <typename QStringType>
153  inline QStringType toQString() const;
154 
156  template <typename QStringType>
157  inline void toQString( QStringType& qstr ) const;
158 
160  inline DtUcString& operator += ( const DtUcString& str );
162  inline DtUcString& operator += ( const DtUnicodeChar& x );
164  inline DtUcString operator + ( const DtUcString& str ) const;
165 
167  inline bool operator == ( const DtUcString& other ) const;
169  inline bool operator != ( const DtUcString& other ) const;
172  inline bool operator < ( const DtUcString& other ) const;
173 
176  inline size_t length() const;
177 
179  inline size_t sizeInBytes() const;
180 
182  inline size_t calculateCodePoints() const;
183 
185  inline const CodeUnit* codeUnits() const;
186 
188  inline int indexOf( const DtUnicodeChar& x, size_t pos = 0 ) const;
189 
191  inline int indexOf( const DtUcString& x, size_t pos = 0 ) const;
192 
194  inline int lastIndexOf( const DtUnicodeChar& x, size_t pos = 0 ) const;
195 
197  inline int lastIndexOf( const DtUcString& x, size_t pos = 0 ) const;
198 
202  inline DtUcString subString( size_t pos = 0, int len = -1 ) const;
203 
211 
212  virtual bool snipFront( const char ch, bool toLastCh = false );
213  virtual bool snipBack( const char ch, bool fromFirstCh = false );
214 
216 
218  inline void push_back( const DtUnicodeChar& x );
219 
227  inline void resize( int size );
228 
229 protected: // THESE are the protected functions not to be used directly.
230 
231  enum Encoding
232  {
236  };
237 
238  // Ascii CTOR
239  inline explicit DtUcString( const char* ascii, int len = -1, Encoding encoding = Ascii );
240 
241  // UTF16 constructor
242  inline explicit DtUcString( const unsigned short* utf16, int len = -1 );
243 
244  // UTF32 constructor.
245  inline explicit DtUcString( const unsigned int* utf32, int len = -1 );
246 
247  // Std string constructor.
248  inline explicit DtUcString( const std::string& ascii );
249 
250  // Concat constructor.
251  inline explicit DtUcString( const DtUcString& a, const DtUcString& b );
252 
253 protected:
254 
258  {
259 
261  inline CodeUnitArray();
262 
264  inline CodeUnitArray( const CodeUnitArray& );
265 
267  inline ~CodeUnitArray();
268 
270  inline CodeUnitArray& operator = ( const CodeUnitArray& );
271 
273  inline void resize( size_t newSize );
274 
277  inline void clear();
278 
280  inline void reserve( size_t capacity );
281 
283  inline size_t calculateCapacity( size_t necessarySize );
284 
286  inline size_t size() const;
287 
289  inline size_t capacity() const;
290 
292  inline void concat( const CodeUnitArray& other );
293 
295  inline bool unique() const;
296 
298  inline bool empty() const;
299 
301  inline void add_ref();
302 
304  inline void dec_ref();
305 
306  volatile long* count;
310 
311  };
312 
314 
315 };
316 
317 
318 inline std::size_t hash_value( const DtUcString& val )
319 {
320  // Copied from TR1 implementation.
321  size_t seed = 2166136261U;
322  size_t first = 0;
323  size_t last = val.length();
324  size_t stride = 1 + last / 10;
325 
326  if ( stride < last )
327  {
328  last -= stride;
329  }
330  for ( ; first < last; first += stride )
331  {
332  seed = 16777619U * seed ^ (size_t)val.codeUnits()[first];
333  }
334 
335  return seed;
336 }
337 
338 
339 #include "vlUnicode.inl"
DtUcString operator+(const DtUcString &str) const
Append two strings together.
static DtUcString fromQString(QStringType qstr)
Template function for setting from Qt 4 style string.
std::string toUtf8() const
void concat(const CodeUnitArray &other)
Concat another array, supports concat operations on itself.
DtUcString()
CTOR creates an empty string.
QStringType toQString() const
Template function for outputting a Qt 4 style string.
bool operator!=(const DtUcString &other) const
Test if two strings are not identical.
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...
Definition: vlUnicode.h:233
void setFromWCharArray(const wchar_t utf16[], int len=-1)
Set from an wchar string, the string MUST be zero terminated if the length is not passed...
int lastIndexOf(const DtUnicodeChar &x, size_t pos=0) const
Search for a character starting a pos, returns index, or -1 if not found.
static DtUcString tr(const char *ascii)
Ascii translation function, used to be compatible with Qt&#39;s translation tools.
void setFromQString(QStringType qstr)
Template function for setting from Qt 4 style string.
static DtUcString 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...
size_t length() const
Get the length in Code Units.
volatile long * count
Definition: vlUnicode.h:306
size_t sizeInBytes() const
Get the size in 8bit bytes.
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 size() const
Return size in bytes.
bool empty() const
Am I empty.
CodeUnitArray & operator=(const CodeUnitArray &)
Assignment operator.
std::string toAscii() const
size_t calculateCapacity(size_t necessarySize)
Calculate the capacity.
const CodeUnit * codeUnits() const
Get an array of code units.
Contains the DtUnicodeChar class declaration/definition.
CodeUnit * begin
Definition: vlUnicode.h:307
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...
static DtUcString 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...
void resize(int size)
Resize a string to a new size, if the size &lt; 0 the string will be empty.
DtUcString & operator+=(const DtUcString &str)
Append a string to the end.
void resize(size_t newSize)
Resize the array.
A unicode string.
Definition: vlUnicode.h:27
static DtUcString fromWCharArray(const wchar_t utf16[], int len=-1)
Set from a wchar_t array, compiler encoding specific, the string MUST be zero terminated if the lengt...
~DtUcString()
DTOR may NOT deallocate memory as this string may share data.
std::string toLatin1() const
static DtUcString fromWideString(const std::wstring &str)
Set from a std::wstring.
Definition: vlUnicode.h:235
DtUcString & operator=(const DtUcString &str)
Assignment operator copies the string, this function will NOT allocate memory for the string...
A single unicode character.
Definition: vlUnicodeChar.h:16
virtual bool snipBack(const char ch, bool fromFirstCh=false)
Truncates everything before or after the character, including the character, respectively.
Internal structure used to store the code units.
Definition: vlUnicode.h:257
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 dec_ref()
Decrement the reference count.
CodeUnitArray myCodeUnits
Definition: vlUnicode.h:313
bool unique() const
Am I unique.
static DtUcString fromAscii(const std::string &ascii)
Create a unicode string from an ascii string, the string MUST be zero terminated if the length is not...
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...
bool operator<(const DtUcString &other) const
Test if a string is less than another based on their first different character or the length...
QStringType translate() const
Utility function for translating a DtUcString variable initialized using the static DtUcString::tr fu...
static DtUcString 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...
void reserve(size_t capacity)
Ensure the array&#39;s capacity is at least capacity, do not change size.
CodeUnit * last
Definition: vlUnicode.h:309
size_t calculateCodePoints() const
Calculate the number of Code Points.
Encoding
Definition: vlUnicode.h:231
virtual bool snipFront(const char ch, bool toLastCh=false)
Truncates everything before or after the character, including the character, respectively.
std::wstring toWideString() const
CodeUnit * end
Definition: vlUnicode.h:308
void clear()
Resize the array to 0, does not change the memory allocation if the array is unique.
std::size_t hash_value(const DtUcString &val)
Definition: vlUnicode.h:318
void add_ref()
Increment the reference count.
bool operator==(const DtUcString &other) const
Test if two strings are identical.
Definition: vlUnicode.h:234
DtUcString subString(size_t pos=0, int len=-1) const
Create a substring starting at position, for n characters.
void setFromAscii(const std::string &ascii)
Set from an ascii string, the string MUST be zero terminated if the length is not passed...
size_t capacity() const
Return capacity in bytes.
void push_back(const DtUnicodeChar &x)
Append a unicode character to the string.
unsigned short CodeUnit
Definition: vlUnicode.h:31

Document ID: Generated on Thu Sep 19 02:12:35 EDT 2024 from SVN revision 269601
Copyright © 2005-2024 MAK Technologies. All Rights Reserved (www.mak.com)