TODO move me
This commit is contained in:
parent
02dc2d59cf
commit
35e4d4bedc
@ -90,6 +90,10 @@ namespace Aurora::Locale::Encoding
|
||||
AuStreamReadWrittenPair_t EncodeUTF8(TypeIn_t binary, AuUInt32 binaryLength, void *utf8, AuUInt32 utfLen)
|
||||
{
|
||||
int offset = 0;
|
||||
|
||||
if (!binary) return {};
|
||||
if (!binaryLength) return {};
|
||||
|
||||
if (!std::exchange(readHeader, true))
|
||||
{
|
||||
if (page == ECodePage::eUnsupported)
|
||||
@ -123,13 +127,21 @@ namespace Aurora::Locale::Encoding
|
||||
binaryLength = binaryLength - offset;
|
||||
|
||||
if (page == ECodePage::eGBK)
|
||||
{
|
||||
binaryLength = GetLenGBKString(reinterpret_cast<TypeCast_t>(binary) + offset, binaryLength);
|
||||
}
|
||||
else if (page == ECodePage::eSJIS)
|
||||
{
|
||||
binaryLength = GetLenSJISString(reinterpret_cast<TypeCast_t>(binary) + offset, binaryLength);
|
||||
}
|
||||
else if ((page == ECodePage::eUTF16) || (page == ECodePage::eUTF16BE))
|
||||
{
|
||||
binaryLength &= ~1;
|
||||
}
|
||||
else if ((page == ECodePage::eUTF32) || (page == ECodePage::eUTF32))
|
||||
{
|
||||
binaryLength &= ~3;
|
||||
}
|
||||
|
||||
auto real = state.CPToUTF8(reinterpret_cast<TypeCast_t>(binary) + offset, binaryLength, utf8, utfLen);
|
||||
return AuMakePair(real.first + offset, real.second);
|
||||
@ -137,10 +149,9 @@ namespace Aurora::Locale::Encoding
|
||||
|
||||
AuStreamReadWrittenPair_t EncodeUTF8(TypeIn_t binary, AuUInt32 binaryLength, AuString &out)
|
||||
{
|
||||
int evil = 4;
|
||||
if (page == ECodePage::eUTF16 || page == ECodePage::eUTF16BE) evil = 2; // utf 16 can only be 1.5x as long taking up 3 bytes
|
||||
if (!AuTryResize(out, binaryLength * evil)) return {};
|
||||
auto main = EncodeUTF8(binary, binaryLength, out.data(), out.size());
|
||||
auto preemptive = EncodeUTF8(binary, binaryLength, nullptr, 0);
|
||||
if (!AuTryResize(out, preemptive.second)) return {};
|
||||
auto main = EncodeUTF8(binary, preemptive.second, out.data(), out.size());
|
||||
if (main.second == 0) return {};
|
||||
if (!AuTryResize(out, main.second)) return {};
|
||||
out.shrink_to_fit();
|
||||
@ -164,9 +175,19 @@ namespace Aurora::Locale::Encoding
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
out.resize(length * 4);
|
||||
auto written = state.UTF8ToCp(utf8In, length, out.data(), AuUInt32(out.size()));
|
||||
|
||||
if (!utf8In)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
if (!length)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
auto preemptive = state.UTF8ToCp(utf8In, length, nullptr, 0);
|
||||
auto written = state.UTF8ToCp(utf8In, preemptive.second, out.data(), AuUInt32(out.size()));
|
||||
out.resize(written.second);
|
||||
out.shrink_to_fit();
|
||||
return written;
|
||||
|
Loading…
Reference in New Issue
Block a user