forked from electronicarts/CnC_Generals_Zero_Hour
-
Notifications
You must be signed in to change notification settings - Fork 250
feat(string): Implement UTF-8 string conversion and validation functions #2528
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
xezon
merged 26 commits into
TheSuperHackers:main
from
bobtista:bobtista/feat/utf8-string-functions
Aug 18, 2026
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
40393b8
feat(utf8): add UTF-8 string conversion and validation functions
bobtista abb71f0
refactor(utf8): Return size_t from conversions, use consistent len na…
bobtista 0c9074d
refactor(utf8): Update callers to use new conversion API
bobtista 149a07f
refactor(utf8): rename to Utf16Le_To_Utf8 and return required size on…
bobtista 6097799
refactor(utf8): add writeDirect mode, use _Len helpers, const locals,…
bobtista 9078de5
refactor(utf8): simplify conversion API and reject UTF-16 surrogates
bobtista 327bb4b
style(utf8): assert after write, add braces, const locals
bobtista 4d5d2dc
style(utf8): Use >= 0 in length return ternaries
bobtista f582ac8
refactor(utf8): remove unused validators and simplify conversion fail…
bobtista 40683de
style(string): add braces to translate conversion check
bobtista 44f8fca
fix(string): return empty string when ThreadUtils conversion fails
bobtista fc3add5
refactor(string): drop unrelated ensureUniqueBufferOfSize null-termin…
bobtista ce8a2f4
refactor(utf8): replace Win32 wrappers with portable RFC 3629 transcoder
bobtista d4a7e73
fix(string): fall back to 1:1 byte cast for non-UTF-8 data in Unicode…
bobtista e0a425e
fix(utf8): Substitute U+FFFD for wide values with no UTF-8 representa…
bobtista 159da01
refactor(utf8): Return UTF8_INVALID instead of 0 for malformed UTF-8
bobtista 51a0460
fix(gamespy): Fall back to byte cast for non-UTF-8 text in MultiByteT…
bobtista 1a58d95
refactor(utf8): Rename conversions to Wide_To_Utf8 and Utf8_To_Wide
bobtista 81a47bf
refactor(utf8): Remove unused Utf8_Validate
bobtista d98492c
build: Use qualified include path for WWLib utf8.h
bobtista ce1cae6
refactor(utf8): Give Wide_Read the same interface as Utf8_Decode
bobtista 61a1839
refactor(utf8): Return the size the whole conversion needs so callers…
bobtista ef2adc5
refactor(string): Name the conversion destination length dstLen
bobtista 4c4aca2
chore(utf8): Clarify the return value documentation of the conversion…
bobtista d6835f3
refactor(utf8): Name the conversion length accumulators needed
bobtista 3d82ce3
refactor(string): Order UnicodeString::translate branches by likelihood
bobtista File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,18 +28,37 @@ | |
|
|
||
| #include "PreRTS.h" // This must go first in EVERY cpp file in the GameEngine | ||
|
|
||
| #include "WWLib/utf8.h" | ||
|
|
||
| //------------------------------------------------------------------------- | ||
|
|
||
| // TheSuperHackers @refactor bobtista 02/04/2026 Use WWLib UTF-8 functions instead of raw Win32 API calls | ||
| std::wstring MultiByteToWideCharSingleLine( const char *orig ) | ||
| { | ||
| Int len = strlen(orig); | ||
| WideChar *dest = NEW WideChar[len+1]; | ||
|
|
||
| MultiByteToWideChar(CP_UTF8, 0, orig, -1, dest, len); | ||
| const size_t srcLen = strlen(orig); | ||
| const size_t dstLen = Utf8_To_Wide_Len(orig, srcLen); | ||
| if (dstLen == 0) | ||
| return std::wstring(); | ||
| std::wstring ret; | ||
| if (dstLen == UTF8_INVALID) | ||
| { | ||
| // Not UTF-8. Fall back to a 1:1 byte cast so legacy data keeps its characters, matching | ||
| // UnicodeString::translate. | ||
| ret.resize(srcLen); | ||
| for (size_t i = 0; i < srcLen; ++i) | ||
| { | ||
| ret[i] = (WideChar)(unsigned char)orig[i]; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| ret.resize(dstLen); | ||
| Utf8_To_Wide(&ret[0], dstLen, orig, srcLen); | ||
| } | ||
| WideChar *c = nullptr; | ||
| do | ||
| { | ||
| c = wcschr(dest, L'\n'); | ||
| c = wcschr(&ret[0], L'\n'); | ||
| if (c) | ||
| { | ||
| *c = L' '; | ||
|
|
@@ -48,32 +67,26 @@ std::wstring MultiByteToWideCharSingleLine( const char *orig ) | |
| while ( c != nullptr ); | ||
| do | ||
| { | ||
| c = wcschr(dest, L'\r'); | ||
| c = wcschr(&ret[0], L'\r'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While at it, maybe optimize these loops. It is searching for the character from the beginning of the string on every iteration. Or better in a follow up change. |
||
| if (c) | ||
| { | ||
| *c = L' '; | ||
| } | ||
| } | ||
| while ( c != nullptr ); | ||
|
|
||
| dest[len] = 0; | ||
| std::wstring ret = dest; | ||
| delete[] dest; | ||
| return ret; | ||
| } | ||
|
|
||
| std::string WideCharStringToMultiByte( const WideChar *orig ) | ||
| { | ||
| const size_t srcLen = wcslen(orig); | ||
| const size_t dstLen = Wide_To_Utf8_Len(orig, srcLen); | ||
| if (dstLen == 0) | ||
| return std::string(); | ||
| std::string ret; | ||
| Int len = WideCharToMultiByte( CP_UTF8, 0, orig, wcslen(orig), nullptr, 0, nullptr, nullptr ) + 1; | ||
| if (len > 0) | ||
| { | ||
| char *dest = NEW char[len]; | ||
| WideCharToMultiByte( CP_UTF8, 0, orig, -1, dest, len, nullptr, nullptr ); | ||
| dest[len-1] = 0; | ||
| ret = dest; | ||
| delete[] dest; | ||
| } | ||
| ret.resize(dstLen); | ||
| Wide_To_Utf8(&ret[0], dstLen, orig, srcLen); | ||
| return ret; | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -133,6 +133,8 @@ set(WWLIB_SRC | |
| trim.cpp | ||
| trim.h | ||
| uarray.h | ||
| utf8.cpp | ||
| utf8.h | ||
| vector.cpp | ||
| Vector.h | ||
| visualc.h | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any idea why this is removing \r\n ? Probably should have been a different function doing that.