-
Notifications
You must be signed in to change notification settings - Fork 5k
Includes type forwarding for System.Text.Unicode.Utf8 to the Microsoft.Bcl.Memory library #111292
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
Open
AlexRadch
wants to merge
12
commits into
dotnet:main
Choose a base branch
from
AlexRadch:BclMemoryUtf8
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c043cbd
Add support for System.Text.Unicode.Utf8
AlexRadch f60df7c
Enhance UTF-8 and Unicode support in BCL Memory
AlexRadch 5b86cd6
Enhance Unicode handling in tests and project structure
AlexRadch 22be776
Add compilation constant for Microsoft BCL Memory
AlexRadch 79214bd
Apply suggestions from code review
AlexRadch def40c4
Update Microsoft.Bcl.Memory for framework compatibility
AlexRadch a1caf07
Space
AlexRadch 695e362
Remove MICROSOFT_BCL_MEMORY from project constants
AlexRadch 063359a
Refactor Utf8Tests for .NET compatibility improvements
AlexRadch b5992c6
Use `Rune.DecodeFromUtf8` on all frameworks.
teo-tsirpanis 90a3613
Fix compile errors.
teo-tsirpanis 412071a
Address PR feedback.
teo-tsirpanis 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
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
75 changes: 75 additions & 0 deletions
75
...braries/Microsoft.Bcl.Memory/src/Polyfills/System.Numerics.BitOperations.netstandard20.cs
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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
// Contains a polyfill implementation of System.Numerics.BitOperations that works on netstandard2.0. | ||
// Implementation copied from: | ||
// https://github.com/dotnet/runtime/blob/82ab89241b90ca3d64b22971f3a1e248da72828a/src/libraries/System.Private.CoreLib/src/System/Numerics/BitOperations.cs | ||
// | ||
// Some routines inspired by the Stanford Bit Twiddling Hacks by Sean Eron Anderson: | ||
// http://graphics.stanford.edu/~seander/bithacks.html | ||
|
||
namespace System.Numerics | ||
{ | ||
internal static class BitOperations | ||
{ | ||
// C# no-alloc optimization that directly wraps the data section of the dll (similar to string constants) | ||
// https://github.com/dotnet/roslyn/pull/24621 | ||
|
||
private static ReadOnlySpan<byte> TrailingZeroCountDeBruijn => // 32 | ||
[ | ||
00, 01, 28, 02, 29, 14, 24, 03, | ||
30, 22, 20, 15, 25, 17, 04, 08, | ||
31, 27, 13, 23, 21, 19, 16, 07, | ||
26, 12, 18, 06, 11, 05, 10, 09 | ||
]; | ||
|
||
/// <summary> | ||
/// Count the number of trailing zero bits in an integer value. | ||
/// Similar in behavior to the x86 instruction TZCNT. | ||
/// </summary> | ||
/// <param name="value">The value.</param> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static int TrailingZeroCount(uint value) | ||
{ | ||
// Unguarded fallback contract is 0->0, BSF contract is 0->undefined | ||
if (value == 0) | ||
{ | ||
return 32; | ||
} | ||
|
||
// uint.MaxValue >> 27 is always in range [0 - 31] so we use Unsafe.AddByteOffset to avoid bounds check | ||
return Unsafe.AddByteOffset( | ||
// Using deBruijn sequence, k=2, n=5 (2^5=32) : 0b_0000_0111_0111_1100_1011_0101_0011_0001u | ||
ref MemoryMarshal.GetReference(TrailingZeroCountDeBruijn), | ||
// uint|long -> IntPtr cast on 32-bit platforms does expensive overflow checks not needed here | ||
(IntPtr)(int)(((value & (uint)-(int)value) * 0x077CB531u) >> 27)); // Multi-cast mitigates redundant conv.u8 | ||
} | ||
|
||
/// <summary> | ||
/// Rotates the specified value left by the specified number of bits. | ||
/// Similar in behavior to the x86 instruction ROL. | ||
/// </summary> | ||
/// <param name="value">The value to rotate.</param> | ||
/// <param name="offset">The number of bits to rotate by. | ||
/// Any value outside the range [0..31] is treated as congruent mod 32.</param> | ||
/// <returns>The rotated value.</returns> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static uint RotateLeft(uint value, int offset) | ||
=> (value << offset) | (value >> (32 - offset)); | ||
|
||
/// <summary> | ||
/// Rotates the specified value right by the specified number of bits. | ||
/// Similar in behavior to the x86 instruction ROR. | ||
/// </summary> | ||
/// <param name="value">The value to rotate.</param> | ||
/// <param name="offset">The number of bits to rotate by. | ||
/// Any value outside the range [0..31] is treated as congruent mod 32.</param> | ||
/// <returns>The rotated value.</returns> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static uint RotateRight(uint value, int offset) | ||
=> (value >> offset) | (value << (32 - offset)); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/libraries/Microsoft.Bcl.Memory/src/System/ThrowHelper.cs
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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace System | ||
{ | ||
internal static class ThrowHelper | ||
{ | ||
[DoesNotReturn] | ||
internal static void ThrowArgumentException_DestinationTooShort() | ||
{ | ||
throw new ArgumentException(SR.Argument_DestinationTooShort, "destination"); | ||
} | ||
|
||
[DoesNotReturn] | ||
internal static void ThrowArgumentNullException(ExceptionArgument argument) | ||
{ | ||
throw new ArgumentNullException(GetArgumentName(argument)); | ||
} | ||
|
||
[DoesNotReturn] | ||
internal static void ThrowArgumentOutOfRangeException(ExceptionArgument argument) | ||
{ | ||
throw new ArgumentOutOfRangeException(GetArgumentName(argument)); | ||
} | ||
|
||
private static string GetArgumentName(ExceptionArgument argument) | ||
{ | ||
switch (argument) | ||
{ | ||
case ExceptionArgument.ch: | ||
return nameof(ExceptionArgument.ch); | ||
case ExceptionArgument.culture: | ||
return nameof(ExceptionArgument.culture); | ||
case ExceptionArgument.index: | ||
return nameof(ExceptionArgument.index); | ||
case ExceptionArgument.input: | ||
return nameof(ExceptionArgument.input); | ||
case ExceptionArgument.value: | ||
return nameof(ExceptionArgument.value); | ||
default: | ||
Debug.Fail("The enum value is not defined, please check the ExceptionArgument Enum."); | ||
return ""; | ||
|
||
} | ||
} | ||
} | ||
|
||
// | ||
// The convention for this enum is using the argument name as the enum name | ||
// | ||
internal enum ExceptionArgument | ||
{ | ||
ch, | ||
culture, | ||
index, | ||
input, | ||
value, | ||
} | ||
} |
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.
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.
nit: this can be a local in TrailingZeroCount (single use). Slightly better on netfx as well.
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.
I can do it in a subsequent PR.