Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
HexConverter.cs
Go to the documentation of this file.
2
3namespace System;
4
5internal static class HexConverter
6{
7 public enum Casing : uint
8 {
9 Upper = 0u,
10 Lower = 8224u
11 }
12
13 public static ReadOnlySpan<byte> CharToHexLookup => new byte[256]
14 {
15 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
16 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
17 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
18 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
19 255, 255, 255, 255, 255, 255, 255, 255, 0, 1,
20 2, 3, 4, 5, 6, 7, 8, 9, 255, 255,
21 255, 255, 255, 255, 255, 10, 11, 12, 13, 14,
22 15, 255, 255, 255, 255, 255, 255, 255, 255, 255,
23 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
24 255, 255, 255, 255, 255, 255, 255, 10, 11, 12,
25 13, 14, 15, 255, 255, 255, 255, 255, 255, 255,
26 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
27 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
28 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
29 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
30 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
31 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
32 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
33 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
34 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
35 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
36 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
37 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
38 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
39 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
40 255, 255, 255, 255, 255, 255
41 };
42
43 [MethodImpl(MethodImplOptions.AggressiveInlining)]
44 public static void ToCharsBuffer(byte value, Span<char> buffer, int startingIndex = 0, Casing casing = Casing.Upper)
45 {
46 uint num = (uint)(((value & 0xF0) << 4) + (value & 0xF) - 35209);
47 uint num2 = ((((0 - num) & 0x7070) >> 4) + num + 47545) | (uint)casing;
48 buffer[startingIndex + 1] = (char)(num2 & 0xFFu);
49 buffer[startingIndex] = (char)(num2 >> 8);
50 }
51
52 public static void EncodeToUtf16(ReadOnlySpan<byte> bytes, Span<char> chars, Casing casing = Casing.Upper)
53 {
54 for (int i = 0; i < bytes.Length; i++)
55 {
56 ToCharsBuffer(bytes[i], chars, i * 2, casing);
57 }
58 }
59
60 [MethodImpl(MethodImplOptions.AggressiveInlining)]
61 public static char ToCharUpper(int value)
62 {
63 value &= 0xF;
64 value += 48;
65 if (value > 57)
66 {
67 value += 7;
68 }
69 return (char)value;
70 }
71
72 [MethodImpl(MethodImplOptions.AggressiveInlining)]
73 public static int FromChar(int c)
74 {
75 if (c < CharToHexLookup.Length)
76 {
77 return CharToHexLookup[c];
78 }
79 return 255;
80 }
81}
static void EncodeToUtf16(ReadOnlySpan< byte > bytes, Span< char > chars, Casing casing=Casing.Upper)
static void ToCharsBuffer(byte value, Span< char > buffer, int startingIndex=0, Casing casing=Casing.Upper)
static ReadOnlySpan< byte > CharToHexLookup
static int FromChar(int c)
static char ToCharUpper(int value)