Skip to content

Commit 452a8bf

Browse files
committed
refactor(string): Add srcLen parameter to conversion functions to avoid double string scan
1 parent 7fac56f commit 452a8bf

5 files changed

Lines changed: 41 additions & 31 deletions

File tree

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,12 +276,15 @@ 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-
size_t size = Get_Utf8_Size(src);
279+
size_t srcLen = wcslen(src);
280+
size_t size = Get_Utf8_Size(src, srcLen);
280281
if (size == 0)
281282
return;
282283
char* buf = getBufferForRead((Int)size);
283-
if (!Unicode_To_Utf8(buf, src, size + 1))
284+
if (!Unicode_To_Utf8(buf, src, srcLen, size))
284285
clear();
286+
else
287+
buf[size] = '\0';
285288
validate();
286289
}
287290

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,15 @@ 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-
size_t size = Get_Unicode_Size(src);
228+
size_t srcLen = strlen(src);
229+
size_t size = Get_Unicode_Size(src, srcLen);
229230
if (size == 0)
230231
return;
231232
WideChar* buf = getBufferForRead((Int)size);
232-
if (!Utf8_To_Unicode(buf, src, size + 1))
233+
if (!Utf8_To_Unicode(buf, src, srcLen, size))
233234
clear();
235+
else
236+
buf[size] = L'\0';
234237
validate();
235238
}
236239

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@
3535
// TheSuperHackers @refactor bobtista 02/04/2026 Use WWLib UTF-8 functions instead of raw Win32 API calls
3636
std::wstring MultiByteToWideCharSingleLine( const char *orig )
3737
{
38-
size_t size = Get_Unicode_Size(orig);
38+
size_t srcLen = strlen(orig);
39+
size_t size = Get_Unicode_Size(orig, srcLen);
3940
if (size == 0)
4041
return std::wstring();
4142
std::wstring ret(size, L'\0');
42-
Utf8_To_Unicode(&ret[0], orig, size + 1);
43+
Utf8_To_Unicode(&ret[0], orig, srcLen, size);
4344
WideChar *c = nullptr;
4445
do
4546
{
@@ -65,11 +66,12 @@ std::wstring MultiByteToWideCharSingleLine( const char *orig )
6566

6667
std::string WideCharStringToMultiByte( const WideChar *orig )
6768
{
68-
size_t size = Get_Utf8_Size(orig);
69+
size_t srcLen = wcslen(orig);
70+
size_t size = Get_Utf8_Size(orig, srcLen);
6971
if (size == 0)
7072
return std::string();
7173
std::string ret(size, '\0');
72-
Unicode_To_Utf8(&ret[0], orig, size + 1);
74+
Unicode_To_Utf8(&ret[0], orig, srcLen, size);
7375
return ret;
7476
}
7577

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,33 +84,33 @@ bool Utf8_Validate(const char* str, size_t length)
8484
return true;
8585
}
8686

87-
size_t Get_Utf8_Size(const wchar_t* src)
87+
size_t Get_Utf8_Size(const wchar_t* src, size_t srcLen)
8888
{
89-
int bytes = WideCharToMultiByte(CP_UTF8, 0, src, -1, nullptr, 0, nullptr, nullptr);
90-
return (bytes > 1) ? (size_t)(bytes - 1) : 0;
89+
int bytes = WideCharToMultiByte(CP_UTF8, 0, src, (int)srcLen, nullptr, 0, nullptr, nullptr);
90+
return (bytes > 0) ? (size_t)bytes : 0;
9191
}
9292

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

99-
bool Unicode_To_Utf8(char* dest, const wchar_t* src, size_t dest_size)
99+
bool Unicode_To_Utf8(char* dest, const wchar_t* src, size_t srcLen, size_t dest_size)
100100
{
101101
if (dest_size == 0)
102102
return false;
103-
int result = WideCharToMultiByte(CP_UTF8, 0, src, -1, dest, (int)dest_size, nullptr, nullptr);
103+
int result = WideCharToMultiByte(CP_UTF8, 0, src, (int)srcLen, dest, (int)dest_size, nullptr, nullptr);
104104
if (result == 0)
105105
dest[0] = '\0';
106106
return result != 0;
107107
}
108108

109-
bool Utf8_To_Unicode(wchar_t* dest, const char* src, size_t dest_size)
109+
bool Utf8_To_Unicode(wchar_t* dest, const char* src, size_t srcLen, size_t dest_size)
110110
{
111111
if (dest_size == 0)
112112
return false;
113-
int result = MultiByteToWideChar(CP_UTF8, 0, src, -1, dest, (int)dest_size);
113+
int result = MultiByteToWideChar(CP_UTF8, 0, src, (int)srcLen, dest, (int)dest_size);
114114
if (result == 0)
115115
dest[0] = L'\0';
116116
return result != 0;

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,20 @@ 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 in the UTF-8 representation of src, excluding the
37-
// null terminator. Returns 0 on failure or if src is empty.
38-
size_t Get_Utf8_Size(const wchar_t* src);
36+
// Returns the number of bytes in the UTF-8 representation of srcLen wide characters
37+
// from src. Returns 0 on failure or if srcLen is 0.
38+
size_t Get_Utf8_Size(const wchar_t* src, size_t srcLen);
3939

4040
// 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.
42-
size_t Get_Unicode_Size(const char* src);
43-
44-
// Converts a wide character string to UTF-8. dest_size is in bytes.
45-
// Returns true on success, false on failure. On failure, dest[0] is set to '\0'.
46-
bool Unicode_To_Utf8(char* dest, const wchar_t* src, size_t dest_size);
47-
48-
// Converts a UTF-8 string to wide characters. dest_size is in wchar_t elements.
49-
// Returns true on success, false on failure. On failure, dest[0] is set to L'\0'.
50-
bool Utf8_To_Unicode(wchar_t* dest, const char* src, size_t dest_size);
41+
// srcLen bytes from the UTF-8 string src. Returns 0 on failure or if srcLen is 0.
42+
size_t Get_Unicode_Size(const char* src, size_t srcLen);
43+
44+
// Converts srcLen wide characters from src to UTF-8. dest_size is in bytes.
45+
// Does not write a null terminator. Returns true on success, false on failure.
46+
// On failure, dest[0] is set to '\0'.
47+
bool Unicode_To_Utf8(char* dest, const wchar_t* src, size_t srcLen, size_t dest_size);
48+
49+
// Converts srcLen bytes from the UTF-8 string src to wide characters. dest_size is in wchar_t elements.
50+
// Does not write a null terminator. Returns true on success, false on failure.
51+
// On failure, dest[0] is set to L'\0'.
52+
bool Utf8_To_Unicode(wchar_t* dest, const char* src, size_t srcLen, size_t dest_size);

0 commit comments

Comments
 (0)