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.

649 lines
22KB

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