openshot-audio  0.1.2
juce_CharPointer_UTF16.h
Go to the documentation of this file.
1 /*
2  ==============================================================================
3 
4  This file is part of the juce_core module of the JUCE library.
5  Copyright (c) 2015 - ROLI Ltd.
6 
7  Permission to use, copy, modify, and/or distribute this software for any purpose with
8  or without fee is hereby granted, provided that the above copyright notice and this
9  permission notice appear in all copies.
10 
11  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
12  TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
13  NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14  DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
15  IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16  CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 
18  ------------------------------------------------------------------------------
19 
20  NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
21  All other JUCE modules are covered by a dual GPL/commercial license, so if you are
22  using any other modules, be sure to check that you also comply with their license.
23 
24  For more details, visit www.juce.com
25 
26  ==============================================================================
27 */
28 
29 #ifndef JUCE_CHARPOINTER_UTF16_H_INCLUDED
30 #define JUCE_CHARPOINTER_UTF16_H_INCLUDED
31 
32 
33 //==============================================================================
40 {
41 public:
42  #if JUCE_NATIVE_WCHAR_IS_UTF16
43  typedef wchar_t CharType;
44  #else
45  typedef int16 CharType;
46  #endif
47 
48  inline explicit CharPointer_UTF16 (const CharType* const rawPointer) noexcept
49  : data (const_cast <CharType*> (rawPointer))
50  {
51  }
52 
54  : data (other.data)
55  {
56  }
57 
59  {
60  data = other.data;
61  return *this;
62  }
63 
64  inline CharPointer_UTF16 operator= (const CharType* text) noexcept
65  {
66  data = const_cast <CharType*> (text);
67  return *this;
68  }
69 
71  inline bool operator== (CharPointer_UTF16 other) const noexcept { return data == other.data; }
72  inline bool operator!= (CharPointer_UTF16 other) const noexcept { return data != other.data; }
73  inline bool operator<= (CharPointer_UTF16 other) const noexcept { return data <= other.data; }
74  inline bool operator< (CharPointer_UTF16 other) const noexcept { return data < other.data; }
75  inline bool operator>= (CharPointer_UTF16 other) const noexcept { return data >= other.data; }
76  inline bool operator> (CharPointer_UTF16 other) const noexcept { return data > other.data; }
77 
79  inline CharType* getAddress() const noexcept { return data; }
80 
82  inline operator const CharType*() const noexcept { return data; }
83 
85  inline bool isEmpty() const noexcept { return *data == 0; }
86 
89  {
90  uint32 n = (uint32) (uint16) *data;
91 
92  if (n >= 0xd800 && n <= 0xdfff && ((uint32) (uint16) data[1]) >= 0xdc00)
93  n = 0x10000 + (((n - 0xd800) << 10) | (((uint32) (uint16) data[1]) - 0xdc00));
94 
95  return (juce_wchar) n;
96  }
97 
100  {
101  const juce_wchar n = *data++;
102 
103  if (n >= 0xd800 && n <= 0xdfff && ((uint32) (uint16) *data) >= 0xdc00)
104  ++data;
105 
106  return *this;
107  }
108 
111  {
112  const juce_wchar n = *--data;
113 
114  if (n >= 0xdc00 && n <= 0xdfff)
115  --data;
116 
117  return *this;
118  }
119 
123  {
124  uint32 n = (uint32) (uint16) *data++;
125 
126  if (n >= 0xd800 && n <= 0xdfff && ((uint32) (uint16) *data) >= 0xdc00)
127  n = 0x10000 + ((((n - 0xd800) << 10) | (((uint32) (uint16) *data++) - 0xdc00)));
128 
129  return (juce_wchar) n;
130  }
131 
134  {
135  CharPointer_UTF16 temp (*this);
136  ++*this;
137  return temp;
138  }
139 
141  void operator+= (int numToSkip) noexcept
142  {
143  if (numToSkip < 0)
144  {
145  while (++numToSkip <= 0)
146  --*this;
147  }
148  else
149  {
150  while (--numToSkip >= 0)
151  ++*this;
152  }
153  }
154 
156  void operator-= (int numToSkip) noexcept
157  {
158  operator+= (-numToSkip);
159  }
160 
162  juce_wchar operator[] (const int characterIndex) const noexcept
163  {
164  CharPointer_UTF16 p (*this);
165  p += characterIndex;
166  return *p;
167  }
168 
170  CharPointer_UTF16 operator+ (const int numToSkip) const noexcept
171  {
172  CharPointer_UTF16 p (*this);
173  p += numToSkip;
174  return p;
175  }
176 
178  CharPointer_UTF16 operator- (const int numToSkip) const noexcept
179  {
180  CharPointer_UTF16 p (*this);
181  p += -numToSkip;
182  return p;
183  }
184 
186  void write (juce_wchar charToWrite) noexcept
187  {
188  if (charToWrite >= 0x10000)
189  {
190  charToWrite -= 0x10000;
191  *data++ = (CharType) (0xd800 + (charToWrite >> 10));
192  *data++ = (CharType) (0xdc00 + (charToWrite & 0x3ff));
193  }
194  else
195  {
196  *data++ = (CharType) charToWrite;
197  }
198  }
199 
201  inline void writeNull() const noexcept
202  {
203  *data = 0;
204  }
205 
207  size_t length() const noexcept
208  {
209  const CharType* d = data;
210  size_t count = 0;
211 
212  for (;;)
213  {
214  const int n = *d++;
215 
216  if (n >= 0xd800 && n <= 0xdfff)
217  {
218  if (*d++ == 0)
219  break;
220  }
221  else if (n == 0)
222  break;
223 
224  ++count;
225  }
226 
227  return count;
228  }
229 
231  size_t lengthUpTo (const size_t maxCharsToCount) const noexcept
232  {
233  return CharacterFunctions::lengthUpTo (*this, maxCharsToCount);
234  }
235 
237  size_t lengthUpTo (const CharPointer_UTF16 end) const noexcept
238  {
239  return CharacterFunctions::lengthUpTo (*this, end);
240  }
241 
245  size_t sizeInBytes() const noexcept
246  {
247  return sizeof (CharType) * (findNullIndex (data) + 1);
248  }
249 
253  static size_t getBytesRequiredFor (const juce_wchar charToWrite) noexcept
254  {
255  return (charToWrite >= 0x10000) ? (sizeof (CharType) * 2) : sizeof (CharType);
256  }
257 
262  template <class CharPointer>
263  static size_t getBytesRequiredFor (CharPointer text) noexcept
264  {
265  size_t count = 0;
266  juce_wchar n;
267 
268  while ((n = text.getAndAdvance()) != 0)
269  count += getBytesRequiredFor (n);
270 
271  return count;
272  }
273 
276  {
277  const CharType* t = data;
278 
279  while (*t != 0)
280  ++t;
281 
282  return CharPointer_UTF16 (t);
283  }
284 
286  template <typename CharPointer>
287  void writeAll (const CharPointer src) noexcept
288  {
289  CharacterFunctions::copyAll (*this, src);
290  }
291 
294  {
295  const CharType* s = src.data;
296 
297  while ((*data = *s) != 0)
298  {
299  ++data;
300  ++s;
301  }
302  }
303 
308  template <typename CharPointer>
309  size_t writeWithDestByteLimit (const CharPointer src, const size_t maxDestBytes) noexcept
310  {
311  return CharacterFunctions::copyWithDestByteLimit (*this, src, maxDestBytes);
312  }
313 
318  template <typename CharPointer>
319  void writeWithCharLimit (const CharPointer src, const int maxChars) noexcept
320  {
321  CharacterFunctions::copyWithCharLimit (*this, src, maxChars);
322  }
323 
325  template <typename CharPointer>
326  int compare (const CharPointer other) const noexcept
327  {
328  return CharacterFunctions::compare (*this, other);
329  }
330 
332  template <typename CharPointer>
333  int compareUpTo (const CharPointer other, const int maxChars) const noexcept
334  {
335  return CharacterFunctions::compareUpTo (*this, other, maxChars);
336  }
337 
339  template <typename CharPointer>
340  int compareIgnoreCase (const CharPointer other) const noexcept
341  {
342  return CharacterFunctions::compareIgnoreCase (*this, other);
343  }
344 
346  template <typename CharPointer>
347  int compareIgnoreCaseUpTo (const CharPointer other, const int maxChars) const noexcept
348  {
349  return CharacterFunctions::compareIgnoreCaseUpTo (*this, other, maxChars);
350  }
351 
352  #if JUCE_MSVC && ! DOXYGEN
353  int compareIgnoreCase (const CharPointer_UTF16 other) const noexcept
354  {
355  return _wcsicmp (data, other.data);
356  }
357 
358  int compareIgnoreCaseUpTo (const CharPointer_UTF16 other, int maxChars) const noexcept
359  {
360  return _wcsnicmp (data, other.data, (size_t) maxChars);
361  }
362 
363  int indexOf (const CharPointer_UTF16 stringToFind) const noexcept
364  {
365  const CharType* const t = wcsstr (data, stringToFind.getAddress());
366  return t == nullptr ? -1 : (int) (t - data);
367  }
368  #endif
369 
371  template <typename CharPointer>
372  int indexOf (const CharPointer stringToFind) const noexcept
373  {
374  return CharacterFunctions::indexOf (*this, stringToFind);
375  }
376 
378  int indexOf (const juce_wchar charToFind) const noexcept
379  {
380  return CharacterFunctions::indexOfChar (*this, charToFind);
381  }
382 
384  int indexOf (const juce_wchar charToFind, const bool ignoreCase) const noexcept
385  {
386  return ignoreCase ? CharacterFunctions::indexOfCharIgnoreCase (*this, charToFind)
387  : CharacterFunctions::indexOfChar (*this, charToFind);
388  }
389 
391  bool isWhitespace() const noexcept { return CharacterFunctions::isWhitespace (operator*()) != 0; }
393  bool isDigit() const noexcept { return CharacterFunctions::isDigit (operator*()) != 0; }
395  bool isLetter() const noexcept { return CharacterFunctions::isLetter (operator*()) != 0; }
397  bool isLetterOrDigit() const noexcept { return CharacterFunctions::isLetterOrDigit (operator*()) != 0; }
399  bool isUpperCase() const noexcept { return CharacterFunctions::isUpperCase (operator*()) != 0; }
401  bool isLowerCase() const noexcept { return CharacterFunctions::isLowerCase (operator*()) != 0; }
402 
407 
410  {
411  #if JUCE_MSVC
412  return _wtoi (data);
413  #else
414  return CharacterFunctions::getIntValue <int, CharPointer_UTF16> (*this);
415  #endif
416  }
417 
420  {
421  #if JUCE_MSVC
422  return _wtoi64 (data);
423  #else
424  return CharacterFunctions::getIntValue <int64, CharPointer_UTF16> (*this);
425  #endif
426  }
427 
429  double getDoubleValue() const noexcept { return CharacterFunctions::getDoubleValue (*this); }
430 
433 
435  static bool canRepresent (juce_wchar character) noexcept
436  {
437  return ((unsigned int) character) < (unsigned int) 0x10ffff
438  && (((unsigned int) character) < 0xd800 || ((unsigned int) character) > 0xdfff);
439  }
440 
442  static bool isValidString (const CharType* dataToTest, int maxBytesToRead)
443  {
444  maxBytesToRead /= (int) sizeof (CharType);
445 
446  while (--maxBytesToRead >= 0 && *dataToTest != 0)
447  {
448  const uint32 n = (uint32) (uint16) *dataToTest++;
449 
450  if (n >= 0xd800)
451  {
452  if (n > 0x10ffff)
453  return false;
454 
455  if (n <= 0xdfff)
456  {
457  if (n > 0xdc00)
458  return false;
459 
460  const uint32 nextChar = (uint32) (uint16) *dataToTest++;
461 
462  if (nextChar < 0xdc00 || nextChar > 0xdfff)
463  return false;
464  }
465  }
466  }
467 
468  return true;
469  }
470 
473  {
474  return CharPointer_UTF16 (reinterpret_cast <Atomic<CharType*>&> (data).exchange (newValue.data));
475  }
476 
478  enum
479  {
484  };
485 
489  static bool isByteOrderMarkBigEndian (const void* possibleByteOrder) noexcept
490  {
491  jassert (possibleByteOrder != nullptr);
492  const uint8* const c = static_cast<const uint8*> (possibleByteOrder);
493 
494  return c[0] == (uint8) byteOrderMarkBE1
495  && c[1] == (uint8) byteOrderMarkBE2;
496  }
497 
501  static bool isByteOrderMarkLittleEndian (const void* possibleByteOrder) noexcept
502  {
503  jassert (possibleByteOrder != nullptr);
504  const uint8* const c = static_cast<const uint8*> (possibleByteOrder);
505 
506  return c[0] == (uint8) byteOrderMarkLE1
507  && c[1] == (uint8) byteOrderMarkLE2;
508  }
509 
510 private:
511  CharType* data;
512 
513  static unsigned int findNullIndex (const CharType* const t) noexcept
514  {
515  unsigned int n = 0;
516 
517  while (t[n] != 0)
518  ++n;
519 
520  return n;
521  }
522 };
523 
524 
525 #endif // JUCE_CHARPOINTER_UTF16_H_INCLUDED
bool isDigit() const noexcept
Definition: juce_CharPointer_UTF16.h:393
Definition: juce_CharPointer_UTF16.h:482
CharPointer_UTF16(const CharType *const rawPointer) noexcept
Definition: juce_CharPointer_UTF16.h:48
static size_t lengthUpTo(CharPointerType text, const size_t maxCharsToCount) noexcept
Definition: juce_CharacterFunctions.h:307
static bool isByteOrderMarkBigEndian(const void *possibleByteOrder) noexcept
Definition: juce_CharPointer_UTF16.h:489
double getDoubleValue() const noexcept
Definition: juce_CharPointer_UTF16.h:429
static bool isByteOrderMarkLittleEndian(const void *possibleByteOrder) noexcept
Definition: juce_CharPointer_UTF16.h:501
CharPointer_UTF16 operator--() noexcept
Definition: juce_CharPointer_UTF16.h:110
CharPointer_UTF16 operator-(const int numToSkip) const noexcept
Definition: juce_CharPointer_UTF16.h:178
#define noexcept
Definition: juce_CompilerSupport.h:141
Definition: juce_CharPointer_UTF16.h:39
Definition: juce_Atomic.h:41
signed short int16
Definition: juce_MathsFunctions.h:45
static juce_wchar toLowerCase(juce_wchar character) noexcept
Definition: juce_CharacterFunctions.cpp:40
void operator-=(int numToSkip) noexcept
Definition: juce_CharPointer_UTF16.h:156
int indexOf(const juce_wchar charToFind) const noexcept
Definition: juce_CharPointer_UTF16.h:378
unsigned short uint16
Definition: juce_MathsFunctions.h:47
juce_wchar getAndAdvance() noexcept
Definition: juce_CharPointer_UTF16.h:122
static int compare(CharPointerType1 s1, CharPointerType2 s2) noexcept
Definition: juce_CharacterFunctions.h:393
static Type findEndOfWhitespace(Type text) noexcept
Definition: juce_CharacterFunctions.h:586
size_t lengthUpTo(const size_t maxCharsToCount) const noexcept
Definition: juce_CharPointer_UTF16.h:231
static double getDoubleValue(CharPointerType text) noexcept
Definition: juce_CharacterFunctions.h:253
CharPointer_UTF16 atomicSwap(const CharPointer_UTF16 newValue)
Definition: juce_CharPointer_UTF16.h:472
size_t length() const noexcept
Definition: juce_CharPointer_UTF16.h:207
bool isEmpty() const noexcept
Definition: juce_CharPointer_UTF16.h:85
bool operator>(CharPointer_UTF16 other) const noexcept
Definition: juce_CharPointer_UTF16.h:76
CharPointer_UTF16 findEndOfWhitespace() const noexcept
Definition: juce_CharPointer_UTF16.h:432
void writeAll(const CharPointer_UTF16 src) noexcept
Definition: juce_CharPointer_UTF16.h:293
static bool isDigit(char character) noexcept
Definition: juce_CharacterFunctions.cpp:78
Definition: juce_CharPointer_UTF16.h:483
static bool isLetterOrDigit(char character) noexcept
Definition: juce_CharacterFunctions.cpp:99
void write(juce_wchar charToWrite) noexcept
Definition: juce_CharPointer_UTF16.h:186
static bool isWhitespace(char character) noexcept
Definition: juce_CharacterFunctions.cpp:68
static juce_wchar toUpperCase(juce_wchar character) noexcept
Definition: juce_CharacterFunctions.cpp:35
static bool isLetter(char character) noexcept
Definition: juce_CharacterFunctions.cpp:88
static int indexOf(CharPointerType1 textToSearch, const CharPointerType2 substringToLookFor) noexcept
Definition: juce_CharacterFunctions.h:467
static void copyAll(DestCharPointerType &dest, SrcCharPointerType src) noexcept
Definition: juce_CharacterFunctions.h:332
CharPointer_UTF16(const CharPointer_UTF16 &other) noexcept
Definition: juce_CharPointer_UTF16.h:53
static size_t copyWithDestByteLimit(DestCharPointerType &dest, SrcCharPointerType src, size_t maxBytesToWrite) noexcept
Definition: juce_CharacterFunctions.h:350
bool isLowerCase() const noexcept
Definition: juce_CharPointer_UTF16.h:401
size_t writeWithDestByteLimit(const CharPointer src, const size_t maxDestBytes) noexcept
Definition: juce_CharPointer_UTF16.h:309
void writeNull() const noexcept
Definition: juce_CharPointer_UTF16.h:201
void writeWithCharLimit(const CharPointer src, const int maxChars) noexcept
Definition: juce_CharPointer_UTF16.h:319
bool operator<=(CharPointer_UTF16 other) const noexcept
Definition: juce_CharPointer_UTF16.h:73
bool isLetterOrDigit() const noexcept
Definition: juce_CharPointer_UTF16.h:397
unsigned int uint32
Definition: juce_MathsFunctions.h:51
bool operator==(CharPointer_UTF16 other) const noexcept
Definition: juce_CharPointer_UTF16.h:71
juce_wchar operator[](const int characterIndex) const noexcept
Definition: juce_CharPointer_UTF16.h:162
static void copyWithCharLimit(DestCharPointerType &dest, SrcCharPointerType src, int maxChars) noexcept
Definition: juce_CharacterFunctions.h:377
bool operator>=(CharPointer_UTF16 other) const noexcept
Definition: juce_CharPointer_UTF16.h:75
CharPointer_UTF16 findTerminatingNull() const noexcept
Definition: juce_CharPointer_UTF16.h:275
int64 getIntValue64() const noexcept
Definition: juce_CharPointer_UTF16.h:419
bool operator!=(CharPointer_UTF16 other) const noexcept
Definition: juce_CharPointer_UTF16.h:72
static int compareIgnoreCase(CharPointerType1 s1, CharPointerType2 s2) noexcept
Definition: juce_CharacterFunctions.h:427
long long int64
Definition: juce_MathsFunctions.h:60
size_t lengthUpTo(const CharPointer_UTF16 end) const noexcept
Definition: juce_CharPointer_UTF16.h:237
static bool canRepresent(juce_wchar character) noexcept
Definition: juce_CharPointer_UTF16.h:435
CharPointer_UTF16 operator+(const int numToSkip) const noexcept
Definition: juce_CharPointer_UTF16.h:170
int compare(const CharPointer other) const noexcept
Definition: juce_CharPointer_UTF16.h:326
bool isLetter() const noexcept
Definition: juce_CharPointer_UTF16.h:395
static int indexOfChar(Type text, const juce_wchar charToFind) noexcept
Definition: juce_CharacterFunctions.h:544
static bool isValidString(const CharType *dataToTest, int maxBytesToRead)
Definition: juce_CharPointer_UTF16.h:442
int16 CharType
Definition: juce_CharPointer_UTF16.h:45
static size_t getBytesRequiredFor(const juce_wchar charToWrite) noexcept
Definition: juce_CharPointer_UTF16.h:253
#define jassert(a)
Definition: juce_PlatformDefs.h:146
static int compareUpTo(CharPointerType1 s1, CharPointerType2 s2, int maxChars) noexcept
Definition: juce_CharacterFunctions.h:410
int compareUpTo(const CharPointer other, const int maxChars) const noexcept
Definition: juce_CharPointer_UTF16.h:333
static int indexOfCharIgnoreCase(Type text, juce_wchar charToFind) noexcept
Definition: juce_CharacterFunctions.h:564
bool operator<(CharPointer_UTF16 other) const noexcept
Definition: juce_CharPointer_UTF16.h:74
static bool isLowerCase(juce_wchar character) noexcept
Definition: juce_CharacterFunctions.cpp:54
int compareIgnoreCase(const CharPointer other) const noexcept
Definition: juce_CharPointer_UTF16.h:340
Definition: juce_CharPointer_UTF16.h:480
juce_wchar operator*() const noexcept
Definition: juce_CharPointer_UTF16.h:88
int indexOf(const juce_wchar charToFind, const bool ignoreCase) const noexcept
Definition: juce_CharPointer_UTF16.h:384
static bool isUpperCase(juce_wchar character) noexcept
Definition: juce_CharacterFunctions.cpp:45
size_t sizeInBytes() const noexcept
Definition: juce_CharPointer_UTF16.h:245
static int compareIgnoreCaseUpTo(CharPointerType1 s1, CharPointerType2 s2, int maxChars) noexcept
Definition: juce_CharacterFunctions.h:446
static size_t getBytesRequiredFor(CharPointer text) noexcept
Definition: juce_CharPointer_UTF16.h:263
juce_wchar toUpperCase() const noexcept
Definition: juce_CharPointer_UTF16.h:404
unsigned char uint8
Definition: juce_MathsFunctions.h:43
Definition: juce_CharPointer_UTF16.h:481
int compareIgnoreCaseUpTo(const CharPointer other, const int maxChars) const noexcept
Definition: juce_CharPointer_UTF16.h:347
void operator+=(int numToSkip) noexcept
Definition: juce_CharPointer_UTF16.h:141
juce_wchar toLowerCase() const noexcept
Definition: juce_CharPointer_UTF16.h:406
wchar_t juce_wchar
Definition: juce_CharacterFunctions.h:49
CharType * getAddress() const noexcept
Definition: juce_CharPointer_UTF16.h:79
bool isWhitespace() const noexcept
Definition: juce_CharPointer_UTF16.h:391
CharPointer_UTF16 operator++() noexcept
Definition: juce_CharPointer_UTF16.h:99
int indexOf(const CharPointer stringToFind) const noexcept
Definition: juce_CharPointer_UTF16.h:372
CharPointer_UTF16 operator=(CharPointer_UTF16 other) noexcept
Definition: juce_CharPointer_UTF16.h:58
void writeAll(const CharPointer src) noexcept
Definition: juce_CharPointer_UTF16.h:287
bool isUpperCase() const noexcept
Definition: juce_CharPointer_UTF16.h:399
int getIntValue32() const noexcept
Definition: juce_CharPointer_UTF16.h:409