1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

String: Fix the string length being passed in a UTF conversion test

This commit is contained in:
Anthony Nicholls 2024-11-25 13:37:51 +00:00
parent bd322d0f78
commit 0823ee6aed

View file

@ -2375,24 +2375,25 @@ public:
const String s (createRandomWideCharString (r, stringLength)); const String s (createRandomWideCharString (r, stringLength));
using CharType = typename CharPointerType::CharType; using CharType = typename CharPointerType::CharType;
CharType buffer[300]; constexpr auto bytesPerCodeUnit = sizeof (CharType);
constexpr auto maxCodeUnitsPerCodePoint = 4 / bytesPerCodeUnit;
memset (buffer, 0xff, sizeof (buffer)); std::array<CharType, stringLength * maxCodeUnitsPerCodePoint + 1> codeUnits{};
CharPointerType (buffer).writeAll (s.toUTF32()); const auto codeUnitsSizeInBytes = codeUnits.size() * bytesPerCodeUnit;
test.expectEquals (String (CharPointerType (buffer)), s);
memset (buffer, 0xff, sizeof (buffer)); std::memset (codeUnits.data(), 0xff, codeUnitsSizeInBytes);
CharPointerType (buffer).writeAll (s.toUTF16()); CharPointerType (codeUnits.data()).writeAll (s.toUTF32());
test.expectEquals (String (CharPointerType (buffer)), s); test.expectEquals (String (CharPointerType (codeUnits.data())), s);
memset (buffer, 0xff, sizeof (buffer)); std::memset (codeUnits.data(), 0xff, codeUnitsSizeInBytes);
CharPointerType (buffer).writeAll (s.toUTF8()); CharPointerType (codeUnits.data()).writeAll (s.toUTF16());
test.expectEquals (String (CharPointerType (buffer)), s); test.expectEquals (String (CharPointerType (codeUnits.data())), s);
const auto nullTerminator = std::find (buffer, buffer + std::size (buffer), (CharType) 0); std::memset (codeUnits.data(), 0xff, codeUnitsSizeInBytes);
const auto numValidBytes = (int) std::distance (buffer, nullTerminator) * (int) sizeof (CharType); CharPointerType (codeUnits.data()).writeAll (s.toUTF8());
test.expectEquals (String (CharPointerType (codeUnits.data())), s);
test.expect (CharPointerType::isValidString (buffer, numValidBytes)); test.expect (CharPointerType::isValidString (codeUnits.data(), codeUnitsSizeInBytes));
} }
}; };