OpenShot Library | OpenShotAudio  0.2.2
juce_CharPointer_ASCII.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 ASCII character string, and provides
33  various methods to operate on the data.
34 
35  A valid ASCII string is assumed to not contain any characters above 127.
36 
37  @see CharPointer_UTF8, CharPointer_UTF16, CharPointer_UTF32
38 
39  @tags{Core}
40 */
41 class CharPointer_ASCII final
42 {
43 public:
44  using CharType = char;
45 
46  inline explicit CharPointer_ASCII (const CharType* rawPointer) noexcept
47  : data (const_cast<CharType*> (rawPointer))
48  {
49  }
50 
51  inline CharPointer_ASCII (const CharPointer_ASCII& other) = default;
52 
53  inline CharPointer_ASCII operator= (const CharPointer_ASCII other) noexcept
54  {
55  data = other.data;
56  return *this;
57  }
58 
59  inline CharPointer_ASCII operator= (const CharType* text) noexcept
60  {
61  data = const_cast<CharType*> (text);
62  return *this;
63  }
64 
65  /** This is a pointer comparison, it doesn't compare the actual text. */
66  inline bool operator== (CharPointer_ASCII other) const noexcept { return data == other.data; }
67  inline bool operator!= (CharPointer_ASCII other) const noexcept { return data != other.data; }
68  inline bool operator<= (CharPointer_ASCII other) const noexcept { return data <= other.data; }
69  inline bool operator< (CharPointer_ASCII other) const noexcept { return data < other.data; }
70  inline bool operator>= (CharPointer_ASCII other) const noexcept { return data >= other.data; }
71  inline bool operator> (CharPointer_ASCII other) const noexcept { return data > other.data; }
72 
73  /** Returns the address that this pointer is pointing to. */
74  inline CharType* getAddress() const noexcept { return data; }
75 
76  /** Returns the address that this pointer is pointing to. */
77  inline operator const CharType*() const noexcept { return data; }
78 
79  /** Returns true if this pointer is pointing to a null character. */
80  inline bool isEmpty() const noexcept { return *data == 0; }
81 
82  /** Returns true if this pointer is not pointing to a null character. */
83  inline bool isNotEmpty() const noexcept { return *data != 0; }
84 
85  /** Returns the unicode character that this pointer is pointing to. */
86  inline juce_wchar operator*() const noexcept { return (juce_wchar) (uint8) *data; }
87 
88  /** Moves this pointer along to the next character in the string. */
89  inline CharPointer_ASCII operator++() noexcept
90  {
91  ++data;
92  return *this;
93  }
94 
95  /** Moves this pointer to the previous character in the string. */
96  inline CharPointer_ASCII operator--() noexcept
97  {
98  --data;
99  return *this;
100  }
101 
102  /** Returns the character that this pointer is currently pointing to, and then
103  advances the pointer to point to the next character. */
104  inline juce_wchar getAndAdvance() noexcept { return (juce_wchar) (uint8) *data++; }
105 
106  /** Moves this pointer along to the next character in the string. */
108  {
109  auto temp (*this);
110  ++data;
111  return temp;
112  }
113 
114  /** Moves this pointer forwards by the specified number of characters. */
115  inline void operator+= (const int numToSkip) noexcept
116  {
117  data += numToSkip;
118  }
119 
120  inline void operator-= (const int numToSkip) noexcept
121  {
122  data -= numToSkip;
123  }
124 
125  /** Returns the character at a given character index from the start of the string. */
126  inline juce_wchar operator[] (const int characterIndex) const noexcept
127  {
128  return (juce_wchar) (uint8) data [characterIndex];
129  }
130 
131  /** Returns a pointer which is moved forwards from this one by the specified number of characters. */
132  CharPointer_ASCII operator+ (const int numToSkip) const noexcept
133  {
134  return CharPointer_ASCII (data + numToSkip);
135  }
136 
137  /** Returns a pointer which is moved backwards from this one by the specified number of characters. */
138  CharPointer_ASCII operator- (const int numToSkip) const noexcept
139  {
140  return CharPointer_ASCII (data - numToSkip);
141  }
142 
143  /** Writes a unicode character to this string, and advances this pointer to point to the next position. */
144  inline void write (const juce_wchar charToWrite) noexcept
145  {
146  *data++ = (char) charToWrite;
147  }
148 
149  inline void replaceChar (const juce_wchar newChar) noexcept
150  {
151  *data = (char) newChar;
152  }
153 
154  /** Writes a null character to this string (leaving the pointer's position unchanged). */
155  inline void writeNull() const noexcept
156  {
157  *data = 0;
158  }
159 
160  /** Returns the number of characters in this string. */
161  size_t length() const noexcept
162  {
163  return (size_t) strlen (data);
164  }
165 
166  /** Returns the number of characters in this string, or the given value, whichever is lower. */
167  size_t lengthUpTo (const size_t maxCharsToCount) const noexcept
168  {
169  return CharacterFunctions::lengthUpTo (*this, maxCharsToCount);
170  }
171 
172  /** Returns the number of characters in this string, or up to the given end pointer, whichever is lower. */
173  size_t lengthUpTo (const CharPointer_ASCII end) const noexcept
174  {
175  return CharacterFunctions::lengthUpTo (*this, end);
176  }
177 
178  /** Returns the number of bytes that are used to represent this string.
179  This includes the terminating null character.
180  */
181  size_t sizeInBytes() const noexcept
182  {
183  return length() + 1;
184  }
185 
186  /** Returns the number of bytes that would be needed to represent the given
187  unicode character in this encoding format.
188  */
189  static inline size_t getBytesRequiredFor (const juce_wchar) noexcept
190  {
191  return 1;
192  }
193 
194  /** Returns the number of bytes that would be needed to represent the given
195  string in this encoding format.
196  The value returned does NOT include the terminating null character.
197  */
198  template <class CharPointer>
199  static size_t getBytesRequiredFor (const CharPointer text) noexcept
200  {
201  return text.length();
202  }
203 
204  /** Returns a pointer to the null character that terminates this string. */
206  {
207  return CharPointer_ASCII (data + length());
208  }
209 
210  /** Copies a source string to this pointer, advancing this pointer as it goes. */
211  template <typename CharPointer>
212  void writeAll (const CharPointer src) noexcept
213  {
214  CharacterFunctions::copyAll (*this, src);
215  }
216 
217  /** Copies a source string to this pointer, advancing this pointer as it goes.
218  The maxDestBytes parameter specifies the maximum number of bytes that can be written
219  to the destination buffer before stopping.
220  */
221  template <typename CharPointer>
222  size_t writeWithDestByteLimit (const CharPointer src, const size_t maxDestBytes) noexcept
223  {
224  return CharacterFunctions::copyWithDestByteLimit (*this, src, maxDestBytes);
225  }
226 
227  /** Copies a source string to this pointer, advancing this pointer as it goes.
228  The maxChars parameter specifies the maximum number of characters that can be
229  written to the destination buffer before stopping (including the terminating null).
230  */
231  template <typename CharPointer>
232  void writeWithCharLimit (const CharPointer src, const int maxChars) noexcept
233  {
234  CharacterFunctions::copyWithCharLimit (*this, src, maxChars);
235  }
236 
237  /** Compares this string with another one. */
238  template <typename CharPointer>
239  int compare (const CharPointer other) const noexcept
240  {
241  return CharacterFunctions::compare (*this, other);
242  }
243 
244  /** Compares this string with another one. */
245  int compare (const CharPointer_ASCII other) const noexcept
246  {
247  return strcmp (data, other.data);
248  }
249 
250  /** Compares this string with another one, up to a specified number of characters. */
251  template <typename CharPointer>
252  int compareUpTo (const CharPointer other, const int maxChars) const noexcept
253  {
254  return CharacterFunctions::compareUpTo (*this, other, maxChars);
255  }
256 
257  /** Compares this string with another one, up to a specified number of characters. */
258  int compareUpTo (const CharPointer_ASCII other, const int maxChars) const noexcept
259  {
260  return strncmp (data, other.data, (size_t) maxChars);
261  }
262 
263  /** Compares this string with another one. */
264  template <typename CharPointer>
265  int compareIgnoreCase (const CharPointer other) const
266  {
267  return CharacterFunctions::compareIgnoreCase (*this, other);
268  }
269 
270  int compareIgnoreCase (const CharPointer_ASCII other) const
271  {
272  #if JUCE_MINGW || (JUCE_WINDOWS && JUCE_CLANG)
273  return CharacterFunctions::compareIgnoreCase (*this, other);
274  #elif JUCE_WINDOWS
275  return stricmp (data, other.data);
276  #else
277  return strcasecmp (data, other.data);
278  #endif
279  }
280 
281  /** Compares this string with another one, up to a specified number of characters. */
282  template <typename CharPointer>
283  int compareIgnoreCaseUpTo (const CharPointer other, const int maxChars) const noexcept
284  {
285  return CharacterFunctions::compareIgnoreCaseUpTo (*this, other, maxChars);
286  }
287 
288  /** Returns the character index of a substring, or -1 if it isn't found. */
289  template <typename CharPointer>
290  int indexOf (const CharPointer stringToFind) const noexcept
291  {
292  return CharacterFunctions::indexOf (*this, stringToFind);
293  }
294 
295  /** Returns the character index of a unicode character, or -1 if it isn't found. */
296  int indexOf (const juce_wchar charToFind) const noexcept
297  {
298  int i = 0;
299 
300  while (data[i] != 0)
301  {
302  if (data[i] == (char) charToFind)
303  return i;
304 
305  ++i;
306  }
307 
308  return -1;
309  }
310 
311  /** Returns the character index of a unicode character, or -1 if it isn't found. */
312  int indexOf (const juce_wchar charToFind, const bool ignoreCase) const noexcept
313  {
314  return ignoreCase ? CharacterFunctions::indexOfCharIgnoreCase (*this, charToFind)
315  : CharacterFunctions::indexOfChar (*this, charToFind);
316  }
317 
318  /** Returns true if the first character of this string is whitespace. */
319  bool isWhitespace() const { return CharacterFunctions::isWhitespace (*data) != 0; }
320  /** Returns true if the first character of this string is a digit. */
321  bool isDigit() const { return CharacterFunctions::isDigit (*data) != 0; }
322  /** Returns true if the first character of this string is a letter. */
323  bool isLetter() const { return CharacterFunctions::isLetter (*data) != 0; }
324  /** Returns true if the first character of this string is a letter or digit. */
325  bool isLetterOrDigit() const { return CharacterFunctions::isLetterOrDigit (*data) != 0; }
326  /** Returns true if the first character of this string is upper-case. */
327  bool isUpperCase() const { return CharacterFunctions::isUpperCase ((juce_wchar) (uint8) *data) != 0; }
328  /** Returns true if the first character of this string is lower-case. */
329  bool isLowerCase() const { return CharacterFunctions::isLowerCase ((juce_wchar) (uint8) *data) != 0; }
330 
331  /** Returns an upper-case version of the first character of this string. */
332  juce_wchar toUpperCase() const noexcept { return CharacterFunctions::toUpperCase ((juce_wchar) (uint8) *data); }
333  /** Returns a lower-case version of the first character of this string. */
334  juce_wchar toLowerCase() const noexcept { return CharacterFunctions::toLowerCase ((juce_wchar) (uint8) *data); }
335 
336  /** Parses this string as a 32-bit integer. */
337  int getIntValue32() const noexcept { return atoi (data); }
338 
339  /** Parses this string as a 64-bit integer. */
340  int64 getIntValue64() const noexcept
341  {
342  #if JUCE_LINUX || JUCE_ANDROID || JUCE_MINGW
343  return atoll (data);
344  #elif JUCE_WINDOWS
345  return _atoi64 (data);
346  #else
347  return CharacterFunctions::getIntValue <int64, CharPointer_ASCII> (*this);
348  #endif
349  }
350 
351  /** Parses this string as a floating point double. */
352  double getDoubleValue() const noexcept { return CharacterFunctions::getDoubleValue (*this); }
353 
354  /** Returns the first non-whitespace character in the string. */
356 
357  /** Returns true if the given unicode character can be represented in this encoding. */
358  static bool canRepresent (juce_wchar character) noexcept
359  {
360  return ((unsigned int) character) < (unsigned int) 128;
361  }
362 
363  /** Returns true if this data contains a valid string in this encoding. */
364  static bool isValidString (const CharType* dataToTest, int maxBytesToRead)
365  {
366  while (--maxBytesToRead >= 0)
367  {
368  if (((signed char) *dataToTest) <= 0)
369  return *dataToTest == 0;
370 
371  ++dataToTest;
372  }
373 
374  return true;
375  }
376 
377 private:
378  CharType* data;
379 };
380 
381 } // namespace juce
382 
383 /** @}*/
Wraps a pointer to a null-terminated ASCII character string, and provides various methods to operate ...
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 isLetter() const
Returns true if the first character of this string is a letter.
size_t length() const noexcept
Returns the number of characters in this string.
static bool isValidString(const CharType *dataToTest, int maxBytesToRead)
Returns true if this data contains a valid string in this encoding.
size_t sizeInBytes() const noexcept
Returns the number of bytes that are used to represent this string.
CharType * getAddress() const noexcept
Returns the address that this pointer is pointing to.
CharPointer_ASCII operator+(const int numToSkip) const noexcept
Returns a pointer which is moved forwards from this one by the specified number of characters.
size_t lengthUpTo(const CharPointer_ASCII end) const noexcept
Returns the number of characters in this string, or up to the given end pointer, whichever is lower.
bool isNotEmpty() const noexcept
Returns true if this pointer is not pointing to a null character.
void writeAll(const CharPointer src) noexcept
Copies a source string to this pointer, advancing this pointer as it goes.
static size_t getBytesRequiredFor(const juce_wchar) noexcept
Returns the number of bytes that would be needed to represent the given unicode character in this enc...
int compareIgnoreCaseUpTo(const CharPointer other, const int maxChars) const noexcept
Compares this string with another one, up to a specified number of characters.
CharPointer_ASCII operator++() noexcept
Moves this pointer along to the next character in the string.
bool isWhitespace() const
Returns true if the first character of this string is whitespace.
int compareIgnoreCase(const CharPointer other) const
Compares this string with another one.
CharPointer_ASCII operator--() noexcept
Moves this pointer to the previous character in the string.
bool isLetterOrDigit() const
Returns true if the first character of this string is a letter or digit.
static size_t getBytesRequiredFor(const CharPointer text) noexcept
Returns the number of bytes that would be needed to represent the given string in this encoding forma...
juce_wchar toUpperCase() const noexcept
Returns an upper-case version of the first character of this string.
int compareUpTo(const CharPointer_ASCII other, const int maxChars) const noexcept
Compares this string with another one, up to a specified number of characters.
int compare(const CharPointer_ASCII other) const noexcept
Compares this string with another one.
bool isLowerCase() const
Returns true if the first character of this string is lower-case.
juce_wchar toLowerCase() const noexcept
Returns a lower-case version of the first character of this string.
int indexOf(const juce_wchar charToFind, const bool ignoreCase) const noexcept
Returns the character index of a unicode character, or -1 if it isn't found.
void writeNull() const noexcept
Writes a null character to this string (leaving the pointer's position unchanged).
void write(const 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 isUpperCase() const
Returns true if the first character of this string is upper-case.
juce_wchar operator[](const int characterIndex) const noexcept
Returns the character at a given character index from the start of the string.
CharPointer_ASCII operator-(const int numToSkip) const noexcept
Returns a pointer which is moved backwards from this one by the specified number of characters.
int indexOf(const CharPointer stringToFind) const noexcept
Returns the character index of a substring, or -1 if it isn't found.
CharPointer_ASCII findEndOfWhitespace() const noexcept
Returns the first non-whitespace character in the string.
juce_wchar operator*() const noexcept
Returns the unicode character that this pointer is pointing to.
void operator+=(const int numToSkip) noexcept
Moves this pointer forwards by the specified number of characters.
int compare(const CharPointer other) const noexcept
Compares this string with another one.
int indexOf(const juce_wchar charToFind) const noexcept
Returns the character index of a unicode character, or -1 if it isn't found.
bool isEmpty() const noexcept
Returns true if this pointer is pointing to a null character.
juce_wchar getAndAdvance() noexcept
Returns the character that this pointer is currently pointing to, and then advances the pointer to po...
void writeWithCharLimit(const CharPointer src, const int maxChars) noexcept
Copies a source string to this pointer, advancing this pointer as it goes.
size_t writeWithDestByteLimit(const CharPointer src, const size_t maxDestBytes) noexcept
Copies a source string to this pointer, advancing this pointer as it goes.
bool operator==(CharPointer_ASCII other) const noexcept
This is a pointer comparison, it doesn't compare the actual text.
int compareUpTo(const CharPointer other, const 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.
CharPointer_ASCII findTerminatingNull() const noexcept
Returns a pointer to the null character that terminates this string.
size_t lengthUpTo(const 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 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 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 isWhitespace(char character) noexcept
Checks whether a character is whitespace.
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.