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.

856 lines
28KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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. using juce_wchar = wchar_t;
  35. #else
  36. using juce_wchar = uint32;
  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. #if ! DOXYGEN
  52. //==============================================================================
  53. // GNU libstdc++ does not have std::make_unsigned
  54. namespace internal
  55. {
  56. template <typename Type> struct make_unsigned { using type = Type; };
  57. template <> struct make_unsigned<signed char> { using type = unsigned char; };
  58. template <> struct make_unsigned<char> { using type = unsigned char; };
  59. template <> struct make_unsigned<short> { using type = unsigned short; };
  60. template <> struct make_unsigned<int> { using type = unsigned int; };
  61. template <> struct make_unsigned<long> { using type = unsigned long; };
  62. template <> struct make_unsigned<long long> { using type = unsigned long long; };
  63. }
  64. #endif
  65. //==============================================================================
  66. /**
  67. A collection of functions for manipulating characters and character strings.
  68. Most of these methods are designed for internal use by the String and CharPointer
  69. classes, but some of them may be useful to call directly.
  70. @see String, CharPointer_UTF8, CharPointer_UTF16, CharPointer_UTF32
  71. @tags{Core}
  72. */
  73. class JUCE_API CharacterFunctions
  74. {
  75. public:
  76. //==============================================================================
  77. /** Converts a character to upper-case. */
  78. static juce_wchar toUpperCase (juce_wchar character) noexcept;
  79. /** Converts a character to lower-case. */
  80. static juce_wchar toLowerCase (juce_wchar character) noexcept;
  81. /** Checks whether a unicode character is upper-case. */
  82. static bool isUpperCase (juce_wchar character) noexcept;
  83. /** Checks whether a unicode character is lower-case. */
  84. static bool isLowerCase (juce_wchar character) noexcept;
  85. /** Checks whether a character is whitespace. */
  86. static bool isWhitespace (char character) noexcept;
  87. /** Checks whether a character is whitespace. */
  88. static bool isWhitespace (juce_wchar character) noexcept;
  89. /** Checks whether a character is a digit. */
  90. static bool isDigit (char character) noexcept;
  91. /** Checks whether a character is a digit. */
  92. static bool isDigit (juce_wchar character) noexcept;
  93. /** Checks whether a character is alphabetic. */
  94. static bool isLetter (char character) noexcept;
  95. /** Checks whether a character is alphabetic. */
  96. static bool isLetter (juce_wchar character) noexcept;
  97. /** Checks whether a character is alphabetic or numeric. */
  98. static bool isLetterOrDigit (char character) noexcept;
  99. /** Checks whether a character is alphabetic or numeric. */
  100. static bool isLetterOrDigit (juce_wchar 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 (char character) noexcept;
  105. /** Checks whether a character is a printable character, i.e. alphabetic, numeric,
  106. a punctuation character or a space.
  107. */
  108. static bool isPrintable (juce_wchar character) noexcept;
  109. /** Returns 0 to 16 for '0' to 'F", or -1 for characters that aren't a legal hex digit. */
  110. static int getHexDigitValue (juce_wchar digit) noexcept;
  111. /** Converts a byte of Windows 1252 codepage to unicode. */
  112. static juce_wchar getUnicodeCharFromWindows1252Codepage (uint8 windows1252Char) noexcept;
  113. //==============================================================================
  114. /** Parses a character string to read a floating-point number.
  115. Note that this will advance the pointer that is passed in, leaving it at
  116. the end of the number.
  117. */
  118. template <typename CharPointerType>
  119. static double readDoubleValue (CharPointerType& text) noexcept
  120. {
  121. constexpr auto inf = std::numeric_limits<double>::infinity();
  122. bool isNegative = false;
  123. #if ! JUCE_MINGW
  124. constexpr const int maxSignificantDigits = 17 + 1; // An additional digit for rounding
  125. constexpr const int bufferSize = maxSignificantDigits + 7 + 1; // -.E-XXX and a trailing null-terminator
  126. char buffer[(size_t) bufferSize] = {};
  127. char* writePtr = &(buffer[0]);
  128. #endif
  129. const auto endOfWhitspace = text.findEndOfWhitespace();
  130. text = endOfWhitspace;
  131. auto c = *text;
  132. switch (c)
  133. {
  134. case '-':
  135. isNegative = true;
  136. #if ! JUCE_MINGW
  137. *writePtr++ = '-';
  138. #endif
  139. JUCE_FALLTHROUGH
  140. case '+':
  141. c = *++text;
  142. break;
  143. default:
  144. break;
  145. }
  146. switch (c)
  147. {
  148. case 'n':
  149. case 'N':
  150. {
  151. if ((text[1] == 'a' || text[1] == 'A') && (text[2] == 'n' || text[2] == 'N'))
  152. {
  153. text += 3;
  154. return std::numeric_limits<double>::quiet_NaN();
  155. }
  156. text = endOfWhitspace;
  157. return 0.0;
  158. }
  159. case 'i':
  160. case 'I':
  161. {
  162. if ((text[1] == 'n' || text[1] == 'N') && (text[2] == 'f' || text[2] == 'F'))
  163. {
  164. text += 3;
  165. return isNegative ? -inf : inf;
  166. }
  167. text = endOfWhitspace;
  168. return 0.0;
  169. }
  170. default:
  171. break;
  172. }
  173. #if JUCE_MINGW
  174. // MinGW does not have access to the locale functions required for strtold, so we parse the doubles
  175. // ourselves. There are some edge cases where the least significant digit will be wrong!
  176. double result[3] = { 0 }, accumulator[2] = { 0 };
  177. int exponentAdjustment[2] = { 0 }, exponentAccumulator[2] = { -1, -1 };
  178. int exponent = 0, decPointIndex = 0, digit = 0;
  179. int lastDigit = 0, numSignificantDigits = 0;
  180. bool digitsFound = false;
  181. constexpr const int maxSignificantDigits = 17 + 1;
  182. for (;;)
  183. {
  184. if (text.isDigit())
  185. {
  186. lastDigit = digit;
  187. digit = (int) text.getAndAdvance() - '0';
  188. digitsFound = true;
  189. if (decPointIndex != 0)
  190. exponentAdjustment[1]++;
  191. if (numSignificantDigits == 0 && digit == 0)
  192. continue;
  193. if (++numSignificantDigits > maxSignificantDigits)
  194. {
  195. if (digit > 5)
  196. ++accumulator [decPointIndex];
  197. else if (digit == 5 && (lastDigit & 1) != 0)
  198. ++accumulator [decPointIndex];
  199. if (decPointIndex > 0)
  200. exponentAdjustment[1]--;
  201. else
  202. exponentAdjustment[0]++;
  203. while (text.isDigit())
  204. {
  205. ++text;
  206. if (decPointIndex == 0)
  207. exponentAdjustment[0]++;
  208. }
  209. }
  210. else
  211. {
  212. const auto maxAccumulatorValue = (double) ((std::numeric_limits<unsigned int>::max() - 9) / 10);
  213. if (accumulator [decPointIndex] > maxAccumulatorValue)
  214. {
  215. result [decPointIndex] = mulexp10 (result [decPointIndex], exponentAccumulator [decPointIndex])
  216. + accumulator [decPointIndex];
  217. accumulator [decPointIndex] = 0;
  218. exponentAccumulator [decPointIndex] = 0;
  219. }
  220. accumulator [decPointIndex] = accumulator[decPointIndex] * 10 + digit;
  221. exponentAccumulator [decPointIndex]++;
  222. }
  223. }
  224. else if (decPointIndex == 0 && *text == '.')
  225. {
  226. ++text;
  227. decPointIndex = 1;
  228. if (numSignificantDigits > maxSignificantDigits)
  229. {
  230. while (text.isDigit())
  231. ++text;
  232. break;
  233. }
  234. }
  235. else
  236. {
  237. break;
  238. }
  239. }
  240. result[0] = mulexp10 (result[0], exponentAccumulator[0]) + accumulator[0];
  241. if (decPointIndex != 0)
  242. result[1] = mulexp10 (result[1], exponentAccumulator[1]) + accumulator[1];
  243. c = *text;
  244. if ((c == 'e' || c == 'E') && digitsFound)
  245. {
  246. auto negativeExponent = false;
  247. switch (*++text)
  248. {
  249. case '-': negativeExponent = true; JUCE_FALLTHROUGH
  250. case '+': ++text;
  251. }
  252. while (text.isDigit())
  253. exponent = (exponent * 10) + ((int) text.getAndAdvance() - '0');
  254. if (negativeExponent)
  255. exponent = -exponent;
  256. }
  257. auto r = mulexp10 (result[0], exponent + exponentAdjustment[0]);
  258. if (decPointIndex != 0)
  259. r += mulexp10 (result[1], exponent - exponentAdjustment[1]);
  260. return isNegative ? -r : r;
  261. #else // ! JUCE_MINGW
  262. int numSigFigs = 0, extraExponent = 0;
  263. bool decimalPointFound = false, leadingZeros = false;
  264. for (;;)
  265. {
  266. if (text.isDigit())
  267. {
  268. auto digit = (int) text.getAndAdvance() - '0';
  269. if (decimalPointFound)
  270. {
  271. if (numSigFigs >= maxSignificantDigits)
  272. continue;
  273. }
  274. else
  275. {
  276. if (numSigFigs >= maxSignificantDigits)
  277. {
  278. ++extraExponent;
  279. continue;
  280. }
  281. if (numSigFigs == 0 && digit == 0)
  282. {
  283. leadingZeros = true;
  284. continue;
  285. }
  286. }
  287. *writePtr++ = (char) ('0' + (char) digit);
  288. numSigFigs++;
  289. }
  290. else if ((! decimalPointFound) && *text == '.')
  291. {
  292. ++text;
  293. *writePtr++ = '.';
  294. decimalPointFound = true;
  295. }
  296. else
  297. {
  298. break;
  299. }
  300. }
  301. if ((! leadingZeros) && (numSigFigs == 0))
  302. {
  303. text = endOfWhitspace;
  304. return 0.0;
  305. }
  306. auto writeExponentDigits = [] (int exponent, char* destination)
  307. {
  308. auto exponentDivisor = 100;
  309. while (exponentDivisor > 1)
  310. {
  311. auto digit = exponent / exponentDivisor;
  312. *destination++ = (char) ('0' + (char) digit);
  313. exponent -= digit * exponentDivisor;
  314. exponentDivisor /= 10;
  315. }
  316. *destination++ = (char) ('0' + (char) exponent);
  317. };
  318. c = *text;
  319. if (c == 'e' || c == 'E')
  320. {
  321. const auto startOfExponent = text;
  322. *writePtr++ = 'e';
  323. bool parsedExponentIsPositive = true;
  324. switch (*++text)
  325. {
  326. case '-':
  327. parsedExponentIsPositive = false;
  328. JUCE_FALLTHROUGH
  329. case '+':
  330. ++text;
  331. break;
  332. default:
  333. break;
  334. }
  335. int exponent = 0;
  336. const auto startOfExponentDigits = text;
  337. while (text.isDigit())
  338. {
  339. auto digit = (int) text.getAndAdvance() - '0';
  340. if (digit != 0 || exponent != 0)
  341. exponent = (exponent * 10) + digit;
  342. }
  343. if (text == startOfExponentDigits)
  344. text = startOfExponent;
  345. exponent = extraExponent + (parsedExponentIsPositive ? exponent : -exponent);
  346. if (exponent < 0)
  347. {
  348. if (exponent < std::numeric_limits<double>::min_exponent10 - 1)
  349. return isNegative ? -0.0 : 0.0;
  350. *writePtr++ = '-';
  351. exponent = -exponent;
  352. }
  353. else if (exponent > std::numeric_limits<double>::max_exponent10 + 1)
  354. {
  355. return isNegative ? -inf : inf;
  356. }
  357. writeExponentDigits (exponent, writePtr);
  358. }
  359. else if (extraExponent > 0)
  360. {
  361. *writePtr++ = 'e';
  362. writeExponentDigits (extraExponent, writePtr);
  363. }
  364. #if JUCE_WINDOWS
  365. static _locale_t locale = _create_locale (LC_ALL, "C");
  366. return _strtod_l (&buffer[0], nullptr, locale);
  367. #else
  368. static locale_t locale = newlocale (LC_ALL_MASK, "C", nullptr);
  369. #if JUCE_ANDROID
  370. return (double) strtold_l (&buffer[0], nullptr, locale);
  371. #else
  372. return strtod_l (&buffer[0], nullptr, locale);
  373. #endif
  374. #endif
  375. #endif // JUCE_MINGW
  376. }
  377. /** Parses a character string, to read a floating-point value. */
  378. template <typename CharPointerType>
  379. static double getDoubleValue (CharPointerType text) noexcept
  380. {
  381. return readDoubleValue (text);
  382. }
  383. //==============================================================================
  384. /** Parses a character string, to read an integer value. */
  385. template <typename IntType, typename CharPointerType>
  386. static IntType getIntValue (const CharPointerType text) noexcept
  387. {
  388. using UIntType = typename internal::make_unsigned<IntType>::type;
  389. UIntType v = 0;
  390. auto s = text.findEndOfWhitespace();
  391. const bool isNeg = *s == '-';
  392. if (isNeg)
  393. ++s;
  394. for (;;)
  395. {
  396. auto c = s.getAndAdvance();
  397. if (c >= '0' && c <= '9')
  398. v = v * 10 + (UIntType) (c - '0');
  399. else
  400. break;
  401. }
  402. return isNeg ? - (IntType) v : (IntType) v;
  403. }
  404. /** Parses a character string, to read a hexadecimal value. */
  405. template <typename ResultType>
  406. struct HexParser
  407. {
  408. template <typename CharPointerType>
  409. static ResultType parse (CharPointerType t) noexcept
  410. {
  411. ResultType result = 0;
  412. while (! t.isEmpty())
  413. {
  414. auto hexValue = CharacterFunctions::getHexDigitValue (t.getAndAdvance());
  415. if (hexValue >= 0)
  416. result = (result << 4) | hexValue;
  417. }
  418. return result;
  419. }
  420. };
  421. //==============================================================================
  422. /** Counts the number of characters in a given string, stopping if the count exceeds
  423. a specified limit. */
  424. template <typename CharPointerType>
  425. static size_t lengthUpTo (CharPointerType text, const size_t maxCharsToCount) noexcept
  426. {
  427. size_t len = 0;
  428. while (len < maxCharsToCount && text.getAndAdvance() != 0)
  429. ++len;
  430. return len;
  431. }
  432. /** Counts the number of characters in a given string, stopping if the count exceeds
  433. a specified end-pointer. */
  434. template <typename CharPointerType>
  435. static size_t lengthUpTo (CharPointerType start, const CharPointerType end) noexcept
  436. {
  437. size_t len = 0;
  438. while (start < end && start.getAndAdvance() != 0)
  439. ++len;
  440. return len;
  441. }
  442. /** Copies null-terminated characters from one string to another. */
  443. template <typename DestCharPointerType, typename SrcCharPointerType>
  444. static void copyAll (DestCharPointerType& dest, SrcCharPointerType src) noexcept
  445. {
  446. while (auto c = src.getAndAdvance())
  447. dest.write (c);
  448. dest.writeNull();
  449. }
  450. /** Copies characters from one string to another, up to a null terminator
  451. or a given byte size limit. */
  452. template <typename DestCharPointerType, typename SrcCharPointerType>
  453. static size_t copyWithDestByteLimit (DestCharPointerType& dest, SrcCharPointerType src, size_t maxBytesToWrite) noexcept
  454. {
  455. auto startAddress = dest.getAddress();
  456. auto maxBytes = (ssize_t) maxBytesToWrite;
  457. maxBytes -= (ssize_t) sizeof (typename DestCharPointerType::CharType); // (allow for a terminating null)
  458. for (;;)
  459. {
  460. auto c = src.getAndAdvance();
  461. auto bytesNeeded = (ssize_t) DestCharPointerType::getBytesRequiredFor (c);
  462. maxBytes -= bytesNeeded;
  463. if (c == 0 || maxBytes < 0)
  464. break;
  465. dest.write (c);
  466. }
  467. dest.writeNull();
  468. return (size_t) getAddressDifference (dest.getAddress(), startAddress)
  469. + sizeof (typename DestCharPointerType::CharType);
  470. }
  471. /** Copies characters from one string to another, up to a null terminator
  472. or a given maximum number of characters. */
  473. template <typename DestCharPointerType, typename SrcCharPointerType>
  474. static void copyWithCharLimit (DestCharPointerType& dest, SrcCharPointerType src, int maxChars) noexcept
  475. {
  476. while (--maxChars > 0)
  477. {
  478. auto c = src.getAndAdvance();
  479. if (c == 0)
  480. break;
  481. dest.write (c);
  482. }
  483. dest.writeNull();
  484. }
  485. /** Compares two characters. */
  486. static int compare (juce_wchar char1, juce_wchar char2) noexcept
  487. {
  488. if (auto diff = static_cast<int> (char1) - static_cast<int> (char2))
  489. return diff < 0 ? -1 : 1;
  490. return 0;
  491. }
  492. /** Compares two null-terminated character strings. */
  493. template <typename CharPointerType1, typename CharPointerType2>
  494. static int compare (CharPointerType1 s1, CharPointerType2 s2) noexcept
  495. {
  496. for (;;)
  497. {
  498. auto c1 = s1.getAndAdvance();
  499. if (auto diff = compare (c1, s2.getAndAdvance()))
  500. return diff;
  501. if (c1 == 0)
  502. break;
  503. }
  504. return 0;
  505. }
  506. /** Compares two null-terminated character strings, up to a given number of characters. */
  507. template <typename CharPointerType1, typename CharPointerType2>
  508. static int compareUpTo (CharPointerType1 s1, CharPointerType2 s2, int maxChars) noexcept
  509. {
  510. while (--maxChars >= 0)
  511. {
  512. auto c1 = s1.getAndAdvance();
  513. if (auto diff = compare (c1, s2.getAndAdvance()))
  514. return diff;
  515. if (c1 == 0)
  516. break;
  517. }
  518. return 0;
  519. }
  520. /** Compares two characters, using a case-independant match. */
  521. static int compareIgnoreCase (juce_wchar char1, juce_wchar char2) noexcept
  522. {
  523. return char1 != char2 ? compare (toUpperCase (char1), toUpperCase (char2)) : 0;
  524. }
  525. /** Compares two null-terminated character strings, using a case-independant match. */
  526. template <typename CharPointerType1, typename CharPointerType2>
  527. static int compareIgnoreCase (CharPointerType1 s1, CharPointerType2 s2) noexcept
  528. {
  529. for (;;)
  530. {
  531. auto c1 = s1.getAndAdvance();
  532. if (auto diff = compareIgnoreCase (c1, s2.getAndAdvance()))
  533. return diff;
  534. if (c1 == 0)
  535. break;
  536. }
  537. return 0;
  538. }
  539. /** Compares two null-terminated character strings, using a case-independent match. */
  540. template <typename CharPointerType1, typename CharPointerType2>
  541. static int compareIgnoreCaseUpTo (CharPointerType1 s1, CharPointerType2 s2, int maxChars) noexcept
  542. {
  543. while (--maxChars >= 0)
  544. {
  545. auto c1 = s1.getAndAdvance();
  546. if (auto diff = compareIgnoreCase (c1, s2.getAndAdvance()))
  547. return diff;
  548. if (c1 == 0)
  549. break;
  550. }
  551. return 0;
  552. }
  553. /** Finds the character index of a given substring in another string.
  554. Returns -1 if the substring is not found.
  555. */
  556. template <typename CharPointerType1, typename CharPointerType2>
  557. static int indexOf (CharPointerType1 textToSearch, const CharPointerType2 substringToLookFor) noexcept
  558. {
  559. int index = 0;
  560. auto substringLength = (int) substringToLookFor.length();
  561. for (;;)
  562. {
  563. if (textToSearch.compareUpTo (substringToLookFor, substringLength) == 0)
  564. return index;
  565. if (textToSearch.getAndAdvance() == 0)
  566. return -1;
  567. ++index;
  568. }
  569. }
  570. /** Returns a pointer to the first occurrence of a substring in a string.
  571. If the substring is not found, this will return a pointer to the string's
  572. null terminator.
  573. */
  574. template <typename CharPointerType1, typename CharPointerType2>
  575. static CharPointerType1 find (CharPointerType1 textToSearch, const CharPointerType2 substringToLookFor) noexcept
  576. {
  577. auto substringLength = (int) substringToLookFor.length();
  578. while (textToSearch.compareUpTo (substringToLookFor, substringLength) != 0
  579. && ! textToSearch.isEmpty())
  580. ++textToSearch;
  581. return textToSearch;
  582. }
  583. /** Returns a pointer to the first occurrence of a substring in a string.
  584. If the substring is not found, this will return a pointer to the string's
  585. null terminator.
  586. */
  587. template <typename CharPointerType>
  588. static CharPointerType find (CharPointerType textToSearch, const juce_wchar charToLookFor) noexcept
  589. {
  590. for (;; ++textToSearch)
  591. {
  592. auto c = *textToSearch;
  593. if (c == charToLookFor || c == 0)
  594. break;
  595. }
  596. return textToSearch;
  597. }
  598. /** Finds the character index of a given substring in another string, using
  599. a case-independent match.
  600. Returns -1 if the substring is not found.
  601. */
  602. template <typename CharPointerType1, typename CharPointerType2>
  603. static int indexOfIgnoreCase (CharPointerType1 haystack, const CharPointerType2 needle) noexcept
  604. {
  605. int index = 0;
  606. auto needleLength = (int) needle.length();
  607. for (;;)
  608. {
  609. if (haystack.compareIgnoreCaseUpTo (needle, needleLength) == 0)
  610. return index;
  611. if (haystack.getAndAdvance() == 0)
  612. return -1;
  613. ++index;
  614. }
  615. }
  616. /** Finds the character index of a given character in another string.
  617. Returns -1 if the character is not found.
  618. */
  619. template <typename Type>
  620. static int indexOfChar (Type text, const juce_wchar charToFind) noexcept
  621. {
  622. int i = 0;
  623. while (! text.isEmpty())
  624. {
  625. if (text.getAndAdvance() == charToFind)
  626. return i;
  627. ++i;
  628. }
  629. return -1;
  630. }
  631. /** Finds the character index of a given character in another string, using
  632. a case-independent match.
  633. Returns -1 if the character is not found.
  634. */
  635. template <typename Type>
  636. static int indexOfCharIgnoreCase (Type text, juce_wchar charToFind) noexcept
  637. {
  638. charToFind = CharacterFunctions::toLowerCase (charToFind);
  639. int i = 0;
  640. while (! text.isEmpty())
  641. {
  642. if (text.toLowerCase() == charToFind)
  643. return i;
  644. ++text;
  645. ++i;
  646. }
  647. return -1;
  648. }
  649. /** Increments a pointer until it points to the first non-whitespace character
  650. in a string.
  651. If the string contains only whitespace, the pointer will point to the
  652. string's null terminator.
  653. */
  654. template <typename Type>
  655. static void incrementToEndOfWhitespace (Type& text) noexcept
  656. {
  657. while (text.isWhitespace())
  658. ++text;
  659. }
  660. /** Returns a pointer to the first non-whitespace character in a string.
  661. If the string contains only whitespace, this will return a pointer
  662. to its null terminator.
  663. */
  664. template <typename Type>
  665. static Type findEndOfWhitespace (Type text) noexcept
  666. {
  667. incrementToEndOfWhitespace (text);
  668. return text;
  669. }
  670. /** Returns a pointer to the first character in the string which is found in
  671. the breakCharacters string.
  672. */
  673. template <typename Type, typename BreakType>
  674. static Type findEndOfToken (Type text, BreakType breakCharacters, Type quoteCharacters)
  675. {
  676. juce_wchar currentQuoteChar = 0;
  677. while (! text.isEmpty())
  678. {
  679. auto c = text.getAndAdvance();
  680. if (currentQuoteChar == 0 && breakCharacters.indexOf (c) >= 0)
  681. {
  682. --text;
  683. break;
  684. }
  685. if (quoteCharacters.indexOf (c) >= 0)
  686. {
  687. if (currentQuoteChar == 0)
  688. currentQuoteChar = c;
  689. else if (currentQuoteChar == c)
  690. currentQuoteChar = 0;
  691. }
  692. }
  693. return text;
  694. }
  695. private:
  696. static double mulexp10 (double value, int exponent) noexcept;
  697. };
  698. } // namespace juce