Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
NumberFormatter.cs
Go to the documentation of this file.
2using System.Text;
4
6
7internal sealed class NumberFormatter : NumberFormatterBase
8{
9 private readonly string _formatString;
10
11 private readonly int _lang;
12
13 private readonly string _letterValue;
14
15 private readonly string _groupingSeparator;
16
17 private readonly int _groupingSize;
18
19 private readonly List<TokenInfo> _tokens;
20
21 private static readonly TokenInfo s_defaultFormat = TokenInfo.CreateFormat("0", 0, 1);
22
23 private static readonly TokenInfo s_defaultSeparator = TokenInfo.CreateSeparator(".", 0, 1);
24
25 public NumberFormatter(string formatString, int lang, string letterValue, string groupingSeparator, int groupingSize)
26 {
27 _formatString = formatString;
28 _lang = lang;
32 if (formatString == "1" || formatString.Length == 0)
33 {
34 return;
35 }
37 int num = 0;
38 bool flag = CharUtil.IsAlphaNumeric(formatString[num]);
39 if (flag)
40 {
41 _tokens.Add(null);
42 }
43 for (int i = 0; i <= formatString.Length; i++)
44 {
45 if (i == formatString.Length || flag != CharUtil.IsAlphaNumeric(formatString[i]))
46 {
47 if (flag)
48 {
49 _tokens.Add(TokenInfo.CreateFormat(formatString, num, i - num));
50 }
51 else
52 {
53 _tokens.Add(TokenInfo.CreateSeparator(formatString, num, i - num));
54 }
55 num = i;
56 flag = !flag;
57 }
58 }
59 }
60
62 {
64 if (val.Count == 1 && val[0].ValueType == typeof(double))
65 {
66 double valueAsDouble = val[0].ValueAsDouble;
67 if (!(0.5 <= valueAsDouble) || !(valueAsDouble < double.PositiveInfinity))
68 {
70 }
71 }
72 if (_tokens == null)
73 {
74 for (int i = 0; i < val.Count; i++)
75 {
76 if (i > 0)
77 {
78 stringBuilder.Append('.');
79 }
80 FormatItem(stringBuilder, val[i], '1', 1);
81 }
82 }
83 else
84 {
85 int num = _tokens.Count;
87 TokenInfo tokenInfo2 = ((num % 2 != 0) ? _tokens[--num] : null);
88 TokenInfo tokenInfo3 = ((2 < num) ? _tokens[num - 2] : s_defaultSeparator);
89 TokenInfo tokenInfo4 = ((0 < num) ? _tokens[num - 1] : s_defaultFormat);
90 if (tokenInfo != null)
91 {
92 stringBuilder.Append(tokenInfo.formatString, tokenInfo.startIdx, tokenInfo.length);
93 }
94 int count = val.Count;
95 for (int j = 0; j < count; j++)
96 {
97 int num2 = j * 2;
98 bool flag = num2 < num;
99 if (j > 0)
100 {
102 stringBuilder.Append(tokenInfo5.formatString, tokenInfo5.startIdx, tokenInfo5.length);
103 }
104 TokenInfo tokenInfo6 = (flag ? _tokens[num2 + 1] : tokenInfo4);
105 FormatItem(stringBuilder, val[j], tokenInfo6.startChar, tokenInfo6.length);
106 }
107 if (tokenInfo2 != null)
108 {
109 stringBuilder.Append(tokenInfo2.formatString, tokenInfo2.startIdx, tokenInfo2.length);
110 }
111 }
112 return stringBuilder.ToString();
113 }
114
115 private void FormatItem(StringBuilder sb, XPathItem item, char startChar, int length)
116 {
117 double num = ((!(item.ValueType == typeof(int))) ? XsltFunctions.Round(item.ValueAsDouble) : ((double)item.ValueAsInt));
118 char zero = '0';
119 switch (startChar)
120 {
121 case 'A':
122 case 'a':
123 if (num <= 2147483647.0)
124 {
125 NumberFormatterBase.ConvertToAlphabetic(sb, num, startChar, 26);
126 return;
127 }
128 break;
129 case 'I':
130 case 'i':
131 if (num <= 32767.0)
132 {
133 NumberFormatterBase.ConvertToRoman(sb, num, startChar == 'I');
134 return;
135 }
136 break;
137 default:
138 zero = (char)(startChar - 1);
139 break;
140 case '1':
141 break;
142 }
144 }
145
146 private unsafe static string ConvertToDecimal(double val, int minLen, char zero, string groupSeparator, int groupSize)
147 {
148 string text = XPathConvert.DoubleToString(val);
149 int num = zero - 48;
150 int length = text.Length;
151 int num2 = Math.Max(length, minLen);
152 char* ptr;
153 char c;
154 checked
155 {
156 if (groupSize != 0)
157 {
159 }
160 if (num2 == length && num == 0)
161 {
162 return text;
163 }
164 if (groupSize == 0 && num == 0)
165 {
166 return text.PadLeft(num2, zero);
167 }
168 ptr = stackalloc char[num2];
169 c = ((groupSeparator.Length > 0) ? groupSeparator[0] : ' ');
170 }
171 fixed (char* ptr2 = text)
172 {
173 char* ptr3 = ptr2 + length - 1;
174 char* ptr4 = ptr + num2 - 1;
175 int num3 = groupSize;
176 while (true)
177 {
178 *(ptr4--) = ((ptr3 >= ptr2) ? ((char)(*(ptr3--) + num)) : zero);
179 if (ptr4 < ptr)
180 {
181 break;
182 }
183 if (--num3 == 0)
184 {
185 *(ptr4--) = c;
186 num3 = groupSize;
187 }
188 }
189 }
190 return new string(ptr, 0, num2);
191 }
192}
void Add(TKey key, TValue value)
static byte Max(byte val1, byte val2)
Definition Math.cs:738
StringBuilder Append(char value, int repeatCount)
static bool IsAlphaNumeric(char ch)
Definition CharUtil.cs:7
static void ConvertToRoman(StringBuilder sb, double val, bool upperCase)
static void ConvertToAlphabetic(StringBuilder sb, double val, char firstChar, int totalChars)
void FormatItem(StringBuilder sb, XPathItem item, char startChar, int length)
static readonly TokenInfo s_defaultSeparator
static unsafe string ConvertToDecimal(double val, int minLen, char zero, string groupSeparator, int groupSize)
NumberFormatter(string formatString, int lang, string letterValue, string groupingSeparator, int groupingSize)
readonly List< TokenInfo > _tokens
static readonly TokenInfo s_defaultFormat
string FormatSequence(IList< XPathItem > val)
static TokenInfo CreateFormat(string formatString, int startIdx, int tokLen)
Definition TokenInfo.cs:26
static TokenInfo CreateSeparator(string formatString, int startIdx, int tokLen)
Definition TokenInfo.cs:17
static double Round(double value)
static unsafe string DoubleToString(double dbl)