Avoid address sanitizer error

This commit is contained in:
Benau 2022-05-04 10:42:16 +08:00
parent 281ddb1b17
commit d2da2e22e1

View File

@ -245,34 +245,32 @@ namespace StringUtils
* code. */ * code. */
inline irr::core::stringw getCountryFlag(const std::string& country_code) inline irr::core::stringw getCountryFlag(const std::string& country_code)
{ {
irr::core::stringw result;
if (country_code.empty() || country_code.size() != 2) if (country_code.empty() || country_code.size() != 2)
return L""; return result;
uint32_t flag[3] = uint32_t flag[2] =
{ {
(uint32_t)(country_code[0]) + 127397, (uint32_t)(country_code[0]) + 127397,
(uint32_t)(country_code[1]) + 127397, (uint32_t)(country_code[1]) + 127397
0
}; };
if (sizeof(wchar_t) == 4) if (sizeof(wchar_t) == 4)
{ {
return (wchar_t*)flag; result.reserve(2);
result.append((wchar_t)flag[0]);
result.append((wchar_t)flag[1]);
} }
else if (sizeof(wchar_t) == 2) else if (sizeof(wchar_t) == 2)
{ {
flag[0] -= 0x10000; flag[0] -= 0x10000;
flag[1] -= 0x10000; flag[1] -= 0x10000;
wchar_t u16[5] = result.reserve(4);
{
//make a surrogate pair //make a surrogate pair
static_cast<wchar_t>((flag[0] >> 10) + 0xd800), result.append(static_cast<wchar_t>((flag[0] >> 10) + 0xd800));
static_cast<wchar_t>((flag[0] & 0x3ff) + 0xdc00), result.append(static_cast<wchar_t>((flag[0] & 0x3ff) + 0xdc00));
static_cast<wchar_t>((flag[1] >> 10) + 0xd800), result.append(static_cast<wchar_t>((flag[1] >> 10) + 0xd800));
static_cast<wchar_t>((flag[1] & 0x3ff) + 0xdc00), result.append(static_cast<wchar_t>((flag[1] & 0x3ff) + 0xdc00));
0
};
return u16;
} }
return L""; return result;
} // getCountryFlag } // getCountryFlag
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------