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 public unsafe static string ToString(ReadOnlySpan<byte> bytes, Casing casing = Casing.Upper)
61 {
62 fixed (byte* ptr = bytes)
63 {
64 return string.Create(bytes.Length * 2, ((IntPtr)ptr, bytes.Length, casing), delegate(Span<char> chars, (IntPtr Ptr, int Length, Casing casing) args)
65 {
66 ReadOnlySpan<byte> bytes2 = new ReadOnlySpan<byte>((void*)args.Ptr, args.Length);
67 EncodeToUtf16(bytes2, chars, args.casing);
68 });
69 }
70 }
71
72 [MethodImpl(MethodImplOptions.AggressiveInlining)]
73 public static char ToCharUpper(int value)
74 {
75 value &= 0xF;
76 value += 48;
77 if (value > 57)
78 {
79 value += 7;
80 }
81 return (char)value;
82 }
83
84 [MethodImpl(MethodImplOptions.AggressiveInlining)]
85 public static int FromUpperChar(int c)
86 {
87 if (c <= 71)
88 {
89 return CharToHexLookup[c];
90 }
91 return 255;
92 }
93}
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 int FromUpperChar(int c)
static ReadOnlySpan< byte > CharToHexLookup
static char ToCharUpper(int value)
static unsafe string ToString(ReadOnlySpan< byte > bytes, Casing casing=Casing.Upper)