The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

641 lines
21KB

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