JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_CharPointer_UTF16.h
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
26//==============================================================================
35{
36public:
37 #if JUCE_NATIVE_WCHAR_IS_UTF16
38 using CharType = wchar_t;
39 #else
40 using CharType = int16;
41 #endif
42
43 inline explicit CharPointer_UTF16 (const CharType* rawPointer) noexcept
44 : data (const_cast<CharType*> (rawPointer))
45 {
46 }
47
48 inline CharPointer_UTF16 (const CharPointer_UTF16& other) = default;
49
50 inline CharPointer_UTF16 operator= (CharPointer_UTF16 other) noexcept
51 {
52 data = other.data;
53 return *this;
54 }
55
56 inline CharPointer_UTF16 operator= (const CharType* text) noexcept
57 {
58 data = const_cast<CharType*> (text);
59 return *this;
60 }
61
63 inline bool operator== (CharPointer_UTF16 other) const noexcept { return data == other.data; }
64 inline bool operator!= (CharPointer_UTF16 other) const noexcept { return data != other.data; }
65 inline bool operator<= (CharPointer_UTF16 other) const noexcept { return data <= other.data; }
66 inline bool operator< (CharPointer_UTF16 other) const noexcept { return data < other.data; }
67 inline bool operator>= (CharPointer_UTF16 other) const noexcept { return data >= other.data; }
68 inline bool operator> (CharPointer_UTF16 other) const noexcept { return data > other.data; }
69
71 inline CharType* getAddress() const noexcept { return data; }
72
74 inline operator const CharType*() const noexcept { return data; }
75
77 inline bool isEmpty() const noexcept { return *data == 0; }
78
80 inline bool isNotEmpty() const noexcept { return *data != 0; }
81
84 {
85 auto n = (uint32) (uint16) *data;
86
87 if (n >= 0xd800 && n <= 0xdfff && ((uint32) (uint16) data[1]) >= 0xdc00)
88 n = 0x10000 + (((n - 0xd800) << 10) | (((uint32) (uint16) data[1]) - 0xdc00));
89
90 return (juce_wchar) n;
91 }
92
95 {
96 auto n = (uint32) (uint16) *data++;
97
98 if (n >= 0xd800 && n <= 0xdfff && ((uint32) (uint16) *data) >= 0xdc00)
99 ++data;
100
101 return *this;
102 }
103
106 {
107 auto n = (uint32) (uint16) (*--data);
108
109 if (n >= 0xdc00 && n <= 0xdfff)
110 --data;
111
112 return *this;
113 }
114
118 {
119 auto n = (uint32) (uint16) *data++;
120
121 if (n >= 0xd800 && n <= 0xdfff && ((uint32) (uint16) *data) >= 0xdc00)
122 n = 0x10000 + ((((n - 0xd800) << 10) | (((uint32) (uint16) *data++) - 0xdc00)));
123
124 return (juce_wchar) n;
125 }
126
129 {
130 auto temp (*this);
131 ++*this;
132 return temp;
133 }
134
136 void operator+= (int numToSkip) noexcept
137 {
138 if (numToSkip < 0)
139 {
140 while (++numToSkip <= 0)
141 --*this;
142 }
143 else
144 {
145 while (--numToSkip >= 0)
146 ++*this;
147 }
148 }
149
151 void operator-= (int numToSkip) noexcept
152 {
154 }
155
158 {
159 auto p (*this);
160 p += characterIndex;
161 return *p;
162 }
163
166 {
167 auto p (*this);
168 p += numToSkip;
169 return p;
170 }
171
174 {
175 auto p (*this);
176 p += -numToSkip;
177 return p;
178 }
179
182 {
183 if (charToWrite >= 0x10000)
184 {
185 charToWrite -= 0x10000;
186 *data++ = (CharType) (0xd800 + (charToWrite >> 10));
187 *data++ = (CharType) (0xdc00 + (charToWrite & 0x3ff));
188 }
189 else
190 {
191 *data++ = (CharType) charToWrite;
192 }
193 }
194
196 inline void writeNull() const noexcept
197 {
198 *data = 0;
199 }
200
202 size_t length() const noexcept
203 {
204 auto* d = data;
205 size_t count = 0;
206
207 for (;;)
208 {
209 auto n = (uint32) (uint16) *d++;
210
211 if (n >= 0xd800 && n <= 0xdfff)
212 {
213 if (*d++ == 0)
214 break;
215 }
216 else if (n == 0)
217 break;
218
219 ++count;
220 }
221
222 return count;
223 }
224
226 size_t lengthUpTo (size_t maxCharsToCount) const noexcept
227 {
229 }
230
232 size_t lengthUpTo (CharPointer_UTF16 end) const noexcept
233 {
234 return CharacterFunctions::lengthUpTo (*this, end);
235 }
236
240 size_t sizeInBytes() const noexcept
241 {
242 return sizeof (CharType) * (findNullIndex (data) + 1);
243 }
244
249 {
250 return (charToWrite >= 0x10000) ? (sizeof (CharType) * 2) : sizeof (CharType);
251 }
252
257 template <class CharPointer>
258 static size_t getBytesRequiredFor (CharPointer text) noexcept
259 {
260 size_t count = 0;
261 juce_wchar n;
262
263 while ((n = text.getAndAdvance()) != 0)
264 count += getBytesRequiredFor (n);
265
266 return count;
267 }
268
271 {
272 auto* t = data;
273
274 while (*t != 0)
275 ++t;
276
277 return CharPointer_UTF16 (t);
278 }
279
281 template <typename CharPointer>
282 void writeAll (CharPointer src) noexcept
283 {
284 CharacterFunctions::copyAll (*this, src);
285 }
286
288 void writeAll (CharPointer_UTF16 src) noexcept
289 {
290 auto* s = src.data;
291
292 while ((*data = *s) != 0)
293 {
294 ++data;
295 ++s;
296 }
297 }
298
303 template <typename CharPointer>
304 size_t writeWithDestByteLimit (CharPointer src, size_t maxDestBytes) noexcept
305 {
307 }
308
313 template <typename CharPointer>
314 void writeWithCharLimit (CharPointer src, int maxChars) noexcept
315 {
316 CharacterFunctions::copyWithCharLimit (*this, src, maxChars);
317 }
318
320 template <typename CharPointer>
321 int compare (CharPointer other) const noexcept
322 {
323 return CharacterFunctions::compare (*this, other);
324 }
325
327 template <typename CharPointer>
328 int compareUpTo (CharPointer other, int maxChars) const noexcept
329 {
330 return CharacterFunctions::compareUpTo (*this, other, maxChars);
331 }
332
334 template <typename CharPointer>
336 {
338 }
339
341 template <typename CharPointer>
342 int compareIgnoreCaseUpTo (CharPointer other, int maxChars) const noexcept
343 {
344 return CharacterFunctions::compareIgnoreCaseUpTo (*this, other, maxChars);
345 }
346
347 #if JUCE_MSVC && ! defined (DOXYGEN)
348 int compareIgnoreCase (CharPointer_UTF16 other) const noexcept
349 {
350 return _wcsicmp (data, other.data);
351 }
352
353 int compareIgnoreCaseUpTo (CharPointer_UTF16 other, int maxChars) const noexcept
354 {
355 return _wcsnicmp (data, other.data, (size_t) maxChars);
356 }
357
358 int indexOf (CharPointer_UTF16 stringToFind) const noexcept
359 {
360 const CharType* const t = wcsstr (data, stringToFind.getAddress());
361 return t == nullptr ? -1 : (int) (t - data);
362 }
363 #endif
364
366 template <typename CharPointer>
367 int indexOf (CharPointer stringToFind) const noexcept
368 {
370 }
371
373 int indexOf (juce_wchar charToFind) const noexcept
374 {
376 }
377
379 int indexOf (juce_wchar charToFind, bool ignoreCase) const noexcept
380 {
381 return ignoreCase ? CharacterFunctions::indexOfCharIgnoreCase (*this, charToFind)
383 }
384
386 bool isWhitespace() const noexcept { return CharacterFunctions::isWhitespace (operator*()) != 0; }
388 bool isDigit() const noexcept { return CharacterFunctions::isDigit (operator*()) != 0; }
390 bool isLetter() const noexcept { return CharacterFunctions::isLetter (operator*()) != 0; }
392 bool isLetterOrDigit() const noexcept { return CharacterFunctions::isLetterOrDigit (operator*()) != 0; }
394 bool isUpperCase() const noexcept { return CharacterFunctions::isUpperCase (operator*()) != 0; }
396 bool isLowerCase() const noexcept { return CharacterFunctions::isLowerCase (operator*()) != 0; }
397
399 juce_wchar toUpperCase() const noexcept { return CharacterFunctions::toUpperCase (operator*()); }
401 juce_wchar toLowerCase() const noexcept { return CharacterFunctions::toLowerCase (operator*()); }
402
404 int getIntValue32() const noexcept
405 {
406 #if JUCE_MSVC
407 return _wtoi (data);
408 #else
409 return CharacterFunctions::getIntValue<int, CharPointer_UTF16> (*this);
410 #endif
411 }
412
415 {
416 #if JUCE_MSVC
417 return _wtoi64 (data);
418 #else
419 return CharacterFunctions::getIntValue<int64, CharPointer_UTF16> (*this);
420 #endif
421 }
422
424 double getDoubleValue() const noexcept { return CharacterFunctions::getDoubleValue (*this); }
425
428
431
433 static bool canRepresent (juce_wchar character) noexcept
434 {
435 auto n = (uint32) character;
436 return n < 0x10ffff && (n < 0xd800 || n > 0xdfff);
437 }
438
440 static bool isValidString (const CharType* dataToTest, int maxBytesToRead)
441 {
442 maxBytesToRead /= (int) sizeof (CharType);
443
444 while (--maxBytesToRead >= 0 && *dataToTest != 0)
445 {
446 auto n = (uint32) (uint16) *dataToTest++;
447
448 if (n >= 0xd800)
449 {
450 if (n > 0x10ffff)
451 return false;
452
453 if (n <= 0xdfff)
454 {
455 if (n > 0xdc00)
456 return false;
457
458 auto nextChar = (uint32) (uint16) *dataToTest++;
459
461 return false;
462 }
463 }
464 }
465
466 return true;
467 }
468
471 {
472 return CharPointer_UTF16 (reinterpret_cast<Atomic<CharType*>&> (data).exchange (newValue.data));
473 }
474
476 enum
477 {
478 byteOrderMarkBE1 = 0xfe,
479 byteOrderMarkBE2 = 0xff,
480 byteOrderMarkLE1 = 0xff,
481 byteOrderMarkLE2 = 0xfe
482 };
483
487 static bool isByteOrderMarkBigEndian (const void* possibleByteOrder) noexcept
488 {
489 JUCE_BEGIN_IGNORE_WARNINGS_MSVC (28182)
490 jassert (possibleByteOrder != nullptr);
491 auto c = static_cast<const uint8*> (possibleByteOrder);
492
493 return c[0] == (uint8) byteOrderMarkBE1
494 && c[1] == (uint8) byteOrderMarkBE2;
495 JUCE_END_IGNORE_WARNINGS_MSVC
496 }
497
501 static bool isByteOrderMarkLittleEndian (const void* possibleByteOrder) noexcept
502 {
503 JUCE_BEGIN_IGNORE_WARNINGS_MSVC (28182)
504 jassert (possibleByteOrder != nullptr);
505 auto c = static_cast<const uint8*> (possibleByteOrder);
506
507 return c[0] == (uint8) byteOrderMarkLE1
508 && c[1] == (uint8) byteOrderMarkLE2;
509 JUCE_END_IGNORE_WARNINGS_MSVC
510 }
511
512private:
513 CharType* data;
514
515 static unsigned int findNullIndex (const CharType* t) noexcept
516 {
517 unsigned int n = 0;
518
519 while (t[n] != 0)
520 ++n;
521
522 return n;
523 }
524};
525
526} // namespace juce
Wraps a pointer to a null-terminated UTF-16 character string, and provides various methods to operate...
juce_wchar toUpperCase() const noexcept
Returns an upper-case version of the first character of this string.
int getIntValue32() const noexcept
Parses this string as a 32-bit integer.
juce_wchar operator*() const noexcept
Returns the unicode character that this pointer is pointing to.
CharPointer_UTF16 operator-(int numToSkip) const noexcept
Returns a pointer which is moved backwards from this one by the specified number of characters.
bool isNotEmpty() const noexcept
Returns true if this pointer is not pointing to a null character.
CharPointer_UTF16 operator++() noexcept
Moves this pointer along to the next character in the string.
size_t writeWithDestByteLimit(CharPointer src, size_t maxDestBytes) noexcept
Copies a source string to this pointer, advancing this pointer as it goes.
static size_t getBytesRequiredFor(juce_wchar charToWrite) noexcept
Returns the number of bytes that would be needed to represent the given unicode character in this enc...
void writeAll(CharPointer src) noexcept
Copies a source string to this pointer, advancing this pointer as it goes.
static bool canRepresent(juce_wchar character) noexcept
Returns true if the given unicode character can be represented in this encoding.
bool operator==(CharPointer_UTF16 other) const noexcept
This is a pointer comparison, it doesn't compare the actual text.
void writeNull() const noexcept
Writes a null character to this string (leaving the pointer's position unchanged).
juce_wchar toLowerCase() const noexcept
Returns a lower-case version of the first character of this string.
bool isUpperCase() const noexcept
Returns true if the first character of this string is upper-case.
int compareIgnoreCaseUpTo(CharPointer other, int maxChars) const noexcept
Compares this string with another one, up to a specified number of characters.
int64 getIntValue64() const noexcept
Parses this string as a 64-bit integer.
int compare(CharPointer other) const noexcept
Compares this string with another one.
bool isLetter() const noexcept
Returns true if the first character of this string is a letter.
size_t lengthUpTo(size_t maxCharsToCount) const noexcept
Returns the number of characters in this string, or the given value, whichever is lower.
CharPointer_UTF16 atomicSwap(CharPointer_UTF16 newValue)
Atomically swaps this pointer for a new value, returning the previous value.
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...
CharPointer_UTF16 operator--() noexcept
Moves this pointer back to the previous character in the string.
juce_wchar operator[](int characterIndex) const noexcept
Returns the character at a given character index from the start of the string.
double getDoubleValue() const noexcept
Parses this string as a floating point double.
size_t length() const noexcept
Returns the number of characters in this string.
CharPointer_UTF16 findTerminatingNull() const noexcept
Returns a pointer to the null character that terminates this string.
size_t lengthUpTo(CharPointer_UTF16 end) const noexcept
Returns the number of characters in this string, or up to the given end pointer, whichever is lower.
int compareIgnoreCase(CharPointer other) const noexcept
Compares this string with another one.
CharType * getAddress() const noexcept
Returns the address that this pointer is pointing to.
size_t sizeInBytes() const noexcept
Returns the number of bytes that are used to represent this string.
static bool isByteOrderMarkBigEndian(const void *possibleByteOrder) noexcept
Returns true if the first pair of bytes in this pointer are the UTF16 byte-order mark (big endian).
CharPointer_UTF16 operator+(int numToSkip) const noexcept
Returns a pointer which is moved forwards from this one by the specified number of characters.
CharPointer_UTF16 findEndOfWhitespace() const noexcept
Returns the first non-whitespace character in the string.
juce_wchar getAndAdvance() noexcept
Returns the character that this pointer is currently pointing to, and then advances the pointer to po...
bool isDigit() const noexcept
Returns true if the first character of this string is a digit.
int compareUpTo(CharPointer other, int maxChars) const noexcept
Compares this string with another one, up to a specified number of characters.
void writeAll(CharPointer_UTF16 src) noexcept
Copies a source string to this pointer, advancing this pointer as it goes.
bool isLowerCase() const noexcept
Returns true if the first character of this string is lower-case.
void incrementToEndOfWhitespace() noexcept
Move this pointer to the first non-whitespace character in the string.
bool isEmpty() const noexcept
Returns true if this pointer is pointing to a null character.
int indexOf(juce_wchar charToFind) const noexcept
Returns the character index of a unicode character, or -1 if it isn't found.
void writeWithCharLimit(CharPointer src, int maxChars) noexcept
Copies a source string to this pointer, advancing this pointer as it goes.
bool isLetterOrDigit() const noexcept
Returns true if the first character of this string is a letter or digit.
int indexOf(CharPointer stringToFind) const noexcept
Returns the character index of a substring, or -1 if it isn't found.
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 isValidString(const CharType *dataToTest, int maxBytesToRead)
Returns true if this data contains a valid string in this encoding.
bool isWhitespace() const noexcept
Returns true if the first character of this string is whitespace.
void operator-=(int numToSkip) noexcept
Moves this pointer backwards by the specified number of characters.
static bool isByteOrderMarkLittleEndian(const void *possibleByteOrder) noexcept
Returns true if the first pair of bytes in this pointer are the UTF16 byte-order mark (little endian)...
void operator+=(int numToSkip) noexcept
Moves this pointer forwards by the specified number of characters.
int indexOf(juce_wchar charToFind, bool ignoreCase) const noexcept
Returns the character index of a unicode character, or -1 if it isn't found.
static void incrementToEndOfWhitespace(Type &text) noexcept
Increments a pointer until it points to the first non-whitespace character in a string.
static int compare(juce_wchar char1, juce_wchar char2) noexcept
Compares two characters.
static juce_wchar toLowerCase(juce_wchar character) noexcept
Converts a character to lower-case.
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.
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 bool isDigit(char character) noexcept
Checks whether a character is a digit.
static int compareIgnoreCaseUpTo(CharPointerType1 s1, CharPointerType2 s2, int maxChars) noexcept
Compares two null-terminated character strings, using a case-independent match.
static int indexOfChar(Type text, const juce_wchar charToFind) noexcept
Finds the character index of a given character in another string.
static int compareIgnoreCase(juce_wchar char1, juce_wchar char2) noexcept
Compares two characters, using a case-independant match.
static bool isLowerCase(juce_wchar character) noexcept
Checks whether a unicode character is lower-case.
static bool isLetter(char character) noexcept
Checks whether a character is alphabetic.
static int indexOf(CharPointerType1 textToSearch, const CharPointerType2 substringToLookFor) noexcept
Finds the character index of a given substring in another string.
static bool isWhitespace(char character) noexcept
Checks whether a character is whitespace.
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 Type findEndOfWhitespace(Type text) noexcept
Returns a pointer to the first non-whitespace character in a 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 bool isLetterOrDigit(char character) noexcept
Checks whether a character is alphabetic or numeric.
static juce_wchar toUpperCase(juce_wchar character) noexcept
Converts a character to upper-case.
static bool isUpperCase(juce_wchar character) noexcept
Checks whether a unicode character is upper-case.
static double getDoubleValue(CharPointerType text) noexcept
Parses a character string, to read a floating-point value.
static void copyAll(DestCharPointerType &dest, SrcCharPointerType src) noexcept
Copies null-terminated characters from one string to another.
static int compareUpTo(CharPointerType1 s1, CharPointerType2 s2, int maxChars) noexcept
Compares two null-terminated character strings, up to a given number of characters.
#define jassert(expression)
Platform-independent assertion macro.
typedef int
JUCE Namespace.
unsigned short uint16
A platform-independent 16-bit unsigned integer type.
wchar_t juce_wchar
A platform-independent 32-bit unicode character type.
RangedDirectoryIterator end(const RangedDirectoryIterator &)
Returns a default-constructed sentinel value.
signed short int16
A platform-independent 16-bit signed integer type.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
unsigned int uint32
A platform-independent 32-bit unsigned integer type.
unsigned char uint8
A platform-independent 8-bit unsigned integer type.
long long int64
A platform-independent 64-bit integer type.
A simple wrapper around std::atomic.
Definition juce_Atomic.h:42
wcsstr