Skip to content

Commit 6aaa235

Browse files
committed
Apply feedbacks
1 parent 96dea87 commit 6aaa235

10 files changed

+139
-176
lines changed

src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Decoder.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Runtime.CompilerServices;
5+
using static System.Buffers.Text.Base64Helper;
56

67
namespace System.Buffers.Text
78
{
@@ -29,7 +30,7 @@ public static partial class Base64
2930
/// or if the input is incomplete (i.e. not a multiple of 4) and <paramref name="isFinalBlock"/> is <see langword="true"/>.
3031
/// </returns>
3132
public static OperationStatus DecodeFromUtf8(ReadOnlySpan<byte> utf8, Span<byte> bytes, out int bytesConsumed, out int bytesWritten, bool isFinalBlock = true) =>
32-
Base64Helper.DecodeFrom(Base64Helper.s_base64ByteDecoder, utf8, bytes, out bytesConsumed, out bytesWritten, isFinalBlock, ignoreWhiteSpace: true);
33+
DecodeFrom(default(Base64DecoderByte), utf8, bytes, out bytesConsumed, out bytesWritten, isFinalBlock, ignoreWhiteSpace: true);
3334

3435
/// <summary>
3536
/// Returns the maximum length (in bytes) of the result if you were to decode base 64 encoded text within a byte span of size "length".
@@ -61,6 +62,6 @@ public static int GetMaxDecodedFromUtf8Length(int length)
6162
/// hence can only be called once with all the data in the buffer.
6263
/// </returns>
6364
public static OperationStatus DecodeFromUtf8InPlace(Span<byte> buffer, out int bytesWritten) =>
64-
Base64Helper.DecodeFromUtf8InPlace(Base64Helper.s_base64ByteDecoder, buffer, out bytesWritten, ignoreWhiteSpace: true);
65+
Base64Helper.DecodeFromUtf8InPlace(default(Base64DecoderByte), buffer, out bytesWritten, ignoreWhiteSpace: true);
6566
}
6667
}

src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Encoder.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Runtime.CompilerServices;
5+
using static System.Buffers.Text.Base64Helper;
56

67
namespace System.Buffers.Text
78
{
@@ -13,7 +14,6 @@ namespace System.Buffers.Text
1314
/// </summary>
1415
public static partial class Base64
1516
{
16-
1717
/// <summary>
1818
/// Encode the span of binary data into UTF-8 encoded text represented as base64.
1919
/// </summary>
@@ -32,7 +32,7 @@ public static partial class Base64
3232
/// It does not return InvalidData since that is not possible for base64 encoding.
3333
/// </returns>
3434
public static OperationStatus EncodeToUtf8(ReadOnlySpan<byte> bytes, Span<byte> utf8, out int bytesConsumed, out int bytesWritten, bool isFinalBlock = true) =>
35-
Base64Helper.EncodeTo(Base64Helper.s_base64ByteEncoder, bytes, utf8, out bytesConsumed, out bytesWritten, isFinalBlock);
35+
EncodeTo(default(Base64EncoderByte), bytes, utf8, out bytesConsumed, out bytesWritten, isFinalBlock);
3636

3737
/// <summary>
3838
/// Returns the maximum length (in bytes) of the result if you were to encode binary data within a byte span of size "length".
@@ -43,7 +43,7 @@ public static OperationStatus EncodeToUtf8(ReadOnlySpan<byte> bytes, Span<byte>
4343
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4444
public static int GetMaxEncodedToUtf8Length(int length)
4545
{
46-
ArgumentOutOfRangeException.ThrowIfGreaterThan<uint>((uint)length, Base64Helper.MaximumEncodeLength);
46+
ArgumentOutOfRangeException.ThrowIfGreaterThan<uint>((uint)length, MaximumEncodeLength);
4747

4848
return ((length + 2) / 3) * 4;
4949
}
@@ -64,6 +64,6 @@ public static int GetMaxEncodedToUtf8Length(int length)
6464
/// It does not return InvalidData since that is not possible for base 64 encoding.
6565
/// </returns>
6666
public static OperationStatus EncodeToUtf8InPlace(Span<byte> buffer, int dataLength, out int bytesWritten) =>
67-
Base64Helper.EncodeToUtf8InPlace(Base64Helper.s_base64ByteEncoder, buffer, dataLength, out bytesWritten);
67+
Base64Helper.EncodeToUtf8InPlace(default(Base64EncoderByte), buffer, dataLength, out bytesWritten);
6868
}
6969
}

src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Helper/Base64DecoderHelper.cs

+8-17
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System.Diagnostics;
55
using System.Runtime.CompilerServices;
66
using System.Runtime.InteropServices;
7-
#if NETCOREAPP
7+
#if NET
88
using System.Runtime.Intrinsics.Arm;
99
using System.Runtime.Intrinsics.X86;
1010
using System.Runtime.Intrinsics;
@@ -14,10 +14,6 @@ namespace System.Buffers.Text
1414
{
1515
internal static partial class Base64Helper
1616
{
17-
#pragma warning disable CA1805 // Member 's_base64ByteDecoder' is explicitly initialized to its default value
18-
internal static Base64DecoderByte s_base64ByteDecoder = default;
19-
#pragma warning restore CA1805
20-
2117
internal static unsafe OperationStatus DecodeFrom<TBase64Decoder, T>(TBase64Decoder decoder, ReadOnlySpan<T> source, Span<byte> bytes,
2218
out int bytesConsumed, out int bytesWritten, bool isFinalBlock, bool ignoreWhiteSpace)
2319
where TBase64Decoder : IBase64Decoder<T>
@@ -50,7 +46,7 @@ internal static unsafe OperationStatus DecodeFrom<TBase64Decoder, T>(TBase64Deco
5046
T* srcEnd = srcBytes + (uint)srcLength;
5147
T* srcMax = srcBytes + (uint)maxSrcLength;
5248

53-
#if NETCOREAPP
49+
#if NET
5450
if (maxSrcLength >= 24)
5551
{
5652
T* end = srcMax - 88;
@@ -112,7 +108,7 @@ internal static unsafe OperationStatus DecodeFrom<TBase64Decoder, T>(TBase64Deco
112108
// This should never overflow since destLength here is less than int.MaxValue / 4 * 3 (i.e. 1610612733)
113109
// Therefore, (destLength / 3) * 4 will always be less than 2147483641
114110
Debug.Assert(destLength < (int.MaxValue / 4 * 3));
115-
#if NETCOREAPP
111+
#if NET
116112
(maxSrcLength, int remainder) = int.DivRem(destLength, 3);
117113
maxSrcLength *= 4;
118114
#else
@@ -643,13 +639,13 @@ private static OperationStatus DecodeWithWhiteSpaceFromUtf8InPlace<TBase64Decode
643639
return status;
644640
}
645641

646-
#if NETCOREAPP
642+
#if NET
647643
[MethodImpl(MethodImplOptions.AggressiveInlining)]
648644
[CompExactlyDependsOn(typeof(Avx512BW))]
649645
[CompExactlyDependsOn(typeof(Avx512Vbmi))]
650646
private static unsafe void Avx512Decode<TBase64Decoder, T>(TBase64Decoder decoder, ref T* srcBytes, ref byte* destBytes, T* srcEnd, int sourceLength, int destLength, T* srcStart, byte* destStart)
651-
where TBase64Decoder : IBase64Decoder<T>
652-
where T : unmanaged
647+
where TBase64Decoder : IBase64Decoder<T>
648+
where T : unmanaged
653649
{
654650
// Reference for VBMI implementation : https://github.com/WojciechMula/base64simd/tree/master/decode
655651
// If we have AVX512 support, pick off 64 bytes at a time for as long as we can,
@@ -1243,20 +1239,15 @@ internal static bool IsWhiteSpace(int value)
12431239

12441240
public uint AdvSimdLutTwo3Uint1 => 0x1B1AFFFF;
12451241

1246-
public int GetMaxDecodedLength(int utf8Length) =>
1247-
#if NETCOREAPP
1248-
Base64.GetMaxDecodedFromUtf8Length(utf8Length);
1249-
#else
1250-
0;
1251-
#endif
1242+
public int GetMaxDecodedLength(int utf8Length) => Base64.GetMaxDecodedFromUtf8Length(utf8Length);
12521243

12531244
public bool IsInvalidLength(int bufferLength) => bufferLength % 4 != 0; // only decode input if it is a multiple of 4
12541245

12551246
public bool IsValidPadding(uint padChar) => padChar == EncodingPad;
12561247

12571248
public int SrcLength(bool _, int utf8Length) => utf8Length & ~0x3; // only decode input up to the closest multiple of 4.
12581249

1259-
#if NETCOREAPP
1250+
#if NET
12601251
[MethodImpl(MethodImplOptions.AggressiveInlining)]
12611252
[CompExactlyDependsOn(typeof(AdvSimd.Arm64))]
12621253
[CompExactlyDependsOn(typeof(Ssse3))]

src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Helper/Base64EncoderHelper.cs

+7-20
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
using System.Runtime.CompilerServices;
55
using System.Runtime.InteropServices;
6-
#if NETCOREAPP
6+
#if NET
77
using System.Runtime.Intrinsics;
88
using System.Runtime.Intrinsics.Arm;
99
using System.Runtime.Intrinsics.X86;
@@ -13,10 +13,6 @@ namespace System.Buffers.Text
1313
{
1414
internal static partial class Base64Helper
1515
{
16-
#pragma warning disable CA1805 // Member 's_base64ByteEncoder' is explicitly initialized to its default value
17-
internal static Base64EncoderByte s_base64ByteEncoder = default;
18-
#pragma warning restore CA1805
19-
2016
internal static unsafe OperationStatus EncodeTo<TBase64Encoder, T>(TBase64Encoder encoder, ReadOnlySpan<byte> source,
2117
Span<T> destination, out int bytesConsumed, out int bytesWritten, bool isFinalBlock = true)
2218
where TBase64Encoder : IBase64Encoder<T>
@@ -41,7 +37,7 @@ internal static unsafe OperationStatus EncodeTo<TBase64Encoder, T>(TBase64Encode
4137
byte* srcEnd = srcBytes + (uint)srcLength;
4238
byte* srcMax = srcBytes + (uint)maxSrcLength;
4339

44-
#if NETCOREAPP
40+
#if NET
4541
if (maxSrcLength >= 16)
4642
{
4743
byte* end = srcMax - 64;
@@ -132,13 +128,13 @@ internal static unsafe OperationStatus EncodeTo<TBase64Encoder, T>(TBase64Encode
132128
}
133129
}
134130

135-
#if NETCOREAPP
131+
#if NET
136132
[MethodImpl(MethodImplOptions.AggressiveInlining)]
137133
[CompExactlyDependsOn(typeof(Avx512BW))]
138134
[CompExactlyDependsOn(typeof(Avx512Vbmi))]
139135
private static unsafe void Avx512Encode<TBase64Encoder, T>(TBase64Encoder encoder, ref byte* srcBytes, ref T* destBytes, byte* srcEnd, int sourceLength, int destLength, byte* srcStart, T* destStart)
140-
where TBase64Encoder : IBase64Encoder<T>
141-
where T : unmanaged
136+
where TBase64Encoder : IBase64Encoder<T>
137+
where T : unmanaged
142138
{
143139
// Reference for VBMI implementation : https://github.com/WojciechMula/base64simd/tree/master/encode
144140
// If we have AVX512 support, pick off 48 bytes at a time for as long as we can.
@@ -664,21 +660,12 @@ private static unsafe uint Encode(byte* threeBytes, ref byte encodingMap)
664660

665661
[MethodImpl(MethodImplOptions.AggressiveInlining)]
666662
public int GetMaxSrcLength(int srcLength, int destLength) =>
667-
#if NETCOREAPP
668663
srcLength <= MaximumEncodeLength && destLength >= Base64.GetMaxEncodedToUtf8Length(srcLength) ?
669664
srcLength : (destLength >> 2) * 3;
670-
#else
671-
0;
672-
#endif
673665

674666
public uint GetInPlaceDestinationLength(int encodedLength, int _) => (uint)(encodedLength - 4);
675667

676-
public int GetMaxEncodedLength(int srcLength) =>
677-
#if NETCOREAPP
678-
Base64.GetMaxEncodedToUtf8Length(srcLength);
679-
#else
680-
0;
681-
#endif
668+
public int GetMaxEncodedLength(int srcLength) => Base64.GetMaxEncodedToUtf8Length(srcLength);
682669

683670
[MethodImpl(MethodImplOptions.AggressiveInlining)]
684671
public unsafe void EncodeOneOptionallyPadTwo(byte* oneByte, byte* dest, ref byte encodingMap)
@@ -734,7 +721,7 @@ public unsafe void EncodeTwoOptionallyPadOne(byte* twoBytes, byte* dest, ref byt
734721
}
735722
}
736723

737-
#if NETCOREAPP
724+
#if NET
738725
[MethodImpl(MethodImplOptions.AggressiveInlining)]
739726
public unsafe void StoreVector512ToDestination(byte* dest, byte* destStart, int destLength, Vector512<byte> str)
740727
{

src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Helper/Base64Helper.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
using System.Diagnostics;
55
using System.Runtime.CompilerServices;
6-
#if NETCOREAPP
6+
#if NET
77
using System.Runtime.Intrinsics;
88
#endif
99

@@ -82,7 +82,7 @@ internal interface IBase64Encoder<T> where T : unmanaged
8282
unsafe void EncodeThreeAndWrite(byte* threeBytes, T* destination, ref byte encodingMap);
8383
int IncrementPadTwo { get; }
8484
int IncrementPadOne { get; }
85-
#if NETCOREAPP
85+
#if NET
8686
unsafe void StoreVector512ToDestination(T* dest, T* destStart, int destLength, Vector512<byte> str);
8787
unsafe void StoreVector256ToDestination(T* dest, T* destStart, int destLength, Vector256<byte> str);
8888
unsafe void StoreVector128ToDestination(T* dest, T* destStart, int destLength, Vector128<byte> str);
@@ -109,7 +109,7 @@ internal interface IBase64Decoder<T> where T : unmanaged
109109
int GetMaxDecodedLength(int sourceLength);
110110
bool IsInvalidLength(int bufferLength);
111111
bool IsValidPadding(uint padChar);
112-
#if NETCOREAPP
112+
#if NET
113113
bool TryDecode128Core(
114114
Vector128<byte> str,
115115
Vector128<byte> hiNibbles,

src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Helper/Base64ValidatorHelper.cs

+8-14
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,14 @@ namespace System.Buffers.Text
77
{
88
internal static partial class Base64Helper
99
{
10-
#pragma warning disable CA1805 // Member 's_base64ByteEncoder' is explicitly initialized to its default value
11-
internal static Base64ByteValidatable s_base64ByteValidatable = default;
12-
internal static Base64CharValidatable s_base64CharValidatable = default;
13-
#pragma warning restore CA1805
14-
1510
internal static bool IsValid<T, TBase64Validatable>(TBase64Validatable validatable, ReadOnlySpan<T> base64Text, out int decodedLength)
1611
where TBase64Validatable : IBase64Validatable<T>
1712
{
1813
int length = 0, paddingCount = 0;
1914

2015
if (!base64Text.IsEmpty)
2116
{
22-
#if NETCOREAPP
17+
#if NET
2318
while (true)
2419
{
2520

@@ -128,7 +123,7 @@ internal static bool IsValid<T, TBase64Validatable>(TBase64Validatable validatab
128123

129124
internal interface IBase64Validatable<T>
130125
{
131-
#if NETCOREAPP
126+
#if NET
132127
int IndexOfAnyExcept(ReadOnlySpan<T> span);
133128
#else
134129
int DecodeValue(T value);
@@ -140,7 +135,7 @@ internal interface IBase64Validatable<T>
140135

141136
internal readonly struct Base64CharValidatable : IBase64Validatable<char>
142137
{
143-
#if NETCOREAPP
138+
#if NET
144139
private static readonly SearchValues<char> s_validBase64Chars = SearchValues.Create("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
145140

146141
public int IndexOfAnyExcept(ReadOnlySpan<char> span) => span.IndexOfAnyExcept(s_validBase64Chars);
@@ -153,24 +148,23 @@ public int DecodeValue(char value)
153148
return -2;
154149
}
155150

156-
return s_base64ByteDecoder.DecodingMap[value];
151+
return default(Base64DecoderByte).DecodingMap[value];
157152
}
158153
#endif
159154
public bool IsWhiteSpace(char value) => Base64Helper.IsWhiteSpace(value);
160155
public bool IsEncodingPad(char value) => value == EncodingPad;
161156
public bool ValidateAndDecodeLength(int length, int paddingCount, out int decodedLength) =>
162-
s_base64ByteValidatable.ValidateAndDecodeLength(length, paddingCount, out decodedLength);
157+
default(Base64ByteValidatable).ValidateAndDecodeLength(length, paddingCount, out decodedLength);
163158
}
164159

165160
internal readonly struct Base64ByteValidatable : IBase64Validatable<byte>
166161
{
167-
#if NETCOREAPP
168-
private static readonly SearchValues<byte> s_validBase64Chars = SearchValues.Create(s_base64ByteEncoder.EncodingMap);
162+
#if NET
163+
private static readonly SearchValues<byte> s_validBase64Chars = SearchValues.Create(default(Base64EncoderByte).EncodingMap);
169164

170165
public int IndexOfAnyExcept(ReadOnlySpan<byte> span) => span.IndexOfAnyExcept(s_validBase64Chars);
171166
#else
172-
public int DecodeValue(byte value) =>
173-
s_base64ByteDecoder.DecodingMap[value];
167+
public int DecodeValue(byte value) => default(Base64DecoderByte).DecodingMap[value];
174168
#endif
175169
public bool IsWhiteSpace(byte value) => Base64Helper.IsWhiteSpace(value);
176170
public bool IsEncodingPad(byte value) => value == EncodingPad;

0 commit comments

Comments
 (0)