-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Expose UTF-8 support from FromHexString, TryToHexString, and TryToHexStringLower on the Convert class #117965
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
Conversation
…StringLower on the Convert class
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.
Pull Request Overview
This PR adds UTF-8 support to the Convert class's hex string methods, exposing new overloads that work with ReadOnlySpan<byte>
and Span<byte>
for UTF-8 encoded hex strings. This complements the existing UTF-16 char-based methods and provides better performance for scenarios working with UTF-8 data.
Key changes:
- Added UTF-8 overloads for
FromHexString
,TryToHexString
, andTryToHexStringLower
methods - Enhanced
HexConverter
with UTF-8 encoding/decoding support and vectorized optimizations - Updated tests to cover both UTF-16 and UTF-8 code paths comprehensively
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
File | Description |
---|---|
src/libraries/System.Runtime/ref/System.Runtime.cs |
Added public API surface for new UTF-8 hex string methods |
src/libraries/System.Private.CoreLib/src/System/Convert.cs |
Implemented UTF-8 hex string conversion methods and refactored existing code |
src/libraries/Common/src/System/HexConverter.cs |
Added UTF-8 encoding/decoding support with vectorized implementations |
src/libraries/System.Private.CoreLib/src/System/ThrowHelper.cs |
Added helper methods for hex-specific exceptions |
src/libraries/System.Private.CoreLib/src/System/Text/Unicode/Utf16Utility.cs |
Fixed vector comparison to use equality operator |
src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/Convert.ToHexString.cs |
Added comprehensive UTF-8 test coverage for ToHexString methods |
src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/Convert.FromHexString.cs |
Added comprehensive UTF-8 test coverage for FromHexString methods |
Tagging subscribers to this area: @dotnet/area-system-runtime |
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.
Added minor question, LGTM otherwise.
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.
This has my support for introducing in .NET 10 RC1
This got M2 sign-off offline. |
This resolves #114079
Customer Impact
We exposed the
FromHexString
,ToHexString
, and related APIs back in .NET 5 (2020) and have been getting requests to add UTF-8 support essentially as long. However, we didn't really make a more targeted effort to add native UTF-8 support until .NET 8 when we introducedIUtf8SpanParsable
,IUtf8SpanFormattable
, and ensured the core primitive numeric types all implemented these interfaces.Exposing these as part of .NET 10 will unblock customers that have been waiting for this functionality and will give them the higher performance and quality they rely on from built-in APIs.
Testing
All the existing UTF-16 tests were updated to also test the UTF-8 paths.
Risk
Low. This is using the existing UTF-16 algorithms and simply updating them to work for
byte
. In most cases this is 1-to-1, in a couple places it uses the internalwhere TChar : IUtfChar<TChar>
we use for other shared UTF-8/16 code paths.