diff --git a/src/libraries/System.Memory/src/System/Buffers/Text/Base64.cs b/src/libraries/Common/src/System/Buffers/Text/Base64.cs similarity index 100% rename from src/libraries/System.Memory/src/System/Buffers/Text/Base64.cs rename to src/libraries/Common/src/System/Buffers/Text/Base64.cs diff --git a/src/libraries/System.Memory/src/System/Buffers/Text/Base64Decoder.cs b/src/libraries/Common/src/System/Buffers/Text/Base64Decoder.cs similarity index 98% rename from src/libraries/System.Memory/src/System/Buffers/Text/Base64Decoder.cs rename to src/libraries/Common/src/System/Buffers/Text/Base64Decoder.cs index b0c491a5beaba7..143fb6f76a2d00 100644 --- a/src/libraries/System.Memory/src/System/Buffers/Text/Base64Decoder.cs +++ b/src/libraries/Common/src/System/Buffers/Text/Base64Decoder.cs @@ -477,22 +477,6 @@ private static unsafe void Avx2Decode(ref byte* srcBytes, ref byte* destBytes, b destBytes = dest; } - // This can be replaced once https://github.com/dotnet/runtime/issues/63331 is implemented. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static Vector128 SimdShuffle(Vector128 left, Vector128 right, Vector128 mask8F) - { - Debug.Assert((Ssse3.IsSupported || AdvSimd.Arm64.IsSupported) && BitConverter.IsLittleEndian); - - if (Ssse3.IsSupported) - { - return Ssse3.Shuffle(left, right); - } - else - { - return AdvSimd.Arm64.VectorTableLookup(left, Vector128.BitwiseAnd(right, mask8F)); - } - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] private static unsafe void Vector128Decode(ref byte* srcBytes, ref byte* destBytes, byte* srcEnd, int sourceLength, int destLength, byte* srcStart, byte* destStart) { diff --git a/src/libraries/System.Memory/src/System/Buffers/Text/Base64Encoder.cs b/src/libraries/Common/src/System/Buffers/Text/Base64Encoder.cs similarity index 97% rename from src/libraries/System.Memory/src/System/Buffers/Text/Base64Encoder.cs rename to src/libraries/Common/src/System/Buffers/Text/Base64Encoder.cs index 9eb60032b65e4f..8538db086ca62f 100644 --- a/src/libraries/System.Memory/src/System/Buffers/Text/Base64Encoder.cs +++ b/src/libraries/Common/src/System/Buffers/Text/Base64Encoder.cs @@ -1,6 +1,7 @@ // 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.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Intrinsics; @@ -17,6 +18,22 @@ namespace System.Buffers.Text /// public static partial class Base64 { + // This can be replaced once https://github.com/dotnet/runtime/issues/63331 is implemented. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static Vector128 SimdShuffle(Vector128 left, Vector128 right, Vector128 mask8F) + { + Debug.Assert((Ssse3.IsSupported || AdvSimd.Arm64.IsSupported) && BitConverter.IsLittleEndian); + + if (Ssse3.IsSupported) + { + return Ssse3.Shuffle(left, right); + } + else + { + return AdvSimd.Arm64.VectorTableLookup(left, Vector128.BitwiseAnd(right, mask8F)); + } + } + /// /// Encode the span of binary data into UTF-8 encoded text represented as base64. /// diff --git a/src/libraries/System.Memory/src/System.Memory.csproj b/src/libraries/System.Memory/src/System.Memory.csproj index 7294197f7a962f..1f2e8675a5e2f5 100644 --- a/src/libraries/System.Memory/src/System.Memory.csproj +++ b/src/libraries/System.Memory/src/System.Memory.csproj @@ -25,9 +25,9 @@ - - - + + + diff --git a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems index d978ef0b8c4883..27588a75dc15ce 100644 --- a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems +++ b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems @@ -243,6 +243,8 @@ + + diff --git a/src/libraries/System.Private.CoreLib/src/System/Convert.cs b/src/libraries/System.Private.CoreLib/src/System/Convert.cs index 5b212556c684e7..398cb5ddf74b23 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Convert.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Convert.cs @@ -6,6 +6,8 @@ using System.Globalization; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +using System.Buffers.Text; +using System.Text; namespace System { @@ -2336,7 +2338,16 @@ public static string ToBase64String(ReadOnlySpan bytes, Base64FormattingOp } bool insertLineBreaks = (options == Base64FormattingOptions.InsertLineBreaks); - string result = string.FastAllocateString(ToBase64_CalculateAndValidateOutputLength(bytes.Length, insertLineBreaks)); + int outputLength = ToBase64_CalculateAndValidateOutputLength(bytes.Length, insertLineBreaks); + + if (!insertLineBreaks && bytes.Length >= 64) + { + Span utf8Result = outputLength <= 256 ? stackalloc byte[256] : new byte[outputLength]; + Base64.EncodeToUtf8(bytes, utf8Result, out int _, out int _); + return Encoding.Latin1.GetString(utf8Result); + } + + string result = string.FastAllocateString(outputLength); unsafe {