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.

624 lines
21KB

  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_CHARACTERFUNCTIONS_H_INCLUDED
  24. #define JUCE_CHARACTERFUNCTIONS_H_INCLUDED
  25. /** This macro will be set to 1 if the compiler's native wchar_t is an 8-bit type. */
  26. #define JUCE_NATIVE_WCHAR_IS_UTF8 1
  27. namespace water {
  28. /** A platform-independent 32-bit unicode character type. */
  29. typedef uint32 juce_wchar;
  30. //==============================================================================
  31. /**
  32. A collection of functions for manipulating characters and character strings.
  33. Most of these methods are designed for internal use by the String and CharPointer
  34. classes, but some of them may be useful to call directly.
  35. @see String, CharPointer_UTF8, CharPointer_UTF16, CharPointer_UTF32
  36. */
  37. class CharacterFunctions
  38. {
  39. public:
  40. //==============================================================================
  41. /** Converts a character to upper-case. */
  42. static juce_wchar toUpperCase (juce_wchar character) noexcept;
  43. /** Converts a character to lower-case. */
  44. static juce_wchar toLowerCase (juce_wchar character) noexcept;
  45. /** Checks whether a unicode character is upper-case. */
  46. static bool isUpperCase (juce_wchar character) noexcept;
  47. /** Checks whether a unicode character is lower-case. */
  48. static bool isLowerCase (juce_wchar character) noexcept;
  49. /** Checks whether a character is whitespace. */
  50. static bool isWhitespace (char character) noexcept;
  51. /** Checks whether a character is whitespace. */
  52. static bool isWhitespace (juce_wchar character) noexcept;
  53. /** Checks whether a character is a digit. */
  54. static bool isDigit (char character) noexcept;
  55. /** Checks whether a character is a digit. */
  56. static bool isDigit (juce_wchar character) noexcept;
  57. /** Checks whether a character is alphabetic. */
  58. static bool isLetter (char character) noexcept;
  59. /** Checks whether a character is alphabetic. */
  60. static bool isLetter (juce_wchar character) noexcept;
  61. /** Checks whether a character is alphabetic or numeric. */
  62. static bool isLetterOrDigit (char character) noexcept;
  63. /** Checks whether a character is alphabetic or numeric. */
  64. static bool isLetterOrDigit (juce_wchar character) noexcept;
  65. /** Checks whether a character is a printable character, i.e. alphabetic, numeric,
  66. a punctuation character or a space.
  67. */
  68. static bool isPrintable (char character) noexcept;
  69. /** Checks whether a character is a printable character, i.e. alphabetic, numeric,
  70. a punctuation character or a space.
  71. */
  72. static bool isPrintable (juce_wchar character) noexcept;
  73. /** Returns 0 to 16 for '0' to 'F", or -1 for characters that aren't a legal hex digit. */
  74. static int getHexDigitValue (juce_wchar digit) noexcept;
  75. /** Converts a byte of Windows 1252 codepage to unicode. */
  76. static juce_wchar getUnicodeCharFromWindows1252Codepage (uint8 windows1252Char) noexcept;
  77. //==============================================================================
  78. /** Parses a character string to read a floating-point number.
  79. Note that this will advance the pointer that is passed in, leaving it at
  80. the end of the number.
  81. */
  82. template <typename CharPointerType>
  83. static double readDoubleValue (CharPointerType& text) noexcept
  84. {
  85. double result[3] = { 0 }, accumulator[2] = { 0 };
  86. int exponentAdjustment[2] = { 0 }, exponentAccumulator[2] = { -1, -1 };
  87. int exponent = 0, decPointIndex = 0, digit = 0;
  88. int lastDigit = 0, numSignificantDigits = 0;
  89. bool isNegative = false, digitsFound = false;
  90. const int maxSignificantDigits = 15 + 2;
  91. text = text.findEndOfWhitespace();
  92. juce_wchar c = *text;
  93. switch (c)
  94. {
  95. case '-': isNegative = true; // fall-through..
  96. case '+': c = *++text;
  97. }
  98. switch (c)
  99. {
  100. case 'n':
  101. case 'N':
  102. if ((text[1] == 'a' || text[1] == 'A') && (text[2] == 'n' || text[2] == 'N'))
  103. return std::numeric_limits<double>::quiet_NaN();
  104. break;
  105. case 'i':
  106. case 'I':
  107. if ((text[1] == 'n' || text[1] == 'N') && (text[2] == 'f' || text[2] == 'F'))
  108. return std::numeric_limits<double>::infinity();
  109. break;
  110. }
  111. for (;;)
  112. {
  113. if (text.isDigit())
  114. {
  115. lastDigit = digit;
  116. digit = (int) text.getAndAdvance() - '0';
  117. digitsFound = true;
  118. if (decPointIndex != 0)
  119. exponentAdjustment[1]++;
  120. if (numSignificantDigits == 0 && digit == 0)
  121. continue;
  122. if (++numSignificantDigits > maxSignificantDigits)
  123. {
  124. if (digit > 5)
  125. ++accumulator [decPointIndex];
  126. else if (digit == 5 && (lastDigit & 1) != 0)
  127. ++accumulator [decPointIndex];
  128. if (decPointIndex > 0)
  129. exponentAdjustment[1]--;
  130. else
  131. exponentAdjustment[0]++;
  132. while (text.isDigit())
  133. {
  134. ++text;
  135. if (decPointIndex == 0)
  136. exponentAdjustment[0]++;
  137. }
  138. }
  139. else
  140. {
  141. const double maxAccumulatorValue = (double) ((std::numeric_limits<unsigned int>::max() - 9) / 10);
  142. if (accumulator [decPointIndex] > maxAccumulatorValue)
  143. {
  144. result [decPointIndex] = mulexp10 (result [decPointIndex], exponentAccumulator [decPointIndex])
  145. + accumulator [decPointIndex];
  146. accumulator [decPointIndex] = 0;
  147. exponentAccumulator [decPointIndex] = 0;
  148. }
  149. accumulator [decPointIndex] = accumulator[decPointIndex] * 10 + digit;
  150. exponentAccumulator [decPointIndex]++;
  151. }
  152. }
  153. else if (decPointIndex == 0 && *text == '.')
  154. {
  155. ++text;
  156. decPointIndex = 1;
  157. if (numSignificantDigits > maxSignificantDigits)
  158. {
  159. while (text.isDigit())
  160. ++text;
  161. break;
  162. }
  163. }
  164. else
  165. {
  166. break;
  167. }
  168. }
  169. result[0] = mulexp10 (result[0], exponentAccumulator[0]) + accumulator[0];
  170. if (decPointIndex != 0)
  171. result[1] = mulexp10 (result[1], exponentAccumulator[1]) + accumulator[1];
  172. c = *text;
  173. if ((c == 'e' || c == 'E') && digitsFound)
  174. {
  175. bool negativeExponent = false;
  176. switch (*++text)
  177. {
  178. case '-': negativeExponent = true; // fall-through..
  179. case '+': ++text;
  180. }
  181. while (text.isDigit())
  182. exponent = (exponent * 10) + ((int) text.getAndAdvance() - '0');
  183. if (negativeExponent)
  184. exponent = -exponent;
  185. }
  186. double r = mulexp10 (result[0], exponent + exponentAdjustment[0]);
  187. if (decPointIndex != 0)
  188. r += mulexp10 (result[1], exponent - exponentAdjustment[1]);
  189. return isNegative ? -r : r;
  190. }
  191. /** Parses a character string, to read a floating-point value. */
  192. template <typename CharPointerType>
  193. static double getDoubleValue (CharPointerType text) noexcept
  194. {
  195. return readDoubleValue (text);
  196. }
  197. //==============================================================================
  198. /** Parses a character string, to read an integer value. */
  199. template <typename IntType, typename CharPointerType>
  200. static IntType getIntValue (const CharPointerType text) noexcept
  201. {
  202. IntType v = 0;
  203. CharPointerType s (text.findEndOfWhitespace());
  204. const bool isNeg = *s == '-';
  205. if (isNeg)
  206. ++s;
  207. for (;;)
  208. {
  209. const juce_wchar c = s.getAndAdvance();
  210. if (c >= '0' && c <= '9')
  211. v = v * 10 + (IntType) (c - '0');
  212. else
  213. break;
  214. }
  215. return isNeg ? -v : v;
  216. }
  217. template <typename ResultType>
  218. struct HexParser
  219. {
  220. template <typename CharPointerType>
  221. static ResultType parse (CharPointerType t) noexcept
  222. {
  223. ResultType result = 0;
  224. while (! t.isEmpty())
  225. {
  226. const int hexValue = CharacterFunctions::getHexDigitValue (t.getAndAdvance());
  227. if (hexValue >= 0)
  228. result = (result << 4) | hexValue;
  229. }
  230. return result;
  231. }
  232. };
  233. //==============================================================================
  234. /** Counts the number of characters in a given string, stopping if the count exceeds
  235. a specified limit. */
  236. template <typename CharPointerType>
  237. static size_t lengthUpTo (CharPointerType text, const size_t maxCharsToCount) noexcept
  238. {
  239. size_t len = 0;
  240. while (len < maxCharsToCount && text.getAndAdvance() != 0)
  241. ++len;
  242. return len;
  243. }
  244. /** Counts the number of characters in a given string, stopping if the count exceeds
  245. a specified end-pointer. */
  246. template <typename CharPointerType>
  247. static size_t lengthUpTo (CharPointerType start, const CharPointerType end) noexcept
  248. {
  249. size_t len = 0;
  250. while (start < end && start.getAndAdvance() != 0)
  251. ++len;
  252. return len;
  253. }
  254. /** Copies null-terminated characters from one string to another. */
  255. template <typename DestCharPointerType, typename SrcCharPointerType>
  256. static void copyAll (DestCharPointerType& dest, SrcCharPointerType src) noexcept
  257. {
  258. while (juce_wchar c = src.getAndAdvance())
  259. dest.write (c);
  260. dest.writeNull();
  261. }
  262. /** Copies characters from one string to another, up to a null terminator
  263. or a given byte size limit. */
  264. template <typename DestCharPointerType, typename SrcCharPointerType>
  265. static size_t copyWithDestByteLimit (DestCharPointerType& dest, SrcCharPointerType src, size_t maxBytesToWrite) noexcept
  266. {
  267. typename DestCharPointerType::CharType const* const startAddress = dest.getAddress();
  268. ssize_t maxBytes = (ssize_t) maxBytesToWrite;
  269. maxBytes -= sizeof (typename DestCharPointerType::CharType); // (allow for a terminating null)
  270. for (;;)
  271. {
  272. const juce_wchar c = src.getAndAdvance();
  273. const size_t bytesNeeded = DestCharPointerType::getBytesRequiredFor (c);
  274. maxBytes -= bytesNeeded;
  275. if (c == 0 || maxBytes < 0)
  276. break;
  277. dest.write (c);
  278. }
  279. dest.writeNull();
  280. return (size_t) getAddressDifference (dest.getAddress(), startAddress)
  281. + sizeof (typename DestCharPointerType::CharType);
  282. }
  283. /** Copies characters from one string to another, up to a null terminator
  284. or a given maximum number of characters. */
  285. template <typename DestCharPointerType, typename SrcCharPointerType>
  286. static void copyWithCharLimit (DestCharPointerType& dest, SrcCharPointerType src, int maxChars) noexcept
  287. {
  288. while (--maxChars > 0)
  289. {
  290. const juce_wchar c = src.getAndAdvance();
  291. if (c == 0)
  292. break;
  293. dest.write (c);
  294. }
  295. dest.writeNull();
  296. }
  297. /** Compares two characters. */
  298. static inline int compare (juce_wchar char1, juce_wchar char2) noexcept
  299. {
  300. if (int diff = static_cast<int> (char1) - static_cast<int> (char2))
  301. return diff < 0 ? -1 : 1;
  302. return 0;
  303. }
  304. /** Compares two null-terminated character strings. */
  305. template <typename CharPointerType1, typename CharPointerType2>
  306. static int compare (CharPointerType1 s1, CharPointerType2 s2) noexcept
  307. {
  308. for (;;)
  309. {
  310. const juce_wchar c1 = s1.getAndAdvance();
  311. if (int diff = compare (c1, s2.getAndAdvance()))
  312. return diff;
  313. if (c1 == 0)
  314. break;
  315. }
  316. return 0;
  317. }
  318. /** Compares two null-terminated character strings, up to a given number of characters. */
  319. template <typename CharPointerType1, typename CharPointerType2>
  320. static int compareUpTo (CharPointerType1 s1, CharPointerType2 s2, int maxChars) noexcept
  321. {
  322. while (--maxChars >= 0)
  323. {
  324. const juce_wchar c1 = s1.getAndAdvance();
  325. if (int diff = compare (c1, s2.getAndAdvance()))
  326. return diff;
  327. if (c1 == 0)
  328. break;
  329. }
  330. return 0;
  331. }
  332. /** Compares two characters, using a case-independant match. */
  333. static inline int compareIgnoreCase (juce_wchar char1, juce_wchar char2) noexcept
  334. {
  335. return char1 != char2 ? compare (toUpperCase (char1), toUpperCase (char2)) : 0;
  336. }
  337. /** Compares two null-terminated character strings, using a case-independant match. */
  338. template <typename CharPointerType1, typename CharPointerType2>
  339. static int compareIgnoreCase (CharPointerType1 s1, CharPointerType2 s2) noexcept
  340. {
  341. for (;;)
  342. {
  343. const juce_wchar c1 = s1.getAndAdvance();
  344. if (int diff = compareIgnoreCase (c1, s2.getAndAdvance()))
  345. return diff;
  346. if (c1 == 0)
  347. break;
  348. }
  349. return 0;
  350. }
  351. /** Compares two null-terminated character strings, using a case-independent match. */
  352. template <typename CharPointerType1, typename CharPointerType2>
  353. static int compareIgnoreCaseUpTo (CharPointerType1 s1, CharPointerType2 s2, int maxChars) noexcept
  354. {
  355. while (--maxChars >= 0)
  356. {
  357. const juce_wchar c1 = s1.getAndAdvance();
  358. if (int diff = compareIgnoreCase (c1, s2.getAndAdvance()))
  359. return diff;
  360. if (c1 == 0)
  361. break;
  362. }
  363. return 0;
  364. }
  365. /** Finds the character index of a given substring in another string.
  366. Returns -1 if the substring is not found.
  367. */
  368. template <typename CharPointerType1, typename CharPointerType2>
  369. static int indexOf (CharPointerType1 textToSearch, const CharPointerType2 substringToLookFor) noexcept
  370. {
  371. int index = 0;
  372. const int substringLength = (int) substringToLookFor.length();
  373. for (;;)
  374. {
  375. if (textToSearch.compareUpTo (substringToLookFor, substringLength) == 0)
  376. return index;
  377. if (textToSearch.getAndAdvance() == 0)
  378. return -1;
  379. ++index;
  380. }
  381. }
  382. /** Returns a pointer to the first occurrence of a substring in a string.
  383. If the substring is not found, this will return a pointer to the string's
  384. null terminator.
  385. */
  386. template <typename CharPointerType1, typename CharPointerType2>
  387. static CharPointerType1 find (CharPointerType1 textToSearch, const CharPointerType2 substringToLookFor) noexcept
  388. {
  389. const int substringLength = (int) substringToLookFor.length();
  390. while (textToSearch.compareUpTo (substringToLookFor, substringLength) != 0
  391. && ! textToSearch.isEmpty())
  392. ++textToSearch;
  393. return textToSearch;
  394. }
  395. /** Returns a pointer to the first occurrence of a substring in a string.
  396. If the substring is not found, this will return a pointer to the string's
  397. null terminator.
  398. */
  399. template <typename CharPointerType>
  400. static CharPointerType find (CharPointerType textToSearch, const juce_wchar charToLookFor) noexcept
  401. {
  402. for (;; ++textToSearch)
  403. {
  404. const juce_wchar c = *textToSearch;
  405. if (c == charToLookFor || c == 0)
  406. break;
  407. }
  408. return textToSearch;
  409. }
  410. /** Finds the character index of a given substring in another string, using
  411. a case-independent match.
  412. Returns -1 if the substring is not found.
  413. */
  414. template <typename CharPointerType1, typename CharPointerType2>
  415. static int indexOfIgnoreCase (CharPointerType1 haystack, const CharPointerType2 needle) noexcept
  416. {
  417. int index = 0;
  418. const int needleLength = (int) needle.length();
  419. for (;;)
  420. {
  421. if (haystack.compareIgnoreCaseUpTo (needle, needleLength) == 0)
  422. return index;
  423. if (haystack.getAndAdvance() == 0)
  424. return -1;
  425. ++index;
  426. }
  427. }
  428. /** Finds the character index of a given character in another string.
  429. Returns -1 if the character is not found.
  430. */
  431. template <typename Type>
  432. static int indexOfChar (Type text, const juce_wchar charToFind) noexcept
  433. {
  434. int i = 0;
  435. while (! text.isEmpty())
  436. {
  437. if (text.getAndAdvance() == charToFind)
  438. return i;
  439. ++i;
  440. }
  441. return -1;
  442. }
  443. /** Finds the character index of a given character in another string, using
  444. a case-independent match.
  445. Returns -1 if the character is not found.
  446. */
  447. template <typename Type>
  448. static int indexOfCharIgnoreCase (Type text, juce_wchar charToFind) noexcept
  449. {
  450. charToFind = CharacterFunctions::toLowerCase (charToFind);
  451. int i = 0;
  452. while (! text.isEmpty())
  453. {
  454. if (text.toLowerCase() == charToFind)
  455. return i;
  456. ++text;
  457. ++i;
  458. }
  459. return -1;
  460. }
  461. /** Returns a pointer to the first non-whitespace character in a string.
  462. If the string contains only whitespace, this will return a pointer
  463. to its null terminator.
  464. */
  465. template <typename Type>
  466. static Type findEndOfWhitespace (Type text) noexcept
  467. {
  468. while (text.isWhitespace())
  469. ++text;
  470. return text;
  471. }
  472. /** Returns a pointer to the first character in the string which is found in
  473. the breakCharacters string.
  474. */
  475. template <typename Type, typename BreakType>
  476. static Type findEndOfToken (Type text, const BreakType breakCharacters, const Type quoteCharacters)
  477. {
  478. juce_wchar currentQuoteChar = 0;
  479. while (! text.isEmpty())
  480. {
  481. const juce_wchar c = text.getAndAdvance();
  482. if (currentQuoteChar == 0 && breakCharacters.indexOf (c) >= 0)
  483. {
  484. --text;
  485. break;
  486. }
  487. if (quoteCharacters.indexOf (c) >= 0)
  488. {
  489. if (currentQuoteChar == 0)
  490. currentQuoteChar = c;
  491. else if (currentQuoteChar == c)
  492. currentQuoteChar = 0;
  493. }
  494. }
  495. return text;
  496. }
  497. private:
  498. static double mulexp10 (const double value, int exponent) noexcept;
  499. };
  500. }
  501. #endif // JUCE_CHARACTERFUNCTIONS_H_INCLUDED