AuroraRuntime/Source/Locale/Encoding/UTFn/AuUTF32.hpp

55 lines
1.7 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuUTF32.hpp
Date: 2021-10-31
Author: Reece
***/
#pragma once
namespace Aurora::Locale::Encoding::UTF32
{
static void SwapU32(void *base, AuUInt32 count)
{
count *= 4;
for (AuUInt32 i = 0; i < count; i += 4)
{
AuWriteU32BE(base, i, AuReadU32LE(base, i));
}
}
static AuUInt32 Count32(const void *base, AuUInt32 length, bool bytes = false)
{
return bytes ? length & ~3 : (length & ~3) / 4;
}
static AuStreamReadWrittenPair_t UTF8ToCp(const AuMemoryViewRead &utf8, const AuMemoryViewWrite &utf32)
{
const char *begin = utf8.Begin<const char>();
const char *end = utf8.End<const char>();
AuUInt32 *begin2 = utf32.Begin<AuUInt32>();
AuUInt32 *end2 = utf32.End<AuUInt32>();
UTF8::ReadUtf8ByteString(begin2, end2, begin, end);
return AuStreamReadWrittenPair_t {begin - utf8.Begin<const char>(), (begin2 - utf32.Begin<AuUInt32>()) * sizeof(AuUInt32)};
}
static AuStreamReadWrittenPair_t CPToUTF8(const AuMemoryViewRead &utf32, const AuMemoryViewWrite &utf8)
{
const AuUInt32 *begin = utf32.Begin<const AuUInt32>();
const AuUInt32 *end = utf32.End<const AuUInt32>();
char *dest = utf8.Begin<char>();
char *destEnd = utf8.End<char>();
AuUInt32 counter {};
const AuUInt32 *cur = begin;
for (; cur < end; cur++)
{
UTF8::WriteCp(*cur, dest, counter, destEnd - dest);
}
return AuStreamReadWrittenPair_t {(cur - begin) * sizeof(AuUInt32), dest - utf8.Begin<char>()};
}
}