Skip to content

Commit 1918826

Browse files
committed
refactor(string): Change Get_Utf8_Size and Get_Unicode_Size to exclude null terminator
1 parent c110f97 commit 1918826

5 files changed

Lines changed: 22 additions & 18 deletions

File tree

Core/GameEngine/Source/Common/System/AsciiString.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,11 @@ void AsciiString::translate(const UnicodeString& stringSrc)
276276
// TheSuperHackers @fix bobtista 02/04/2026 Implement UTF-8 conversion replacing 7-bit ASCII only implementation
277277
clear();
278278
const WideChar* src = stringSrc.str();
279-
if (!src[0])
280-
return;
281279
size_t size = Get_Utf8_Size(src);
282-
char* buf = getBufferForRead((Int)(size - 1));
283-
Unicode_To_Utf8(buf, src, size);
280+
if (size == 0)
281+
return;
282+
char* buf = getBufferForRead((Int)size);
283+
Unicode_To_Utf8(buf, src, size + 1);
284284
validate();
285285
}
286286

Core/GameEngine/Source/Common/System/UnicodeString.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,11 @@ void UnicodeString::translate(const AsciiString& stringSrc)
225225
// TheSuperHackers @fix bobtista 02/04/2026 Implement UTF-8 conversion replacing 7-bit ASCII only implementation
226226
clear();
227227
const char* src = stringSrc.str();
228-
if (!src[0])
229-
return;
230228
size_t size = Get_Unicode_Size(src);
231-
WideChar* buf = getBufferForRead((Int)(size - 1));
232-
Utf8_To_Unicode(buf, src, size);
229+
if (size == 0)
230+
return;
231+
WideChar* buf = getBufferForRead((Int)size);
232+
Utf8_To_Unicode(buf, src, size + 1);
233233
validate();
234234
}
235235

Core/GameEngine/Source/GameNetwork/GameSpy/Thread/ThreadUtils.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@
3636
std::wstring MultiByteToWideCharSingleLine( const char *orig )
3737
{
3838
size_t size = Get_Unicode_Size(orig);
39-
std::wstring ret(size - 1, L'\0');
40-
Utf8_To_Unicode(&ret[0], orig, size);
39+
if (size == 0)
40+
return std::wstring();
41+
std::wstring ret(size, L'\0');
42+
Utf8_To_Unicode(&ret[0], orig, size + 1);
4143
WideChar *c = nullptr;
4244
do
4345
{
@@ -64,8 +66,10 @@ std::wstring MultiByteToWideCharSingleLine( const char *orig )
6466
std::string WideCharStringToMultiByte( const WideChar *orig )
6567
{
6668
size_t size = Get_Utf8_Size(orig);
67-
std::string ret(size - 1, '\0');
68-
Unicode_To_Utf8(&ret[0], orig, size);
69+
if (size == 0)
70+
return std::string();
71+
std::string ret(size, '\0');
72+
Unicode_To_Utf8(&ret[0], orig, size + 1);
6973
return ret;
7074
}
7175

Core/Libraries/Source/WWVegas/WWLib/utf8.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,13 @@ bool Utf8_Validate(const char* str, size_t length)
8787
size_t Get_Utf8_Size(const wchar_t* src)
8888
{
8989
int bytes = WideCharToMultiByte(CP_UTF8, 0, src, -1, nullptr, 0, nullptr, nullptr);
90-
return (bytes > 0) ? (size_t)bytes : 1;
90+
return (bytes > 1) ? (size_t)(bytes - 1) : 0;
9191
}
9292

9393
size_t Get_Unicode_Size(const char* src)
9494
{
9595
int wchars = MultiByteToWideChar(CP_UTF8, 0, src, -1, nullptr, 0);
96-
return (wchars > 0) ? (size_t)wchars : 1;
96+
return (wchars > 1) ? (size_t)(wchars - 1) : 0;
9797
}
9898

9999
void Unicode_To_Utf8(char* dest, const wchar_t* src, size_t dest_size)

Core/Libraries/Source/WWVegas/WWLib/utf8.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ size_t Utf8_Trailing_Invalid_Bytes(const char* str, size_t length);
3333
bool Utf8_Validate(const char* str);
3434
bool Utf8_Validate(const char* str, size_t length);
3535

36-
// Returns the number of bytes required to store the UTF-8 representation of src,
37-
// including the null terminator.
36+
// Returns the number of bytes in the UTF-8 representation of src, excluding the
37+
// null terminator. Returns 0 on failure or if src is empty.
3838
size_t Get_Utf8_Size(const wchar_t* src);
3939

40-
// Returns the number of wchar_t elements required to store the wide character
41-
// representation of the UTF-8 string src, including the null terminator.
40+
// Returns the number of wchar_t elements in the wide character representation of
41+
// the UTF-8 string src, excluding the null terminator. Returns 0 on failure or if src is empty.
4242
size_t Get_Unicode_Size(const char* src);
4343

4444
// Converts a wide character string to UTF-8. dest_size is in bytes.

0 commit comments

Comments
 (0)