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.

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