-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1.0.10.0 - provides unicode encoders
- Loading branch information
Showing
8 changed files
with
157 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#include "stdafx.h" | ||
#include "vcstring.h" | ||
|
||
#include <KR3/win/handle.h> | ||
|
||
using namespace kr; | ||
|
||
struct Ucrtbase | ||
{ | ||
void* (*malloc)(size_t size); | ||
void(*free)(void* ptr); | ||
|
||
Ucrtbase() noexcept | ||
{ | ||
HMODULE dll = GetModuleHandleW(L"ucrtbase.dll"); | ||
_assert(dll != nullptr); | ||
malloc = (autoptr)GetProcAddress(dll, "malloc"); | ||
free = (autoptr)GetProcAddress(dll, "free"); | ||
} | ||
}; | ||
namespace { | ||
Ucrtbase ucrtbase; | ||
} | ||
|
||
const char String<char>::ERRMSG[BUF_SIZE] = "[out of memory]"; | ||
const char16_t String<char16_t>::ERRMSG[BUF_SIZE] = u"[OOMem]"; | ||
|
||
template <typename CHR> | ||
CHR* String<CHR>::data() noexcept { | ||
if (capacity >= BUF_SIZE) return pointer; | ||
else return buffer; | ||
} | ||
template <typename CHR> | ||
CHR* String<CHR>::resize(size_t nsize) noexcept { | ||
if (nsize > capacity) | ||
{ | ||
if (capacity >= BUF_SIZE) String_free16(pointer, capacity); | ||
capacity = nsize; | ||
pointer = (CHR*)String_allocate16(nsize + 1); | ||
if (pointer == nullptr) | ||
{ | ||
memcpy(buffer, "[out of memory]", 16); | ||
capacity = BUF_SIZE-1; | ||
size = BUF_SIZE-1; | ||
} | ||
return pointer; | ||
} | ||
else | ||
{ | ||
return data(); | ||
} | ||
} | ||
template <typename CHR> | ||
void String<CHR>::assign(kr::Text text) noexcept { | ||
size_t nsize = text.size(); | ||
CHR* dest = resize(nsize); | ||
memcpy(dest, text.data(), nsize); | ||
dest[nsize] = (CHR)'\0'; | ||
size = nsize; | ||
} | ||
|
||
template String<char>; | ||
template String<char16_t>; | ||
|
||
void* String_allocate16(size_t size) noexcept { | ||
if (size >= 0x1000) { | ||
uintptr_t res = (uintptr_t)ucrtbase.malloc(size + 0x27); | ||
if (res == 0) return nullptr; | ||
void** out = (void**)((res + 0x27) & (~0x1f)); | ||
out[-1] = (void*)res; | ||
return out; | ||
} | ||
else { | ||
return ucrtbase.malloc(size); | ||
} | ||
} | ||
|
||
void String_free16(void* p, size_t size) noexcept { | ||
if (size >= 0x1000) { | ||
p = ((void**)p)[-1]; | ||
} | ||
ucrtbase.free(p); | ||
} | ||
String<char16_t>* String_toWide(String<char16_t>* out_str, StringSpan<char>* in_str) noexcept { | ||
Utf8ToUtf16 cvt = Text(in_str->data, in_str->size); | ||
out_str->init(cvt); | ||
return out_str; | ||
} | ||
String<char>* String_toUtf8(String<char>* out_str, StringSpan<char16_t>* in_str) noexcept { | ||
Utf16ToUtf8 cvt = Text16(in_str->data, in_str->size); | ||
out_str->init(cvt); | ||
return out_str; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#pragma once | ||
|
||
void* String_allocate16(size_t size) noexcept; | ||
void String_free16(void* p, size_t size) noexcept; | ||
|
||
template<typename CHR> | ||
struct String { | ||
static constexpr size_t BUF_SIZE = 16 / sizeof(CHR); | ||
static const CHR ERRMSG[BUF_SIZE]; | ||
|
||
union { | ||
CHR buffer[BUF_SIZE]; | ||
CHR* pointer; | ||
}; | ||
size_t size; | ||
size_t capacity; | ||
|
||
CHR* data() noexcept; | ||
CHR* resize(size_t nsize) noexcept; | ||
void assign(kr::Text text) noexcept; | ||
|
||
template <typename Derived, typename Component, typename Parent> | ||
void init(const kr::HasCopyTo<Derived, Component, Parent> &text) noexcept { | ||
size_t sz = text.size(); | ||
|
||
if (sz < BUF_SIZE) { | ||
size = sz; | ||
capacity = BUF_SIZE-1; | ||
text.copyTo(buffer); | ||
buffer[sz] = (CHR)'\0'; | ||
} | ||
else { | ||
pointer = (CHR*)String_allocate16(sz*sizeof(CHR) + sizeof(CHR)); | ||
text.copyTo(pointer); | ||
pointer[sz] = (CHR)'\0'; | ||
capacity = size = sz; | ||
} | ||
} | ||
}; | ||
|
||
template <typename CHR> | ||
struct StringSpan { | ||
size_t size; | ||
const CHR* data; | ||
}; | ||
|
||
String<char16_t>* String_toWide(String<char16_t>* out_str, StringSpan<char>* in_str) noexcept; | ||
String<char>* String_toUtf8(String<char>* out_str, StringSpan<char16_t>* in_str) noexcept; | ||
|
||
extern template String<char>; | ||
extern template String<char16_t>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
|
||
set BDSX_CORE_VERSION=1.0.9.3 | ||
set BDSX_CORE_VERSION=1.0.10.0 |