Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
JsonHelpers.cs
Go to the documentation of this file.
5
6namespace System.Text.Json;
7
8internal static class JsonHelpers
9{
10 private struct DateTimeParseData
11 {
12 public int Year;
13
14 public int Month;
15
16 public int Day;
17
18 public int Hour;
19
20 public int Minute;
21
22 public int Second;
23
24 public int Fraction;
25
26 public int OffsetHours;
27
28 public int OffsetMinutes;
29
30 public byte OffsetToken;
31
32 public bool OffsetNegative => OffsetToken == 45;
33 }
34
35 private static readonly int[] s_daysToMonth365 = new int[13]
36 {
37 0, 31, 59, 90, 120, 151, 181, 212, 243, 273,
38 304, 334, 365
39 };
40
41 private static readonly int[] s_daysToMonth366 = new int[13]
42 {
43 0, 31, 60, 91, 121, 152, 182, 213, 244, 274,
44 305, 335, 366
45 };
46
47 public static bool TryAdd<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, in TKey key, in TValue value)
48 {
49 return dictionary.TryAdd(key, value);
50 }
51
52 [MethodImpl(MethodImplOptions.AggressiveInlining)]
53 public static ReadOnlySpan<byte> GetSpan(this ref Utf8JsonReader reader)
54 {
55 if (!reader.HasValueSequence)
56 {
57 return reader.ValueSpan;
58 }
59 ReadOnlySequence<byte> sequence = reader.ValueSequence;
60 return BuffersExtensions.ToArray(in sequence);
61 }
62
63 [MethodImpl(MethodImplOptions.AggressiveInlining)]
64 public static bool IsInRangeInclusive(uint value, uint lowerBound, uint upperBound)
65 {
67 }
68
69 [MethodImpl(MethodImplOptions.AggressiveInlining)]
70 public static bool IsInRangeInclusive(int value, int lowerBound, int upperBound)
71 {
72 return (uint)(value - lowerBound) <= (uint)(upperBound - lowerBound);
73 }
74
75 [MethodImpl(MethodImplOptions.AggressiveInlining)]
76 public static bool IsInRangeInclusive(long value, long lowerBound, long upperBound)
77 {
78 return (ulong)(value - lowerBound) <= (ulong)(upperBound - lowerBound);
79 }
80
81 public static bool IsDigit(byte value)
82 {
83 return (uint)(value - 48) <= 9u;
84 }
85
86 [MethodImpl(MethodImplOptions.AggressiveInlining)]
87 public static void ReadWithVerify(this ref Utf8JsonReader reader)
88 {
89 bool flag = reader.Read();
90 }
91
93 {
94 return Encoding.UTF8.GetString(bytes);
95 }
96
101
102 public static bool IsFinite(double value)
103 {
104 return double.IsFinite(value);
105 }
106
107 public static bool IsFinite(float value)
108 {
109 return float.IsFinite(value);
110 }
111
112 [MethodImpl(MethodImplOptions.AggressiveInlining)]
113 public static void ValidateInt32MaxArrayLength(uint length)
114 {
115 if (length > 2146435071)
116 {
118 }
119 }
120
121 [MethodImpl(MethodImplOptions.AggressiveInlining)]
123 {
124 return IsInRangeInclusive(length, 10, 252);
125 }
126
128 {
130 {
131 value = default(DateTime);
132 return false;
133 }
134 if (parseData.OffsetToken == 90)
135 {
137 }
138 if (parseData.OffsetToken == 43 || parseData.OffsetToken == 45)
139 {
141 {
142 value = default(DateTime);
143 return false;
144 }
145 value = value2.LocalDateTime;
146 return true;
147 }
148 return TryCreateDateTime(parseData, DateTimeKind.Unspecified, out value);
149 }
150
152 {
154 {
155 value = default(DateTimeOffset);
156 return false;
157 }
158 if (parseData.OffsetToken == 90 || parseData.OffsetToken == 43 || parseData.OffsetToken == 45)
159 {
161 }
163 }
164
166 {
167 parseData = default(DateTimeParseData);
168 uint num = (uint)(source[0] - 48);
169 uint num2 = (uint)(source[1] - 48);
170 uint num3 = (uint)(source[2] - 48);
171 uint num4 = (uint)(source[3] - 48);
172 if (num > 9 || num2 > 9 || num3 > 9 || num4 > 9)
173 {
174 return false;
175 }
176 parseData.Year = (int)(num * 1000 + num2 * 100 + num3 * 10 + num4);
177 if (source[4] != 45 || !TryGetNextTwoDigits(source.Slice(5, 2), ref parseData.Month) || source[7] != 45 || !TryGetNextTwoDigits(source.Slice(8, 2), ref parseData.Day))
178 {
179 return false;
180 }
181 if (source.Length == 10)
182 {
183 return true;
184 }
185 if (source.Length < 16)
186 {
187 return false;
188 }
189 if (source[10] != 84 || source[13] != 58 || !TryGetNextTwoDigits(source.Slice(11, 2), ref parseData.Hour) || !TryGetNextTwoDigits(source.Slice(14, 2), ref parseData.Minute))
190 {
191 return false;
192 }
193 if (source.Length == 16)
194 {
195 return true;
196 }
197 byte b = source[16];
198 int num5 = 17;
199 switch (b)
200 {
201 case 90:
203 return num5 == source.Length;
204 case 43:
205 case 45:
207 return ParseOffset(ref parseData, source.Slice(num5));
208 default:
209 return false;
210 case 58:
211 if (source.Length < 19 || !TryGetNextTwoDigits(source.Slice(17, 2), ref parseData.Second))
212 {
213 return false;
214 }
215 if (source.Length == 19)
216 {
217 return true;
218 }
219 b = source[19];
220 num5 = 20;
221 switch (b)
222 {
223 case 90:
225 return num5 == source.Length;
226 case 43:
227 case 45:
229 return ParseOffset(ref parseData, source.Slice(num5));
230 default:
231 return false;
232 case 46:
233 {
234 if (source.Length < 21)
235 {
236 return false;
237 }
238 int i = 0;
239 for (int num6 = Math.Min(num5 + 16, source.Length); num5 < num6; num5++)
240 {
241 if (!IsDigit(b = source[num5]))
242 {
243 break;
244 }
245 if (i < 7)
246 {
248 i++;
249 }
250 }
251 if (parseData.Fraction != 0)
252 {
253 for (; i < 7; i++)
254 {
255 parseData.Fraction *= 10;
256 }
257 }
258 if (num5 == source.Length)
259 {
260 return true;
261 }
262 b = source[num5++];
263 switch (b)
264 {
265 case 90:
267 return num5 == source.Length;
268 case 43:
269 case 45:
271 return ParseOffset(ref parseData, source.Slice(num5));
272 default:
273 return false;
274 }
275 }
276 }
277 }
279 {
280 if (offsetData.Length < 2 || !TryGetNextTwoDigits(offsetData.Slice(0, 2), ref parseData.OffsetHours))
281 {
282 return false;
283 }
284 if (offsetData.Length == 2)
285 {
286 return true;
287 }
288 if (offsetData.Length != 5 || offsetData[2] != 58 || !TryGetNextTwoDigits(offsetData.Slice(3), ref parseData.OffsetMinutes))
289 {
290 return false;
291 }
292 return true;
293 }
294 }
295
296 [MethodImpl(MethodImplOptions.AggressiveInlining)]
298 {
299 uint num = (uint)(source[0] - 48);
300 uint num2 = (uint)(source[1] - 48);
301 if (num > 9 || num2 > 9)
302 {
303 value = 0;
304 return false;
305 }
306 value = (int)(num * 10 + num2);
307 return true;
308 }
309
311 {
312 if ((uint)parseData.OffsetHours > 14u)
313 {
314 value = default(DateTimeOffset);
315 return false;
316 }
317 if ((uint)parseData.OffsetMinutes > 59u)
318 {
319 value = default(DateTimeOffset);
320 return false;
321 }
322 if (parseData.OffsetHours == 14 && parseData.OffsetMinutes != 0)
323 {
324 value = default(DateTimeOffset);
325 return false;
326 }
327 long num = ((long)parseData.OffsetHours * 3600L + (long)parseData.OffsetMinutes * 60L) * 10000000;
328 if (parseData.OffsetNegative)
329 {
330 num = -num;
331 }
332 try
333 {
334 value = new DateTimeOffset(dateTime.Ticks, new TimeSpan(num));
335 }
337 {
338 value = default(DateTimeOffset);
339 return false;
340 }
341 return true;
342 }
343
345 {
347 {
348 value = default(DateTimeOffset);
349 return false;
350 }
352 {
353 value = default(DateTimeOffset);
354 return false;
355 }
356 return true;
357 }
358
360 {
362 {
363 value = default(DateTimeOffset);
364 return false;
365 }
366 try
367 {
369 }
371 {
372 value = default(DateTimeOffset);
373 return false;
374 }
375 return true;
376 }
377
379 {
380 if (parseData.Year == 0)
381 {
382 value = default(DateTime);
383 return false;
384 }
385 if ((uint)(parseData.Month - 1) >= 12u)
386 {
387 value = default(DateTime);
388 return false;
389 }
390 uint num = (uint)(parseData.Day - 1);
391 if (num >= 28 && num >= DateTime.DaysInMonth(parseData.Year, parseData.Month))
392 {
393 value = default(DateTime);
394 return false;
395 }
396 if ((uint)parseData.Hour > 23u)
397 {
398 value = default(DateTime);
399 return false;
400 }
401 if ((uint)parseData.Minute > 59u)
402 {
403 value = default(DateTime);
404 return false;
405 }
406 if ((uint)parseData.Second > 59u)
407 {
408 value = default(DateTime);
409 return false;
410 }
412 int num2 = parseData.Year - 1;
413 int num3 = num2 * 365 + num2 / 4 - num2 / 100 + num2 / 400 + array[parseData.Month - 1] + parseData.Day - 1;
414 long num4 = num3 * 864000000000L;
415 int num5 = parseData.Hour * 3600 + parseData.Minute * 60 + parseData.Second;
416 num4 += (long)num5 * 10000000L;
417 num4 += parseData.Fraction;
418 value = new DateTime(num4, kind);
419 return true;
420 }
421
422 [MethodImpl(MethodImplOptions.AggressiveInlining)]
424 {
425 int num = JsonWriterHelper.NeedsEscaping(utf8Value, encoder);
426 if (num != -1)
427 {
428 return GetEscapedPropertyNameSection(utf8Value, num, encoder);
429 }
431 }
432
434 {
435 byte[] array = null;
437 Span<byte> span = ((maxEscapedLength > 256) ? ((Span<byte>)(array = ArrayPool<byte>.Shared.Rent(maxEscapedLength))) : stackalloc byte[256]);
440 byte[] result = destination.Slice(0, written).ToArray();
441 if (array != null)
442 {
443 ArrayPool<byte>.Shared.Return(array);
444 }
445 return result;
446 }
447
462
464 {
465 int length = utf8Value.Length;
466 byte[] array = new byte[length + 3];
467 array[0] = 34;
468 utf8Value.CopyTo(array.AsSpan(1, length));
469 array[++length] = 34;
470 array[++length] = 58;
471 return array;
472 }
473}
void CopyTo(KeyValuePair< TKey, TValue >[] array, int index)
static byte Min(byte val1, byte val2)
Definition Math.cs:912
static Encoding UTF8
Definition Encoding.cs:526
static readonly int[] s_daysToMonth366
static string Utf8GetString(ReadOnlySpan< byte > bytes)
static bool TryCreateDateTimeOffset(DateTime dateTime, ref DateTimeParseData parseData, out DateTimeOffset value)
static bool IsFinite(double value)
static bool TryParseAsISO(ReadOnlySpan< byte > source, out DateTimeOffset value)
static bool TryCreateDateTimeOffset(ref DateTimeParseData parseData, out DateTimeOffset value)
static bool TryCreateDateTime(DateTimeParseData parseData, DateTimeKind kind, out DateTime value)
static bool IsInRangeInclusive(uint value, uint lowerBound, uint upperBound)
static Dictionary< TKey, TValue > CreateDictionaryFromCollection< TKey, TValue >(IEnumerable< KeyValuePair< TKey, TValue > > collection, IEqualityComparer< TKey > comparer)
static byte[] EscapeValue(ReadOnlySpan< byte > utf8Value, int firstEscapeIndexVal, JavaScriptEncoder encoder)
static byte[] GetEscapedPropertyNameSection(ReadOnlySpan< byte > utf8Value, int firstEscapeIndexVal, JavaScriptEncoder encoder)
static bool IsInRangeInclusive(int value, int lowerBound, int upperBound)
static void ValidateInt32MaxArrayLength(uint length)
static bool IsInRangeInclusive(long value, long lowerBound, long upperBound)
static bool TryParseDateTimeOffset(ReadOnlySpan< byte > source, out DateTimeParseData parseData)
static ReadOnlySpan< byte > GetSpan(this ref Utf8JsonReader reader)
static bool TryAdd< TKey, TValue >(this Dictionary< TKey, TValue > dictionary, in TKey key, in TValue value)
static bool IsValidDateTimeOffsetParseLength(int length)
static void ReadWithVerify(this ref Utf8JsonReader reader)
static bool TryGetNextTwoDigits(ReadOnlySpan< byte > source, ref int value)
static bool IsFinite(float value)
static byte[] GetPropertyNameSection(ReadOnlySpan< byte > utf8Value)
static bool TryCreateDateTimeOffsetInterpretingDataAsLocalTime(DateTimeParseData parseData, out DateTimeOffset value)
static byte[] GetEscapedPropertyNameSection(ReadOnlySpan< byte > utf8Value, JavaScriptEncoder encoder)
static bool IsDigit(byte value)
static readonly int[] s_daysToMonth365
static bool TryParseAsISO(ReadOnlySpan< byte > source, out DateTime value)
static bool NeedsEscaping(byte value)
static void EscapeString(ReadOnlySpan< byte > value, Span< byte > destination, JavaScriptEncoder encoder, ref int written)
static int GetMaxEscapedLength(int textLength, int firstIndexToEscape)
static void ThrowOutOfMemoryException(uint capacity)
static int DaysInMonth(int year, int month)
Definition DateTime.cs:624
static bool IsLeapYear(int year)
Definition DateTime.cs:898