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.

CharPointer_UTF8.h 19KB

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