-
Notifications
You must be signed in to change notification settings - Fork 5.2k
System.Text.Encodings.Web refactoring and code modernization #49373
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 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
bcc5b3f
System.Text.Encodings.Web refactoring and code modernization
GrabYourPitchforks cf8e998
Make WriteStringInvalidCharacter resilient against U+FFFD escaping
GrabYourPitchforks ee279d4
Fix broken tests & x86 edge case detection
GrabYourPitchforks d13f660
Remove Vector<T> dependency in wasm
GrabYourPitchforks 5f6215e
Ifdef Vector128<byte> field away in browser scenarios
GrabYourPitchforks 08b79db
Remove stray ResourceManager reference from ref csproj
GrabYourPitchforks 31762f0
PR feedback and collapse .Simd.cs and .Ascii.cs files together
GrabYourPitchforks 9d0d006
PR feedback - rename locals, extract common vars
GrabYourPitchforks 3eec93e
Fix build break on netcoreapp3.1
GrabYourPitchforks d0d3d2f
Rename files JavaScriptStringEncoder -> JavaScriptEncoder
GrabYourPitchforks e6f4433
Rename test methods JavaScriptString -> JavaScript
GrabYourPitchforks 0d84fc4
Cherry-pick updates to package baseline
GrabYourPitchforks 76f04f7
Merge remote-tracking branch 'origin/main' into encoder
GrabYourPitchforks 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
64 changes: 64 additions & 0 deletions
64
...es/System.Text.Encodings.Web/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,64 @@ | ||
| // 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/6072e4d3a7a2a1493f514cdf4be75a3d56580e84/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 | ||
| { | ||
| private static ReadOnlySpan<byte> Log2DeBruijn => new byte[32] | ||
| { | ||
| 00, 09, 01, 10, 13, 21, 02, 29, | ||
| 11, 14, 16, 18, 22, 25, 03, 30, | ||
| 08, 12, 20, 28, 15, 17, 24, 07, | ||
| 19, 27, 23, 06, 26, 05, 04, 31 | ||
| }; | ||
|
|
||
| /// <summary> | ||
| /// Returns the integer (floor) log of the specified value, base 2. | ||
| /// Note that by convention, input value 0 returns 0 since log(0) is undefined. | ||
| /// </summary> | ||
| /// <param name="value">The value.</param> | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| public static int Log2(uint value) | ||
| { | ||
| // Fallback contract is 0->0 | ||
| return Log2SoftwareFallback(value | 1); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns the integer (floor) log of the specified value, base 2. | ||
| /// Note that by convention, input value 0 returns 0 since Log(0) is undefined. | ||
| /// Does not directly use any hardware intrinsics, nor does it incur branching. | ||
| /// </summary> | ||
| /// <param name="value">The value.</param> | ||
| private static int Log2SoftwareFallback(uint value) | ||
| { | ||
| // No AggressiveInlining due to large method size | ||
| // Has conventional contract 0->0 (Log(0) is undefined) | ||
|
|
||
| // Fill trailing zeros with ones, eg 00010010 becomes 00011111 | ||
| value |= value >> 01; | ||
| value |= value >> 02; | ||
| value |= value >> 04; | ||
| value |= value >> 08; | ||
| value |= value >> 16; | ||
|
|
||
| // 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_1100_0100_1010_1100_1101_1101u | ||
| ref MemoryMarshal.GetReference(Log2DeBruijn), | ||
| // uint|long -> IntPtr cast on 32-bit platforms does expensive overflow checks not needed here | ||
| (nint)((value * 0x07C4ACDDu) >> 27)); | ||
| } | ||
| } | ||
| } |
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.