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.

552 lines
19KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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. JUCE_BEGIN_IGNORE_WARNINGS_MSVC (4514 4996)
  20. juce_wchar CharacterFunctions::toUpperCase (const juce_wchar character) noexcept
  21. {
  22. return (juce_wchar) towupper ((wint_t) character);
  23. }
  24. juce_wchar CharacterFunctions::toLowerCase (const juce_wchar character) noexcept
  25. {
  26. return (juce_wchar) towlower ((wint_t) character);
  27. }
  28. bool CharacterFunctions::isUpperCase (const juce_wchar character) noexcept
  29. {
  30. #if JUCE_WINDOWS
  31. return iswupper ((wint_t) character) != 0;
  32. #else
  33. return toLowerCase (character) != character;
  34. #endif
  35. }
  36. bool CharacterFunctions::isLowerCase (const juce_wchar character) noexcept
  37. {
  38. #if JUCE_WINDOWS
  39. return iswlower ((wint_t) character) != 0;
  40. #else
  41. return toUpperCase (character) != character;
  42. #endif
  43. }
  44. JUCE_END_IGNORE_WARNINGS_MSVC
  45. //==============================================================================
  46. bool CharacterFunctions::isWhitespace (const char character) noexcept
  47. {
  48. return character == ' ' || (character <= 13 && character >= 9);
  49. }
  50. bool CharacterFunctions::isWhitespace (const juce_wchar character) noexcept
  51. {
  52. return iswspace ((wint_t) character) != 0;
  53. }
  54. bool CharacterFunctions::isDigit (const char character) noexcept
  55. {
  56. return (character >= '0' && character <= '9');
  57. }
  58. bool CharacterFunctions::isDigit (const juce_wchar character) noexcept
  59. {
  60. return iswdigit ((wint_t) character) != 0;
  61. }
  62. bool CharacterFunctions::isLetter (const char character) noexcept
  63. {
  64. return (character >= 'a' && character <= 'z')
  65. || (character >= 'A' && character <= 'Z');
  66. }
  67. bool CharacterFunctions::isLetter (const juce_wchar character) noexcept
  68. {
  69. return iswalpha ((wint_t) character) != 0;
  70. }
  71. bool CharacterFunctions::isLetterOrDigit (const char character) noexcept
  72. {
  73. return (character >= 'a' && character <= 'z')
  74. || (character >= 'A' && character <= 'Z')
  75. || (character >= '0' && character <= '9');
  76. }
  77. bool CharacterFunctions::isLetterOrDigit (const juce_wchar character) noexcept
  78. {
  79. return iswalnum ((wint_t) character) != 0;
  80. }
  81. bool CharacterFunctions::isPrintable (const char character) noexcept
  82. {
  83. return (character >= ' ' && character <= '~');
  84. }
  85. bool CharacterFunctions::isPrintable (const juce_wchar character) noexcept
  86. {
  87. return iswprint ((wint_t) character) != 0;
  88. }
  89. int CharacterFunctions::getHexDigitValue (const juce_wchar digit) noexcept
  90. {
  91. auto d = (unsigned int) (digit - '0');
  92. if (d < (unsigned int) 10)
  93. return (int) d;
  94. d += (unsigned int) ('0' - 'a');
  95. if (d < (unsigned int) 6)
  96. return (int) d + 10;
  97. d += (unsigned int) ('a' - 'A');
  98. if (d < (unsigned int) 6)
  99. return (int) d + 10;
  100. return -1;
  101. }
  102. double CharacterFunctions::mulexp10 (const double value, int exponent) noexcept
  103. {
  104. if (exponent == 0)
  105. return value;
  106. if (exactlyEqual (value, 0.0))
  107. return 0;
  108. const bool negative = (exponent < 0);
  109. if (negative)
  110. exponent = -exponent;
  111. double result = 1.0, power = 10.0;
  112. for (int bit = 1; exponent != 0; bit <<= 1)
  113. {
  114. if ((exponent & bit) != 0)
  115. {
  116. exponent ^= bit;
  117. result *= power;
  118. if (exponent == 0)
  119. break;
  120. }
  121. power *= power;
  122. }
  123. return negative ? (value / result) : (value * result);
  124. }
  125. juce_wchar CharacterFunctions::getUnicodeCharFromWindows1252Codepage (const uint8 c) noexcept
  126. {
  127. if (c < 0x80 || c >= 0xa0)
  128. return (juce_wchar) c;
  129. static const uint16 lookup[] = { 0x20AC, 0x0007, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,
  130. 0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, 0x0007, 0x017D, 0x0007,
  131. 0x0007, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
  132. 0x02DC, 0x2122, 0x0161, 0x203A, 0x0153, 0x0007, 0x017E, 0x0178 };
  133. return (juce_wchar) lookup[c - 0x80];
  134. }
  135. //==============================================================================
  136. //==============================================================================
  137. #if JUCE_UNIT_TESTS
  138. #define QUOTE(x) #x
  139. #define STR(value) QUOTE(value)
  140. #define ASYM_CHARPTR_DOUBLE_PAIR(str, value) std::pair<const char*, double> (STR(str), value)
  141. #define CHARPTR_DOUBLE_PAIR(value) ASYM_CHARPTR_DOUBLE_PAIR(value, value)
  142. #define CHARPTR_DOUBLE_PAIR_COMBOS(value) \
  143. CHARPTR_DOUBLE_PAIR(value), \
  144. CHARPTR_DOUBLE_PAIR(-value), \
  145. ASYM_CHARPTR_DOUBLE_PAIR(+value, value), \
  146. ASYM_CHARPTR_DOUBLE_PAIR(000000 ## value, value), \
  147. ASYM_CHARPTR_DOUBLE_PAIR(+000 ## value, value), \
  148. ASYM_CHARPTR_DOUBLE_PAIR(-0 ## value, -value)
  149. namespace characterFunctionsTests
  150. {
  151. template <typename CharPointerType>
  152. MemoryBlock memoryBlockFromCharPtr (const typename CharPointerType::CharType* charPtr)
  153. {
  154. using CharType = typename CharPointerType::CharType;
  155. MemoryBlock result;
  156. CharPointerType source (charPtr);
  157. result.setSize (CharPointerType::getBytesRequiredFor (source) + sizeof (CharType));
  158. CharPointerType dest { (CharType*) result.getData() };
  159. dest.writeAll (source);
  160. return result;
  161. }
  162. template <typename FromCharPointerType, typename ToCharPointerType>
  163. MemoryBlock convert (const MemoryBlock& source, bool removeNullTerminator = false)
  164. {
  165. using ToCharType = typename ToCharPointerType ::CharType;
  166. using FromCharType = typename FromCharPointerType::CharType;
  167. FromCharPointerType sourcePtr { (FromCharType*) source.getData() };
  168. std::vector<juce_wchar> sourceChars;
  169. size_t requiredSize = 0;
  170. juce_wchar c;
  171. while ((c = sourcePtr.getAndAdvance()) != '\0')
  172. {
  173. requiredSize += ToCharPointerType::getBytesRequiredFor (c);
  174. sourceChars.push_back (c);
  175. }
  176. if (! removeNullTerminator)
  177. requiredSize += sizeof (ToCharType);
  178. MemoryBlock result;
  179. result.setSize (requiredSize);
  180. ToCharPointerType dest { (ToCharType*) result.getData() };
  181. for (auto wc : sourceChars)
  182. dest.write (wc);
  183. if (! removeNullTerminator)
  184. dest.writeNull();
  185. return result;
  186. }
  187. struct SeparatorStrings
  188. {
  189. std::vector<MemoryBlock> terminals, nulls;
  190. };
  191. template <typename CharPointerType>
  192. SeparatorStrings getSeparators()
  193. {
  194. jassertfalse;
  195. return {};
  196. }
  197. template <>
  198. SeparatorStrings getSeparators<CharPointer_ASCII>()
  199. {
  200. SeparatorStrings result;
  201. const CharPointer_ASCII::CharType* terminalCharPtrs[] = {
  202. "", "-", "+", "e", "e+", "E-", "f", " ", ",", ";", "<", "'", "\"", "_", "k",
  203. " +", " -", " -e", "-In ", " +n", "n", " r"
  204. };
  205. for (auto ptr : terminalCharPtrs)
  206. result.terminals.push_back (memoryBlockFromCharPtr<CharPointer_ASCII> (ptr));
  207. const CharPointer_ASCII::CharType* nullCharPtrs[] = { "." };
  208. result.nulls = result.terminals;
  209. for (auto ptr : nullCharPtrs)
  210. result.nulls.push_back (memoryBlockFromCharPtr<CharPointer_ASCII> (ptr));
  211. return result;
  212. }
  213. template <>
  214. SeparatorStrings getSeparators<CharPointer_UTF8>()
  215. {
  216. auto result = getSeparators<CharPointer_ASCII>();
  217. const CharPointer_UTF8::CharType* terminalCharPtrs[] = {
  218. "\xe2\x82\xac", // €
  219. "\xf0\x90\x90\xB7", // 𐐷
  220. "\xf0\x9f\x98\x83", // 😃
  221. "\xf0\x9f\x8f\x81\xF0\x9F\x9A\x97" // 🏁🚗
  222. };
  223. for (auto ptr : terminalCharPtrs)
  224. {
  225. auto block = memoryBlockFromCharPtr<CharPointer_UTF8> (ptr);
  226. for (auto vec : { &result.terminals, &result.nulls })
  227. vec->push_back (block);
  228. }
  229. return result;
  230. }
  231. template <typename CharPointerType, typename StorageType>
  232. SeparatorStrings prefixWithAsciiSeparators (const std::vector<std::vector<StorageType>>& terminalCharPtrs)
  233. {
  234. auto asciiSeparators = getSeparators<CharPointer_ASCII>();
  235. SeparatorStrings result;
  236. for (const auto& block : asciiSeparators.terminals)
  237. result.terminals.push_back (convert<CharPointer_ASCII, CharPointerType> (block));
  238. for (const auto& block : asciiSeparators.nulls)
  239. result.nulls.push_back (convert<CharPointer_ASCII, CharPointerType> (block));
  240. for (auto& t : terminalCharPtrs)
  241. {
  242. const auto block = memoryBlockFromCharPtr<CharPointerType> ((typename CharPointerType::CharType*) t.data());
  243. for (auto vec : { &result.terminals, &result.nulls })
  244. vec->push_back (block);
  245. }
  246. return result;
  247. }
  248. template <>
  249. SeparatorStrings getSeparators<CharPointer_UTF16>()
  250. {
  251. const std::vector<std::vector<char16_t>> terminalCharPtrs {
  252. { 0x0 },
  253. { 0x0076, 0x0 }, // v
  254. { 0x20ac, 0x0 }, // €
  255. { 0xd801, 0xdc37, 0x0 }, // 𐐷
  256. { 0x0065, 0xd83d, 0xde03, 0x0 }, // e😃
  257. { 0xd83c, 0xdfc1, 0xd83d, 0xde97, 0x0 } // 🏁🚗
  258. };
  259. return prefixWithAsciiSeparators<CharPointer_UTF16> (terminalCharPtrs);
  260. }
  261. template <>
  262. SeparatorStrings getSeparators<CharPointer_UTF32>()
  263. {
  264. const std::vector<std::vector<char32_t>> terminalCharPtrs = {
  265. { 0x00000076, 0x0 }, // v
  266. { 0x000020aC, 0x0 }, // €
  267. { 0x00010437, 0x0 }, // 𐐷
  268. { 0x00000065, 0x0001f603, 0x0 }, // e😃
  269. { 0x0001f3c1, 0x0001f697, 0x0 } // 🏁🚗
  270. };
  271. return prefixWithAsciiSeparators<CharPointer_UTF32> (terminalCharPtrs);
  272. }
  273. template <typename TestFunction>
  274. void withAllPrefixesAndSuffixes (const std::vector<MemoryBlock>& prefixes,
  275. const std::vector<MemoryBlock>& suffixes,
  276. const std::vector<MemoryBlock>& testValues,
  277. TestFunction&& test)
  278. {
  279. for (const auto& prefix : prefixes)
  280. {
  281. for (const auto& testValue : testValues)
  282. {
  283. MemoryBlock testBlock = prefix;
  284. testBlock.append (testValue.getData(), testValue.getSize());
  285. for (const auto& suffix : suffixes)
  286. {
  287. MemoryBlock data = testBlock;
  288. data.append (suffix.getData(), suffix.getSize());
  289. test (data, suffix);
  290. }
  291. }
  292. }
  293. }
  294. template <typename CharPointerType>
  295. class CharacterFunctionsTests final : public UnitTest
  296. {
  297. public:
  298. using CharType = typename CharPointerType::CharType;
  299. CharacterFunctionsTests()
  300. : UnitTest ("CharacterFunctions", UnitTestCategories::text)
  301. {}
  302. void runTest() override
  303. {
  304. beginTest ("readDoubleValue");
  305. const std::pair<const char*, double> trials[] =
  306. {
  307. // Integers
  308. CHARPTR_DOUBLE_PAIR_COMBOS (0),
  309. CHARPTR_DOUBLE_PAIR_COMBOS (3),
  310. CHARPTR_DOUBLE_PAIR_COMBOS (4931),
  311. CHARPTR_DOUBLE_PAIR_COMBOS (5000),
  312. CHARPTR_DOUBLE_PAIR_COMBOS (9862097),
  313. // Floating point numbers
  314. CHARPTR_DOUBLE_PAIR_COMBOS (0.),
  315. CHARPTR_DOUBLE_PAIR_COMBOS (9.),
  316. CHARPTR_DOUBLE_PAIR_COMBOS (7.000),
  317. CHARPTR_DOUBLE_PAIR_COMBOS (0.2),
  318. CHARPTR_DOUBLE_PAIR_COMBOS (.298630),
  319. CHARPTR_DOUBLE_PAIR_COMBOS (1.118),
  320. CHARPTR_DOUBLE_PAIR_COMBOS (0.9000),
  321. CHARPTR_DOUBLE_PAIR_COMBOS (0.0000001),
  322. CHARPTR_DOUBLE_PAIR_COMBOS (500.0000001),
  323. CHARPTR_DOUBLE_PAIR_COMBOS (9862098.2398604),
  324. // Exponents
  325. CHARPTR_DOUBLE_PAIR_COMBOS (0e0),
  326. CHARPTR_DOUBLE_PAIR_COMBOS (0.e0),
  327. CHARPTR_DOUBLE_PAIR_COMBOS (0.00000e0),
  328. CHARPTR_DOUBLE_PAIR_COMBOS (.0e7),
  329. CHARPTR_DOUBLE_PAIR_COMBOS (0e-5),
  330. CHARPTR_DOUBLE_PAIR_COMBOS (2E0),
  331. CHARPTR_DOUBLE_PAIR_COMBOS (4.E0),
  332. CHARPTR_DOUBLE_PAIR_COMBOS (1.2000000E0),
  333. CHARPTR_DOUBLE_PAIR_COMBOS (1.2000000E6),
  334. CHARPTR_DOUBLE_PAIR_COMBOS (.398e3),
  335. CHARPTR_DOUBLE_PAIR_COMBOS (10e10),
  336. CHARPTR_DOUBLE_PAIR_COMBOS (1.4962e+2),
  337. CHARPTR_DOUBLE_PAIR_COMBOS (3198693.0973e4),
  338. CHARPTR_DOUBLE_PAIR_COMBOS (10973097.2087E-4),
  339. CHARPTR_DOUBLE_PAIR_COMBOS (1.3986e00006),
  340. CHARPTR_DOUBLE_PAIR_COMBOS (2087.3087e+00006),
  341. CHARPTR_DOUBLE_PAIR_COMBOS (6.0872e-00006),
  342. CHARPTR_DOUBLE_PAIR_COMBOS (1.7976931348623157e+308),
  343. CHARPTR_DOUBLE_PAIR_COMBOS (2.2250738585072014e-308),
  344. // Too many sig figs. The parsing routine on MinGW gets the last
  345. // significant figure wrong.
  346. CHARPTR_DOUBLE_PAIR_COMBOS (17654321098765432.9),
  347. CHARPTR_DOUBLE_PAIR_COMBOS (183456789012345678.9),
  348. CHARPTR_DOUBLE_PAIR_COMBOS (1934567890123456789.9),
  349. CHARPTR_DOUBLE_PAIR_COMBOS (20345678901234567891.9),
  350. CHARPTR_DOUBLE_PAIR_COMBOS (10000000000000000303786028427003666890752.000000),
  351. CHARPTR_DOUBLE_PAIR_COMBOS (10000000000000000303786028427003666890752e3),
  352. CHARPTR_DOUBLE_PAIR_COMBOS (10000000000000000303786028427003666890752e100),
  353. CHARPTR_DOUBLE_PAIR_COMBOS (10000000000000000303786028427003666890752.000000e-5),
  354. CHARPTR_DOUBLE_PAIR_COMBOS (10000000000000000303786028427003666890752.000005e-40),
  355. CHARPTR_DOUBLE_PAIR_COMBOS (1.23456789012345678901234567890),
  356. CHARPTR_DOUBLE_PAIR_COMBOS (1.23456789012345678901234567890e-111),
  357. };
  358. auto asciiToMemoryBlock = [] (const char* asciiPtr, bool removeNullTerminator)
  359. {
  360. auto block = memoryBlockFromCharPtr<CharPointer_ASCII> (asciiPtr);
  361. return convert<CharPointer_ASCII, CharPointerType> (block, removeNullTerminator);
  362. };
  363. const auto separators = getSeparators<CharPointerType>();
  364. for (const auto& trial : trials)
  365. {
  366. for (const auto& terminal : separators.terminals)
  367. {
  368. MemoryBlock data { asciiToMemoryBlock (trial.first, true) };
  369. data.append (terminal.getData(), terminal.getSize());
  370. CharPointerType charPtr { (CharType*) data.getData() };
  371. expectEquals (CharacterFunctions::readDoubleValue (charPtr), trial.second);
  372. expect (*charPtr == *(CharPointerType ((CharType*) terminal.getData())));
  373. }
  374. }
  375. auto asciiToMemoryBlocks = [&] (const std::vector<const char*>& asciiPtrs, bool removeNullTerminator)
  376. {
  377. std::vector<MemoryBlock> result;
  378. for (auto* ptr : asciiPtrs)
  379. result.push_back (asciiToMemoryBlock (ptr, removeNullTerminator));
  380. return result;
  381. };
  382. std::vector<const char*> prefixCharPtrs = { "" , "+", "-" };
  383. const auto prefixes = asciiToMemoryBlocks (prefixCharPtrs, true);
  384. {
  385. std::vector<const char*> nanCharPtrs = { "NaN", "nan", "NAN", "naN" };
  386. auto nans = asciiToMemoryBlocks (nanCharPtrs, true);
  387. withAllPrefixesAndSuffixes (prefixes, separators.terminals, nans, [this] (const MemoryBlock& data,
  388. const MemoryBlock& suffix)
  389. {
  390. CharPointerType charPtr { (CharType*) data.getData() };
  391. expect (std::isnan (CharacterFunctions::readDoubleValue (charPtr)));
  392. expect (*charPtr == *(CharPointerType ((CharType*) suffix.getData())));
  393. });
  394. }
  395. {
  396. std::vector<const char*> infCharPtrs = { "Inf", "inf", "INF", "InF", "1.0E1024", "1.23456789012345678901234567890e123456789" };
  397. auto infs = asciiToMemoryBlocks (infCharPtrs, true);
  398. withAllPrefixesAndSuffixes (prefixes, separators.terminals, infs, [this] (const MemoryBlock& data,
  399. const MemoryBlock& suffix)
  400. {
  401. CharPointerType charPtr { (CharType*) data.getData() };
  402. auto expected = charPtr[0] == '-' ? -std::numeric_limits<double>::infinity()
  403. : std::numeric_limits<double>::infinity();
  404. expectEquals (CharacterFunctions::readDoubleValue (charPtr), expected);
  405. expect (*charPtr == *(CharPointerType ((CharType*) suffix.getData())));
  406. });
  407. }
  408. {
  409. std::vector<const char*> zeroCharPtrs = { "1.0E-400", "1.23456789012345678901234567890e-123456789" };
  410. auto zeros = asciiToMemoryBlocks (zeroCharPtrs, true);
  411. withAllPrefixesAndSuffixes (prefixes, separators.terminals, zeros, [this] (const MemoryBlock& data,
  412. const MemoryBlock& suffix)
  413. {
  414. CharPointerType charPtr { (CharType*) data.getData() };
  415. auto expected = charPtr[0] == '-' ? -0.0 : 0.0;
  416. expectEquals (CharacterFunctions::readDoubleValue (charPtr), expected);
  417. expect (*charPtr == *(CharPointerType ((CharType*) suffix.getData())));
  418. });
  419. }
  420. {
  421. for (const auto& n : separators.nulls)
  422. {
  423. MemoryBlock data { n.getData(), n.getSize() };
  424. CharPointerType charPtr { (CharType*) data.getData() };
  425. expectEquals (CharacterFunctions::readDoubleValue (charPtr), 0.0);
  426. expect (charPtr == CharPointerType { (CharType*) data.getData() }.findEndOfWhitespace());
  427. }
  428. }
  429. }
  430. };
  431. static CharacterFunctionsTests<CharPointer_ASCII> characterFunctionsTestsAscii;
  432. static CharacterFunctionsTests<CharPointer_UTF8> characterFunctionsTestsUtf8;
  433. static CharacterFunctionsTests<CharPointer_UTF16> characterFunctionsTestsUtf16;
  434. static CharacterFunctionsTests<CharPointer_UTF32> characterFunctionsTestsUtf32;
  435. }
  436. #endif
  437. } // namespace juce