You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

EncodingExtensions.cs 791 B

123456789101112131415161718192021222324252627282930313233
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace LLama.Extensions;
  5. internal static class EncodingExtensions
  6. {
  7. #if NETSTANDARD2_0
  8. public static int GetChars(this Encoding encoding, ReadOnlySpan<byte> bytes, Span<char> output)
  9. {
  10. unsafe
  11. {
  12. fixed (byte* bytePtr = bytes)
  13. fixed (char* charPtr = output)
  14. {
  15. return encoding.GetChars(bytePtr, bytes.Length, charPtr, output.Length);
  16. }
  17. }
  18. }
  19. public static int GetCharCount(this Encoding encoding, ReadOnlySpan<byte> bytes)
  20. {
  21. unsafe
  22. {
  23. fixed (byte* bytePtr = bytes)
  24. {
  25. return encoding.GetCharCount(bytePtr, bytes.Length);
  26. }
  27. }
  28. }
  29. #endif
  30. }