Fix StringUtils::ConvertANSI2UTF16 to properly handle multi-byte characters Length

#1225
This commit is contained in:
Wojtek Figat
2023-07-03 12:10:06 +02:00
parent 9a1fd12a85
commit 9d640656e6
9 changed files with 26 additions and 19 deletions

View File

@@ -179,7 +179,7 @@ public:
public:
// Converts characters from ANSI to UTF-16
static void ConvertANSI2UTF16(const char* from, Char* to, int32 len);
static void ConvertANSI2UTF16(const char* from, Char* to, int32 fromLength, int32& toLength);
// Converts characters from UTF-16 to ANSI
static void ConvertUTF162ANSI(const Char* from, char* to, int32 len);

View File

@@ -311,14 +311,14 @@ static inline uint32 Utf8ToUtf32Codepoint(const char* src, int32 length)
}
}
void StringUtils::ConvertANSI2UTF16(const char* from, Char* to, int32 len)
void StringUtils::ConvertANSI2UTF16(const char* from, Char* to, int32 fromLength, int32& toLength)
{
const char* const u8end = from + len;
const char* const u8end = from + fromLength;
const char* u8cur = from;
char16_t* u16cur = to;
while (u8cur < u8end)
{
len = Utf8CodepointLength(*u8cur);
int32 len = Utf8CodepointLength(*u8cur);
uint32 codepoint = Utf8ToUtf32Codepoint(u8cur, len);
// Convert the UTF32 codepoint to one or more UTF16 codepoints
@@ -336,6 +336,7 @@ void StringUtils::ConvertANSI2UTF16(const char* from, Char* to, int32 len)
}
u8cur += len;
}
toLength = (int32)(u16cur - to);
}
static const char32_t kByteMask = 0x000000BF;

View File

@@ -179,10 +179,12 @@ const char* StringUtils::Find(const char* str, const char* toFind)
return strstr(str, toFind);
}
void StringUtils::ConvertANSI2UTF16(const char* from, Char* to, int32 len)
void StringUtils::ConvertANSI2UTF16(const char* from, Char* to, int32 fromLength, int32& toLength)
{
if (len)
mbstowcs(to, from, len);
if (fromLength)
toLength = mbstowcs(to, from, fromLength);
else
toLength = 0;
}
void StringUtils::ConvertUTF162ANSI(const Char* from, char* to, int32 len)

View File

@@ -992,7 +992,8 @@ void ReadPipe(HANDLE pipe, Array<char>& rawData, Array<Char>& logData, LogType l
// Log contents
logData.Clear();
logData.Resize(rawData.Count() + 1);
StringUtils::ConvertANSI2UTF16(rawData.Get(), logData.Get(), rawData.Count());
int32 tmp;
StringUtils::ConvertANSI2UTF16(rawData.Get(), logData.Get(), rawData.Count(), tmp);
logData.Last() = '\0';
if (settings.LogOutput)
Log::Logger::Write(logType, StringView(logData.Get(), rawData.Count()));