// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. #if !NET namespace System.Text; internal static class EncodingExtensions { /// /// Gets the number of bytes required to encode the specified characters. /// public static int GetByteCount(this Encoding encoding, ReadOnlySpan chars) { if (chars.IsEmpty) { return 0; } unsafe { fixed (char* charsPtr = chars) { return encoding.GetByteCount(charsPtr, chars.Length); } } } /// /// Encodes the specified characters into the specified byte span. /// public static int GetBytes(this Encoding encoding, ReadOnlySpan chars, Span bytes) { if (chars.IsEmpty) { return 0; } unsafe { fixed (char* charsPtr = chars) fixed (byte* bytesPtr = bytes) { return encoding.GetBytes(charsPtr, chars.Length, bytesPtr, bytes.Length); } } } } #endif