mirror of
https://github.com/OpenFusionProject/OpenFusion.git
synced 2025-12-22 12:00:03 +00:00
U8toU16 now respects buffer sizes
This commit is contained in:
@@ -131,12 +131,15 @@ std::string U16toU8(char16_t* src) {
|
||||
}
|
||||
|
||||
// returns number of char16_t that was written at des
|
||||
size_t U8toU16(std::string src, char16_t* des) {
|
||||
size_t U8toU16(std::string src, char16_t* des, size_t max) {
|
||||
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>,char16_t> convert;
|
||||
std::u16string tmp = convert.from_bytes(src);
|
||||
|
||||
// copy utf16 string to buffer
|
||||
memcpy(des, tmp.c_str(), sizeof(char16_t) * tmp.length());
|
||||
if (sizeof(char16_t) * tmp.length() > max) // make sure we don't write outside the buffer
|
||||
memcpy(des, tmp.c_str(), sizeof(char16_t) * max);
|
||||
else
|
||||
memcpy(des, tmp.c_str(), sizeof(char16_t) * tmp.length());
|
||||
des[tmp.length()] = '\0';
|
||||
|
||||
return tmp.length();
|
||||
|
||||
Reference in New Issue
Block a user