Skip to content

Commit 7fac56f

Browse files
committed
refactor(string): Change Unicode_To_Utf8 and Utf8_To_Unicode to return bool on failure
1 parent 1918826 commit 7fac56f

4 files changed

Lines changed: 14 additions & 8 deletions

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,8 @@ void AsciiString::translate(const UnicodeString& stringSrc)
280280
if (size == 0)
281281
return;
282282
char* buf = getBufferForRead((Int)size);
283-
Unicode_To_Utf8(buf, src, size + 1);
283+
if (!Unicode_To_Utf8(buf, src, size + 1))
284+
clear();
284285
validate();
285286
}
286287

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ void UnicodeString::translate(const AsciiString& stringSrc)
229229
if (size == 0)
230230
return;
231231
WideChar* buf = getBufferForRead((Int)size);
232-
Utf8_To_Unicode(buf, src, size + 1);
232+
if (!Utf8_To_Unicode(buf, src, size + 1))
233+
clear();
233234
validate();
234235
}
235236

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,22 +96,24 @@ size_t Get_Unicode_Size(const char* src)
9696
return (wchars > 1) ? (size_t)(wchars - 1) : 0;
9797
}
9898

99-
void 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 dest_size)
100100
{
101101
if (dest_size == 0)
102-
return;
102+
return false;
103103
int result = WideCharToMultiByte(CP_UTF8, 0, src, -1, dest, (int)dest_size, nullptr, nullptr);
104104
if (result == 0)
105105
dest[0] = '\0';
106+
return result != 0;
106107
}
107108

108-
void 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 dest_size)
109110
{
110111
if (dest_size == 0)
111-
return;
112+
return false;
112113
int result = MultiByteToWideChar(CP_UTF8, 0, src, -1, dest, (int)dest_size);
113114
if (result == 0)
114115
dest[0] = L'\0';
116+
return result != 0;
115117
}
116118

117119
#else

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ size_t Get_Utf8_Size(const wchar_t* src);
4242
size_t Get_Unicode_Size(const char* src);
4343

4444
// Converts a wide character string to UTF-8. dest_size is in bytes.
45-
void Unicode_To_Utf8(char* dest, const wchar_t* src, size_t dest_size);
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);
4647

4748
// Converts a UTF-8 string to wide characters. dest_size is in wchar_t elements.
48-
void Utf8_To_Unicode(wchar_t* dest, const char* src, size_t dest_size);
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);

0 commit comments

Comments
 (0)