using System; using System.Collections.Generic; using System.Text; namespace LLama.Extensions; internal static class EncodingExtensions { #if NETSTANDARD2_0 public static int GetChars(this Encoding encoding, ReadOnlySpan bytes, Span output) { return GetCharsImpl(encoding, bytes, output); } public static int GetCharCount(this Encoding encoding, ReadOnlySpan bytes) { return GetCharCountImpl(encoding, bytes); } #endif internal static int GetCharsImpl(Encoding encoding, ReadOnlySpan bytes, Span output) { if (bytes.Length == 0) return 0; unsafe { fixed (byte* bytePtr = bytes) fixed (char* charPtr = output) { return encoding.GetChars(bytePtr, bytes.Length, charPtr, output.Length); } } } internal static int GetCharCountImpl(Encoding encoding, ReadOnlySpan bytes) { unsafe { fixed (byte* bytePtr = bytes) { return encoding.GetCharCount(bytePtr, bytes.Length); } } } }