-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Expose the IBinaryInteger<T>.ReadBig/LittleEndian APIs #73274
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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3eadbcf
Adding the IBinaryInteger<T>.ReadLittleEndian and IBinaryInteger<T>.R…
tannergooding 3eaf1d1
Adding tests for the IBinaryInteger<T>.ReadLittleEndian and IBinaryIn…
tannergooding f0cd89b
Ensure IBinaryInteger<T>.ReadBig/LittleEndian APIs take ROSpan
tannergooding 05ac035
Update src/libraries/System.Private.CoreLib/src/System/Byte.cs
tannergooding 660c968
Fix accidentally removed 0 in char
tannergooding d6d269d
Update IndexOfAny to compare against >= 0, rather than != -1
tannergooding 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,16 @@ | |
| using System.Numerics; | ||
|
Member
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. Not related to this PR, but we should rename this file at some point. |
||
| using System.Runtime.CompilerServices; | ||
|
|
||
| #pragma warning disable SA1121 // explicitly using type aliases instead of built-in types | ||
|
|
||
| #if TARGET_64BIT | ||
| using nint_t = System.Int64; | ||
| using nuint_t = System.UInt64; | ||
| #else | ||
| using nint_t = System.Int32; | ||
| using nuint_t = System.UInt32; | ||
| #endif | ||
|
|
||
| namespace System.Buffers.Binary | ||
| { | ||
| /// <summary> | ||
|
|
@@ -44,6 +54,24 @@ public static partial class BinaryPrimitives | |
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| public static long ReverseEndianness(long value) => (long)ReverseEndianness((ulong)value); | ||
|
|
||
| /// <summary> | ||
| /// Reverses a signed native-sized integral value - performs an endianness swap | ||
| /// </summary> | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| internal static nint ReverseEndianness(nint value) => (nint)ReverseEndianness((nint_t)value); | ||
|
|
||
| /// <summary> | ||
| /// Reverses a signed 128-bit integral value - performs an endianness swap | ||
| /// </summary> | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| internal static Int128 ReverseEndianness(Int128 value) | ||
| { | ||
| return new Int128( | ||
| ReverseEndianness(value.Lower), | ||
| ReverseEndianness(value.Upper) | ||
| ); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// This is a no-op and added only for consistency. | ||
| /// This allows the caller to read a struct of numeric primitives and reverse each field | ||
|
|
@@ -70,6 +98,12 @@ public static ushort ReverseEndianness(ushort value) | |
| return (ushort)((value >> 8) + (value << 8)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Reverses a 16-bit character value - performs an endianness swap | ||
| /// </summary> | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| internal static char ReverseEndianness(char value) => (char)ReverseEndianness((ushort)value); | ||
|
|
||
| /// <summary> | ||
| /// Reverses a primitive value - performs an endianness swap | ||
| /// </summary> | ||
|
|
@@ -116,5 +150,23 @@ public static ulong ReverseEndianness(ulong value) | |
| return ((ulong)ReverseEndianness((uint)value) << 32) | ||
| + ReverseEndianness((uint)(value >> 32)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Reverses an unsigned native-sized integral value - performs an endianness swap | ||
| /// </summary> | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| internal static nuint ReverseEndianness(nuint value) => (nuint)ReverseEndianness((nuint_t)value); | ||
|
|
||
| /// <summary> | ||
| /// Reverses an unsigned 128-bit integral value - performs an endianness swap | ||
| /// </summary> | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| internal static UInt128 ReverseEndianness(UInt128 value) | ||
| { | ||
| return new UInt128( | ||
| ReverseEndianness(value.Lower), | ||
| ReverseEndianness(value.Upper) | ||
| ); | ||
| } | ||
| } | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.