StringHelper.cs 601 B

123456789101112131415161718192021222324
  1. using System.Runtime.CompilerServices;
  2. namespace Lua.Standard.Text;
  3. internal static class StringHelper
  4. {
  5. public static int UnicodeToAscii(int i)
  6. {
  7. if (i >= 0 && i <= 255) return i;
  8. throw new ArgumentOutOfRangeException(nameof(i));
  9. }
  10. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  11. public static ReadOnlySpan<char> Slice(string s, int i, int j)
  12. {
  13. if (i < 0) i = s.Length + i + 1;
  14. if (j < 0) j = s.Length + j + 1;
  15. if (i < 1) i = 1;
  16. if (j > s.Length) j = s.Length;
  17. return i > j ? "" : s.AsSpan()[(i - 1)..j];
  18. }
  19. }