From fd4f5aeee43b50dc69a653b4cceaaaa09055f753 Mon Sep 17 00:00:00 2001 From: "g.verdier" Date: Wed, 29 May 2024 17:57:36 -0400 Subject: [PATCH] csharp: use span-based ToBase64 in ByteString --- csharp/src/Google.Protobuf/ByteString.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/csharp/src/Google.Protobuf/ByteString.cs b/csharp/src/Google.Protobuf/ByteString.cs index fc5d925fa7..b8e0b39133 100644 --- a/csharp/src/Google.Protobuf/ByteString.cs +++ b/csharp/src/Google.Protobuf/ByteString.cs @@ -117,6 +117,9 @@ namespace Google.Protobuf /// A base64 representation of this ByteString. public string ToBase64() { +#if NET5_0_OR_GREATER + return Convert.ToBase64String(bytes.Span); +#else if (MemoryMarshal.TryGetArray(bytes, out ArraySegment segment)) { // Fast path. ByteString was created with an array, so pass the underlying array. @@ -127,6 +130,7 @@ namespace Google.Protobuf // Slow path. BytesString is not an array. Convert memory and pass result to ToBase64String. return Convert.ToBase64String(bytes.ToArray()); } +#endif } ///