Terraria v1.4.4.9
Terraria source code documentation
All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events Macros
OrdinalCasing.cs
Go to the documentation of this file.
3using System.Text;
6
8
9internal static class OrdinalCasing
10{
11 private static ushort[] s_noCasingPage = Array.Empty<ushort>();
12
13 private static ushort[] s_basicLatin = new ushort[256]
14 {
15 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
16 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
17 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
18 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
19 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
20 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
21 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
22 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
23 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
24 90, 91, 92, 93, 94, 95, 96, 65, 66, 67,
25 68, 69, 70, 71, 72, 73, 74, 75, 76, 77,
26 78, 79, 80, 81, 82, 83, 84, 85, 86, 87,
27 88, 89, 90, 123, 124, 125, 126, 127, 128, 129,
28 130, 131, 132, 133, 134, 135, 136, 137, 138, 139,
29 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
30 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
31 160, 161, 162, 163, 164, 165, 166, 167, 168, 169,
32 170, 171, 172, 173, 174, 175, 176, 177, 178, 179,
33 180, 924, 182, 183, 184, 185, 186, 187, 188, 189,
34 190, 191, 192, 193, 194, 195, 196, 197, 198, 199,
35 200, 201, 202, 203, 204, 205, 206, 207, 208, 209,
36 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
37 220, 221, 222, 223, 192, 193, 194, 195, 196, 197,
38 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
39 208, 209, 210, 211, 212, 213, 214, 247, 216, 217,
40 218, 219, 220, 221, 222, 376
41 };
42
43 private static ushort[][] s_casingTable = InitCasingTable();
44
45 private static ReadOnlySpan<byte> s_casingTableInit => new byte[32]
46 {
47 0, 0, 76, 0, 55, 224, 31, 255, 255, 255,
48 255, 255, 255, 255, 255, 255, 255, 255, 255, 254,
49 244, 15, 255, 255, 255, 255, 254, 255, 255, 255,
50 255, 200
51 };
52
53 [MethodImpl(MethodImplOptions.AggressiveInlining)]
54 internal static char ToUpper(char c)
55 {
56 int num = (int)c >> 8;
57 if (num == 0)
58 {
59 return (char)s_basicLatin[(uint)c];
60 }
61 ushort[] array = s_casingTable[num];
62 if (array == s_noCasingPage)
63 {
64 return c;
65 }
66 if (array == null)
67 {
69 }
70 return (char)array[c & 0xFF];
71 }
72
74 {
75 for (int i = 0; i < source.Length; i++)
76 {
77 char c = source[i];
78 if (c <= 'ÿ')
79 {
80 destination[i] = (char)s_basicLatin[(uint)c];
81 continue;
82 }
83 if (char.IsHighSurrogate(c) && i < source.Length - 1)
84 {
85 char c2 = source[i + 1];
86 if (char.IsLowSurrogate(c2))
87 {
88 SurrogateCasing.ToUpper(c, c2, out destination[i], out destination[i + 1]);
89 i++;
90 continue;
91 }
92 }
93 destination[i] = ToUpper(c);
94 }
95 }
96
97 internal static int CompareStringIgnoreCase(ref char strA, int lengthA, ref char strB, int lengthB)
98 {
99 int num = Math.Min(lengthA, lengthB);
100 ref char reference = ref strA;
101 ref char reference2 = ref strB;
102 int num2 = 0;
103 while (num2 < num)
104 {
105 char c = reference;
106 char c2 = reference2;
107 char c3 = '\0';
108 if (!char.IsHighSurrogate(c) || num2 >= lengthA - 1 || !char.IsLowSurrogate(c3 = Unsafe.Add(ref reference, 1)))
109 {
110 if (!char.IsHighSurrogate(c2) || num2 >= lengthB - 1 || !char.IsLowSurrogate(Unsafe.Add(ref reference2, 1)))
111 {
112 if (c2 == c)
113 {
114 num2++;
115 reference = ref Unsafe.Add(ref reference, 1);
116 reference2 = ref Unsafe.Add(ref reference2, 1);
117 continue;
118 }
119 char c4 = ToUpper(c);
120 char c5 = ToUpper(c2);
121 if (c4 == c5)
122 {
123 num2++;
124 reference = ref Unsafe.Add(ref reference, 1);
125 reference2 = ref Unsafe.Add(ref reference2, 1);
126 continue;
127 }
128 return c - c2;
129 }
130 return -1;
131 }
132 char c6 = '\0';
133 if (!char.IsHighSurrogate(c2) || num2 >= lengthB - 1 || !char.IsLowSurrogate(c6 = Unsafe.Add(ref reference2, 1)))
134 {
135 return 1;
136 }
137 if (c == c2 && c3 == c6)
138 {
139 num2 += 2;
140 reference = ref Unsafe.Add(ref reference, 2);
141 reference2 = ref Unsafe.Add(ref reference2, 2);
142 continue;
143 }
146 if (num3 == num4)
147 {
148 num2 += 2;
149 reference = ref Unsafe.Add(ref reference, 2);
150 reference2 = ref Unsafe.Add(ref reference2, 2);
151 continue;
152 }
153 return (int)(num3 - num4);
154 }
155 return lengthA - lengthB;
156 }
157
159 {
160 fixed (char* ptr = &MemoryMarshal.GetReference(source))
161 {
162 fixed (char* ptr3 = &MemoryMarshal.GetReference(value))
163 {
164 char* ptr2 = ptr + (source.Length - value.Length);
165 char* ptr4 = ptr3 + value.Length - 1;
166 for (char* ptr5 = ptr; ptr5 <= ptr2; ptr5++)
167 {
168 char* ptr6 = ptr3;
169 char* ptr7 = ptr5;
170 while (ptr6 <= ptr4)
171 {
172 if (!char.IsHighSurrogate(*ptr6) || ptr6 == ptr4)
173 {
174 if (*ptr6 != *ptr7 && ToUpper(*ptr6) != ToUpper(*ptr7))
175 {
176 break;
177 }
178 ptr6++;
179 ptr7++;
180 }
181 else if (char.IsHighSurrogate(*ptr7) && char.IsLowSurrogate(ptr7[1]) && char.IsLowSurrogate(ptr6[1]))
182 {
183 if (!SurrogateCasing.Equal(*ptr7, ptr7[1], *ptr6, ptr6[1]))
184 {
185 break;
186 }
187 ptr7 += 2;
188 ptr6 += 2;
189 }
190 else
191 {
192 if (*ptr6 != *ptr7)
193 {
194 break;
195 }
196 ptr7++;
197 ptr6++;
198 }
199 }
200 if (ptr6 > ptr4)
201 {
202 return (int)(ptr5 - ptr);
203 }
204 }
205 return -1;
206 }
207 }
208 }
209
211 {
212 fixed (char* ptr3 = &MemoryMarshal.GetReference(source))
213 {
214 fixed (char* ptr = &MemoryMarshal.GetReference(value))
215 {
216 char* ptr2 = ptr + value.Length - 1;
217 for (char* ptr4 = ptr3 + (source.Length - value.Length); ptr4 >= ptr3; ptr4--)
218 {
219 char* ptr5 = ptr;
220 char* ptr6 = ptr4;
221 while (ptr5 <= ptr2)
222 {
223 if (!char.IsHighSurrogate(*ptr5) || ptr5 == ptr2)
224 {
225 if (*ptr5 != *ptr6 && ToUpper(*ptr5) != ToUpper(*ptr6))
226 {
227 break;
228 }
229 ptr5++;
230 ptr6++;
231 }
232 else if (char.IsHighSurrogate(*ptr6) && char.IsLowSurrogate(ptr6[1]) && char.IsLowSurrogate(ptr5[1]))
233 {
234 if (!SurrogateCasing.Equal(*ptr6, ptr6[1], *ptr5, ptr5[1]))
235 {
236 break;
237 }
238 ptr6 += 2;
239 ptr5 += 2;
240 }
241 else
242 {
243 if (*ptr5 != *ptr6)
244 {
245 break;
246 }
247 ptr6++;
248 ptr5++;
249 }
250 }
251 if (ptr5 > ptr2)
252 {
253 return (int)(ptr4 - ptr3);
254 }
255 }
256 return -1;
257 }
258 }
259 }
260
261 private static ushort[][] InitCasingTable()
262 {
263 ushort[][] array = new ushort[s_casingTableInit.Length * 8][];
264 for (int i = 0; i < s_casingTableInit.Length * 8; i++)
265 {
266 byte b = (byte)(s_casingTableInit[i / 8] >> 7 - i % 8);
267 if ((b & 1) == 1)
268 {
270 }
271 }
272 array[0] = s_basicLatin;
273 return array;
274 }
275
276 private unsafe static ushort[] InitOrdinalCasingPage(int pageNumber)
277 {
278 ushort[] array = new ushort[256];
279 fixed (ushort* ptr = array)
280 {
281 char* pTarget = (char*)ptr;
282 Interop.Globalization.InitOrdinalCasingPage(pageNumber, pTarget);
283 }
284 Volatile.Write(ref s_casingTable[pageNumber], array);
285 return array;
286 }
287}
static unsafe void InitOrdinalCasingPage(int pageNumber, char *pTarget)
static char ToUpper(char codePoint)
static unsafe ushort[] InitOrdinalCasingPage(int pageNumber)
static void ToUpperOrdinal(ReadOnlySpan< char > source, Span< char > destination)
static unsafe int LastIndexOf(ReadOnlySpan< char > source, ReadOnlySpan< char > value)
static unsafe int IndexOf(ReadOnlySpan< char > source, ReadOnlySpan< char > value)
static int CompareStringIgnoreCase(ref char strA, int lengthA, ref char strB, int lengthB)
static ReadOnlySpan< byte > s_casingTableInit
static void ToUpper(char h, char l, out char hr, out char lr)
static bool Equal(char h1, char l1, char h2, char l2)
static byte Min(byte val1, byte val2)
Definition Math.cs:912
static uint GetScalarFromUtf16SurrogatePair(uint highSurrogateCodePoint, uint lowSurrogateCodePoint)
static void Write(ref bool location, bool value)
Definition Volatile.cs:74