CyberEngineMkIII
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
CYBUTF16String.cpp
Go to the documentation of this file.
1 #include "CYB.hpp"
3 
4 char* CYB::API::String::UTF16::SetupData(const UTF8& AUTF8, unsigned long long& ALengthReference) {
5  const auto BufferSize(Core().FModuleManager.Call<CYB::Platform::Modules::Kernel32::MultiByteToWideChar>(CYB::Platform::Win32::UINT(CP_UTF8), CYB::Platform::Win32::DWORD(0), AUTF8.CString(), -1, nullptr, 0));
6  ALengthReference = BufferSize * sizeof(wchar_t);
7  Assert::LessThan<unsigned long long>(ALengthReference, static_cast<unsigned long long>(std::numeric_limits<int>::max()));
8  auto NewData(static_cast<wchar_t*>(Context().FAllocator.FHeap.Alloc(static_cast<int>(ALengthReference))));
9  if (CYB::Core().FModuleManager.Call<CYB::Platform::Modules::Kernel32::MultiByteToWideChar>(CYB::Platform::Win32::UINT(CP_UTF8), CYB::Platform::Win32::DWORD(0), AUTF8.CString(), -1, NewData, BufferSize) == 0) {
10  Context().FAllocator.FHeap.Free(NewData);
12  }
13  return reinterpret_cast<char*>(NewData);
14 }
16  const auto BufferSize(CYB::Core().FModuleManager.Call<CYB::Platform::Modules::Kernel32::WideCharToMultiByte>(CYB::Platform::Win32::UINT(CP_UTF8), CYB::Platform::Win32::DWORD(0), AWString, -1, nullptr, 0, nullptr, nullptr));
17  auto NewData(static_cast<char*>(Context().FAllocator.FHeap.Alloc(BufferSize)));
18  if (NewData != nullptr && Core().FModuleManager.Call<CYB::Platform::Modules::Kernel32::WideCharToMultiByte>(CYB::Platform::Win32::UINT(CP_UTF8), CYB::Platform::Win32::DWORD(0), AWString, -1, NewData, BufferSize, nullptr, nullptr) == 0)
20  return UTF8(FromData(NewData));
21 }
22 
24  UTF16(AUTF8, 0)
25 {}
26 
27 CYB::API::String::UTF16::UTF16(const UTF8& AUTF8, unsigned long long ALengthReference) :
28  Dynamic(SetupData(AUTF8, ALengthReference))
29 {
30  API::Assert::LessThan(ALengthReference, static_cast<unsigned long long>(std::numeric_limits<int>::max()));
31  FLength = static_cast<int>(ALengthReference);
32 }
33 
34 wchar_t* CYB::API::String::UTF16::WideData(void) noexcept {
35  return reinterpret_cast<wchar_t*>(FData);
36 }
37 const wchar_t* CYB::API::String::UTF16::WString(void) const noexcept {
38  return reinterpret_cast<const wchar_t*>(FData);
39 }
A variable length UTF-8 string.
Definition: UTF8String.hpp:8
UTF16() noexcept=default
Empty string constructor.
API::Interop::Context & Context(void) noexcept
Get the API's Context.
Heap & FHeap
The Heap this allocator uses.
Definition: Allocator.hpp:10
wchar_t * WideData(void) noexcept
Get the underlying wide char array.
UTF-16 String enabled only under windows.
Used for string of allocated variable length.
static void LessThan(const AType &ALHS, const AType &ARHS) noexcept
Less than assertion function. May not be evaluated.
static UTF8 ToUTF8(const wchar_t *AWString)
Create a new UTF8 string given a wide char array.
virtual void Free(void *const APreviousAllocation) noexcept=0
Return memory to the heap. Data in allocated range will be lost.
Exceptions caused by external call failures or invalid external data. Only classifies ones that can p...
Definition: Exception.hpp:65
int FLength
Number of bytes before null terminator in FData.
Allocator & FAllocator
The Allocator.
Definition: Context.hpp:11
const wchar_t * WString(void) const noexcept
Get the underlying const wide char array.
Precompiled header for inter-engine operations.
const char * CString(void) const noexcept
Get the contained const CString.
Engine::Core & Core(void) noexcept
Retrieve the Core singleton.
Definition: CYBCore.cpp:69
static char * SetupData(const UTF8 &AUTF8, unsigned long long &ALengthReference)
Construct a raw allocated UTF16 wide char array casted to a char array given a UTF8 string...