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.

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