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.

582 lines
19KB

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