Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

573 lines
19KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #ifndef JUCE_CHARPOINTER_UTF8_H_INCLUDED
  24. #define JUCE_CHARPOINTER_UTF8_H_INCLUDED
  25. //==============================================================================
  26. /**
  27. Wraps a pointer to a null-terminated UTF-8 character string, and provides
  28. various methods to operate on the data.
  29. @see CharPointer_UTF16, CharPointer_UTF32
  30. */
  31. class CharPointer_UTF8
  32. {
  33. public:
  34. typedef char CharType;
  35. inline explicit CharPointer_UTF8 (const CharType* const rawPointer) noexcept
  36. : data (const_cast<CharType*> (rawPointer))
  37. {
  38. }
  39. inline CharPointer_UTF8 (const CharPointer_UTF8& other) noexcept
  40. : data (other.data)
  41. {
  42. }
  43. inline CharPointer_UTF8 operator= (CharPointer_UTF8 other) noexcept
  44. {
  45. data = other.data;
  46. return *this;
  47. }
  48. inline CharPointer_UTF8 operator= (const CharType* text) noexcept
  49. {
  50. data = const_cast<CharType*> (text);
  51. return *this;
  52. }
  53. /** This is a pointer comparison, it doesn't compare the actual text. */
  54. inline bool operator== (CharPointer_UTF8 other) const noexcept { return data == other.data; }
  55. inline bool operator!= (CharPointer_UTF8 other) const noexcept { return data != other.data; }
  56. inline bool operator<= (CharPointer_UTF8 other) const noexcept { return data <= other.data; }
  57. inline bool operator< (CharPointer_UTF8 other) const noexcept { return data < other.data; }
  58. inline bool operator>= (CharPointer_UTF8 other) const noexcept { return data >= other.data; }
  59. inline bool operator> (CharPointer_UTF8 other) const noexcept { return data > other.data; }
  60. /** Returns the address that this pointer is pointing to. */
  61. inline CharType* getAddress() const noexcept { return data; }
  62. /** Returns the address that this pointer is pointing to. */
  63. inline operator const CharType*() const noexcept { return data; }
  64. /** Returns true if this pointer is pointing to a null character. */
  65. inline bool isEmpty() const noexcept { return *data == 0; }
  66. /** Returns the unicode character that this pointer is pointing to. */
  67. juce_wchar operator*() const noexcept
  68. {
  69. const signed char byte = (signed char) *data;
  70. if (byte >= 0)
  71. return (juce_wchar) (uint8) byte;
  72. uint32 n = (uint32) (uint8) byte;
  73. uint32 mask = 0x7f;
  74. uint32 bit = 0x40;
  75. int numExtraValues = 0;
  76. while ((n & bit) != 0 && bit > 0x8)
  77. {
  78. mask >>= 1;
  79. ++numExtraValues;
  80. bit >>= 1;
  81. }
  82. n &= mask;
  83. for (int i = 1; i <= numExtraValues; ++i)
  84. {
  85. const uint32 nextByte = (uint32) (uint8) data[i];
  86. if ((nextByte & 0xc0) != 0x80)
  87. break;
  88. n <<= 6;
  89. n |= (nextByte & 0x3f);
  90. }
  91. return (juce_wchar) n;
  92. }
  93. /** Moves this pointer along to the next character in the string. */
  94. CharPointer_UTF8& operator++() noexcept
  95. {
  96. jassert (*data != 0); // trying to advance past the end of the string?
  97. const signed char n = (signed char) *data++;
  98. if (n < 0)
  99. {
  100. juce_wchar bit = 0x40;
  101. while ((n & bit) != 0 && bit > 0x8)
  102. {
  103. ++data;
  104. bit >>= 1;
  105. }
  106. }
  107. return *this;
  108. }
  109. /** Moves this pointer back to the previous character in the string. */
  110. CharPointer_UTF8 operator--() noexcept
  111. {
  112. int count = 0;
  113. while ((*--data & 0xc0) == 0x80 && ++count < 4)
  114. {}
  115. return *this;
  116. }
  117. /** Returns the character that this pointer is currently pointing to, and then
  118. advances the pointer to point to the next character. */
  119. juce_wchar getAndAdvance() noexcept
  120. {
  121. const signed char byte = (signed char) *data++;
  122. if (byte >= 0)
  123. return (juce_wchar) (uint8) byte;
  124. uint32 n = (uint32) (uint8) byte;
  125. uint32 mask = 0x7f;
  126. uint32 bit = 0x40;
  127. int numExtraValues = 0;
  128. while ((n & bit) != 0 && bit > 0x8)
  129. {
  130. mask >>= 1;
  131. ++numExtraValues;
  132. bit >>= 1;
  133. }
  134. n &= mask;
  135. while (--numExtraValues >= 0)
  136. {
  137. const uint32 nextByte = (uint32) (uint8) *data;
  138. if ((nextByte & 0xc0) != 0x80)
  139. break;
  140. ++data;
  141. n <<= 6;
  142. n |= (nextByte & 0x3f);
  143. }
  144. return (juce_wchar) n;
  145. }
  146. /** Moves this pointer along to the next character in the string. */
  147. CharPointer_UTF8 operator++ (int) noexcept
  148. {
  149. CharPointer_UTF8 temp (*this);
  150. ++*this;
  151. return temp;
  152. }
  153. /** Moves this pointer forwards by the specified number of characters. */
  154. void operator+= (int numToSkip) noexcept
  155. {
  156. if (numToSkip < 0)
  157. {
  158. while (++numToSkip <= 0)
  159. --*this;
  160. }
  161. else
  162. {
  163. while (--numToSkip >= 0)
  164. ++*this;
  165. }
  166. }
  167. /** Moves this pointer backwards by the specified number of characters. */
  168. void operator-= (int numToSkip) noexcept
  169. {
  170. operator+= (-numToSkip);
  171. }
  172. /** Returns the character at a given character index from the start of the string. */
  173. juce_wchar operator[] (int characterIndex) const noexcept
  174. {
  175. CharPointer_UTF8 p (*this);
  176. p += characterIndex;
  177. return *p;
  178. }
  179. /** Returns a pointer which is moved forwards from this one by the specified number of characters. */
  180. CharPointer_UTF8 operator+ (int numToSkip) const noexcept
  181. {
  182. CharPointer_UTF8 p (*this);
  183. p += numToSkip;
  184. return p;
  185. }
  186. /** Returns a pointer which is moved backwards from this one by the specified number of characters. */
  187. CharPointer_UTF8 operator- (int numToSkip) const noexcept
  188. {
  189. CharPointer_UTF8 p (*this);
  190. p += -numToSkip;
  191. return p;
  192. }
  193. /** Returns the number of characters in this string. */
  194. size_t length() const noexcept
  195. {
  196. const CharType* d = data;
  197. size_t count = 0;
  198. for (;;)
  199. {
  200. const uint32 n = (uint32) (uint8) *d++;
  201. if ((n & 0x80) != 0)
  202. {
  203. while ((*d & 0xc0) == 0x80)
  204. ++d;
  205. }
  206. else if (n == 0)
  207. break;
  208. ++count;
  209. }
  210. return count;
  211. }
  212. /** Returns the number of characters in this string, or the given value, whichever is lower. */
  213. size_t lengthUpTo (const size_t maxCharsToCount) const noexcept
  214. {
  215. return CharacterFunctions::lengthUpTo (*this, maxCharsToCount);
  216. }
  217. /** Returns the number of characters in this string, or up to the given end pointer, whichever is lower. */
  218. size_t lengthUpTo (const CharPointer_UTF8 end) const noexcept
  219. {
  220. return CharacterFunctions::lengthUpTo (*this, end);
  221. }
  222. /** Returns the number of bytes that are used to represent this string.
  223. This includes the terminating null character.
  224. */
  225. size_t sizeInBytes() const noexcept
  226. {
  227. jassert (data != nullptr);
  228. return strlen (data) + 1;
  229. }
  230. /** Returns the number of bytes that would be needed to represent the given
  231. unicode character in this encoding format.
  232. */
  233. static size_t getBytesRequiredFor (const juce_wchar charToWrite) noexcept
  234. {
  235. size_t num = 1;
  236. const uint32 c = (uint32) charToWrite;
  237. if (c >= 0x80)
  238. {
  239. ++num;
  240. if (c >= 0x800)
  241. {
  242. ++num;
  243. if (c >= 0x10000)
  244. ++num;
  245. }
  246. }
  247. return num;
  248. }
  249. /** Returns the number of bytes that would be needed to represent the given
  250. string in this encoding format.
  251. The value returned does NOT include the terminating null character.
  252. */
  253. template <class CharPointer>
  254. static size_t getBytesRequiredFor (CharPointer text) noexcept
  255. {
  256. size_t count = 0;
  257. while (juce_wchar n = text.getAndAdvance())
  258. count += getBytesRequiredFor (n);
  259. return count;
  260. }
  261. /** Returns a pointer to the null character that terminates this string. */
  262. CharPointer_UTF8 findTerminatingNull() const noexcept
  263. {
  264. return CharPointer_UTF8 (data + strlen (data));
  265. }
  266. /** Writes a unicode character to this string, and advances this pointer to point to the next position. */
  267. void write (const juce_wchar charToWrite) noexcept
  268. {
  269. const uint32 c = (uint32) charToWrite;
  270. if (c >= 0x80)
  271. {
  272. int numExtraBytes = 1;
  273. if (c >= 0x800)
  274. {
  275. ++numExtraBytes;
  276. if (c >= 0x10000)
  277. ++numExtraBytes;
  278. }
  279. *data++ = (CharType) ((uint32) (0xff << (7 - numExtraBytes)) | (c >> (numExtraBytes * 6)));
  280. while (--numExtraBytes >= 0)
  281. *data++ = (CharType) (0x80 | (0x3f & (c >> (numExtraBytes * 6))));
  282. }
  283. else
  284. {
  285. *data++ = (CharType) c;
  286. }
  287. }
  288. /** Writes a null character to this string (leaving the pointer's position unchanged). */
  289. inline void writeNull() const noexcept
  290. {
  291. *data = 0;
  292. }
  293. /** Copies a source string to this pointer, advancing this pointer as it goes. */
  294. template <typename CharPointer>
  295. void writeAll (const CharPointer src) noexcept
  296. {
  297. CharacterFunctions::copyAll (*this, src);
  298. }
  299. /** Copies a source string to this pointer, advancing this pointer as it goes. */
  300. void writeAll (const CharPointer_UTF8 src) noexcept
  301. {
  302. const CharType* s = src.data;
  303. while ((*data = *s) != 0)
  304. {
  305. ++data;
  306. ++s;
  307. }
  308. }
  309. /** Copies a source string to this pointer, advancing this pointer as it goes.
  310. The maxDestBytes parameter specifies the maximum number of bytes that can be written
  311. to the destination buffer before stopping.
  312. */
  313. template <typename CharPointer>
  314. size_t writeWithDestByteLimit (const CharPointer src, const size_t maxDestBytes) noexcept
  315. {
  316. return CharacterFunctions::copyWithDestByteLimit (*this, src, maxDestBytes);
  317. }
  318. /** Copies a source string to this pointer, advancing this pointer as it goes.
  319. The maxChars parameter specifies the maximum number of characters that can be
  320. written to the destination buffer before stopping (including the terminating null).
  321. */
  322. template <typename CharPointer>
  323. void writeWithCharLimit (const CharPointer src, const int maxChars) noexcept
  324. {
  325. CharacterFunctions::copyWithCharLimit (*this, src, maxChars);
  326. }
  327. /** Compares this string with another one. */
  328. template <typename CharPointer>
  329. int compare (const CharPointer other) const noexcept
  330. {
  331. return CharacterFunctions::compare (*this, other);
  332. }
  333. /** Compares this string with another one, up to a specified number of characters. */
  334. template <typename CharPointer>
  335. int compareUpTo (const CharPointer other, const int maxChars) const noexcept
  336. {
  337. return CharacterFunctions::compareUpTo (*this, other, maxChars);
  338. }
  339. /** Compares this string with another one. */
  340. template <typename CharPointer>
  341. int compareIgnoreCase (const CharPointer other) const noexcept
  342. {
  343. return CharacterFunctions::compareIgnoreCase (*this, other);
  344. }
  345. /** Compares this string with another one. */
  346. int compareIgnoreCase (const CharPointer_UTF8 other) const noexcept
  347. {
  348. return CharacterFunctions::compareIgnoreCase (*this, other);
  349. }
  350. /** Compares this string with another one, up to a specified number of characters. */
  351. template <typename CharPointer>
  352. int compareIgnoreCaseUpTo (const CharPointer other, const int maxChars) const noexcept
  353. {
  354. return CharacterFunctions::compareIgnoreCaseUpTo (*this, other, maxChars);
  355. }
  356. /** Returns the character index of a substring, or -1 if it isn't found. */
  357. template <typename CharPointer>
  358. int indexOf (const CharPointer stringToFind) const noexcept
  359. {
  360. return CharacterFunctions::indexOf (*this, stringToFind);
  361. }
  362. /** Returns the character index of a unicode character, or -1 if it isn't found. */
  363. int indexOf (const juce_wchar charToFind) const noexcept
  364. {
  365. return CharacterFunctions::indexOfChar (*this, charToFind);
  366. }
  367. /** Returns the character index of a unicode character, or -1 if it isn't found. */
  368. int indexOf (const juce_wchar charToFind, const bool ignoreCase) const noexcept
  369. {
  370. return ignoreCase ? CharacterFunctions::indexOfCharIgnoreCase (*this, charToFind)
  371. : CharacterFunctions::indexOfChar (*this, charToFind);
  372. }
  373. /** Returns true if the first character of this string is whitespace. */
  374. bool isWhitespace() const noexcept { const CharType c = *data; return c == ' ' || (c <= 13 && c >= 9); }
  375. /** Returns true if the first character of this string is a digit. */
  376. bool isDigit() const noexcept { const CharType c = *data; return c >= '0' && c <= '9'; }
  377. /** Returns true if the first character of this string is a letter. */
  378. bool isLetter() const noexcept { return CharacterFunctions::isLetter (operator*()) != 0; }
  379. /** Returns true if the first character of this string is a letter or digit. */
  380. bool isLetterOrDigit() const noexcept { return CharacterFunctions::isLetterOrDigit (operator*()) != 0; }
  381. /** Returns true if the first character of this string is upper-case. */
  382. bool isUpperCase() const noexcept { return CharacterFunctions::isUpperCase (operator*()) != 0; }
  383. /** Returns true if the first character of this string is lower-case. */
  384. bool isLowerCase() const noexcept { return CharacterFunctions::isLowerCase (operator*()) != 0; }
  385. /** Returns an upper-case version of the first character of this string. */
  386. juce_wchar toUpperCase() const noexcept { return CharacterFunctions::toUpperCase (operator*()); }
  387. /** Returns a lower-case version of the first character of this string. */
  388. juce_wchar toLowerCase() const noexcept { return CharacterFunctions::toLowerCase (operator*()); }
  389. /** Parses this string as a 32-bit integer. */
  390. int getIntValue32() const noexcept { return atoi (data); }
  391. /** Parses this string as a 64-bit integer. */
  392. int64 getIntValue64() const noexcept
  393. {
  394. #if JUCE_LINUX || JUCE_ANDROID || JUCE_MINGW
  395. return atoll (data);
  396. #elif JUCE_WINDOWS
  397. return _atoi64 (data);
  398. #else
  399. return CharacterFunctions::getIntValue <int64, CharPointer_UTF8> (*this);
  400. #endif
  401. }
  402. /** Parses this string as a floating point double. */
  403. double getDoubleValue() const noexcept { return CharacterFunctions::getDoubleValue (*this); }
  404. /** Returns the first non-whitespace character in the string. */
  405. CharPointer_UTF8 findEndOfWhitespace() const noexcept { return CharacterFunctions::findEndOfWhitespace (*this); }
  406. /** Returns true if the given unicode character can be represented in this encoding. */
  407. static bool canRepresent (juce_wchar character) noexcept
  408. {
  409. return ((unsigned int) character) < (unsigned int) 0x10ffff;
  410. }
  411. /** Returns true if this data contains a valid string in this encoding. */
  412. static bool isValidString (const CharType* dataToTest, int maxBytesToRead)
  413. {
  414. while (--maxBytesToRead >= 0 && *dataToTest != 0)
  415. {
  416. const signed char byte = (signed char) *dataToTest++;
  417. if (byte < 0)
  418. {
  419. int bit = 0x40;
  420. int numExtraValues = 0;
  421. while ((byte & bit) != 0)
  422. {
  423. if (bit < 8)
  424. return false;
  425. ++numExtraValues;
  426. bit >>= 1;
  427. if (bit == 8 && (numExtraValues > maxBytesToRead
  428. || *CharPointer_UTF8 (dataToTest - 1) > 0x10ffff))
  429. return false;
  430. }
  431. if (numExtraValues == 0)
  432. return false;
  433. maxBytesToRead -= numExtraValues;
  434. if (maxBytesToRead < 0)
  435. return false;
  436. while (--numExtraValues >= 0)
  437. if ((*dataToTest++ & 0xc0) != 0x80)
  438. return false;
  439. }
  440. }
  441. return true;
  442. }
  443. /** Atomically swaps this pointer for a new value, returning the previous value. */
  444. CharPointer_UTF8 atomicSwap (const CharPointer_UTF8 newValue)
  445. {
  446. return CharPointer_UTF8 (reinterpret_cast<Atomic<CharType*>&> (data).exchange (newValue.data));
  447. }
  448. /** These values are the byte-order mark (BOM) values for a UTF-8 stream. */
  449. enum
  450. {
  451. byteOrderMark1 = 0xef,
  452. byteOrderMark2 = 0xbb,
  453. byteOrderMark3 = 0xbf
  454. };
  455. /** Returns true if the first three bytes in this pointer are the UTF8 byte-order mark (BOM).
  456. The pointer must not be null, and must point to at least 3 valid bytes.
  457. */
  458. static bool isByteOrderMark (const void* possibleByteOrder) noexcept
  459. {
  460. jassert (possibleByteOrder != nullptr);
  461. const uint8* const c = static_cast<const uint8*> (possibleByteOrder);
  462. return c[0] == (uint8) byteOrderMark1
  463. && c[1] == (uint8) byteOrderMark2
  464. && c[2] == (uint8) byteOrderMark3;
  465. }
  466. private:
  467. CharType* data;
  468. };
  469. #endif // JUCE_CHARPOINTER_UTF8_H_INCLUDED