OpenShot Library | libopenshot-audio  0.1.9
juce_CharPointer_UTF32.h
1 
2 /** @weakgroup juce_core-text
3  * @{
4  */
5 /*
6  ==============================================================================
7 
8  This file is part of the JUCE library.
9  Copyright (c) 2017 - ROLI Ltd.
10 
11  JUCE is an open source library subject to commercial or open-source
12  licensing.
13 
14  The code included in this file is provided under the terms of the ISC license
15  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
16  To use, copy, modify, and/or distribute this software for any purpose with or
17  without fee is hereby granted provided that the above copyright notice and
18  this permission notice appear in all copies.
19 
20  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22  DISCLAIMED.
23 
24  ==============================================================================
25 */
26 
27 namespace juce
28 {
29 
30 //==============================================================================
31 /**
32  Wraps a pointer to a null-terminated UTF-32 character string, and provides
33  various methods to operate on the data.
34  @see CharPointer_UTF8, CharPointer_UTF16
35 
36  @tags{Core}
37 */
38 class CharPointer_UTF32 final
39 {
40 public:
41  using CharType = juce_wchar;
42 
43  inline explicit CharPointer_UTF32 (const CharType* rawPointer) noexcept
44  : data (const_cast<CharType*> (rawPointer))
45  {
46  }
47 
48  inline CharPointer_UTF32 (const CharPointer_UTF32& other) = default;
49 
50  inline CharPointer_UTF32 operator= (CharPointer_UTF32 other) noexcept
51  {
52  data = other.data;
53  return *this;
54  }
55 
56  inline CharPointer_UTF32 operator= (const CharType* text) noexcept
57  {
58  data = const_cast<CharType*> (text);
59  return *this;
60  }
61 
62  /** This is a pointer comparison, it doesn't compare the actual text. */
63  inline bool operator== (CharPointer_UTF32 other) const noexcept { return data == other.data; }
64  inline bool operator!= (CharPointer_UTF32 other) const noexcept { return data != other.data; }
65  inline bool operator<= (CharPointer_UTF32 other) const noexcept { return data <= other.data; }
66  inline bool operator< (CharPointer_UTF32 other) const noexcept { return data < other.data; }
67  inline bool operator>= (CharPointer_UTF32 other) const noexcept { return data >= other.data; }
68  inline bool operator> (CharPointer_UTF32 other) const noexcept { return data > other.data; }
69 
70  /** Returns the address that this pointer is pointing to. */
71  inline CharType* getAddress() const noexcept { return data; }
72 
73  /** Returns the address that this pointer is pointing to. */
74  inline operator const CharType*() const noexcept { return data; }
75 
76  /** Returns true if this pointer is pointing to a null character. */
77  inline bool isEmpty() const noexcept { return *data == 0; }
78 
79  /** Returns true if this pointer is not pointing to a null character. */
80  inline bool isNotEmpty() const noexcept { return *data != 0; }
81 
82  /** Returns the unicode character that this pointer is pointing to. */
83  inline juce_wchar operator*() const noexcept { return *data; }
84 
85  /** Moves this pointer along to the next character in the string. */
86  inline CharPointer_UTF32 operator++() noexcept
87  {
88  ++data;
89  return *this;
90  }
91 
92  /** Moves this pointer to the previous character in the string. */
93  inline CharPointer_UTF32 operator--() noexcept
94  {
95  --data;
96  return *this;
97  }
98 
99  /** Returns the character that this pointer is currently pointing to, and then
100  advances the pointer to point to the next character. */
101  inline juce_wchar getAndAdvance() noexcept { return *data++; }
102 
103  /** Moves this pointer along to the next character in the string. */
105  {
106  auto temp (*this);
107  ++data;
108  return temp;
109  }
110 
111  /** Moves this pointer forwards by the specified number of characters. */
112  inline void operator+= (int numToSkip) noexcept
113  {
114  data += numToSkip;
115  }
116 
117  inline void operator-= (int numToSkip) noexcept
118  {
119  data -= numToSkip;
120  }
121 
122  /** Returns the character at a given character index from the start of the string. */
123  inline juce_wchar& operator[] (int characterIndex) const noexcept
124  {
125  return data [characterIndex];
126  }
127 
128  /** Returns a pointer which is moved forwards from this one by the specified number of characters. */
129  CharPointer_UTF32 operator+ (int numToSkip) const noexcept
130  {
131  return CharPointer_UTF32 (data + numToSkip);
132  }
133 
134  /** Returns a pointer which is moved backwards from this one by the specified number of characters. */
135  CharPointer_UTF32 operator- (int numToSkip) const noexcept
136  {
137  return CharPointer_UTF32 (data - numToSkip);
138  }
139 
140  /** Writes a unicode character to this string, and advances this pointer to point to the next position. */
141  inline void write (juce_wchar charToWrite) noexcept
142  {
143  *data++ = charToWrite;
144  }
145 
146  inline void replaceChar (juce_wchar newChar) noexcept
147  {
148  *data = newChar;
149  }
150 
151  /** Writes a null character to this string (leaving the pointer's position unchanged). */
152  inline void writeNull() const noexcept
153  {
154  *data = 0;
155  }
156 
157  /** Returns the number of characters in this string. */
158  size_t length() const noexcept
159  {
160  #if JUCE_NATIVE_WCHAR_IS_UTF32 && ! JUCE_ANDROID
161  return wcslen (data);
162  #else
163  size_t n = 0;
164  while (data[n] != 0)
165  ++n;
166  return n;
167  #endif
168  }
169 
170  /** Returns the number of characters in this string, or the given value, whichever is lower. */
171  size_t lengthUpTo (size_t maxCharsToCount) const noexcept
172  {
173  return CharacterFunctions::lengthUpTo (*this, maxCharsToCount);
174  }
175 
176  /** Returns the number of characters in this string, or up to the given end pointer, whichever is lower. */
177  size_t lengthUpTo (CharPointer_UTF32 end) const noexcept
178  {
179  return CharacterFunctions::lengthUpTo (*this, end);
180  }
181 
182  /** Returns the number of bytes that are used to represent this string.
183  This includes the terminating null character.
184  */
185  size_t sizeInBytes() const noexcept
186  {
187  return sizeof (CharType) * (length() + 1);
188  }
189 
190  /** Returns the number of bytes that would be needed to represent the given
191  unicode character in this encoding format.
192  */
193  static inline size_t getBytesRequiredFor (juce_wchar) noexcept
194  {
195  return sizeof (CharType);
196  }
197 
198  /** Returns the number of bytes that would be needed to represent the given
199  string in this encoding format.
200  The value returned does NOT include the terminating null character.
201  */
202  template <class CharPointer>
203  static size_t getBytesRequiredFor (CharPointer text) noexcept
204  {
205  return sizeof (CharType) * text.length();
206  }
207 
208  /** Returns a pointer to the null character that terminates this string. */
210  {
211  return CharPointer_UTF32 (data + length());
212  }
213 
214  /** Copies a source string to this pointer, advancing this pointer as it goes. */
215  template <typename CharPointer>
216  void writeAll (CharPointer src) noexcept
217  {
218  CharacterFunctions::copyAll (*this, src);
219  }
220 
221  /** Copies a source string to this pointer, advancing this pointer as it goes. */
222  void writeAll (CharPointer_UTF32 src) noexcept
223  {
224  auto* s = src.data;
225 
226  while ((*data = *s) != 0)
227  {
228  ++data;
229  ++s;
230  }
231  }
232 
233  /** Copies a source string to this pointer, advancing this pointer as it goes.
234  The maxDestBytes parameter specifies the maximum number of bytes that can be written
235  to the destination buffer before stopping.
236  */
237  template <typename CharPointer>
238  size_t writeWithDestByteLimit (CharPointer src, size_t maxDestBytes) noexcept
239  {
240  return CharacterFunctions::copyWithDestByteLimit (*this, src, maxDestBytes);
241  }
242 
243  /** Copies a source string to this pointer, advancing this pointer as it goes.
244  The maxChars parameter specifies the maximum number of characters that can be
245  written to the destination buffer before stopping (including the terminating null).
246  */
247  template <typename CharPointer>
248  void writeWithCharLimit (CharPointer src, int maxChars) noexcept
249  {
250  CharacterFunctions::copyWithCharLimit (*this, src, maxChars);
251  }
252 
253  /** Compares this string with another one. */
254  template <typename CharPointer>
255  int compare (CharPointer other) const noexcept
256  {
257  return CharacterFunctions::compare (*this, other);
258  }
259 
260  #if JUCE_NATIVE_WCHAR_IS_UTF32 && ! JUCE_ANDROID
261  /** Compares this string with another one. */
262  int compare (CharPointer_UTF32 other) const noexcept
263  {
264  return wcscmp (data, other.data);
265  }
266  #endif
267 
268  /** Compares this string with another one, up to a specified number of characters. */
269  template <typename CharPointer>
270  int compareUpTo (CharPointer other, int maxChars) const noexcept
271  {
272  return CharacterFunctions::compareUpTo (*this, other, maxChars);
273  }
274 
275  /** Compares this string with another one. */
276  template <typename CharPointer>
277  int compareIgnoreCase (CharPointer other) const
278  {
279  return CharacterFunctions::compareIgnoreCase (*this, other);
280  }
281 
282  /** Compares this string with another one, up to a specified number of characters. */
283  template <typename CharPointer>
284  int compareIgnoreCaseUpTo (CharPointer other, int maxChars) const noexcept
285  {
286  return CharacterFunctions::compareIgnoreCaseUpTo (*this, other, maxChars);
287  }
288 
289  /** Returns the character index of a substring, or -1 if it isn't found. */
290  template <typename CharPointer>
291  int indexOf (CharPointer stringToFind) const noexcept
292  {
293  return CharacterFunctions::indexOf (*this, stringToFind);
294  }
295 
296  /** Returns the character index of a unicode character, or -1 if it isn't found. */
297  int indexOf (juce_wchar charToFind) const noexcept
298  {
299  int i = 0;
300 
301  while (data[i] != 0)
302  {
303  if (data[i] == charToFind)
304  return i;
305 
306  ++i;
307  }
308 
309  return -1;
310  }
311 
312  /** Returns the character index of a unicode character, or -1 if it isn't found. */
313  int indexOf (juce_wchar charToFind, bool ignoreCase) const noexcept
314  {
315  return ignoreCase ? CharacterFunctions::indexOfCharIgnoreCase (*this, charToFind)
316  : CharacterFunctions::indexOfChar (*this, charToFind);
317  }
318 
319  /** Returns true if the first character of this string is whitespace. */
320  bool isWhitespace() const { return CharacterFunctions::isWhitespace (*data) != 0; }
321  /** Returns true if the first character of this string is a digit. */
322  bool isDigit() const { return CharacterFunctions::isDigit (*data) != 0; }
323  /** Returns true if the first character of this string is a letter. */
324  bool isLetter() const { return CharacterFunctions::isLetter (*data) != 0; }
325  /** Returns true if the first character of this string is a letter or digit. */
326  bool isLetterOrDigit() const { return CharacterFunctions::isLetterOrDigit (*data) != 0; }
327  /** Returns true if the first character of this string is upper-case. */
328  bool isUpperCase() const { return CharacterFunctions::isUpperCase (*data) != 0; }
329  /** Returns true if the first character of this string is lower-case. */
330  bool isLowerCase() const { return CharacterFunctions::isLowerCase (*data) != 0; }
331 
332  /** Returns an upper-case version of the first character of this string. */
333  juce_wchar toUpperCase() const noexcept { return CharacterFunctions::toUpperCase (*data); }
334  /** Returns a lower-case version of the first character of this string. */
335  juce_wchar toLowerCase() const noexcept { return CharacterFunctions::toLowerCase (*data); }
336 
337  /** Parses this string as a 32-bit integer. */
338  int getIntValue32() const noexcept { return CharacterFunctions::getIntValue <int, CharPointer_UTF32> (*this); }
339  /** Parses this string as a 64-bit integer. */
340  int64 getIntValue64() const noexcept { return CharacterFunctions::getIntValue <int64, CharPointer_UTF32> (*this); }
341 
342  /** Parses this string as a floating point double. */
343  double getDoubleValue() const noexcept { return CharacterFunctions::getDoubleValue (*this); }
344 
345  /** Returns the first non-whitespace character in the string. */
347 
348  /** Returns true if the given unicode character can be represented in this encoding. */
349  static bool canRepresent (juce_wchar character) noexcept
350  {
351  return ((uint32) character) < (uint32) 0x10ffff;
352  }
353 
354  /** Returns true if this data contains a valid string in this encoding. */
355  static bool isValidString (const CharType* dataToTest, int maxBytesToRead)
356  {
357  maxBytesToRead /= (int) sizeof (CharType);
358 
359  while (--maxBytesToRead >= 0 && *dataToTest != 0)
360  if (! canRepresent (*dataToTest++))
361  return false;
362 
363  return true;
364  }
365 
366  /** Atomically swaps this pointer for a new value, returning the previous value. */
368  {
369  return CharPointer_UTF32 (reinterpret_cast<Atomic<CharType*>&> (data).exchange (newValue.data));
370  }
371 
372 private:
373  CharType* data;
374 };
375 
376 } // namespace juce
377 
378 /** @}*/
A simple wrapper around std::atomic.
Definition: juce_Atomic.h:45
bool isEmpty() const noexcept
Returns true if this pointer is pointing to a null character.
static double getDoubleValue(CharPointerType text) noexcept
Parses a character string, to read a floating-point value.
static size_t getBytesRequiredFor(juce_wchar) noexcept
Returns the number of bytes that would be needed to represent the given unicode character in this enc...
static juce_wchar toUpperCase(juce_wchar character) noexcept
Converts a character to upper-case.
CharPointer_UTF32 operator--() noexcept
Moves this pointer to the previous character in the string.
int compareUpTo(CharPointer other, int maxChars) const noexcept
Compares this string with another one, up to a specified number of characters.
bool isLowerCase() const
Returns true if the first character of this string is lower-case.
CharPointer_UTF32 operator+(int numToSkip) const noexcept
Returns a pointer which is moved forwards from this one by the specified number of characters...
static int indexOfCharIgnoreCase(Type text, juce_wchar charToFind) noexcept
Finds the character index of a given character in another string, using a case-independent match...
static void copyAll(DestCharPointerType &dest, SrcCharPointerType src) noexcept
Copies null-terminated characters from one string to another.
static int compareIgnoreCase(juce_wchar char1, juce_wchar char2) noexcept
Compares two characters, using a case-independant match.
CharPointer_UTF32 operator-(int numToSkip) const noexcept
Returns a pointer which is moved backwards from this one by the specified number of characters...
int compare(CharPointer other) const noexcept
Compares this string with another one.
size_t writeWithDestByteLimit(CharPointer src, size_t maxDestBytes) noexcept
Copies a source string to this pointer, advancing this pointer as it goes.
static bool isDigit(char character) noexcept
Checks whether a character is a digit.
static Type findEndOfWhitespace(Type text) noexcept
Returns a pointer to the first non-whitespace character in a string.
void writeWithCharLimit(CharPointer src, int maxChars) noexcept
Copies a source string to this pointer, advancing this pointer as it goes.
bool isLetter() const
Returns true if the first character of this string is a letter.
void writeNull() const noexcept
Writes a null character to this string (leaving the pointer&#39;s position unchanged).
int64 getIntValue64() const noexcept
Parses this string as a 64-bit integer.
bool isWhitespace() const
Returns true if the first character of this string is whitespace.
size_t lengthUpTo(CharPointer_UTF32 end) const noexcept
Returns the number of characters in this string, or up to the given end pointer, whichever is lower...
int indexOf(juce_wchar charToFind, bool ignoreCase) const noexcept
Returns the character index of a unicode character, or -1 if it isn&#39;t found.
static size_t getBytesRequiredFor(CharPointer text) noexcept
Returns the number of bytes that would be needed to represent the given string in this encoding forma...
static bool isWhitespace(char character) noexcept
Checks whether a character is whitespace.
CharType * getAddress() const noexcept
Returns the address that this pointer is pointing to.
juce_wchar operator*() const noexcept
Returns the unicode character that this pointer is pointing to.
void writeAll(CharPointer_UTF32 src) noexcept
Copies a source string to this pointer, advancing this pointer as it goes.
CharPointer_UTF32 atomicSwap(CharPointer_UTF32 newValue)
Atomically swaps this pointer for a new value, returning the previous value.
size_t lengthUpTo(size_t maxCharsToCount) const noexcept
Returns the number of characters in this string, or the given value, whichever is lower...
bool isDigit() const
Returns true if the first character of this string is a digit.
static int indexOfChar(Type text, const juce_wchar charToFind) noexcept
Finds the character index of a given character in another string.
static int compareIgnoreCaseUpTo(CharPointerType1 s1, CharPointerType2 s2, int maxChars) noexcept
Compares two null-terminated character strings, using a case-independent match.
int indexOf(juce_wchar charToFind) const noexcept
Returns the character index of a unicode character, or -1 if it isn&#39;t found.
Wraps a pointer to a null-terminated UTF-32 character string, and provides various methods to operate...
bool isNotEmpty() const noexcept
Returns true if this pointer is not pointing to a null character.
static bool isUpperCase(juce_wchar character) noexcept
Checks whether a unicode character is upper-case.
int compareIgnoreCaseUpTo(CharPointer other, int maxChars) const noexcept
Compares this string with another one, up to a specified number of characters.
juce_wchar getAndAdvance() noexcept
Returns the character that this pointer is currently pointing to, and then advances the pointer to po...
static size_t lengthUpTo(CharPointerType text, const size_t maxCharsToCount) noexcept
Counts the number of characters in a given string, stopping if the count exceeds a specified limit...
static bool isValidString(const CharType *dataToTest, int maxBytesToRead)
Returns true if this data contains a valid string in this encoding.
static size_t copyWithDestByteLimit(DestCharPointerType &dest, SrcCharPointerType src, size_t maxBytesToWrite) noexcept
Copies characters from one string to another, up to a null terminator or a given byte size limit...
size_t sizeInBytes() const noexcept
Returns the number of bytes that are used to represent this string.
int getIntValue32() const noexcept
Parses this string as a 32-bit integer.
double getDoubleValue() const noexcept
Parses this string as a floating point double.
bool isUpperCase() const
Returns true if the first character of this string is upper-case.
int indexOf(CharPointer stringToFind) const noexcept
Returns the character index of a substring, or -1 if it isn&#39;t found.
bool isLetterOrDigit() const
Returns true if the first character of this string is a letter or digit.
size_t length() const noexcept
Returns the number of characters in this string.
static juce_wchar toLowerCase(juce_wchar character) noexcept
Converts a character to lower-case.
static bool isLetter(char character) noexcept
Checks whether a character is alphabetic.
int compareIgnoreCase(CharPointer other) const
Compares this string with another one.
juce_wchar toUpperCase() const noexcept
Returns an upper-case version of the first character of this string.
CharPointer_UTF32 operator++() noexcept
Moves this pointer along to the next character in the string.
juce_wchar toLowerCase() const noexcept
Returns a lower-case version of the first character of this string.
static int indexOf(CharPointerType1 textToSearch, const CharPointerType2 substringToLookFor) noexcept
Finds the character index of a given substring in another string.
void operator+=(int numToSkip) noexcept
Moves this pointer forwards by the specified number of characters.
static bool isLowerCase(juce_wchar character) noexcept
Checks whether a unicode character is lower-case.
void write(juce_wchar charToWrite) noexcept
Writes a unicode character to this string, and advances this pointer to point to the next position...
static bool canRepresent(juce_wchar character) noexcept
Returns true if the given unicode character can be represented in this encoding.
bool operator==(CharPointer_UTF32 other) const noexcept
This is a pointer comparison, it doesn&#39;t compare the actual text.
juce_wchar & operator[](int characterIndex) const noexcept
Returns the character at a given character index from the start of the string.
static bool isLetterOrDigit(char character) noexcept
Checks whether a character is alphabetic or numeric.
CharPointer_UTF32 findEndOfWhitespace() const noexcept
Returns the first non-whitespace character in the string.
static int compareUpTo(CharPointerType1 s1, CharPointerType2 s2, int maxChars) noexcept
Compares two null-terminated character strings, up to a given number of characters.
CharPointer_UTF32 findTerminatingNull() const noexcept
Returns a pointer to the null character that terminates this string.
static void copyWithCharLimit(DestCharPointerType &dest, SrcCharPointerType src, int maxChars) noexcept
Copies characters from one string to another, up to a null terminator or a given maximum number of ch...
static int compare(juce_wchar char1, juce_wchar char2) noexcept
Compares two characters.
void writeAll(CharPointer src) noexcept
Copies a source string to this pointer, advancing this pointer as it goes.