Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
StandardFormat.cs
Go to the documentation of this file.
2
3namespace System.Buffers;
4
5public readonly struct StandardFormat : IEquatable<StandardFormat>
6{
7 public const byte NoPrecision = byte.MaxValue;
8
9 public const byte MaxPrecision = 99;
10
11 private readonly byte _format;
12
13 private readonly byte _precision;
14
15 public char Symbol => (char)_format;
16
17 public byte Precision => _precision;
18
19 public bool HasPrecision => _precision != byte.MaxValue;
20
21 public bool IsDefault
22 {
23 get
24 {
25 if (_format == 0)
26 {
27 return _precision == 0;
28 }
29 return false;
30 }
31 }
32
33 public StandardFormat(char symbol, byte precision = byte.MaxValue)
34 {
35 if (precision != byte.MaxValue && precision > 99)
36 {
38 }
39 if (symbol != (byte)symbol)
40 {
42 }
43 _format = (byte)symbol;
44 _precision = precision;
45 }
46
47 public static implicit operator StandardFormat(char symbol)
48 {
49 return new StandardFormat(symbol);
50 }
51
53 {
54 ParseHelper(format, out var standardFormat, throws: true);
55 return standardFormat;
56 }
57
58 public static StandardFormat Parse(string? format)
59 {
60 if (format != null)
61 {
62 return Parse(format.AsSpan());
63 }
64 return default(StandardFormat);
65 }
66
67 public static bool TryParse(ReadOnlySpan<char> format, out StandardFormat result)
68 {
69 return ParseHelper(format, out result);
70 }
71
72 private static bool ParseHelper(ReadOnlySpan<char> format, out StandardFormat standardFormat, bool throws = false)
73 {
74 standardFormat = default(StandardFormat);
75 if (format.Length == 0)
76 {
77 return true;
78 }
79 char symbol = format[0];
80 byte precision;
81 if (format.Length == 1)
82 {
83 precision = byte.MaxValue;
84 }
85 else
86 {
87 uint num = 0u;
88 for (int i = 1; i < format.Length; i++)
89 {
90 uint num2 = (uint)(format[i] - 48);
91 if (num2 > 9)
92 {
93 if (!throws)
94 {
95 return false;
96 }
98 }
99 num = num * 10 + num2;
100 if (num > 99)
101 {
102 if (!throws)
103 {
104 return false;
105 }
107 }
108 }
109 precision = (byte)num;
110 }
111 standardFormat = new StandardFormat(symbol, precision);
112 return true;
113 }
114
115 public override bool Equals([NotNullWhen(true)] object? obj)
116 {
117 if (obj is StandardFormat other)
118 {
119 return Equals(other);
120 }
121 return false;
122 }
123
124 public override int GetHashCode()
125 {
126 return _format.GetHashCode() ^ _precision.GetHashCode();
127 }
128
130 {
131 if (_format == other._format)
132 {
133 return _precision == other._precision;
134 }
135 return false;
136 }
137
138 public override string ToString()
139 {
140 Span<char> destination = stackalloc char[3];
141 return new string(destination[..Format(destination)]);
142 }
143
145 {
146 int num = 0;
147 char symbol = Symbol;
148 if (symbol != 0 && destination.Length == 3)
149 {
150 destination[0] = symbol;
151 num = 1;
152 uint num2 = Precision;
153 if (num2 != 255)
154 {
155 if (num2 >= 10)
156 {
157 uint num3;
158 (num3, num2) = Math.DivRem(num2, 10u);
159 destination[1] = (char)(48 + num3 % 10);
160 num = 2;
161 }
162 destination[num] = (char)(48 + num2);
163 num++;
164 }
165 }
166 return num;
167 }
168
169 public static bool operator ==(StandardFormat left, StandardFormat right)
170 {
171 return left.Equals(right);
172 }
173
174 public static bool operator !=(StandardFormat left, StandardFormat right)
175 {
176 return !left.Equals(right);
177 }
178}
static int DivRem(int a, int b, out int result)
Definition Math.cs:329
static string Argument_PrecisionTooLarge
Definition SR.cs:2096
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string Argument_CannotParsePrecision
Definition SR.cs:2092
Definition SR.cs:7
static void ThrowArgumentOutOfRangeException_PrecisionTooLarge()
static void ThrowArgumentOutOfRangeException_SymbolDoesNotFit()
static StandardFormat Parse(ReadOnlySpan< char > format)
static bool operator==(StandardFormat left, StandardFormat right)
int Format(Span< char > destination)
bool Equals(StandardFormat other)
static bool ParseHelper(ReadOnlySpan< char > format, out StandardFormat standardFormat, bool throws=false)
static StandardFormat Parse(string? format)
static bool operator!=(StandardFormat left, StandardFormat right)
static bool TryParse(ReadOnlySpan< char > format, out StandardFormat result)
override bool Equals([NotNullWhen(true)] object? obj)
StandardFormat(char symbol, byte precision=byte.MaxValue)