Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
InvariantModeCasing.cs
Go to the documentation of this file.
3using System.Text;
5
7
8internal static class InvariantModeCasing
9{
10 [MethodImpl(MethodImplOptions.AggressiveInlining)]
11 internal static char ToLower(char c)
12 {
13 return CharUnicodeInfo.ToLower(c);
14 }
15
16 [MethodImpl(MethodImplOptions.AggressiveInlining)]
17 internal static char ToUpper(char c)
18 {
19 return CharUnicodeInfo.ToUpper(c);
20 }
21
22 internal static string ToLower(string s)
23 {
24 if (s.Length == 0)
25 {
26 return string.Empty;
27 }
28 ReadOnlySpan<char> readOnlySpan = s;
29 int num = 0;
30 while (num < s.Length)
31 {
32 if (char.IsHighSurrogate(readOnlySpan[num]) && num < s.Length - 1 && char.IsLowSurrogate(readOnlySpan[num + 1]))
33 {
34 SurrogateCasing.ToLower(readOnlySpan[num], readOnlySpan[num + 1], out var hr, out var lr);
35 if (readOnlySpan[num] != hr || readOnlySpan[num + 1] != lr)
36 {
37 break;
38 }
39 num += 2;
40 }
41 else
42 {
43 if (ToLower(readOnlySpan[num]) != readOnlySpan[num])
44 {
45 break;
46 }
47 num++;
48 }
49 }
50 if (num >= s.Length)
51 {
52 return s;
53 }
54 return string.Create(s.Length, (s, num), delegate(Span<char> destination, (string s, int i) state)
55 {
56 ReadOnlySpan<char> readOnlySpan2 = state.s;
57 readOnlySpan2.Slice(0, state.i).CopyTo(destination);
58 ToLower(readOnlySpan2.Slice(state.i), destination.Slice(state.i));
59 });
60 }
61
62 internal static string ToUpper(string s)
63 {
64 if (s.Length == 0)
65 {
66 return string.Empty;
67 }
68 ReadOnlySpan<char> readOnlySpan = s;
69 int num = 0;
70 while (num < s.Length)
71 {
72 if (char.IsHighSurrogate(readOnlySpan[num]) && num < s.Length - 1 && char.IsLowSurrogate(readOnlySpan[num + 1]))
73 {
74 SurrogateCasing.ToUpper(readOnlySpan[num], readOnlySpan[num + 1], out var hr, out var lr);
75 if (readOnlySpan[num] != hr || readOnlySpan[num + 1] != lr)
76 {
77 break;
78 }
79 num += 2;
80 }
81 else
82 {
83 if (ToUpper(readOnlySpan[num]) != readOnlySpan[num])
84 {
85 break;
86 }
87 num++;
88 }
89 }
90 if (num >= s.Length)
91 {
92 return s;
93 }
94 return string.Create(s.Length, (s, num), delegate(Span<char> destination, (string s, int i) state)
95 {
96 ReadOnlySpan<char> readOnlySpan2 = state.s;
97 readOnlySpan2.Slice(0, state.i).CopyTo(destination);
98 ToUpper(readOnlySpan2.Slice(state.i), destination.Slice(state.i));
99 });
100 }
101
103 {
104 for (int i = 0; i < source.Length; i++)
105 {
106 char c = source[i];
107 if (char.IsHighSurrogate(c) && i < source.Length - 1)
108 {
109 char c2 = source[i + 1];
110 if (char.IsLowSurrogate(c2))
111 {
112 SurrogateCasing.ToUpper(c, c2, out var hr, out var lr);
113 destination[i] = hr;
114 destination[i + 1] = lr;
115 i++;
116 continue;
117 }
118 }
119 destination[i] = ToUpper(c);
120 }
121 }
122
124 {
125 for (int i = 0; i < source.Length; i++)
126 {
127 char c = source[i];
128 if (char.IsHighSurrogate(c) && i < source.Length - 1)
129 {
130 char c2 = source[i + 1];
131 if (char.IsLowSurrogate(c2))
132 {
133 SurrogateCasing.ToLower(c, c2, out var hr, out var lr);
134 destination[i] = hr;
135 destination[i + 1] = lr;
136 i++;
137 continue;
138 }
139 }
140 destination[i] = ToLower(c);
141 }
142 }
143
144 [MethodImpl(MethodImplOptions.AggressiveInlining)]
145 private static (uint, int) GetScalar(ref char source, int index, int length)
146 {
147 char c = source;
148 if (!char.IsHighSurrogate(c) || index >= length - 1)
149 {
150 return (c, 1);
151 }
152 char c2 = Unsafe.Add(ref source, 1);
153 if (!char.IsLowSurrogate(c2))
154 {
155 return (c, 1);
156 }
158 }
159
160 internal static int CompareStringIgnoreCase(ref char strA, int lengthA, ref char strB, int lengthB)
161 {
162 int num = Math.Min(lengthA, lengthB);
163 ref char source = ref strA;
164 ref char source2 = ref strB;
165 int num2 = 0;
166 while (num2 < num)
167 {
168 var (num3, num4) = GetScalar(ref source, num2, lengthA);
169 var (num5, elementOffset) = GetScalar(ref source2, num2, lengthB);
170 if (num3 == num5)
171 {
172 num2 += num4;
173 source = ref Unsafe.Add(ref source, num4);
174 source2 = ref Unsafe.Add(ref source2, elementOffset);
175 continue;
176 }
177 uint num6 = CharUnicodeInfo.ToUpper(num3);
178 uint num7 = CharUnicodeInfo.ToUpper(num5);
179 if (num6 == num7)
180 {
181 num2 += num4;
182 source = ref Unsafe.Add(ref source, num4);
183 source2 = ref Unsafe.Add(ref source2, elementOffset);
184 continue;
185 }
186 return (int)(num3 - num5);
187 }
188 return lengthA - lengthB;
189 }
190
192 {
193 fixed (char* ptr = &MemoryMarshal.GetReference(source))
194 {
195 fixed (char* ptr3 = &MemoryMarshal.GetReference(value))
196 {
197 char* ptr2 = ptr + (source.Length - value.Length);
198 char* ptr4 = ptr3 + value.Length - 1;
199 for (char* ptr5 = ptr; ptr5 <= ptr2; ptr5++)
200 {
201 char* ptr6 = ptr3;
202 char* ptr7 = ptr5;
203 while (ptr6 <= ptr4)
204 {
205 if (!char.IsHighSurrogate(*ptr6) || ptr6 == ptr4)
206 {
207 if (*ptr6 != *ptr7 && ToUpper(*ptr6) != ToUpper(*ptr7))
208 {
209 break;
210 }
211 ptr6++;
212 ptr7++;
213 }
214 else if (char.IsHighSurrogate(*ptr7) && char.IsLowSurrogate(ptr7[1]) && char.IsLowSurrogate(ptr6[1]))
215 {
216 if (!SurrogateCasing.Equal(*ptr7, ptr7[1], *ptr6, ptr6[1]))
217 {
218 break;
219 }
220 ptr7 += 2;
221 ptr6 += 2;
222 }
223 else
224 {
225 if (*ptr6 != *ptr7)
226 {
227 break;
228 }
229 ptr7++;
230 ptr6++;
231 }
232 }
233 if (ptr6 > ptr4)
234 {
235 return (int)(ptr5 - ptr);
236 }
237 }
238 return -1;
239 }
240 }
241 }
242
244 {
245 fixed (char* ptr3 = &MemoryMarshal.GetReference(source))
246 {
247 fixed (char* ptr = &MemoryMarshal.GetReference(value))
248 {
249 char* ptr2 = ptr + value.Length - 1;
250 for (char* ptr4 = ptr3 + (source.Length - value.Length); ptr4 >= ptr3; ptr4--)
251 {
252 char* ptr5 = ptr;
253 char* ptr6 = ptr4;
254 while (ptr5 <= ptr2)
255 {
256 if (!char.IsHighSurrogate(*ptr5) || ptr5 == ptr2)
257 {
258 if (*ptr5 != *ptr6 && ToUpper(*ptr5) != ToUpper(*ptr6))
259 {
260 break;
261 }
262 ptr5++;
263 ptr6++;
264 }
265 else if (char.IsHighSurrogate(*ptr6) && char.IsLowSurrogate(ptr6[1]) && char.IsLowSurrogate(ptr5[1]))
266 {
267 if (!SurrogateCasing.Equal(*ptr6, ptr6[1], *ptr5, ptr5[1]))
268 {
269 break;
270 }
271 ptr6 += 2;
272 ptr5 += 2;
273 }
274 else
275 {
276 if (*ptr5 != *ptr6)
277 {
278 break;
279 }
280 ptr6++;
281 ptr5++;
282 }
283 }
284 if (ptr5 > ptr2)
285 {
286 return (int)(ptr4 - ptr3);
287 }
288 }
289 return -1;
290 }
291 }
292 }
293}
static char ToLower(char codePoint)
static char ToUpper(char codePoint)
static unsafe int IndexOfIgnoreCase(ReadOnlySpan< char > source, ReadOnlySpan< char > value)
static unsafe int LastIndexOfIgnoreCase(ReadOnlySpan< char > source, ReadOnlySpan< char > value)
static void ToLower(ReadOnlySpan< char > source, Span< char > destination)
static int GetScalar(ref char source, int index, int length)
static int CompareStringIgnoreCase(ref char strA, int lengthA, ref char strB, int lengthB)
static void ToUpper(ReadOnlySpan< char > source, Span< char > destination)
static void ToLower(char h, char l, out char hr, out char lr)
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)
ReadOnlySpan< T > Slice(int start)