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.

606 lines
20KB

  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. const int maxSignificantDigits = 17 + 1; // An additional digit for rounding
  106. const int bufferSize = maxSignificantDigits + 7 + 1; // -.E-XXX and a trailing null-terminator
  107. char buffer[bufferSize] = {};
  108. char* currentCharacter = &(buffer[0]);
  109. int numSigFigs = 0;
  110. bool decimalPointFound = false;
  111. text = text.findEndOfWhitespace();
  112. auto c = *text;
  113. switch (c)
  114. {
  115. case '-': *currentCharacter++ = '-'; // 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. int digit = (int) text.getAndAdvance() - '0';
  136. if (numSigFigs >= maxSignificantDigits
  137. || ((numSigFigs == 0 && (! decimalPointFound)) && digit == 0))
  138. continue;
  139. *currentCharacter++ = '0' + (char) digit;
  140. numSigFigs++;
  141. }
  142. else if ((! decimalPointFound) && *text == '.')
  143. {
  144. ++text;
  145. *currentCharacter++ = '.';
  146. decimalPointFound = true;
  147. }
  148. else
  149. {
  150. break;
  151. }
  152. }
  153. c = *text;
  154. if ((c == 'e' || c == 'E') && numSigFigs > 0)
  155. {
  156. *currentCharacter++ = 'e';
  157. switch (*++text)
  158. {
  159. case '-': *currentCharacter++ = '-'; // Fall-through..
  160. case '+': ++text;
  161. }
  162. int exponentMagnitude = 0;
  163. while (text.isDigit())
  164. {
  165. if (currentCharacter == &buffer[bufferSize - 1])
  166. return std::numeric_limits<double>::quiet_NaN();
  167. auto digit = (int) text.getAndAdvance() - '0';
  168. if (digit != 0 || exponentMagnitude != 0)
  169. {
  170. *currentCharacter++ = '0' + (char) digit;
  171. exponentMagnitude = (exponentMagnitude * 10) + digit;
  172. }
  173. }
  174. if (exponentMagnitude > std::numeric_limits<double>::max_exponent10)
  175. return std::numeric_limits<double>::quiet_NaN();
  176. }
  177. return strtod (&buffer[0], nullptr);
  178. }
  179. /** Parses a character string, to read a floating-point value. */
  180. template <typename CharPointerType>
  181. static double getDoubleValue (CharPointerType text) noexcept
  182. {
  183. return readDoubleValue (text);
  184. }
  185. //==============================================================================
  186. /** Parses a character string, to read an integer value. */
  187. template <typename IntType, typename CharPointerType>
  188. static IntType getIntValue (const CharPointerType text) noexcept
  189. {
  190. IntType v = 0;
  191. auto s = text.findEndOfWhitespace();
  192. const bool isNeg = *s == '-';
  193. if (isNeg)
  194. ++s;
  195. for (;;)
  196. {
  197. auto c = s.getAndAdvance();
  198. if (c >= '0' && c <= '9')
  199. v = v * 10 + (IntType) (c - '0');
  200. else
  201. break;
  202. }
  203. return isNeg ? -v : v;
  204. }
  205. template <typename ResultType>
  206. struct HexParser
  207. {
  208. template <typename CharPointerType>
  209. static ResultType parse (CharPointerType t) noexcept
  210. {
  211. ResultType result = 0;
  212. while (! t.isEmpty())
  213. {
  214. auto hexValue = CharacterFunctions::getHexDigitValue (t.getAndAdvance());
  215. if (hexValue >= 0)
  216. result = (result << 4) | hexValue;
  217. }
  218. return result;
  219. }
  220. };
  221. //==============================================================================
  222. /** Counts the number of characters in a given string, stopping if the count exceeds
  223. a specified limit. */
  224. template <typename CharPointerType>
  225. static size_t lengthUpTo (CharPointerType text, const size_t maxCharsToCount) noexcept
  226. {
  227. size_t len = 0;
  228. while (len < maxCharsToCount && text.getAndAdvance() != 0)
  229. ++len;
  230. return len;
  231. }
  232. /** Counts the number of characters in a given string, stopping if the count exceeds
  233. a specified end-pointer. */
  234. template <typename CharPointerType>
  235. static size_t lengthUpTo (CharPointerType start, const CharPointerType end) noexcept
  236. {
  237. size_t len = 0;
  238. while (start < end && start.getAndAdvance() != 0)
  239. ++len;
  240. return len;
  241. }
  242. /** Copies null-terminated characters from one string to another. */
  243. template <typename DestCharPointerType, typename SrcCharPointerType>
  244. static void copyAll (DestCharPointerType& dest, SrcCharPointerType src) noexcept
  245. {
  246. while (juce_wchar c = src.getAndAdvance())
  247. dest.write (c);
  248. dest.writeNull();
  249. }
  250. /** Copies characters from one string to another, up to a null terminator
  251. or a given byte size limit. */
  252. template <typename DestCharPointerType, typename SrcCharPointerType>
  253. static size_t copyWithDestByteLimit (DestCharPointerType& dest, SrcCharPointerType src, size_t maxBytesToWrite) noexcept
  254. {
  255. auto startAddress = dest.getAddress();
  256. auto maxBytes = (ssize_t) maxBytesToWrite;
  257. maxBytes -= sizeof (typename DestCharPointerType::CharType); // (allow for a terminating null)
  258. for (;;)
  259. {
  260. auto c = src.getAndAdvance();
  261. auto bytesNeeded = DestCharPointerType::getBytesRequiredFor (c);
  262. maxBytes -= bytesNeeded;
  263. if (c == 0 || maxBytes < 0)
  264. break;
  265. dest.write (c);
  266. }
  267. dest.writeNull();
  268. return (size_t) getAddressDifference (dest.getAddress(), startAddress)
  269. + sizeof (typename DestCharPointerType::CharType);
  270. }
  271. /** Copies characters from one string to another, up to a null terminator
  272. or a given maximum number of characters. */
  273. template <typename DestCharPointerType, typename SrcCharPointerType>
  274. static void copyWithCharLimit (DestCharPointerType& dest, SrcCharPointerType src, int maxChars) noexcept
  275. {
  276. while (--maxChars > 0)
  277. {
  278. auto c = src.getAndAdvance();
  279. if (c == 0)
  280. break;
  281. dest.write (c);
  282. }
  283. dest.writeNull();
  284. }
  285. /** Compares two characters. */
  286. static inline int compare (juce_wchar char1, juce_wchar char2) noexcept
  287. {
  288. if (auto diff = static_cast<int> (char1) - static_cast<int> (char2))
  289. return diff < 0 ? -1 : 1;
  290. return 0;
  291. }
  292. /** Compares two null-terminated character strings. */
  293. template <typename CharPointerType1, typename CharPointerType2>
  294. static int compare (CharPointerType1 s1, CharPointerType2 s2) noexcept
  295. {
  296. for (;;)
  297. {
  298. auto c1 = s1.getAndAdvance();
  299. if (auto diff = compare (c1, s2.getAndAdvance()))
  300. return diff;
  301. if (c1 == 0)
  302. break;
  303. }
  304. return 0;
  305. }
  306. /** Compares two null-terminated character strings, up to a given number of characters. */
  307. template <typename CharPointerType1, typename CharPointerType2>
  308. static int compareUpTo (CharPointerType1 s1, CharPointerType2 s2, int maxChars) noexcept
  309. {
  310. while (--maxChars >= 0)
  311. {
  312. auto c1 = s1.getAndAdvance();
  313. if (auto diff = compare (c1, s2.getAndAdvance()))
  314. return diff;
  315. if (c1 == 0)
  316. break;
  317. }
  318. return 0;
  319. }
  320. /** Compares two characters, using a case-independant match. */
  321. static inline int compareIgnoreCase (juce_wchar char1, juce_wchar char2) noexcept
  322. {
  323. return char1 != char2 ? compare (toUpperCase (char1), toUpperCase (char2)) : 0;
  324. }
  325. /** Compares two null-terminated character strings, using a case-independant match. */
  326. template <typename CharPointerType1, typename CharPointerType2>
  327. static int compareIgnoreCase (CharPointerType1 s1, CharPointerType2 s2) noexcept
  328. {
  329. for (;;)
  330. {
  331. auto c1 = s1.getAndAdvance();
  332. if (auto diff = compareIgnoreCase (c1, s2.getAndAdvance()))
  333. return diff;
  334. if (c1 == 0)
  335. break;
  336. }
  337. return 0;
  338. }
  339. /** Compares two null-terminated character strings, using a case-independent match. */
  340. template <typename CharPointerType1, typename CharPointerType2>
  341. static int compareIgnoreCaseUpTo (CharPointerType1 s1, CharPointerType2 s2, int maxChars) noexcept
  342. {
  343. while (--maxChars >= 0)
  344. {
  345. auto c1 = s1.getAndAdvance();
  346. if (auto diff = compareIgnoreCase (c1, s2.getAndAdvance()))
  347. return diff;
  348. if (c1 == 0)
  349. break;
  350. }
  351. return 0;
  352. }
  353. /** Finds the character index of a given substring in another string.
  354. Returns -1 if the substring is not found.
  355. */
  356. template <typename CharPointerType1, typename CharPointerType2>
  357. static int indexOf (CharPointerType1 textToSearch, const CharPointerType2 substringToLookFor) noexcept
  358. {
  359. int index = 0;
  360. auto substringLength = (int) substringToLookFor.length();
  361. for (;;)
  362. {
  363. if (textToSearch.compareUpTo (substringToLookFor, substringLength) == 0)
  364. return index;
  365. if (textToSearch.getAndAdvance() == 0)
  366. return -1;
  367. ++index;
  368. }
  369. }
  370. /** Returns a pointer to the first occurrence of a substring in a string.
  371. If the substring is not found, this will return a pointer to the string's
  372. null terminator.
  373. */
  374. template <typename CharPointerType1, typename CharPointerType2>
  375. static CharPointerType1 find (CharPointerType1 textToSearch, const CharPointerType2 substringToLookFor) noexcept
  376. {
  377. auto substringLength = (int) substringToLookFor.length();
  378. while (textToSearch.compareUpTo (substringToLookFor, substringLength) != 0
  379. && ! textToSearch.isEmpty())
  380. ++textToSearch;
  381. return textToSearch;
  382. }
  383. /** Returns a pointer to the first occurrence of a substring in a string.
  384. If the substring is not found, this will return a pointer to the string's
  385. null terminator.
  386. */
  387. template <typename CharPointerType>
  388. static CharPointerType find (CharPointerType textToSearch, const juce_wchar charToLookFor) noexcept
  389. {
  390. for (;; ++textToSearch)
  391. {
  392. auto c = *textToSearch;
  393. if (c == charToLookFor || c == 0)
  394. break;
  395. }
  396. return textToSearch;
  397. }
  398. /** Finds the character index of a given substring in another string, using
  399. a case-independent match.
  400. Returns -1 if the substring is not found.
  401. */
  402. template <typename CharPointerType1, typename CharPointerType2>
  403. static int indexOfIgnoreCase (CharPointerType1 haystack, const CharPointerType2 needle) noexcept
  404. {
  405. int index = 0;
  406. auto needleLength = (int) needle.length();
  407. for (;;)
  408. {
  409. if (haystack.compareIgnoreCaseUpTo (needle, needleLength) == 0)
  410. return index;
  411. if (haystack.getAndAdvance() == 0)
  412. return -1;
  413. ++index;
  414. }
  415. }
  416. /** Finds the character index of a given character in another string.
  417. Returns -1 if the character is not found.
  418. */
  419. template <typename Type>
  420. static int indexOfChar (Type text, const juce_wchar charToFind) noexcept
  421. {
  422. int i = 0;
  423. while (! text.isEmpty())
  424. {
  425. if (text.getAndAdvance() == charToFind)
  426. return i;
  427. ++i;
  428. }
  429. return -1;
  430. }
  431. /** Finds the character index of a given character in another string, using
  432. a case-independent match.
  433. Returns -1 if the character is not found.
  434. */
  435. template <typename Type>
  436. static int indexOfCharIgnoreCase (Type text, juce_wchar charToFind) noexcept
  437. {
  438. charToFind = CharacterFunctions::toLowerCase (charToFind);
  439. int i = 0;
  440. while (! text.isEmpty())
  441. {
  442. if (text.toLowerCase() == charToFind)
  443. return i;
  444. ++text;
  445. ++i;
  446. }
  447. return -1;
  448. }
  449. /** Returns a pointer to the first non-whitespace character in a string.
  450. If the string contains only whitespace, this will return a pointer
  451. to its null terminator.
  452. */
  453. template <typename Type>
  454. static Type findEndOfWhitespace (Type text) noexcept
  455. {
  456. while (text.isWhitespace())
  457. ++text;
  458. return text;
  459. }
  460. /** Returns a pointer to the first character in the string which is found in
  461. the breakCharacters string.
  462. */
  463. template <typename Type, typename BreakType>
  464. static Type findEndOfToken (Type text, BreakType breakCharacters, Type quoteCharacters)
  465. {
  466. juce_wchar currentQuoteChar = 0;
  467. while (! text.isEmpty())
  468. {
  469. auto c = text.getAndAdvance();
  470. if (currentQuoteChar == 0 && breakCharacters.indexOf (c) >= 0)
  471. {
  472. --text;
  473. break;
  474. }
  475. if (quoteCharacters.indexOf (c) >= 0)
  476. {
  477. if (currentQuoteChar == 0)
  478. currentQuoteChar = c;
  479. else if (currentQuoteChar == c)
  480. currentQuoteChar = 0;
  481. }
  482. }
  483. return text;
  484. }
  485. private:
  486. static double mulexp10 (double value, int exponent) noexcept;
  487. };