Character.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System.Runtime.CompilerServices;
  2. namespace Jint.Extensions;
  3. internal static class Character
  4. {
  5. /// <summary>
  6. /// https://tc39.es/ecma262/#ASCII-word-characters
  7. /// </summary>
  8. public const string AsciiWordCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
  9. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  10. public static bool IsInRange(this char c, ushort min, ushort max) => (uint) (c - min) <= (uint) (max - min);
  11. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  12. public static bool IsOctalDigit(this char c) => c.IsInRange('0', '7');
  13. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  14. public static bool IsDecimalDigit(this char c) => c.IsInRange('0', '9');
  15. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  16. public static bool IsHexDigit(this char c)
  17. {
  18. // NOTE: On 32-bit architectures this is not optimal, lookup is supposed to be faster.
  19. // But to keep it simple, we use this method regardless of CPU architecture, and if performance
  20. // needs to be improved further, the lookup approach can be ported from Esprima.HexConverter.
  21. // This code path, when used, has no branches and doesn't depend on cache hits,
  22. // so it's faster and does not vary in speed depending on input data distribution.
  23. // The magic constant 18428868213665201664 is a 64 bit value containing 1s at the
  24. // indices corresponding to all the valid hex characters (ie. "0123456789ABCDEFabcdef")
  25. // minus 48 (ie. '0'), and backwards (so from the most significant bit and downwards).
  26. // The offset of 48 for each bit is necessary so that the entire range fits in 64 bits.
  27. // First, we subtract '0' to the input digit (after casting to uint to account for any
  28. // negative inputs). Note that even if this subtraction underflows, this happens before
  29. // the result is zero-extended to ulong, meaning that `i` will always have upper 32 bits
  30. // equal to 0. We then left shift the constant with this offset, and apply a bitmask that
  31. // has the highest bit set (the sign bit) if and only if `c` is in the ['0', '0' + 64) range.
  32. // Then we only need to check whether this final result is less than 0: this will only be
  33. // the case if both `i` was in fact the index of a set bit in the magic constant, and also
  34. // `c` was in the allowed range (this ensures that false positive bit shifts are ignored).
  35. ulong i = (uint) c - '0';
  36. ulong shift = 18428868213665201664UL << (int) i;
  37. ulong mask = i - 64;
  38. return (long) (shift & mask) < 0 ? true : false;
  39. }
  40. }