Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
XsdDuration.cs
Go to the documentation of this file.
1using System.Text;
2
3namespace System.Xml.Schema;
4
5internal struct XsdDuration
6{
7 private enum Parts
8 {
9 HasNone = 0,
10 HasYears = 1,
11 HasMonths = 2,
12 HasDays = 4,
13 HasHours = 8,
14 HasMinutes = 0x10,
15 HasSeconds = 0x20
16 }
17
18 public enum DurationType
19 {
23 }
24
25 private int _years;
26
27 private int _months;
28
29 private int _days;
30
31 private int _hours;
32
33 private int _minutes;
34
35 private int _seconds;
36
37 private uint _nanoseconds;
38
39 public bool IsNegative => (_nanoseconds & 0x80000000u) != 0;
40
41 public int Years => _years;
42
43 public int Months => _months;
44
45 public int Days => _days;
46
47 public int Hours => _hours;
48
49 public int Minutes => _minutes;
50
51 public int Seconds => _seconds;
52
53 public int Nanoseconds => (int)(_nanoseconds & 0x7FFFFFFF);
54
55 public XsdDuration(bool isNegative, int years, int months, int days, int hours, int minutes, int seconds, int nanoseconds)
56 {
57 if (years < 0)
58 {
59 throw new ArgumentOutOfRangeException("years");
60 }
61 if (months < 0)
62 {
63 throw new ArgumentOutOfRangeException("months");
64 }
65 if (days < 0)
66 {
67 throw new ArgumentOutOfRangeException("days");
68 }
69 if (hours < 0)
70 {
71 throw new ArgumentOutOfRangeException("hours");
72 }
73 if (minutes < 0)
74 {
75 throw new ArgumentOutOfRangeException("minutes");
76 }
77 if (seconds < 0)
78 {
79 throw new ArgumentOutOfRangeException("seconds");
80 }
82 {
83 throw new ArgumentOutOfRangeException("nanoseconds");
84 }
85 _years = years;
87 _days = days;
88 _hours = hours;
92 if (isNegative)
93 {
94 _nanoseconds |= 2147483648u;
95 }
96 }
97
102
104 {
105 long ticks = timeSpan.Ticks;
106 bool flag;
107 ulong num;
108 if (ticks < 0)
109 {
110 flag = true;
111 num = (ulong)(-ticks);
112 }
113 else
114 {
115 flag = false;
116 num = (ulong)ticks;
117 }
118 if (durationType == DurationType.YearMonthDuration)
119 {
120 int num2 = (int)(num / 315360000000000L);
121 int num3 = (int)(num % 315360000000000L / 25920000000000L);
122 if (num3 == 12)
123 {
124 num2++;
125 num3 = 0;
126 }
127 this = new XsdDuration(flag, num2, num3, 0, 0, 0, 0, 0);
128 return;
129 }
130 _nanoseconds = (uint)((int)(num % 10000000) * 100);
131 if (flag)
132 {
133 _nanoseconds |= 2147483648u;
134 }
135 _years = 0;
136 _months = 0;
137 _days = (int)(num / 864000000000L);
138 _hours = (int)(num / 36000000000L % 24);
139 _minutes = (int)(num / 600000000 % 60);
140 _seconds = (int)(num / 10000000 % 60);
141 }
142
143 public XsdDuration(string s)
145 {
146 }
147
149 {
150 XsdDuration result;
152 if (ex != null)
153 {
154 throw ex;
155 }
156 _years = result.Years;
157 _months = result.Months;
158 _days = result.Days;
159 _hours = result.Hours;
160 _minutes = result.Minutes;
161 _seconds = result.Seconds;
162 _nanoseconds = (uint)result.Nanoseconds;
163 if (result.IsNegative)
164 {
165 _nanoseconds |= 2147483648u;
166 }
167 }
168
170 {
171 return ToTimeSpan(DurationType.Duration);
172 }
173
175 {
176 TimeSpan result;
178 if (ex != null)
179 {
180 throw ex;
181 }
182 return result;
183 }
184
186 {
187 return TryToTimeSpan(DurationType.Duration, out result);
188 }
189
191 {
192 Exception ex = null;
193 ulong num = 0uL;
194 checked
195 {
196 try
197 {
198 if (durationType != DurationType.DayTimeDuration)
199 {
200 num += ((ulong)_years + unchecked(checked((ulong)_months) / 12)) * 365;
201 num += unchecked(checked((ulong)_months) % 12) * 30;
202 }
203 if (durationType != DurationType.YearMonthDuration)
204 {
205 num += (ulong)_days;
206 num *= 24;
207 num += (ulong)_hours;
208 num *= 60;
209 num += (ulong)_minutes;
210 num *= 60;
211 num += (ulong)_seconds;
212 num *= 10000000;
213 num += unchecked(checked((ulong)Nanoseconds) / 100);
214 }
215 else
216 {
217 num *= 864000000000L;
218 }
219 if (IsNegative)
220 {
221 if (num == 9223372036854775808uL)
222 {
223 result = new TimeSpan(long.MinValue);
224 }
225 else
226 {
227 result = new TimeSpan(-(long)num);
228 }
229 }
230 else
231 {
232 result = new TimeSpan((long)num);
233 }
234 return null;
235 }
236 catch (OverflowException)
237 {
238 result = TimeSpan.MinValue;
240 }
241 }
242 }
243
244 public override string ToString()
245 {
246 return ToString(DurationType.Duration);
247 }
248
250 {
252 if (IsNegative)
253 {
254 stringBuilder.Append('-');
255 }
256 stringBuilder.Append('P');
257 if (durationType != DurationType.DayTimeDuration)
258 {
259 if (_years != 0)
260 {
262 stringBuilder.Append('Y');
263 }
264 if (_months != 0)
265 {
267 stringBuilder.Append('M');
268 }
269 }
270 if (durationType != DurationType.YearMonthDuration)
271 {
272 if (_days != 0)
273 {
275 stringBuilder.Append('D');
276 }
277 if (_hours != 0 || _minutes != 0 || _seconds != 0 || Nanoseconds != 0)
278 {
279 stringBuilder.Append('T');
280 if (_hours != 0)
281 {
283 stringBuilder.Append('H');
284 }
285 if (_minutes != 0)
286 {
288 stringBuilder.Append('M');
289 }
290 int num = Nanoseconds;
291 if (_seconds != 0 || num != 0)
292 {
294 if (num != 0)
295 {
296 stringBuilder.Append('.');
297 int length = stringBuilder.Length;
299 int num2 = stringBuilder.Length - 1;
300 for (int num3 = num2; num3 >= length; num3--)
301 {
302 int num4 = num % 10;
303 stringBuilder[num3] = (char)(num4 + 48);
304 if (num2 == num3 && num4 == 0)
305 {
306 num2--;
307 }
308 num /= 10;
309 }
311 }
312 stringBuilder.Append('S');
313 }
314 }
315 if (stringBuilder[stringBuilder.Length - 1] == 'P')
316 {
317 stringBuilder.Append("T0S");
318 }
319 }
320 else if (stringBuilder[stringBuilder.Length - 1] == 'P')
321 {
322 stringBuilder.Append("0M");
323 }
324 return stringBuilder.ToString();
325 }
326
327 internal static Exception TryParse(string s, out XsdDuration result)
328 {
329 return TryParse(s, DurationType.Duration, out result);
330 }
331
332 internal static Exception TryParse(string s, DurationType durationType, out XsdDuration result)
333 {
334 Parts parts = Parts.HasNone;
335 result = default(XsdDuration);
336 s = s.Trim();
337 int length = s.Length;
338 int offset = 0;
339 int i = 0;
340 int result2;
341 if (offset < length)
342 {
343 if (s[offset] == '-')
344 {
345 offset++;
346 result._nanoseconds = 2147483648u;
347 }
348 else
349 {
351 }
352 if (offset < length && s[offset++] == 'P')
353 {
354 string text = TryParseDigits(s, ref offset, eatDigits: false, out result2, out i);
355 if (text != null)
356 {
357 goto IL_02ec;
358 }
359 if (offset < length)
360 {
361 if (s[offset] != 'Y')
362 {
363 goto IL_00c1;
364 }
365 if (i != 0)
366 {
367 parts |= Parts.HasYears;
369 if (++offset == length)
370 {
371 goto IL_02b8;
372 }
374 if (text != null)
375 {
376 goto IL_02ec;
377 }
378 if (offset < length)
379 {
380 goto IL_00c1;
381 }
382 }
383 }
384 }
385 }
386 goto IL_02d5;
387 IL_02ec:
389 IL_014b:
390 if (s[offset] != 'T')
391 {
392 goto IL_02b0;
393 }
394 if (i == 0)
395 {
396 offset++;
397 string text = TryParseDigits(s, ref offset, eatDigits: false, out result2, out i);
398 if (text != null)
399 {
400 goto IL_02ec;
401 }
402 if (offset < length)
403 {
404 if (s[offset] != 'H')
405 {
406 goto IL_01c4;
407 }
408 if (i != 0)
409 {
410 parts |= Parts.HasHours;
412 if (++offset == length)
413 {
414 goto IL_02b8;
415 }
417 if (text != null)
418 {
419 goto IL_02ec;
420 }
421 if (offset < length)
422 {
423 goto IL_01c4;
424 }
425 }
426 }
427 }
428 goto IL_02d5;
429 IL_00c1:
430 if (s[offset] != 'M')
431 {
432 goto IL_0106;
433 }
434 if (i != 0)
435 {
436 parts |= Parts.HasMonths;
438 if (++offset == length)
439 {
440 goto IL_02b8;
441 }
442 string text = TryParseDigits(s, ref offset, eatDigits: false, out result2, out i);
443 if (text != null)
444 {
445 goto IL_02ec;
446 }
447 if (offset < length)
448 {
449 goto IL_0106;
450 }
451 }
452 goto IL_02d5;
453 IL_020a:
454 if (s[offset] == '.')
455 {
456 offset++;
457 parts |= Parts.HasSeconds;
459 string text = TryParseDigits(s, ref offset, eatDigits: true, out result2, out i);
460 if (text != null)
461 {
462 goto IL_02ec;
463 }
464 if (i == 0)
465 {
466 result2 = 0;
467 }
468 while (i > 9)
469 {
470 result2 /= 10;
471 i--;
472 }
473 for (; i < 9; i++)
474 {
475 result2 *= 10;
476 }
478 if (offset >= length || s[offset] != 'S')
479 {
480 goto IL_02d5;
481 }
482 if (++offset == length)
483 {
484 goto IL_02b8;
485 }
486 }
487 else if (s[offset] == 'S')
488 {
489 if (i == 0)
490 {
491 goto IL_02d5;
492 }
493 parts |= Parts.HasSeconds;
495 if (++offset == length)
496 {
497 goto IL_02b8;
498 }
499 }
500 goto IL_02b0;
501 IL_02d5:
503 IL_01c4:
504 if (s[offset] != 'M')
505 {
506 goto IL_020a;
507 }
508 if (i != 0)
509 {
510 parts |= Parts.HasMinutes;
512 if (++offset == length)
513 {
514 goto IL_02b8;
515 }
516 string text = TryParseDigits(s, ref offset, eatDigits: false, out result2, out i);
517 if (text != null)
518 {
519 goto IL_02ec;
520 }
521 if (offset < length)
522 {
523 goto IL_020a;
524 }
525 }
526 goto IL_02d5;
527 IL_02b0:
528 if (i == 0 && offset == length)
529 {
530 goto IL_02b8;
531 }
532 goto IL_02d5;
533 IL_0106:
534 if (s[offset] != 'D')
535 {
536 goto IL_014b;
537 }
538 if (i != 0)
539 {
540 parts |= Parts.HasDays;
542 if (++offset == length)
543 {
544 goto IL_02b8;
545 }
546 string text = TryParseDigits(s, ref offset, eatDigits: false, out result2, out i);
547 if (text != null)
548 {
549 goto IL_02ec;
550 }
551 if (offset < length)
552 {
553 goto IL_014b;
554 }
555 }
556 goto IL_02d5;
557 IL_02b8:
558 if (parts != 0)
559 {
560 if (durationType == DurationType.DayTimeDuration)
561 {
562 if ((parts & (Parts)3) == 0)
563 {
564 goto IL_02d3;
565 }
566 }
567 else if (durationType != DurationType.YearMonthDuration || (parts & (Parts)(-4)) == 0)
568 {
569 goto IL_02d3;
570 }
571 }
572 goto IL_02d5;
573 IL_02d3:
574 return null;
575 }
576
577 private static string TryParseDigits(string s, ref int offset, bool eatDigits, out int result, out int numDigits)
578 {
579 int num = offset;
580 int length = s.Length;
581 result = 0;
582 numDigits = 0;
583 while (offset < length && s[offset] >= '0' && s[offset] <= '9')
584 {
585 int num2 = s[offset] - 48;
586 if (result > (int.MaxValue - num2) / 10)
587 {
588 if (!eatDigits)
589 {
591 }
592 numDigits = offset - num;
593 while (offset < length && s[offset] >= '0' && s[offset] <= '9')
594 {
595 offset++;
596 }
597 return null;
598 }
599 result = result * 10 + num2;
600 offset++;
601 }
602 numDigits = offset - num;
603 return null;
604 }
605}
static string XmlConvert_Overflow
Definition SR.cs:372
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string XmlConvert_BadFormat
Definition SR.cs:370
Definition SR.cs:7
static string ToString(bool value)
static readonly TimeSpan MinValue
Definition TimeSpan.cs:25
static Exception TryParse(string s, out XsdDuration result)
string ToString(DurationType durationType)
XsdDuration(TimeSpan timeSpan, DurationType durationType)
Exception TryToTimeSpan(out TimeSpan result)
TimeSpan ToTimeSpan(DurationType durationType)
Exception TryToTimeSpan(DurationType durationType, out TimeSpan result)
XsdDuration(string s, DurationType durationType)
XsdDuration(bool isNegative, int years, int months, int days, int hours, int minutes, int seconds, int nanoseconds)
XsdDuration(TimeSpan timeSpan)
static string TryParseDigits(string s, ref int offset, bool eatDigits, out int result, out int numDigits)
static Exception TryParse(string s, DurationType durationType, out XsdDuration result)