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.

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