1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
|
/*
==============================================================================
This file is part of the JUCE framework.
Copyright (c) Raw Material Software Limited
JUCE is an open source framework subject to commercial or open source
licensing.
By downloading, installing, or using the JUCE framework, or combining the
JUCE framework with any other source code, object code, content or any other
copyrightable work, you agree to the terms of the JUCE End User Licence
Agreement, and all incorporated terms including the JUCE Privacy Policy and
the JUCE Website Terms of Service, as applicable, which will bind you. If you
do not agree to the terms of these agreements, we will not license the JUCE
framework to you, and you must discontinue the installation or download
process and cease use of the JUCE framework.
JUCE End User Licence Agreement: https://juce.com/legal/juce-8-licence/
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
Or:
You may also use this code under the terms of the AGPLv3:
https://www.gnu.org/licenses/agpl-3.0.en.html
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
==============================================================================
*/
namespace juce
{
JUCE_BEGIN_IGNORE_WARNINGS_MSVC (4514 4996)
juce_wchar CharacterFunctions::toUpperCase (const juce_wchar character) noexcept
{
return (juce_wchar) towupper ((wint_t) character);
}
juce_wchar CharacterFunctions::toLowerCase (const juce_wchar character) noexcept
{
return (juce_wchar) towlower ((wint_t) character);
}
bool CharacterFunctions::isUpperCase (const juce_wchar character) noexcept
{
#if JUCE_WINDOWS
return iswupper ((wint_t) character) != 0;
#else
return toLowerCase (character) != character;
#endif
}
bool CharacterFunctions::isLowerCase (const juce_wchar character) noexcept
{
#if JUCE_WINDOWS
return iswlower ((wint_t) character) != 0;
#else
return toUpperCase (character) != character;
#endif
}
JUCE_END_IGNORE_WARNINGS_MSVC
//==============================================================================
bool CharacterFunctions::isWhitespace (const char character) noexcept
{
return character == ' ' || (character <= 13 && character >= 9);
}
bool CharacterFunctions::isWhitespace (const juce_wchar character) noexcept
{
return iswspace ((wint_t) character) != 0;
}
bool CharacterFunctions::isDigit (const char character) noexcept
{
return (character >= '0' && character <= '9');
}
bool CharacterFunctions::isDigit (const juce_wchar character) noexcept
{
return iswdigit ((wint_t) character) != 0;
}
bool CharacterFunctions::isLetter (const char character) noexcept
{
return (character >= 'a' && character <= 'z')
|| (character >= 'A' && character <= 'Z');
}
bool CharacterFunctions::isLetter (const juce_wchar character) noexcept
{
return iswalpha ((wint_t) character) != 0;
}
bool CharacterFunctions::isLetterOrDigit (const char character) noexcept
{
return (character >= 'a' && character <= 'z')
|| (character >= 'A' && character <= 'Z')
|| (character >= '0' && character <= '9');
}
bool CharacterFunctions::isLetterOrDigit (const juce_wchar character) noexcept
{
return iswalnum ((wint_t) character) != 0;
}
bool CharacterFunctions::isPrintable (const char character) noexcept
{
return (character >= ' ' && character <= '~');
}
bool CharacterFunctions::isPrintable (const juce_wchar character) noexcept
{
return iswprint ((wint_t) character) != 0;
}
int CharacterFunctions::getHexDigitValue (const juce_wchar digit) noexcept
{
auto d = (unsigned int) (digit - '0');
if (d < (unsigned int) 10)
return (int) d;
d += (unsigned int) ('0' - 'a');
if (d < (unsigned int) 6)
return (int) d + 10;
d += (unsigned int) ('a' - 'A');
if (d < (unsigned int) 6)
return (int) d + 10;
return -1;
}
double CharacterFunctions::mulexp10 (const double value, int exponent) noexcept
{
if (exponent == 0)
return value;
if (exactlyEqual (value, 0.0))
return 0;
const bool negative = (exponent < 0);
if (negative)
exponent = -exponent;
double result = 1.0, power = 10.0;
for (int bit = 1; exponent != 0; bit <<= 1)
{
if ((exponent & bit) != 0)
{
exponent ^= bit;
result *= power;
if (exponent == 0)
break;
}
power *= power;
}
return negative ? (value / result) : (value * result);
}
juce_wchar CharacterFunctions::getUnicodeCharFromWindows1252Codepage (const uint8 c) noexcept
{
if (c < 0x80 || c >= 0xa0)
return (juce_wchar) c;
static const uint16 lookup[] = { 0x20AC, 0x0007, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,
0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, 0x0007, 0x017D, 0x0007,
0x0007, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
0x02DC, 0x2122, 0x0161, 0x203A, 0x0153, 0x0007, 0x017E, 0x0178 };
return (juce_wchar) lookup[c - 0x80];
}
//==============================================================================
//==============================================================================
#if JUCE_UNIT_TESTS
#define QUOTE(x) #x
#define STR(value) QUOTE(value)
#define ASYM_CHARPTR_DOUBLE_PAIR(str, value) std::pair<const char*, double> (STR(str), value)
#define CHARPTR_DOUBLE_PAIR(value) ASYM_CHARPTR_DOUBLE_PAIR(value, value)
#define CHARPTR_DOUBLE_PAIR_COMBOS(value) \
CHARPTR_DOUBLE_PAIR(value), \
CHARPTR_DOUBLE_PAIR(-value), \
ASYM_CHARPTR_DOUBLE_PAIR(+value, value), \
ASYM_CHARPTR_DOUBLE_PAIR(000000 ## value, value), \
ASYM_CHARPTR_DOUBLE_PAIR(+000 ## value, value), \
ASYM_CHARPTR_DOUBLE_PAIR(-0 ## value, -value)
namespace characterFunctionsTests
{
template <typename CharPointerType>
MemoryBlock memoryBlockFromCharPtr (const typename CharPointerType::CharType* charPtr)
{
using CharType = typename CharPointerType::CharType;
MemoryBlock result;
CharPointerType source (charPtr);
result.setSize (CharPointerType::getBytesRequiredFor (source) + sizeof (CharType));
CharPointerType dest { (CharType*) result.getData() };
dest.writeAll (source);
return result;
}
template <typename FromCharPointerType, typename ToCharPointerType>
MemoryBlock convert (const MemoryBlock& source, bool removeNullTerminator = false)
{
using ToCharType = typename ToCharPointerType ::CharType;
using FromCharType = typename FromCharPointerType::CharType;
FromCharPointerType sourcePtr { (FromCharType*) source.getData() };
std::vector<juce_wchar> sourceChars;
size_t requiredSize = 0;
juce_wchar c;
while ((c = sourcePtr.getAndAdvance()) != '\0')
{
requiredSize += ToCharPointerType::getBytesRequiredFor (c);
sourceChars.push_back (c);
}
if (! removeNullTerminator)
requiredSize += sizeof (ToCharType);
MemoryBlock result;
result.setSize (requiredSize);
ToCharPointerType dest { (ToCharType*) result.getData() };
for (auto wc : sourceChars)
dest.write (wc);
if (! removeNullTerminator)
dest.writeNull();
return result;
}
struct SeparatorStrings
{
std::vector<MemoryBlock> terminals, nulls;
};
template <typename CharPointerType>
SeparatorStrings getSeparators()
{
jassertfalse;
return {};
}
template <>
SeparatorStrings getSeparators<CharPointer_ASCII>()
{
SeparatorStrings result;
const CharPointer_ASCII::CharType* terminalCharPtrs[] = {
"", "-", "+", "e", "e+", "E-", "f", " ", ",", ";", "<", "'", "\"", "_", "k",
" +", " -", " -e", "-In ", " +n", "n", " r"
};
for (auto ptr : terminalCharPtrs)
result.terminals.push_back (memoryBlockFromCharPtr<CharPointer_ASCII> (ptr));
const CharPointer_ASCII::CharType* nullCharPtrs[] = { "." };
result.nulls = result.terminals;
for (auto ptr : nullCharPtrs)
result.nulls.push_back (memoryBlockFromCharPtr<CharPointer_ASCII> (ptr));
return result;
}
template <>
SeparatorStrings getSeparators<CharPointer_UTF8>()
{
auto result = getSeparators<CharPointer_ASCII>();
const CharPointer_UTF8::CharType* terminalCharPtrs[] = {
"\xe2\x82\xac", // €
"\xf0\x90\x90\xB7", // 𐐷
"\xf0\x9f\x98\x83", // 😃
"\xf0\x9f\x8f\x81\xF0\x9F\x9A\x97" // 🏁🚗
};
for (auto ptr : terminalCharPtrs)
{
auto block = memoryBlockFromCharPtr<CharPointer_UTF8> (ptr);
for (auto vec : { &result.terminals, &result.nulls })
vec->push_back (block);
}
return result;
}
template <typename CharPointerType, typename StorageType>
SeparatorStrings prefixWithAsciiSeparators (const std::vector<std::vector<StorageType>>& terminalCharPtrs)
{
auto asciiSeparators = getSeparators<CharPointer_ASCII>();
SeparatorStrings result;
for (const auto& block : asciiSeparators.terminals)
result.terminals.push_back (convert<CharPointer_ASCII, CharPointerType> (block));
for (const auto& block : asciiSeparators.nulls)
result.nulls.push_back (convert<CharPointer_ASCII, CharPointerType> (block));
for (auto& t : terminalCharPtrs)
{
const auto block = memoryBlockFromCharPtr<CharPointerType> ((typename CharPointerType::CharType*) t.data());
for (auto vec : { &result.terminals, &result.nulls })
vec->push_back (block);
}
return result;
}
template <>
SeparatorStrings getSeparators<CharPointer_UTF16>()
{
const std::vector<std::vector<char16_t>> terminalCharPtrs {
{ 0x0 },
{ 0x0076, 0x0 }, // v
{ 0x20ac, 0x0 }, // €
{ 0xd801, 0xdc37, 0x0 }, // 𐐷
{ 0x0065, 0xd83d, 0xde03, 0x0 }, // e😃
{ 0xd83c, 0xdfc1, 0xd83d, 0xde97, 0x0 } // 🏁🚗
};
return prefixWithAsciiSeparators<CharPointer_UTF16> (terminalCharPtrs);
}
template <>
SeparatorStrings getSeparators<CharPointer_UTF32>()
{
const std::vector<std::vector<char32_t>> terminalCharPtrs = {
{ 0x00000076, 0x0 }, // v
{ 0x000020aC, 0x0 }, // €
{ 0x00010437, 0x0 }, // 𐐷
{ 0x00000065, 0x0001f603, 0x0 }, // e😃
{ 0x0001f3c1, 0x0001f697, 0x0 } // 🏁🚗
};
return prefixWithAsciiSeparators<CharPointer_UTF32> (terminalCharPtrs);
}
template <typename TestFunction>
void withAllPrefixesAndSuffixes (const std::vector<MemoryBlock>& prefixes,
const std::vector<MemoryBlock>& suffixes,
const std::vector<MemoryBlock>& testValues,
TestFunction&& test)
{
for (const auto& prefix : prefixes)
{
for (const auto& testValue : testValues)
{
MemoryBlock testBlock = prefix;
testBlock.append (testValue.getData(), testValue.getSize());
for (const auto& suffix : suffixes)
{
MemoryBlock data = testBlock;
data.append (suffix.getData(), suffix.getSize());
test (data, suffix);
}
}
}
}
template <typename CharPointerType>
class CharacterFunctionsTests final : public UnitTest
{
public:
using CharType = typename CharPointerType::CharType;
CharacterFunctionsTests()
: UnitTest ("CharacterFunctions", UnitTestCategories::text)
{}
void runTest() override
{
beginTest ("readDoubleValue");
const std::pair<const char*, double> trials[] =
{
// Integers
CHARPTR_DOUBLE_PAIR_COMBOS (0),
CHARPTR_DOUBLE_PAIR_COMBOS (3),
CHARPTR_DOUBLE_PAIR_COMBOS (4931),
CHARPTR_DOUBLE_PAIR_COMBOS (5000),
CHARPTR_DOUBLE_PAIR_COMBOS (9862097),
// Floating point numbers
CHARPTR_DOUBLE_PAIR_COMBOS (0.),
CHARPTR_DOUBLE_PAIR_COMBOS (9.),
CHARPTR_DOUBLE_PAIR_COMBOS (7.000),
CHARPTR_DOUBLE_PAIR_COMBOS (0.2),
CHARPTR_DOUBLE_PAIR_COMBOS (.298630),
CHARPTR_DOUBLE_PAIR_COMBOS (1.118),
CHARPTR_DOUBLE_PAIR_COMBOS (0.9000),
CHARPTR_DOUBLE_PAIR_COMBOS (0.0000001),
CHARPTR_DOUBLE_PAIR_COMBOS (500.0000001),
CHARPTR_DOUBLE_PAIR_COMBOS (9862098.2398604),
// Exponents
CHARPTR_DOUBLE_PAIR_COMBOS (0e0),
CHARPTR_DOUBLE_PAIR_COMBOS (0.e0),
CHARPTR_DOUBLE_PAIR_COMBOS (0.00000e0),
CHARPTR_DOUBLE_PAIR_COMBOS (.0e7),
CHARPTR_DOUBLE_PAIR_COMBOS (0e-5),
CHARPTR_DOUBLE_PAIR_COMBOS (2E0),
CHARPTR_DOUBLE_PAIR_COMBOS (4.E0),
CHARPTR_DOUBLE_PAIR_COMBOS (1.2000000E0),
CHARPTR_DOUBLE_PAIR_COMBOS (1.2000000E6),
CHARPTR_DOUBLE_PAIR_COMBOS (.398e3),
CHARPTR_DOUBLE_PAIR_COMBOS (10e10),
CHARPTR_DOUBLE_PAIR_COMBOS (1.4962e+2),
CHARPTR_DOUBLE_PAIR_COMBOS (3198693.0973e4),
CHARPTR_DOUBLE_PAIR_COMBOS (10973097.2087E-4),
CHARPTR_DOUBLE_PAIR_COMBOS (1.3986e00006),
CHARPTR_DOUBLE_PAIR_COMBOS (2087.3087e+00006),
CHARPTR_DOUBLE_PAIR_COMBOS (6.0872e-00006),
CHARPTR_DOUBLE_PAIR_COMBOS (1.7976931348623157e+308),
CHARPTR_DOUBLE_PAIR_COMBOS (2.2250738585072014e-308),
CHARPTR_DOUBLE_PAIR_COMBOS (17654321098765432.9),
CHARPTR_DOUBLE_PAIR_COMBOS (183456789012345678.9),
CHARPTR_DOUBLE_PAIR_COMBOS (1934567890123456789.9),
CHARPTR_DOUBLE_PAIR_COMBOS (20345678901234567891.9),
CHARPTR_DOUBLE_PAIR_COMBOS (10000000000000000303786028427003666890752.000000),
CHARPTR_DOUBLE_PAIR_COMBOS (10000000000000000303786028427003666890752e3),
CHARPTR_DOUBLE_PAIR_COMBOS (10000000000000000303786028427003666890752e100),
CHARPTR_DOUBLE_PAIR_COMBOS (10000000000000000303786028427003666890752.000000e-5),
CHARPTR_DOUBLE_PAIR_COMBOS (10000000000000000303786028427003666890752.000005e-40),
CHARPTR_DOUBLE_PAIR_COMBOS (1.23456789012345678901234567890),
CHARPTR_DOUBLE_PAIR_COMBOS (1.23456789012345678901234567890e-111),
};
auto asciiToMemoryBlock = [] (const char* asciiPtr, bool removeNullTerminator)
{
auto block = memoryBlockFromCharPtr<CharPointer_ASCII> (asciiPtr);
return convert<CharPointer_ASCII, CharPointerType> (block, removeNullTerminator);
};
const auto separators = getSeparators<CharPointerType>();
for (const auto& trial : trials)
{
for (const auto& terminal : separators.terminals)
{
MemoryBlock data { asciiToMemoryBlock (trial.first, true) };
data.append (terminal.getData(), terminal.getSize());
CharPointerType charPtr { (CharType*) data.getData() };
expectEquals (CharacterFunctions::readDoubleValue (charPtr), trial.second);
expect (*charPtr == *(CharPointerType ((CharType*) terminal.getData())));
}
}
auto asciiToMemoryBlocks = [&] (const std::vector<const char*>& asciiPtrs, bool removeNullTerminator)
{
std::vector<MemoryBlock> result;
for (auto* ptr : asciiPtrs)
result.push_back (asciiToMemoryBlock (ptr, removeNullTerminator));
return result;
};
std::vector<const char*> prefixCharPtrs = { "" , "+", "-" };
const auto prefixes = asciiToMemoryBlocks (prefixCharPtrs, true);
{
std::vector<const char*> nanCharPtrs = { "NaN", "nan", "NAN", "naN" };
auto nans = asciiToMemoryBlocks (nanCharPtrs, true);
withAllPrefixesAndSuffixes (prefixes, separators.terminals, nans, [this] (const MemoryBlock& data,
const MemoryBlock& suffix)
{
CharPointerType charPtr { (CharType*) data.getData() };
expect (std::isnan (CharacterFunctions::readDoubleValue (charPtr)));
expect (*charPtr == *(CharPointerType ((CharType*) suffix.getData())));
});
}
{
std::vector<const char*> infCharPtrs = { "Inf", "inf", "INF", "InF", "1.0E1024", "1.23456789012345678901234567890e123456789" };
auto infs = asciiToMemoryBlocks (infCharPtrs, true);
withAllPrefixesAndSuffixes (prefixes, separators.terminals, infs, [this] (const MemoryBlock& data,
const MemoryBlock& suffix)
{
CharPointerType charPtr { (CharType*) data.getData() };
auto expected = charPtr[0] == '-' ? -std::numeric_limits<double>::infinity()
: std::numeric_limits<double>::infinity();
expectEquals (CharacterFunctions::readDoubleValue (charPtr), expected);
expect (*charPtr == *(CharPointerType ((CharType*) suffix.getData())));
});
}
{
std::vector<const char*> zeroCharPtrs = { "1.0E-400", "1.23456789012345678901234567890e-123456789" };
auto zeros = asciiToMemoryBlocks (zeroCharPtrs, true);
withAllPrefixesAndSuffixes (prefixes, separators.terminals, zeros, [this] (const MemoryBlock& data,
const MemoryBlock& suffix)
{
CharPointerType charPtr { (CharType*) data.getData() };
auto expected = charPtr[0] == '-' ? -0.0 : 0.0;
expectEquals (CharacterFunctions::readDoubleValue (charPtr), expected);
expect (*charPtr == *(CharPointerType ((CharType*) suffix.getData())));
});
}
{
for (const auto& n : separators.nulls)
{
MemoryBlock data { n.getData(), n.getSize() };
CharPointerType charPtr { (CharType*) data.getData() };
expectEquals (CharacterFunctions::readDoubleValue (charPtr), 0.0);
expect (charPtr == CharPointerType { (CharType*) data.getData() }.findEndOfWhitespace());
}
}
}
};
static CharacterFunctionsTests<CharPointer_ASCII> characterFunctionsTestsAscii;
static CharacterFunctionsTests<CharPointer_UTF8> characterFunctionsTestsUtf8;
static CharacterFunctionsTests<CharPointer_UTF16> characterFunctionsTestsUtf16;
static CharacterFunctionsTests<CharPointer_UTF32> characterFunctionsTestsUtf32;
}
#endif
} // namespace juce
|