| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | //////////////////////////////////////////////////////////////////////////// |
| | | 5 | | // |
| | | 6 | | // Purpose: Used by TimeSpan to parse a time interval string. |
| | | 7 | | // |
| | | 8 | | // Standard Format: |
| | | 9 | | // -=-=-=-=-=-=-=- |
| | | 10 | | // "c": Constant format. [-][d'.']hh':'mm':'ss['.'fffffff] |
| | | 11 | | // Not culture sensitive. Default format (and null/empty format string) map to this format. |
| | | 12 | | // |
| | | 13 | | // "g": General format, short: [-][d':']h':'mm':'ss'.'FFFFFFF |
| | | 14 | | // Only print what's needed. Localized (if you want Invariant, pass in Invariant). |
| | | 15 | | // The fractional seconds separator is localized, equal to the culture's DecimalSeparator. |
| | | 16 | | // |
| | | 17 | | // "G": General format, long: [-]d':'hh':'mm':'ss'.'fffffff |
| | | 18 | | // Always print days and 7 fractional digits. Localized (if you want Invariant, pass in Invariant). |
| | | 19 | | // The fractional seconds separator is localized, equal to the culture's DecimalSeparator. |
| | | 20 | | // |
| | | 21 | | // * "TryParseTimeSpan" is the main method for Parse/TryParse |
| | | 22 | | // |
| | | 23 | | // - TimeSpanTokenizer.GetNextToken() is used to split the input string into number and literal tokens. |
| | | 24 | | // - TimeSpanRawInfo.ProcessToken() adds the next token into the parsing intermediary state structure |
| | | 25 | | // - ProcessTerminalState() uses the fully initialized TimeSpanRawInfo to find a legal parse match. |
| | | 26 | | // The terminal states are attempted as follows: |
| | | 27 | | // foreach (+InvariantPattern, -InvariantPattern, +LocalizedPattern, -LocalizedPattern) try |
| | | 28 | | // 1 number => d |
| | | 29 | | // 2 numbers => h:m |
| | | 30 | | // 3 numbers => h:m:s | d.h:m | h:m:.f |
| | | 31 | | // 4 numbers => h:m:s.f | d.h:m:s | d.h:m:.f |
| | | 32 | | // 5 numbers => d.h:m:s.f |
| | | 33 | | // |
| | | 34 | | // Custom Format: |
| | | 35 | | // -=-=-=-=-=-=-= |
| | | 36 | | // |
| | | 37 | | // * "TryParseExactTimeSpan" is the main method for ParseExact/TryParseExact methods |
| | | 38 | | // * "TryParseExactMultipleTimeSpan" is the main method for ParseExact/TryparseExact |
| | | 39 | | // methods that take a string[] of formats |
| | | 40 | | // |
| | | 41 | | // - For single-letter formats "TryParseTimeSpan" is called (see above) |
| | | 42 | | // - For multi-letter formats "TryParseByFormat" is called |
| | | 43 | | // - TryParseByFormat uses helper methods (ParseExactLiteral, ParseExactDigits, etc) |
| | | 44 | | // which drive the underlying TimeSpanTokenizer. However, unlike standard formatting which |
| | | 45 | | // operates on whole-tokens, ParseExact operates at the character-level. As such, |
| | | 46 | | // TimeSpanTokenizer.NextChar and TimeSpanTokenizer.BackOne() are called directly. |
| | | 47 | | // |
| | | 48 | | //////////////////////////////////////////////////////////////////////////// |
| | | 49 | | |
| | | 50 | | using System.Buffers.Text; |
| | | 51 | | using System.Diagnostics; |
| | | 52 | | using System.Text; |
| | | 53 | | |
| | | 54 | | namespace System.Globalization |
| | | 55 | | { |
| | | 56 | | internal static class TimeSpanParse |
| | | 57 | | { |
| | | 58 | | private const int MaxFractionDigits = 7; |
| | | 59 | | private const int MaxDays = 10675199; |
| | | 60 | | private const int MaxHours = 23; |
| | | 61 | | private const int MaxMinutes = 59; |
| | | 62 | | private const int MaxSeconds = 59; |
| | | 63 | | private const int MaxFraction = 9999999; |
| | | 64 | | |
| | | 65 | | [Flags] |
| | | 66 | | private enum TimeSpanStandardStyles : byte |
| | | 67 | | { |
| | | 68 | | // Standard Format Styles |
| | | 69 | | None = 0x00000000, |
| | | 70 | | Invariant = 0x00000001, // Allow Invariant Culture |
| | | 71 | | Localized = 0x00000002, // Allow Localized Culture |
| | | 72 | | RequireFull = 0x00000004, // Require the input to be in DHMSF format |
| | | 73 | | Any = Invariant | Localized, |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | // TimeSpan Token Types |
| | | 77 | | private enum TTT : byte |
| | | 78 | | { |
| | | 79 | | None = 0, // None of the TimeSpanToken fields are set |
| | | 80 | | End = 1, // '\0' |
| | | 81 | | Num = 2, // Number |
| | | 82 | | Sep = 3, // literal |
| | | 83 | | NumOverflow = 4, // Number that overflowed |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | private ref struct TimeSpanToken |
| | | 87 | | { |
| | | 88 | | internal TTT _ttt; |
| | | 89 | | internal int _num; // Store the number that we are parsing (if any) |
| | | 90 | | internal int _zeroes; // Store the number of leading zeroes (if any) |
| | | 91 | | internal ReadOnlySpan<char> _sep; // Store the literal that we are parsing (if any) |
| | | 92 | | |
| | 0 | 93 | | public TimeSpanToken(TTT type) : this(type, 0, 0, default) { } |
| | | 94 | | |
| | 0 | 95 | | public TimeSpanToken(int number) : this(TTT.Num, number, 0, default) { } |
| | | 96 | | |
| | 0 | 97 | | public TimeSpanToken(int number, int leadingZeroes) : this(TTT.Num, number, leadingZeroes, default) { } |
| | | 98 | | |
| | | 99 | | public TimeSpanToken(TTT type, int number, int leadingZeroes, ReadOnlySpan<char> separator) |
| | | 100 | | { |
| | 0 | 101 | | _ttt = type; |
| | 0 | 102 | | _num = number; |
| | 0 | 103 | | _zeroes = leadingZeroes; |
| | 0 | 104 | | _sep = separator; |
| | 0 | 105 | | } |
| | | 106 | | |
| | | 107 | | public bool NormalizeAndValidateFraction() |
| | | 108 | | { |
| | 0 | 109 | | Debug.Assert(_ttt == TTT.Num); |
| | 0 | 110 | | Debug.Assert(_num > -1); |
| | | 111 | | |
| | 0 | 112 | | if (_num == 0) |
| | 0 | 113 | | return true; |
| | | 114 | | |
| | 0 | 115 | | if (_zeroes == 0 && _num > MaxFraction) |
| | 0 | 116 | | return false; |
| | | 117 | | |
| | 0 | 118 | | int totalDigitsCount = FormattingHelpers.CountDigits((uint)_num) + _zeroes; |
| | | 119 | | |
| | 0 | 120 | | if (totalDigitsCount == MaxFractionDigits) |
| | | 121 | | { |
| | | 122 | | // Already normalized. no more action needed |
| | | 123 | | // .9999999 normalize to 9,999,999 ticks |
| | | 124 | | // .0000001 normalize to 1 ticks |
| | 0 | 125 | | return true; |
| | | 126 | | } |
| | | 127 | | |
| | 0 | 128 | | if (totalDigitsCount < MaxFractionDigits) |
| | | 129 | | { |
| | | 130 | | // normalize the fraction to the 7-digits |
| | | 131 | | // .999999 normalize to 9,999,990 ticks |
| | | 132 | | // .99999 normalize to 9,999,900 ticks |
| | | 133 | | // .000001 normalize to 10 ticks |
| | | 134 | | // .1 normalize to 1,000,000 ticks |
| | | 135 | | |
| | 0 | 136 | | _num *= Pow10UpToMaxFractionDigits(MaxFractionDigits - totalDigitsCount); |
| | 0 | 137 | | return true; |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | // totalDigitsCount is greater then MaxFractionDigits, we'll need to do the rounding to 7-digits length |
| | | 141 | | // .00000001 normalized to 0 ticks |
| | | 142 | | // .00000005 normalized to 1 ticks |
| | | 143 | | // .09999999 normalize to 1,000,000 ticks |
| | | 144 | | // .099999999 normalize to 1,000,000 ticks |
| | | 145 | | |
| | 0 | 146 | | Debug.Assert(_zeroes > 0); // Already validated that in the condition _zeroes == 0 && _num > MaxFraction |
| | | 147 | | |
| | 0 | 148 | | if (_zeroes > MaxFractionDigits) |
| | | 149 | | { |
| | | 150 | | // If there are 8 leading zeroes, it rounds to zero |
| | 0 | 151 | | _num = 0; |
| | 0 | 152 | | return true; |
| | | 153 | | } |
| | | 154 | | |
| | 0 | 155 | | Debug.Assert(totalDigitsCount - MaxFractionDigits <= MaxFractionDigits); |
| | 0 | 156 | | uint power = (uint)Pow10UpToMaxFractionDigits(totalDigitsCount - MaxFractionDigits); |
| | | 157 | | // Unsigned integer division, rounding away from zero |
| | 0 | 158 | | _num = (int)(((uint)_num + power / 2) / power); |
| | 0 | 159 | | Debug.Assert(_num < MaxFraction); |
| | | 160 | | |
| | 0 | 161 | | return true; |
| | | 162 | | } |
| | | 163 | | } |
| | | 164 | | |
| | | 165 | | private ref struct TimeSpanTokenizer |
| | | 166 | | { |
| | | 167 | | private readonly ReadOnlySpan<char> _value; |
| | | 168 | | private int _pos; |
| | | 169 | | |
| | 0 | 170 | | internal TimeSpanTokenizer(ReadOnlySpan<char> input) : this(input, 0) { } |
| | | 171 | | |
| | | 172 | | internal TimeSpanTokenizer(ReadOnlySpan<char> input, int startPosition) |
| | | 173 | | { |
| | 0 | 174 | | _value = input; |
| | 0 | 175 | | _pos = startPosition; |
| | 0 | 176 | | } |
| | | 177 | | |
| | | 178 | | /// <summary>Returns the next token in the input string</summary> |
| | | 179 | | /// <remarks>Used by the parsing routines that operate on standard-formats.</remarks> |
| | | 180 | | internal TimeSpanToken GetNextToken() |
| | | 181 | | { |
| | | 182 | | // Get the position of the next character to be processed. If there is no |
| | | 183 | | // next character, we're at the end. |
| | 0 | 184 | | int startPos = _pos; |
| | 0 | 185 | | int pos = startPos; |
| | 0 | 186 | | ReadOnlySpan<char> value = _value; |
| | 0 | 187 | | Debug.Assert(pos > -1); |
| | 0 | 188 | | if ((uint)pos >= (uint)value.Length) |
| | | 189 | | { |
| | 0 | 190 | | return new TimeSpanToken(TTT.End); |
| | | 191 | | } |
| | | 192 | | |
| | | 193 | | // Now retrieve that character. If it's a digit, we're processing a number. |
| | 0 | 194 | | int num = value[pos] - '0'; |
| | 0 | 195 | | if ((uint)num <= 9) |
| | | 196 | | { |
| | 0 | 197 | | int zeroes = 0; |
| | 0 | 198 | | if (num == 0) |
| | | 199 | | { |
| | | 200 | | // Read all leading zeroes. |
| | 0 | 201 | | zeroes = 1; |
| | | 202 | | while (true) |
| | | 203 | | { |
| | | 204 | | int digit; |
| | 0 | 205 | | if ((uint)++pos >= (uint)value.Length || (uint)(digit = value[pos] - '0') > 9) |
| | | 206 | | { |
| | 0 | 207 | | _pos = pos; |
| | 0 | 208 | | return new TimeSpanToken(TTT.Num, 0, zeroes, default); |
| | | 209 | | } |
| | | 210 | | |
| | 0 | 211 | | if (digit == 0) |
| | | 212 | | { |
| | 0 | 213 | | zeroes++; |
| | 0 | 214 | | continue; |
| | | 215 | | } |
| | | 216 | | |
| | 0 | 217 | | num = digit; |
| | | 218 | | break; |
| | | 219 | | } |
| | | 220 | | |
| | 0 | 221 | | _pos = pos; |
| | | 222 | | } |
| | | 223 | | |
| | | 224 | | // Continue to read as long as we're reading digits. |
| | 0 | 225 | | while ((uint)++pos < (uint)value.Length) |
| | | 226 | | { |
| | 0 | 227 | | int digit = value[pos] - '0'; |
| | 0 | 228 | | if ((uint)digit > 9) |
| | | 229 | | { |
| | | 230 | | break; |
| | | 231 | | } |
| | | 232 | | |
| | 0 | 233 | | num = num * 10 + digit; |
| | 0 | 234 | | if ((num & 0xF0000000) != 0) // Max limit we can support 268435455 which is FFFFFFF |
| | | 235 | | { |
| | 0 | 236 | | _pos = pos; |
| | 0 | 237 | | return new TimeSpanToken(TTT.NumOverflow); |
| | | 238 | | } |
| | | 239 | | } |
| | | 240 | | |
| | 0 | 241 | | _pos = pos; |
| | 0 | 242 | | return new TimeSpanToken(TTT.Num, num, zeroes, default); |
| | | 243 | | } |
| | | 244 | | |
| | | 245 | | // Otherwise, we're processing a separator, and we've already processed the first |
| | | 246 | | // character of it. Continue processing characters as long as they're not digits. |
| | 0 | 247 | | int length = 1; |
| | 0 | 248 | | while ((uint)++pos < (uint)value.Length && !char.IsAsciiDigit(value[pos])) |
| | | 249 | | { |
| | 0 | 250 | | length++; |
| | | 251 | | } |
| | 0 | 252 | | _pos = pos; |
| | | 253 | | |
| | | 254 | | // Return the separator. |
| | 0 | 255 | | return new TimeSpanToken(TTT.Sep, 0, 0, _value.Slice(startPos, length)); |
| | | 256 | | } |
| | | 257 | | |
| | 0 | 258 | | internal bool EOL => _pos >= (_value.Length - 1); |
| | | 259 | | |
| | | 260 | | internal void BackOne() |
| | | 261 | | { |
| | 0 | 262 | | if (_pos > 0) --_pos; |
| | 0 | 263 | | } |
| | | 264 | | |
| | | 265 | | internal char NextChar() |
| | | 266 | | { |
| | 0 | 267 | | int pos = ++_pos; |
| | 0 | 268 | | return (uint)pos < (uint)_value.Length ? |
| | 0 | 269 | | _value[pos] : |
| | 0 | 270 | | (char)0; |
| | | 271 | | } |
| | | 272 | | } |
| | | 273 | | |
| | | 274 | | /// <summary>Stores intermediary parsing state for the standard formats.</summary> |
| | | 275 | | private ref struct TimeSpanRawInfo |
| | | 276 | | { |
| | | 277 | | internal TimeSpanFormat.FormatLiterals PositiveLocalized |
| | | 278 | | { |
| | | 279 | | get |
| | | 280 | | { |
| | 0 | 281 | | if (!_posLocInit) |
| | | 282 | | { |
| | 0 | 283 | | _posLoc = default; |
| | 0 | 284 | | _posLoc.Init(_fullPosPattern, false); |
| | 0 | 285 | | _posLocInit = true; |
| | | 286 | | } |
| | 0 | 287 | | return _posLoc; |
| | | 288 | | } |
| | | 289 | | } |
| | | 290 | | |
| | | 291 | | internal TimeSpanFormat.FormatLiterals NegativeLocalized |
| | | 292 | | { |
| | | 293 | | get |
| | | 294 | | { |
| | 0 | 295 | | if (!_negLocInit) |
| | | 296 | | { |
| | 0 | 297 | | _negLoc = default; |
| | 0 | 298 | | _negLoc.Init(_fullNegPattern, false); |
| | 0 | 299 | | _negLocInit = true; |
| | | 300 | | } |
| | 0 | 301 | | return _negLoc; |
| | | 302 | | } |
| | | 303 | | } |
| | | 304 | | |
| | | 305 | | internal bool FullAppCompatMatch(TimeSpanFormat.FormatLiterals pattern) => |
| | 0 | 306 | | _sepCount == 5 |
| | 0 | 307 | | && _numCount == 4 |
| | 0 | 308 | | && _literals0.EqualsOrdinal(pattern.Start) |
| | 0 | 309 | | && _literals1.EqualsOrdinal(pattern.DayHourSep) |
| | 0 | 310 | | && _literals2.EqualsOrdinal(pattern.HourMinuteSep) |
| | 0 | 311 | | && _literals3.EqualsOrdinal(pattern.AppCompatLiteral) |
| | 0 | 312 | | && _literals4.EqualsOrdinal(pattern.End); |
| | | 313 | | |
| | | 314 | | internal bool PartialAppCompatMatch(TimeSpanFormat.FormatLiterals pattern) => |
| | 0 | 315 | | _sepCount == 4 |
| | 0 | 316 | | && _numCount == 3 |
| | 0 | 317 | | && _literals0.EqualsOrdinal(pattern.Start) |
| | 0 | 318 | | && _literals1.EqualsOrdinal(pattern.HourMinuteSep) |
| | 0 | 319 | | && _literals2.EqualsOrdinal(pattern.AppCompatLiteral) |
| | 0 | 320 | | && _literals3.EqualsOrdinal(pattern.End); |
| | | 321 | | |
| | | 322 | | /// <summary>DHMSF (all values matched)</summary> |
| | | 323 | | internal bool FullMatch(TimeSpanFormat.FormatLiterals pattern) => |
| | 0 | 324 | | _sepCount == MaxLiteralTokens |
| | 0 | 325 | | && _numCount == MaxNumericTokens |
| | 0 | 326 | | && _literals0.EqualsOrdinal(pattern.Start) |
| | 0 | 327 | | && _literals1.EqualsOrdinal(pattern.DayHourSep) |
| | 0 | 328 | | && _literals2.EqualsOrdinal(pattern.HourMinuteSep) |
| | 0 | 329 | | && _literals3.EqualsOrdinal(pattern.MinuteSecondSep) |
| | 0 | 330 | | && _literals4.EqualsOrdinal(pattern.SecondFractionSep) |
| | 0 | 331 | | && _literals5.EqualsOrdinal(pattern.End); |
| | | 332 | | |
| | | 333 | | /// <summary>D (no hours, minutes, seconds, or fractions)</summary> |
| | | 334 | | internal bool FullDMatch(TimeSpanFormat.FormatLiterals pattern) => |
| | 0 | 335 | | _sepCount == 2 |
| | 0 | 336 | | && _numCount == 1 |
| | 0 | 337 | | && _literals0.EqualsOrdinal(pattern.Start) |
| | 0 | 338 | | && _literals1.EqualsOrdinal(pattern.End); |
| | | 339 | | |
| | | 340 | | /// <summary>HM (no days, seconds, or fractions)</summary> |
| | | 341 | | internal bool FullHMMatch(TimeSpanFormat.FormatLiterals pattern) => |
| | 0 | 342 | | _sepCount == 3 |
| | 0 | 343 | | && _numCount == 2 |
| | 0 | 344 | | && _literals0.EqualsOrdinal(pattern.Start) |
| | 0 | 345 | | && _literals1.EqualsOrdinal(pattern.HourMinuteSep) |
| | 0 | 346 | | && _literals2.EqualsOrdinal(pattern.End); |
| | | 347 | | |
| | | 348 | | /// <summary>DHM (no seconds or fraction)</summary> |
| | | 349 | | internal bool FullDHMMatch(TimeSpanFormat.FormatLiterals pattern) => |
| | 0 | 350 | | _sepCount == 4 |
| | 0 | 351 | | && _numCount == 3 |
| | 0 | 352 | | && _literals0.EqualsOrdinal(pattern.Start) |
| | 0 | 353 | | && _literals1.EqualsOrdinal(pattern.DayHourSep) |
| | 0 | 354 | | && _literals2.EqualsOrdinal(pattern.HourMinuteSep) |
| | 0 | 355 | | && _literals3.EqualsOrdinal(pattern.End); |
| | | 356 | | |
| | | 357 | | /// <summary>HMS (no days or fraction)</summary> |
| | | 358 | | internal bool FullHMSMatch(TimeSpanFormat.FormatLiterals pattern) => |
| | 0 | 359 | | _sepCount == 4 |
| | 0 | 360 | | && _numCount == 3 |
| | 0 | 361 | | && _literals0.EqualsOrdinal(pattern.Start) |
| | 0 | 362 | | && _literals1.EqualsOrdinal(pattern.HourMinuteSep) |
| | 0 | 363 | | && _literals2.EqualsOrdinal(pattern.MinuteSecondSep) |
| | 0 | 364 | | && _literals3.EqualsOrdinal(pattern.End); |
| | | 365 | | |
| | | 366 | | /// <summary>DHMS (no fraction)</summary> |
| | | 367 | | internal bool FullDHMSMatch(TimeSpanFormat.FormatLiterals pattern) => |
| | 0 | 368 | | _sepCount == 5 |
| | 0 | 369 | | && _numCount == 4 |
| | 0 | 370 | | && _literals0.EqualsOrdinal(pattern.Start) |
| | 0 | 371 | | && _literals1.EqualsOrdinal(pattern.DayHourSep) |
| | 0 | 372 | | && _literals2.EqualsOrdinal(pattern.HourMinuteSep) |
| | 0 | 373 | | && _literals3.EqualsOrdinal(pattern.MinuteSecondSep) |
| | 0 | 374 | | && _literals4.EqualsOrdinal(pattern.End); |
| | | 375 | | |
| | | 376 | | /// <summary>HMSF (no days)</summary> |
| | | 377 | | internal bool FullHMSFMatch(TimeSpanFormat.FormatLiterals pattern) => |
| | 0 | 378 | | _sepCount == 5 |
| | 0 | 379 | | && _numCount == 4 |
| | 0 | 380 | | && _literals0.EqualsOrdinal(pattern.Start) |
| | 0 | 381 | | && _literals1.EqualsOrdinal(pattern.HourMinuteSep) |
| | 0 | 382 | | && _literals2.EqualsOrdinal(pattern.MinuteSecondSep) |
| | 0 | 383 | | && _literals3.EqualsOrdinal(pattern.SecondFractionSep) |
| | 0 | 384 | | && _literals4.EqualsOrdinal(pattern.End); |
| | | 385 | | |
| | | 386 | | internal TTT _lastSeenTTT; |
| | | 387 | | internal int _tokenCount; |
| | | 388 | | internal int _sepCount; |
| | | 389 | | internal int _numCount; |
| | | 390 | | |
| | | 391 | | private TimeSpanFormat.FormatLiterals _posLoc; |
| | | 392 | | private TimeSpanFormat.FormatLiterals _negLoc; |
| | | 393 | | private bool _posLocInit; |
| | | 394 | | private bool _negLocInit; |
| | | 395 | | private string _fullPosPattern; |
| | | 396 | | private string _fullNegPattern; |
| | | 397 | | |
| | | 398 | | private const int MaxTokens = 11; |
| | | 399 | | private const int MaxLiteralTokens = 6; |
| | | 400 | | private const int MaxNumericTokens = 5; |
| | | 401 | | |
| | | 402 | | internal TimeSpanToken _numbers0, _numbers1, _numbers2, _numbers3, _numbers4; // MaxNumericTokens = 5 |
| | | 403 | | internal ReadOnlySpan<char> _literals0, _literals1, _literals2, _literals3, _literals4, _literals5; // MaxLi |
| | | 404 | | |
| | | 405 | | internal void Init(DateTimeFormatInfo dtfi) |
| | | 406 | | { |
| | 0 | 407 | | Debug.Assert(dtfi != null); |
| | | 408 | | |
| | 0 | 409 | | _lastSeenTTT = TTT.None; |
| | 0 | 410 | | _tokenCount = 0; |
| | 0 | 411 | | _sepCount = 0; |
| | 0 | 412 | | _numCount = 0; |
| | | 413 | | |
| | 0 | 414 | | _fullPosPattern = dtfi.FullTimeSpanPositivePattern; |
| | 0 | 415 | | _fullNegPattern = dtfi.FullTimeSpanNegativePattern; |
| | 0 | 416 | | _posLocInit = false; |
| | 0 | 417 | | _negLocInit = false; |
| | 0 | 418 | | } |
| | | 419 | | |
| | | 420 | | internal bool ProcessToken(ref TimeSpanToken tok, ref TimeSpanResult result) |
| | | 421 | | { |
| | 0 | 422 | | switch (tok._ttt) |
| | | 423 | | { |
| | | 424 | | case TTT.Num: |
| | 0 | 425 | | if ((_tokenCount == 0 && !AddSep(default, ref result)) || !AddNum(tok, ref result)) |
| | | 426 | | { |
| | 0 | 427 | | return false; |
| | | 428 | | } |
| | | 429 | | break; |
| | | 430 | | |
| | | 431 | | case TTT.Sep: |
| | 0 | 432 | | if (!AddSep(tok._sep, ref result)) |
| | | 433 | | { |
| | 0 | 434 | | return false; |
| | | 435 | | } |
| | | 436 | | break; |
| | | 437 | | |
| | | 438 | | case TTT.NumOverflow: |
| | 0 | 439 | | return result.SetOverflowFailure(); |
| | | 440 | | |
| | | 441 | | default: |
| | | 442 | | // Some unknown token or a repeat token type in the input |
| | 0 | 443 | | return result.SetBadTimeSpanFailure(); |
| | | 444 | | } |
| | | 445 | | |
| | 0 | 446 | | _lastSeenTTT = tok._ttt; |
| | 0 | 447 | | Debug.Assert(_tokenCount == (_sepCount + _numCount), "tokenCount == (SepCount + NumCount)"); |
| | 0 | 448 | | return true; |
| | | 449 | | } |
| | | 450 | | |
| | | 451 | | private bool AddSep(ReadOnlySpan<char> sep, ref TimeSpanResult result) |
| | | 452 | | { |
| | 0 | 453 | | if (_sepCount >= MaxLiteralTokens || _tokenCount >= MaxTokens) |
| | | 454 | | { |
| | 0 | 455 | | return result.SetBadTimeSpanFailure(); |
| | | 456 | | } |
| | | 457 | | |
| | 0 | 458 | | switch (_sepCount++) |
| | | 459 | | { |
| | 0 | 460 | | case 0: _literals0 = sep; break; |
| | 0 | 461 | | case 1: _literals1 = sep; break; |
| | 0 | 462 | | case 2: _literals2 = sep; break; |
| | 0 | 463 | | case 3: _literals3 = sep; break; |
| | 0 | 464 | | case 4: _literals4 = sep; break; |
| | 0 | 465 | | default: _literals5 = sep; break; |
| | | 466 | | } |
| | | 467 | | |
| | 0 | 468 | | _tokenCount++; |
| | 0 | 469 | | return true; |
| | | 470 | | } |
| | | 471 | | private bool AddNum(TimeSpanToken num, ref TimeSpanResult result) |
| | | 472 | | { |
| | 0 | 473 | | if (_numCount >= MaxNumericTokens || _tokenCount >= MaxTokens) |
| | | 474 | | { |
| | 0 | 475 | | return result.SetBadTimeSpanFailure(); |
| | | 476 | | } |
| | | 477 | | |
| | 0 | 478 | | switch (_numCount++) |
| | | 479 | | { |
| | 0 | 480 | | case 0: _numbers0 = num; break; |
| | 0 | 481 | | case 1: _numbers1 = num; break; |
| | 0 | 482 | | case 2: _numbers2 = num; break; |
| | 0 | 483 | | case 3: _numbers3 = num; break; |
| | 0 | 484 | | default: _numbers4 = num; break; |
| | | 485 | | } |
| | | 486 | | |
| | 0 | 487 | | _tokenCount++; |
| | 0 | 488 | | return true; |
| | | 489 | | } |
| | | 490 | | } |
| | | 491 | | |
| | | 492 | | /// <summary>Store the result of the parsing.</summary> |
| | | 493 | | private ref struct TimeSpanResult |
| | | 494 | | { |
| | | 495 | | internal TimeSpan parsedTimeSpan; |
| | | 496 | | private readonly bool _throwOnFailure; |
| | | 497 | | private readonly ReadOnlySpan<char> _originalTimeSpanString; |
| | | 498 | | |
| | | 499 | | internal TimeSpanResult(bool throwOnFailure, ReadOnlySpan<char> originalTimeSpanString) |
| | | 500 | | { |
| | 0 | 501 | | parsedTimeSpan = default; |
| | 0 | 502 | | _throwOnFailure = throwOnFailure; |
| | 0 | 503 | | _originalTimeSpanString = originalTimeSpanString; |
| | 0 | 504 | | } |
| | | 505 | | |
| | | 506 | | internal bool SetNoFormatSpecifierFailure() |
| | | 507 | | { |
| | 0 | 508 | | if (!_throwOnFailure) |
| | | 509 | | { |
| | 0 | 510 | | return false; |
| | | 511 | | } |
| | | 512 | | |
| | 0 | 513 | | throw new FormatException(SR.Format_NoFormatSpecifier); |
| | | 514 | | } |
| | | 515 | | |
| | | 516 | | internal bool SetBadQuoteFailure(char failingCharacter) |
| | | 517 | | { |
| | 0 | 518 | | if (!_throwOnFailure) |
| | | 519 | | { |
| | 0 | 520 | | return false; |
| | | 521 | | } |
| | | 522 | | |
| | 0 | 523 | | throw new FormatException(SR.Format(SR.Format_BadQuote, failingCharacter)); |
| | | 524 | | } |
| | | 525 | | |
| | | 526 | | internal bool SetInvalidStringFailure() |
| | | 527 | | { |
| | 0 | 528 | | if (!_throwOnFailure) |
| | | 529 | | { |
| | 0 | 530 | | return false; |
| | | 531 | | } |
| | | 532 | | |
| | 0 | 533 | | throw new FormatException(SR.Format_InvalidString); |
| | | 534 | | } |
| | | 535 | | |
| | | 536 | | internal bool SetArgumentNullFailure(string argumentName) |
| | | 537 | | { |
| | 0 | 538 | | if (_throwOnFailure) |
| | | 539 | | { |
| | 0 | 540 | | Debug.Assert(argumentName != null); |
| | 0 | 541 | | ArgumentNullException.Throw(argumentName); |
| | | 542 | | } |
| | | 543 | | |
| | 0 | 544 | | return false; |
| | | 545 | | } |
| | | 546 | | |
| | | 547 | | internal bool SetOverflowFailure() |
| | | 548 | | { |
| | 0 | 549 | | if (!_throwOnFailure) |
| | | 550 | | { |
| | 0 | 551 | | return false; |
| | | 552 | | } |
| | | 553 | | |
| | 0 | 554 | | throw new OverflowException(SR.Format(SR.Overflow_TimeSpanElementTooLarge, new string(_originalTimeSpanS |
| | | 555 | | } |
| | | 556 | | |
| | | 557 | | internal bool SetBadTimeSpanFailure() |
| | | 558 | | { |
| | 0 | 559 | | if (!_throwOnFailure) |
| | | 560 | | { |
| | 0 | 561 | | return false; |
| | | 562 | | } |
| | | 563 | | |
| | 0 | 564 | | throw new FormatException(SR.Format(SR.Format_BadTimeSpan, new string(_originalTimeSpanString))); |
| | | 565 | | } |
| | | 566 | | |
| | | 567 | | internal bool SetBadFormatSpecifierFailure(char? formatSpecifierCharacter = null) |
| | | 568 | | { |
| | 0 | 569 | | if (!_throwOnFailure) |
| | | 570 | | { |
| | 0 | 571 | | return false; |
| | | 572 | | } |
| | | 573 | | |
| | 0 | 574 | | throw new FormatException(SR.Format(SR.Format_BadFormatSpecifier, formatSpecifierCharacter)); |
| | | 575 | | } |
| | | 576 | | } |
| | | 577 | | |
| | | 578 | | internal static int Pow10UpToMaxFractionDigits(int pow) |
| | | 579 | | { |
| | 0 | 580 | | ReadOnlySpan<int> powersOfTen = |
| | 0 | 581 | | [ |
| | 0 | 582 | | 1, |
| | 0 | 583 | | 10, |
| | 0 | 584 | | 100, |
| | 0 | 585 | | 1000, |
| | 0 | 586 | | 10000, |
| | 0 | 587 | | 100000, |
| | 0 | 588 | | 1000000, |
| | 0 | 589 | | 10000000, |
| | 0 | 590 | | ]; |
| | 0 | 591 | | Debug.Assert(powersOfTen.Length == MaxFractionDigits + 1); |
| | 0 | 592 | | return powersOfTen[pow]; |
| | | 593 | | } |
| | | 594 | | |
| | | 595 | | private static bool TryTimeToTicks(bool positive, TimeSpanToken days, TimeSpanToken hours, TimeSpanToken minutes |
| | | 596 | | { |
| | 0 | 597 | | if (days._num > MaxDays || |
| | 0 | 598 | | hours._num > MaxHours || |
| | 0 | 599 | | minutes._num > MaxMinutes || |
| | 0 | 600 | | seconds._num > MaxSeconds || |
| | 0 | 601 | | !fraction.NormalizeAndValidateFraction()) |
| | | 602 | | { |
| | 0 | 603 | | result = 0; |
| | 0 | 604 | | return false; |
| | | 605 | | } |
| | | 606 | | |
| | | 607 | | const long MaxMilliSeconds = long.MaxValue / TimeSpan.TicksPerMillisecond; |
| | | 608 | | const long MinMilliSeconds = long.MinValue / TimeSpan.TicksPerMillisecond; |
| | | 609 | | |
| | 0 | 610 | | long ticks = ((long)days._num * 3600 * 24 + (long)hours._num * 3600 + (long)minutes._num * 60 + seconds._num |
| | 0 | 611 | | if (ticks > MaxMilliSeconds || ticks < MinMilliSeconds) |
| | | 612 | | { |
| | 0 | 613 | | result = 0; |
| | 0 | 614 | | return false; |
| | | 615 | | } |
| | | 616 | | |
| | 0 | 617 | | result = ticks * TimeSpan.TicksPerMillisecond + fraction._num; |
| | 0 | 618 | | if (positive && result < 0) |
| | | 619 | | { |
| | 0 | 620 | | result = 0; |
| | 0 | 621 | | return false; |
| | | 622 | | } |
| | | 623 | | |
| | 0 | 624 | | return true; |
| | | 625 | | } |
| | | 626 | | |
| | | 627 | | internal static TimeSpan Parse(ReadOnlySpan<char> input, IFormatProvider? formatProvider) |
| | | 628 | | { |
| | 0 | 629 | | var parseResult = new TimeSpanResult(throwOnFailure: true, originalTimeSpanString: input); |
| | 0 | 630 | | bool success = TryParseTimeSpan(input, TimeSpanStandardStyles.Any, formatProvider, ref parseResult); |
| | 0 | 631 | | Debug.Assert(success, "Should have thrown on failure"); |
| | 0 | 632 | | return parseResult.parsedTimeSpan; |
| | | 633 | | } |
| | | 634 | | |
| | | 635 | | internal static bool TryParse(ReadOnlySpan<char> input, IFormatProvider? formatProvider, out TimeSpan result) |
| | | 636 | | { |
| | 0 | 637 | | var parseResult = new TimeSpanResult(throwOnFailure: false, originalTimeSpanString: input); |
| | | 638 | | |
| | 0 | 639 | | if (TryParseTimeSpan(input, TimeSpanStandardStyles.Any, formatProvider, ref parseResult)) |
| | | 640 | | { |
| | 0 | 641 | | result = parseResult.parsedTimeSpan; |
| | 0 | 642 | | return true; |
| | | 643 | | } |
| | | 644 | | |
| | 0 | 645 | | result = default; |
| | 0 | 646 | | return false; |
| | | 647 | | } |
| | | 648 | | |
| | | 649 | | internal static TimeSpan ParseExact(ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider? format |
| | | 650 | | { |
| | 0 | 651 | | var parseResult = new TimeSpanResult(throwOnFailure: true, originalTimeSpanString: input); |
| | 0 | 652 | | bool success = TryParseExactTimeSpan(input, format, formatProvider, styles, ref parseResult); |
| | 0 | 653 | | Debug.Assert(success, "Should have thrown on failure"); |
| | 0 | 654 | | return parseResult.parsedTimeSpan; |
| | | 655 | | } |
| | | 656 | | |
| | | 657 | | internal static bool TryParseExact(ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider? formatP |
| | | 658 | | { |
| | 0 | 659 | | var parseResult = new TimeSpanResult(throwOnFailure: false, originalTimeSpanString: input); |
| | | 660 | | |
| | 0 | 661 | | if (TryParseExactTimeSpan(input, format, formatProvider, styles, ref parseResult)) |
| | | 662 | | { |
| | 0 | 663 | | result = parseResult.parsedTimeSpan; |
| | 0 | 664 | | return true; |
| | | 665 | | } |
| | | 666 | | |
| | 0 | 667 | | result = default; |
| | 0 | 668 | | return false; |
| | | 669 | | } |
| | | 670 | | |
| | | 671 | | internal static TimeSpan ParseExactMultiple(ReadOnlySpan<char> input, string?[]? formats, IFormatProvider? forma |
| | | 672 | | { |
| | 0 | 673 | | var parseResult = new TimeSpanResult(throwOnFailure: true, originalTimeSpanString: input); |
| | 0 | 674 | | bool success = TryParseExactMultipleTimeSpan(input, formats, formatProvider, styles, ref parseResult); |
| | 0 | 675 | | Debug.Assert(success, "Should have thrown on failure"); |
| | 0 | 676 | | return parseResult.parsedTimeSpan; |
| | | 677 | | } |
| | | 678 | | |
| | | 679 | | internal static bool TryParseExactMultiple(ReadOnlySpan<char> input, string?[]? formats, IFormatProvider? format |
| | | 680 | | { |
| | 0 | 681 | | var parseResult = new TimeSpanResult(throwOnFailure: false, originalTimeSpanString: input); |
| | | 682 | | |
| | 0 | 683 | | if (TryParseExactMultipleTimeSpan(input, formats, formatProvider, styles, ref parseResult)) |
| | | 684 | | { |
| | 0 | 685 | | result = parseResult.parsedTimeSpan; |
| | 0 | 686 | | return true; |
| | | 687 | | } |
| | | 688 | | |
| | 0 | 689 | | result = default; |
| | 0 | 690 | | return false; |
| | | 691 | | } |
| | | 692 | | |
| | | 693 | | /// <summary>Common private Parse method called by both Parse and TryParse.</summary> |
| | | 694 | | private static bool TryParseTimeSpan(ReadOnlySpan<char> input, TimeSpanStandardStyles style, IFormatProvider? fo |
| | | 695 | | { |
| | 0 | 696 | | input = input.Trim(); |
| | 0 | 697 | | if (input.IsEmpty) |
| | | 698 | | { |
| | 0 | 699 | | return result.SetBadTimeSpanFailure(); |
| | | 700 | | } |
| | | 701 | | |
| | 0 | 702 | | var tokenizer = new TimeSpanTokenizer(input); |
| | | 703 | | |
| | 0 | 704 | | TimeSpanRawInfo raw = default; |
| | 0 | 705 | | raw.Init(DateTimeFormatInfo.GetInstance(formatProvider)); |
| | | 706 | | |
| | 0 | 707 | | TimeSpanToken tok = tokenizer.GetNextToken(); |
| | | 708 | | |
| | | 709 | | // The following loop will break out when we reach the end of the str or |
| | | 710 | | // when we can determine that the input is invalid. |
| | 0 | 711 | | while (tok._ttt != TTT.End) |
| | | 712 | | { |
| | 0 | 713 | | if (!raw.ProcessToken(ref tok, ref result)) |
| | | 714 | | { |
| | 0 | 715 | | return result.SetBadTimeSpanFailure(); |
| | | 716 | | } |
| | 0 | 717 | | tok = tokenizer.GetNextToken(); |
| | | 718 | | } |
| | 0 | 719 | | Debug.Assert(tokenizer.EOL); |
| | | 720 | | |
| | 0 | 721 | | if (!ProcessTerminalState(ref raw, style, ref result)) |
| | | 722 | | { |
| | 0 | 723 | | return result.SetBadTimeSpanFailure(); |
| | | 724 | | } |
| | | 725 | | |
| | 0 | 726 | | return true; |
| | | 727 | | } |
| | | 728 | | |
| | | 729 | | /// <summary> |
| | | 730 | | /// Validate the terminal state of a standard format parse. |
| | | 731 | | /// Sets result.parsedTimeSpan on success. |
| | | 732 | | /// Calculates the resultant TimeSpan from the TimeSpanRawInfo. |
| | | 733 | | /// </summary> |
| | | 734 | | /// <remarks> |
| | | 735 | | /// try => +InvariantPattern, -InvariantPattern, +LocalizedPattern, -LocalizedPattern |
| | | 736 | | /// 1) Verify Start matches |
| | | 737 | | /// 2) Verify End matches |
| | | 738 | | /// 3) 1 number => d |
| | | 739 | | /// 2 numbers => h:m |
| | | 740 | | /// 3 numbers => h:m:s | d.h:m | h:m:.f |
| | | 741 | | /// 4 numbers => h:m:s.f | d.h:m:s | d.h:m:.f |
| | | 742 | | /// 5 numbers => d.h:m:s.f |
| | | 743 | | /// </remarks> |
| | | 744 | | private static bool ProcessTerminalState(ref TimeSpanRawInfo raw, TimeSpanStandardStyles style, ref TimeSpanResu |
| | | 745 | | { |
| | 0 | 746 | | if (raw._lastSeenTTT == TTT.Num) |
| | | 747 | | { |
| | 0 | 748 | | TimeSpanToken tok = default; |
| | 0 | 749 | | tok._ttt = TTT.Sep; |
| | 0 | 750 | | if (!raw.ProcessToken(ref tok, ref result)) |
| | | 751 | | { |
| | 0 | 752 | | return result.SetBadTimeSpanFailure(); |
| | | 753 | | } |
| | | 754 | | } |
| | | 755 | | |
| | 0 | 756 | | return raw._numCount switch |
| | 0 | 757 | | { |
| | 0 | 758 | | 1 => ProcessTerminal_D(ref raw, style, ref result), |
| | 0 | 759 | | 2 => ProcessTerminal_HM(ref raw, style, ref result), |
| | 0 | 760 | | 3 => ProcessTerminal_HM_S_D(ref raw, style, ref result), |
| | 0 | 761 | | 4 => ProcessTerminal_HMS_F_D(ref raw, style, ref result), |
| | 0 | 762 | | 5 => ProcessTerminal_DHMSF(ref raw, style, ref result), |
| | 0 | 763 | | _ => result.SetBadTimeSpanFailure(), |
| | 0 | 764 | | }; |
| | | 765 | | } |
| | | 766 | | |
| | | 767 | | /// <summary>Validate the 5-number "Days.Hours:Minutes:Seconds.Fraction" terminal case.</summary> |
| | | 768 | | private static bool ProcessTerminal_DHMSF(ref TimeSpanRawInfo raw, TimeSpanStandardStyles style, ref TimeSpanRes |
| | | 769 | | { |
| | 0 | 770 | | if (raw._sepCount != 6) |
| | | 771 | | { |
| | 0 | 772 | | return result.SetBadTimeSpanFailure(); |
| | | 773 | | } |
| | 0 | 774 | | Debug.Assert(raw._numCount == 5); |
| | | 775 | | |
| | 0 | 776 | | bool inv = (style & TimeSpanStandardStyles.Invariant) != 0; |
| | 0 | 777 | | bool loc = (style & TimeSpanStandardStyles.Localized) != 0; |
| | 0 | 778 | | bool positive = false; |
| | 0 | 779 | | bool match = false; |
| | | 780 | | |
| | 0 | 781 | | if (inv) |
| | | 782 | | { |
| | 0 | 783 | | if (raw.FullMatch(TimeSpanFormat.PositiveInvariantFormatLiterals)) |
| | | 784 | | { |
| | 0 | 785 | | match = true; |
| | 0 | 786 | | positive = true; |
| | | 787 | | } |
| | 0 | 788 | | if (!match && raw.FullMatch(TimeSpanFormat.NegativeInvariantFormatLiterals)) |
| | | 789 | | { |
| | 0 | 790 | | match = true; |
| | 0 | 791 | | positive = false; |
| | | 792 | | } |
| | | 793 | | } |
| | | 794 | | |
| | 0 | 795 | | if (loc) |
| | | 796 | | { |
| | 0 | 797 | | if (!match && raw.FullMatch(raw.PositiveLocalized)) |
| | | 798 | | { |
| | 0 | 799 | | match = true; |
| | 0 | 800 | | positive = true; |
| | | 801 | | } |
| | 0 | 802 | | if (!match && raw.FullMatch(raw.NegativeLocalized)) |
| | | 803 | | { |
| | 0 | 804 | | match = true; |
| | 0 | 805 | | positive = false; |
| | | 806 | | } |
| | | 807 | | } |
| | | 808 | | |
| | 0 | 809 | | if (match) |
| | | 810 | | { |
| | | 811 | | |
| | 0 | 812 | | if (!TryTimeToTicks(positive, raw._numbers0, raw._numbers1, raw._numbers2, raw._numbers3, raw._numbers4, |
| | | 813 | | { |
| | 0 | 814 | | return result.SetOverflowFailure(); |
| | | 815 | | } |
| | | 816 | | |
| | 0 | 817 | | if (!positive) |
| | | 818 | | { |
| | 0 | 819 | | ticks = -ticks; |
| | 0 | 820 | | if (ticks > 0) |
| | | 821 | | { |
| | 0 | 822 | | return result.SetOverflowFailure(); |
| | | 823 | | } |
| | | 824 | | } |
| | | 825 | | |
| | 0 | 826 | | result.parsedTimeSpan = new TimeSpan(ticks); |
| | 0 | 827 | | return true; |
| | | 828 | | } |
| | | 829 | | |
| | 0 | 830 | | return result.SetBadTimeSpanFailure(); |
| | | 831 | | } |
| | | 832 | | |
| | | 833 | | |
| | | 834 | | /// <summary> |
| | | 835 | | /// Validate the ambiguous 4-number "Hours:Minutes:Seconds.Fraction", "Days.Hours:Minutes:Seconds", |
| | | 836 | | /// or "Days.Hours:Minutes:.Fraction" terminal case. |
| | | 837 | | /// </summary> |
| | | 838 | | private static bool ProcessTerminal_HMS_F_D(ref TimeSpanRawInfo raw, TimeSpanStandardStyles style, ref TimeSpanR |
| | | 839 | | { |
| | 0 | 840 | | if (raw._sepCount != 5 || (style & TimeSpanStandardStyles.RequireFull) != 0) |
| | | 841 | | { |
| | 0 | 842 | | return result.SetBadTimeSpanFailure(); |
| | | 843 | | } |
| | 0 | 844 | | Debug.Assert(raw._numCount == 4); |
| | | 845 | | |
| | 0 | 846 | | bool inv = ((style & TimeSpanStandardStyles.Invariant) != 0); |
| | 0 | 847 | | bool loc = ((style & TimeSpanStandardStyles.Localized) != 0); |
| | | 848 | | |
| | 0 | 849 | | long ticks = 0; |
| | 0 | 850 | | bool positive = false, match = false, overflow = false; |
| | 0 | 851 | | var zero = new TimeSpanToken(0); |
| | | 852 | | |
| | 0 | 853 | | if (inv) |
| | | 854 | | { |
| | 0 | 855 | | if (raw.FullHMSFMatch(TimeSpanFormat.PositiveInvariantFormatLiterals)) |
| | | 856 | | { |
| | 0 | 857 | | positive = true; |
| | 0 | 858 | | match = TryTimeToTicks(positive, zero, raw._numbers0, raw._numbers1, raw._numbers2, raw._numbers3, o |
| | 0 | 859 | | overflow = overflow || !match; |
| | | 860 | | } |
| | | 861 | | |
| | 0 | 862 | | if (!match && raw.FullDHMSMatch(TimeSpanFormat.PositiveInvariantFormatLiterals)) |
| | | 863 | | { |
| | 0 | 864 | | positive = true; |
| | 0 | 865 | | match = TryTimeToTicks(positive, raw._numbers0, raw._numbers1, raw._numbers2, raw._numbers3, zero, o |
| | 0 | 866 | | overflow = overflow || !match; |
| | | 867 | | } |
| | | 868 | | |
| | 0 | 869 | | if (!match && raw.FullAppCompatMatch(TimeSpanFormat.PositiveInvariantFormatLiterals)) |
| | | 870 | | { |
| | 0 | 871 | | positive = true; |
| | 0 | 872 | | match = TryTimeToTicks(positive, raw._numbers0, raw._numbers1, raw._numbers2, zero, raw._numbers3, o |
| | 0 | 873 | | overflow = overflow || !match; |
| | | 874 | | } |
| | | 875 | | |
| | 0 | 876 | | if (!match && raw.FullHMSFMatch(TimeSpanFormat.NegativeInvariantFormatLiterals)) |
| | | 877 | | { |
| | 0 | 878 | | positive = false; |
| | 0 | 879 | | match = TryTimeToTicks(positive, zero, raw._numbers0, raw._numbers1, raw._numbers2, raw._numbers3, o |
| | 0 | 880 | | overflow = overflow || !match; |
| | | 881 | | } |
| | | 882 | | |
| | 0 | 883 | | if (!match && raw.FullDHMSMatch(TimeSpanFormat.NegativeInvariantFormatLiterals)) |
| | | 884 | | { |
| | 0 | 885 | | positive = false; |
| | 0 | 886 | | match = TryTimeToTicks(positive, raw._numbers0, raw._numbers1, raw._numbers2, raw._numbers3, zero, o |
| | 0 | 887 | | overflow = overflow || !match; |
| | | 888 | | } |
| | | 889 | | |
| | 0 | 890 | | if (!match && raw.FullAppCompatMatch(TimeSpanFormat.NegativeInvariantFormatLiterals)) |
| | | 891 | | { |
| | 0 | 892 | | positive = false; |
| | 0 | 893 | | match = TryTimeToTicks(positive, raw._numbers0, raw._numbers1, raw._numbers2, zero, raw._numbers3, o |
| | 0 | 894 | | overflow = overflow || !match; |
| | | 895 | | } |
| | | 896 | | } |
| | | 897 | | |
| | 0 | 898 | | if (loc) |
| | | 899 | | { |
| | 0 | 900 | | if (!match && raw.FullHMSFMatch(raw.PositiveLocalized)) |
| | | 901 | | { |
| | 0 | 902 | | positive = true; |
| | 0 | 903 | | match = TryTimeToTicks(positive, zero, raw._numbers0, raw._numbers1, raw._numbers2, raw._numbers3, o |
| | 0 | 904 | | overflow = overflow || !match; |
| | | 905 | | } |
| | | 906 | | |
| | 0 | 907 | | if (!match && raw.FullDHMSMatch(raw.PositiveLocalized)) |
| | | 908 | | { |
| | 0 | 909 | | positive = true; |
| | 0 | 910 | | match = TryTimeToTicks(positive, raw._numbers0, raw._numbers1, raw._numbers2, raw._numbers3, zero, o |
| | 0 | 911 | | overflow = overflow || !match; |
| | | 912 | | } |
| | | 913 | | |
| | 0 | 914 | | if (!match && raw.FullAppCompatMatch(raw.PositiveLocalized)) |
| | | 915 | | { |
| | 0 | 916 | | positive = true; |
| | 0 | 917 | | match = TryTimeToTicks(positive, raw._numbers0, raw._numbers1, raw._numbers2, zero, raw._numbers3, o |
| | 0 | 918 | | overflow = overflow || !match; |
| | | 919 | | } |
| | | 920 | | |
| | 0 | 921 | | if (!match && raw.FullHMSFMatch(raw.NegativeLocalized)) |
| | | 922 | | { |
| | 0 | 923 | | positive = false; |
| | 0 | 924 | | match = TryTimeToTicks(positive, zero, raw._numbers0, raw._numbers1, raw._numbers2, raw._numbers3, o |
| | 0 | 925 | | overflow = overflow || !match; |
| | | 926 | | } |
| | | 927 | | |
| | 0 | 928 | | if (!match && raw.FullDHMSMatch(raw.NegativeLocalized)) |
| | | 929 | | { |
| | 0 | 930 | | positive = false; |
| | 0 | 931 | | match = TryTimeToTicks(positive, raw._numbers0, raw._numbers1, raw._numbers2, raw._numbers3, zero, o |
| | 0 | 932 | | overflow = overflow || !match; |
| | | 933 | | } |
| | | 934 | | |
| | 0 | 935 | | if (!match && raw.FullAppCompatMatch(raw.NegativeLocalized)) |
| | | 936 | | { |
| | 0 | 937 | | positive = false; |
| | 0 | 938 | | match = TryTimeToTicks(positive, raw._numbers0, raw._numbers1, raw._numbers2, zero, raw._numbers3, o |
| | 0 | 939 | | overflow = overflow || !match; |
| | | 940 | | } |
| | | 941 | | } |
| | | 942 | | |
| | 0 | 943 | | if (match) |
| | | 944 | | { |
| | 0 | 945 | | if (!positive) |
| | | 946 | | { |
| | 0 | 947 | | ticks = -ticks; |
| | 0 | 948 | | if (ticks > 0) |
| | | 949 | | { |
| | 0 | 950 | | return result.SetOverflowFailure(); |
| | | 951 | | } |
| | | 952 | | } |
| | | 953 | | |
| | 0 | 954 | | result.parsedTimeSpan = new TimeSpan(ticks); |
| | 0 | 955 | | return true; |
| | | 956 | | } |
| | | 957 | | |
| | 0 | 958 | | return overflow ? |
| | 0 | 959 | | result.SetOverflowFailure() : // we found at least one literal pattern match but the numbers just didn't |
| | 0 | 960 | | result.SetBadTimeSpanFailure(); // we couldn't find a thing |
| | | 961 | | } |
| | | 962 | | |
| | | 963 | | /// <summary>Validate the ambiguous 3-number "Hours:Minutes:Seconds", "Days.Hours:Minutes", or "Hours:Minutes:.F |
| | | 964 | | private static bool ProcessTerminal_HM_S_D(ref TimeSpanRawInfo raw, TimeSpanStandardStyles style, ref TimeSpanRe |
| | | 965 | | { |
| | 0 | 966 | | if (raw._sepCount != 4 || (style & TimeSpanStandardStyles.RequireFull) != 0) |
| | | 967 | | { |
| | 0 | 968 | | return result.SetBadTimeSpanFailure(); |
| | | 969 | | } |
| | 0 | 970 | | Debug.Assert(raw._numCount == 3); |
| | | 971 | | |
| | 0 | 972 | | bool inv = ((style & TimeSpanStandardStyles.Invariant) != 0); |
| | 0 | 973 | | bool loc = ((style & TimeSpanStandardStyles.Localized) != 0); |
| | | 974 | | |
| | 0 | 975 | | bool positive = false, match = false, overflow = false; |
| | 0 | 976 | | var zero = new TimeSpanToken(0); |
| | 0 | 977 | | long ticks = 0; |
| | | 978 | | |
| | 0 | 979 | | if (inv) |
| | | 980 | | { |
| | 0 | 981 | | if (raw.FullHMSMatch(TimeSpanFormat.PositiveInvariantFormatLiterals)) |
| | | 982 | | { |
| | 0 | 983 | | positive = true; |
| | 0 | 984 | | match = TryTimeToTicks(positive, zero, raw._numbers0, raw._numbers1, raw._numbers2, zero, out ticks) |
| | 0 | 985 | | overflow = overflow || !match; |
| | | 986 | | } |
| | | 987 | | |
| | 0 | 988 | | if (!match && raw.FullDHMMatch(TimeSpanFormat.PositiveInvariantFormatLiterals)) |
| | | 989 | | { |
| | 0 | 990 | | positive = true; |
| | 0 | 991 | | match = TryTimeToTicks(positive, raw._numbers0, raw._numbers1, raw._numbers2, zero, zero, out ticks) |
| | 0 | 992 | | overflow = overflow || !match; |
| | | 993 | | } |
| | | 994 | | |
| | 0 | 995 | | if (!match && raw.PartialAppCompatMatch(TimeSpanFormat.PositiveInvariantFormatLiterals)) |
| | | 996 | | { |
| | 0 | 997 | | positive = true; |
| | 0 | 998 | | match = TryTimeToTicks(positive, zero, raw._numbers0, raw._numbers1, zero, raw._numbers2, out ticks) |
| | 0 | 999 | | overflow = overflow || !match; |
| | | 1000 | | } |
| | | 1001 | | |
| | 0 | 1002 | | if (!match && raw.FullHMSMatch(TimeSpanFormat.NegativeInvariantFormatLiterals)) |
| | | 1003 | | { |
| | 0 | 1004 | | positive = false; |
| | 0 | 1005 | | match = TryTimeToTicks(positive, zero, raw._numbers0, raw._numbers1, raw._numbers2, zero, out ticks) |
| | 0 | 1006 | | overflow = overflow || !match; |
| | | 1007 | | } |
| | | 1008 | | |
| | 0 | 1009 | | if (!match && raw.FullDHMMatch(TimeSpanFormat.NegativeInvariantFormatLiterals)) |
| | | 1010 | | { |
| | 0 | 1011 | | positive = false; |
| | 0 | 1012 | | match = TryTimeToTicks(positive, raw._numbers0, raw._numbers1, raw._numbers2, zero, zero, out ticks) |
| | 0 | 1013 | | overflow = overflow || !match; |
| | | 1014 | | } |
| | | 1015 | | |
| | 0 | 1016 | | if (!match && raw.PartialAppCompatMatch(TimeSpanFormat.NegativeInvariantFormatLiterals)) |
| | | 1017 | | { |
| | 0 | 1018 | | positive = false; |
| | 0 | 1019 | | match = TryTimeToTicks(positive, zero, raw._numbers0, raw._numbers1, zero, raw._numbers2, out ticks) |
| | 0 | 1020 | | overflow = overflow || !match; |
| | | 1021 | | } |
| | | 1022 | | } |
| | | 1023 | | |
| | 0 | 1024 | | if (loc) |
| | | 1025 | | { |
| | 0 | 1026 | | if (!match && raw.FullHMSMatch(raw.PositiveLocalized)) |
| | | 1027 | | { |
| | 0 | 1028 | | positive = true; |
| | 0 | 1029 | | match = TryTimeToTicks(positive, zero, raw._numbers0, raw._numbers1, raw._numbers2, zero, out ticks) |
| | 0 | 1030 | | overflow = overflow || !match; |
| | | 1031 | | } |
| | | 1032 | | |
| | 0 | 1033 | | if (!match && raw.FullDHMMatch(raw.PositiveLocalized)) |
| | | 1034 | | { |
| | 0 | 1035 | | positive = true; |
| | 0 | 1036 | | match = TryTimeToTicks(positive, raw._numbers0, raw._numbers1, raw._numbers2, zero, zero, out ticks) |
| | 0 | 1037 | | overflow = overflow || !match; |
| | | 1038 | | } |
| | | 1039 | | |
| | 0 | 1040 | | if (!match && raw.PartialAppCompatMatch(raw.PositiveLocalized)) |
| | | 1041 | | { |
| | 0 | 1042 | | positive = true; |
| | 0 | 1043 | | match = TryTimeToTicks(positive, zero, raw._numbers0, raw._numbers1, zero, raw._numbers2, out ticks) |
| | 0 | 1044 | | overflow = overflow || !match; |
| | | 1045 | | } |
| | | 1046 | | |
| | 0 | 1047 | | if (!match && raw.FullHMSMatch(raw.NegativeLocalized)) |
| | | 1048 | | { |
| | 0 | 1049 | | positive = false; |
| | 0 | 1050 | | match = TryTimeToTicks(positive, zero, raw._numbers0, raw._numbers1, raw._numbers2, zero, out ticks) |
| | 0 | 1051 | | overflow = overflow || !match; |
| | | 1052 | | } |
| | | 1053 | | |
| | 0 | 1054 | | if (!match && raw.FullDHMMatch(raw.NegativeLocalized)) |
| | | 1055 | | { |
| | 0 | 1056 | | positive = false; |
| | 0 | 1057 | | match = TryTimeToTicks(positive, raw._numbers0, raw._numbers1, raw._numbers2, zero, zero, out ticks) |
| | 0 | 1058 | | overflow = overflow || !match; |
| | | 1059 | | } |
| | | 1060 | | |
| | 0 | 1061 | | if (!match && raw.PartialAppCompatMatch(raw.NegativeLocalized)) |
| | | 1062 | | { |
| | 0 | 1063 | | positive = false; |
| | 0 | 1064 | | match = TryTimeToTicks(positive, zero, raw._numbers0, raw._numbers1, zero, raw._numbers2, out ticks) |
| | 0 | 1065 | | overflow = overflow || !match; |
| | | 1066 | | } |
| | | 1067 | | } |
| | | 1068 | | |
| | 0 | 1069 | | if (match) |
| | | 1070 | | { |
| | 0 | 1071 | | if (!positive) |
| | | 1072 | | { |
| | 0 | 1073 | | ticks = -ticks; |
| | 0 | 1074 | | if (ticks > 0) |
| | | 1075 | | { |
| | 0 | 1076 | | return result.SetOverflowFailure(); |
| | | 1077 | | } |
| | | 1078 | | } |
| | | 1079 | | |
| | 0 | 1080 | | result.parsedTimeSpan = new TimeSpan(ticks); |
| | 0 | 1081 | | return true; |
| | | 1082 | | } |
| | | 1083 | | |
| | 0 | 1084 | | return overflow ? |
| | 0 | 1085 | | result.SetOverflowFailure() : // we found at least one literal pattern match but the numbers just didn't |
| | 0 | 1086 | | result.SetBadTimeSpanFailure(); // we couldn't find a thing |
| | | 1087 | | } |
| | | 1088 | | |
| | | 1089 | | /// <summary>Validate the 2-number "Hours:Minutes" terminal case.</summary> |
| | | 1090 | | private static bool ProcessTerminal_HM(ref TimeSpanRawInfo raw, TimeSpanStandardStyles style, ref TimeSpanResult |
| | | 1091 | | { |
| | 0 | 1092 | | if (raw._sepCount != 3 || (style & TimeSpanStandardStyles.RequireFull) != 0) |
| | | 1093 | | { |
| | 0 | 1094 | | return result.SetBadTimeSpanFailure(); |
| | | 1095 | | } |
| | 0 | 1096 | | Debug.Assert(raw._numCount == 2); |
| | | 1097 | | |
| | 0 | 1098 | | bool inv = ((style & TimeSpanStandardStyles.Invariant) != 0); |
| | 0 | 1099 | | bool loc = ((style & TimeSpanStandardStyles.Localized) != 0); |
| | | 1100 | | |
| | 0 | 1101 | | bool positive = false, match = false; |
| | | 1102 | | |
| | 0 | 1103 | | if (inv) |
| | | 1104 | | { |
| | 0 | 1105 | | if (raw.FullHMMatch(TimeSpanFormat.PositiveInvariantFormatLiterals)) |
| | | 1106 | | { |
| | 0 | 1107 | | match = true; |
| | 0 | 1108 | | positive = true; |
| | | 1109 | | } |
| | | 1110 | | |
| | 0 | 1111 | | if (!match && raw.FullHMMatch(TimeSpanFormat.NegativeInvariantFormatLiterals)) |
| | | 1112 | | { |
| | 0 | 1113 | | match = true; |
| | 0 | 1114 | | positive = false; |
| | | 1115 | | } |
| | | 1116 | | } |
| | | 1117 | | |
| | 0 | 1118 | | if (loc) |
| | | 1119 | | { |
| | 0 | 1120 | | if (!match && raw.FullHMMatch(raw.PositiveLocalized)) |
| | | 1121 | | { |
| | 0 | 1122 | | match = true; |
| | 0 | 1123 | | positive = true; |
| | | 1124 | | } |
| | | 1125 | | |
| | 0 | 1126 | | if (!match && raw.FullHMMatch(raw.NegativeLocalized)) |
| | | 1127 | | { |
| | 0 | 1128 | | match = true; |
| | 0 | 1129 | | positive = false; |
| | | 1130 | | } |
| | | 1131 | | } |
| | | 1132 | | |
| | 0 | 1133 | | if (match) |
| | | 1134 | | { |
| | 0 | 1135 | | var zero = new TimeSpanToken(0); |
| | | 1136 | | |
| | 0 | 1137 | | if (!TryTimeToTicks(positive, zero, raw._numbers0, raw._numbers1, zero, zero, out long ticks)) |
| | | 1138 | | { |
| | 0 | 1139 | | return result.SetOverflowFailure(); |
| | | 1140 | | } |
| | | 1141 | | |
| | 0 | 1142 | | if (!positive) |
| | | 1143 | | { |
| | 0 | 1144 | | ticks = -ticks; |
| | 0 | 1145 | | if (ticks > 0) |
| | | 1146 | | { |
| | 0 | 1147 | | return result.SetOverflowFailure(); |
| | | 1148 | | } |
| | | 1149 | | } |
| | | 1150 | | |
| | 0 | 1151 | | result.parsedTimeSpan = new TimeSpan(ticks); |
| | 0 | 1152 | | return true; |
| | | 1153 | | } |
| | | 1154 | | |
| | 0 | 1155 | | return result.SetBadTimeSpanFailure(); |
| | | 1156 | | } |
| | | 1157 | | |
| | | 1158 | | /// <summary>Validate the 1-number "Days" terminal case.</summary> |
| | | 1159 | | private static bool ProcessTerminal_D(ref TimeSpanRawInfo raw, TimeSpanStandardStyles style, ref TimeSpanResult |
| | | 1160 | | { |
| | 0 | 1161 | | if (raw._sepCount != 2 || (style & TimeSpanStandardStyles.RequireFull) != 0) |
| | | 1162 | | { |
| | 0 | 1163 | | return result.SetBadTimeSpanFailure(); |
| | | 1164 | | } |
| | 0 | 1165 | | Debug.Assert(raw._numCount == 1); |
| | | 1166 | | |
| | 0 | 1167 | | bool inv = ((style & TimeSpanStandardStyles.Invariant) != 0); |
| | 0 | 1168 | | bool loc = ((style & TimeSpanStandardStyles.Localized) != 0); |
| | | 1169 | | |
| | 0 | 1170 | | bool positive = false, match = false; |
| | | 1171 | | |
| | 0 | 1172 | | if (inv) |
| | | 1173 | | { |
| | 0 | 1174 | | if (raw.FullDMatch(TimeSpanFormat.PositiveInvariantFormatLiterals)) |
| | | 1175 | | { |
| | 0 | 1176 | | match = true; |
| | 0 | 1177 | | positive = true; |
| | | 1178 | | } |
| | | 1179 | | |
| | 0 | 1180 | | if (!match && raw.FullDMatch(TimeSpanFormat.NegativeInvariantFormatLiterals)) |
| | | 1181 | | { |
| | 0 | 1182 | | match = true; |
| | 0 | 1183 | | positive = false; |
| | | 1184 | | } |
| | | 1185 | | } |
| | | 1186 | | |
| | 0 | 1187 | | if (loc) |
| | | 1188 | | { |
| | 0 | 1189 | | if (!match && raw.FullDMatch(raw.PositiveLocalized)) |
| | | 1190 | | { |
| | 0 | 1191 | | match = true; |
| | 0 | 1192 | | positive = true; |
| | | 1193 | | } |
| | | 1194 | | |
| | 0 | 1195 | | if (!match && raw.FullDMatch(raw.NegativeLocalized)) |
| | | 1196 | | { |
| | 0 | 1197 | | match = true; |
| | 0 | 1198 | | positive = false; |
| | | 1199 | | } |
| | | 1200 | | } |
| | | 1201 | | |
| | 0 | 1202 | | if (match) |
| | | 1203 | | { |
| | 0 | 1204 | | var zero = new TimeSpanToken(0); |
| | | 1205 | | |
| | 0 | 1206 | | if (!TryTimeToTicks(positive, raw._numbers0, zero, zero, zero, zero, out long ticks)) |
| | | 1207 | | { |
| | 0 | 1208 | | return result.SetOverflowFailure(); |
| | | 1209 | | } |
| | | 1210 | | |
| | 0 | 1211 | | if (!positive) |
| | | 1212 | | { |
| | 0 | 1213 | | ticks = -ticks; |
| | 0 | 1214 | | if (ticks > 0) |
| | | 1215 | | { |
| | 0 | 1216 | | return result.SetOverflowFailure(); |
| | | 1217 | | } |
| | | 1218 | | } |
| | | 1219 | | |
| | 0 | 1220 | | result.parsedTimeSpan = new TimeSpan(ticks); |
| | 0 | 1221 | | return true; |
| | | 1222 | | } |
| | | 1223 | | |
| | 0 | 1224 | | return result.SetBadTimeSpanFailure(); |
| | | 1225 | | } |
| | | 1226 | | |
| | | 1227 | | /// <summary>Common private ParseExact method called by both ParseExact and TryParseExact.</summary> |
| | | 1228 | | private static bool TryParseExactTimeSpan(ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider? |
| | | 1229 | | { |
| | 0 | 1230 | | if (format.Length == 0) |
| | | 1231 | | { |
| | 0 | 1232 | | return result.SetBadFormatSpecifierFailure(); |
| | | 1233 | | } |
| | | 1234 | | |
| | 0 | 1235 | | if (format.Length == 1) |
| | | 1236 | | { |
| | 0 | 1237 | | return format[0] switch |
| | 0 | 1238 | | { |
| | 0 | 1239 | | 'c' or 't' or 'T' => TryParseTimeSpanConstant(input, ref result), // fast path for legacy style Time |
| | 0 | 1240 | | 'g' => TryParseTimeSpan(input, TimeSpanStandardStyles.Localized, formatProvider, ref result), |
| | 0 | 1241 | | 'G' => TryParseTimeSpan(input, TimeSpanStandardStyles.Localized | TimeSpanStandardStyles.RequireFull |
| | 0 | 1242 | | _ => result.SetBadFormatSpecifierFailure(format[0]), |
| | 0 | 1243 | | }; |
| | | 1244 | | } |
| | | 1245 | | |
| | 0 | 1246 | | return TryParseByFormat(input, format, styles, ref result); |
| | | 1247 | | } |
| | | 1248 | | |
| | | 1249 | | /// <summary>Parse the TimeSpan instance using the specified format. Used by TryParseExactTimeSpan.</summary> |
| | | 1250 | | private static bool TryParseByFormat(ReadOnlySpan<char> input, ReadOnlySpan<char> format, TimeSpanStyles styles, |
| | | 1251 | | { |
| | 0 | 1252 | | bool seenDD = false; // already processed days? |
| | 0 | 1253 | | bool seenHH = false; // already processed hours? |
| | 0 | 1254 | | bool seenMM = false; // already processed minutes? |
| | 0 | 1255 | | bool seenSS = false; // already processed seconds? |
| | 0 | 1256 | | bool seenFF = false; // already processed fraction? |
| | | 1257 | | |
| | 0 | 1258 | | int dd = 0; // parsed days |
| | 0 | 1259 | | int hh = 0; // parsed hours |
| | 0 | 1260 | | int mm = 0; // parsed minutes |
| | 0 | 1261 | | int ss = 0; // parsed seconds |
| | 0 | 1262 | | int leadingZeroes = 0; // number of leading zeroes in the parsed fraction |
| | 0 | 1263 | | int ff = 0; // parsed fraction |
| | 0 | 1264 | | int i = 0; // format string position |
| | | 1265 | | int tokenLen; // length of current format token, used to update index 'i' |
| | | 1266 | | |
| | 0 | 1267 | | var tokenizer = new TimeSpanTokenizer(input, -1); |
| | | 1268 | | |
| | 0 | 1269 | | while ((uint)i < (uint)format.Length) |
| | | 1270 | | { |
| | 0 | 1271 | | char ch = format[i]; |
| | | 1272 | | int nextFormatChar; |
| | | 1273 | | switch (ch) |
| | | 1274 | | { |
| | | 1275 | | case 'h': |
| | 0 | 1276 | | tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch); |
| | 0 | 1277 | | if (tokenLen > 2 || seenHH || !ParseExactDigits(ref tokenizer, tokenLen, out hh)) |
| | | 1278 | | { |
| | 0 | 1279 | | return result.SetInvalidStringFailure(); |
| | | 1280 | | } |
| | 0 | 1281 | | seenHH = true; |
| | 0 | 1282 | | break; |
| | | 1283 | | |
| | | 1284 | | case 'm': |
| | 0 | 1285 | | tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch); |
| | 0 | 1286 | | if (tokenLen > 2 || seenMM || !ParseExactDigits(ref tokenizer, tokenLen, out mm)) |
| | | 1287 | | { |
| | 0 | 1288 | | return result.SetInvalidStringFailure(); |
| | | 1289 | | } |
| | 0 | 1290 | | seenMM = true; |
| | 0 | 1291 | | break; |
| | | 1292 | | |
| | | 1293 | | case 's': |
| | 0 | 1294 | | tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch); |
| | 0 | 1295 | | if (tokenLen > 2 || seenSS || !ParseExactDigits(ref tokenizer, tokenLen, out ss)) |
| | | 1296 | | { |
| | 0 | 1297 | | return result.SetInvalidStringFailure(); |
| | | 1298 | | } |
| | 0 | 1299 | | seenSS = true; |
| | 0 | 1300 | | break; |
| | | 1301 | | |
| | | 1302 | | case 'f': |
| | 0 | 1303 | | tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch); |
| | 0 | 1304 | | if (tokenLen > DateTimeFormat.MaxSecondsFractionDigits || seenFF || !ParseExactDigits(ref tokeni |
| | | 1305 | | { |
| | 0 | 1306 | | return result.SetInvalidStringFailure(); |
| | | 1307 | | } |
| | 0 | 1308 | | seenFF = true; |
| | 0 | 1309 | | break; |
| | | 1310 | | |
| | | 1311 | | case 'F': |
| | 0 | 1312 | | tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch); |
| | 0 | 1313 | | if (tokenLen > DateTimeFormat.MaxSecondsFractionDigits || seenFF) |
| | | 1314 | | { |
| | 0 | 1315 | | return result.SetInvalidStringFailure(); |
| | | 1316 | | } |
| | 0 | 1317 | | ParseExactDigits(ref tokenizer, tokenLen, tokenLen, out leadingZeroes, out ff); |
| | 0 | 1318 | | seenFF = true; |
| | 0 | 1319 | | break; |
| | | 1320 | | |
| | | 1321 | | case 'd': |
| | 0 | 1322 | | tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch); |
| | 0 | 1323 | | if (tokenLen > 8 || seenDD || !ParseExactDigits(ref tokenizer, (tokenLen < 2) ? 1 : tokenLen, (t |
| | | 1324 | | { |
| | 0 | 1325 | | return result.SetInvalidStringFailure(); |
| | | 1326 | | } |
| | 0 | 1327 | | seenDD = true; |
| | 0 | 1328 | | break; |
| | | 1329 | | |
| | | 1330 | | case '\'': |
| | | 1331 | | case '\"': |
| | 0 | 1332 | | var enquotedString = new ValueStringBuilder(64); |
| | 0 | 1333 | | if (!DateTimeParse.TryParseQuoteString(format, i, ref enquotedString, out tokenLen)) |
| | | 1334 | | { |
| | 0 | 1335 | | enquotedString.Dispose(); |
| | 0 | 1336 | | return result.SetBadQuoteFailure(ch); |
| | | 1337 | | } |
| | 0 | 1338 | | if (!ParseExactLiteral(ref tokenizer, ref enquotedString)) |
| | | 1339 | | { |
| | 0 | 1340 | | enquotedString.Dispose(); |
| | 0 | 1341 | | return result.SetInvalidStringFailure(); |
| | | 1342 | | } |
| | 0 | 1343 | | enquotedString.Dispose(); |
| | 0 | 1344 | | break; |
| | | 1345 | | |
| | | 1346 | | case '%': |
| | | 1347 | | // Optional format character. |
| | | 1348 | | // For example, format string "%d" will print day |
| | | 1349 | | // Most of the cases, "%" can be ignored. |
| | 0 | 1350 | | nextFormatChar = DateTimeFormat.ParseNextChar(format, i); |
| | | 1351 | | |
| | | 1352 | | // nextFormatChar will be -1 if we already reach the end of the format string. |
| | | 1353 | | // Besides, we will not allow "%%" appear in the pattern. |
| | 0 | 1354 | | if (nextFormatChar >= 0 && nextFormatChar != '%') |
| | | 1355 | | { |
| | 0 | 1356 | | tokenLen = 1; // skip the '%' and process the format character |
| | 0 | 1357 | | break; |
| | | 1358 | | } |
| | | 1359 | | else |
| | | 1360 | | { |
| | | 1361 | | // This means that '%' is at the end of the format string or |
| | | 1362 | | // "%%" appears in the format string. |
| | 0 | 1363 | | return result.SetInvalidStringFailure(); |
| | | 1364 | | } |
| | | 1365 | | |
| | | 1366 | | case '\\': |
| | | 1367 | | // Escaped character. Can be used to insert character into the format string. |
| | | 1368 | | // For example, "\d" will insert the character 'd' into the string. |
| | | 1369 | | // |
| | 0 | 1370 | | nextFormatChar = DateTimeFormat.ParseNextChar(format, i); |
| | 0 | 1371 | | if (nextFormatChar >= 0 && tokenizer.NextChar() == (char)nextFormatChar) |
| | | 1372 | | { |
| | 0 | 1373 | | tokenLen = 2; |
| | | 1374 | | } |
| | | 1375 | | else |
| | | 1376 | | { |
| | | 1377 | | // This means that '\' is at the end of the format string or the literal match failed. |
| | 0 | 1378 | | return result.SetInvalidStringFailure(); |
| | | 1379 | | } |
| | | 1380 | | break; |
| | | 1381 | | |
| | | 1382 | | default: |
| | 0 | 1383 | | return result.SetInvalidStringFailure(); |
| | | 1384 | | } |
| | | 1385 | | |
| | 0 | 1386 | | i += tokenLen; |
| | | 1387 | | } |
| | | 1388 | | |
| | | 1389 | | |
| | 0 | 1390 | | if (!tokenizer.EOL) |
| | | 1391 | | { |
| | | 1392 | | // the custom format didn't consume the entire input |
| | 0 | 1393 | | return result.SetBadTimeSpanFailure(); |
| | | 1394 | | } |
| | | 1395 | | |
| | 0 | 1396 | | bool positive = (styles & TimeSpanStyles.AssumeNegative) == 0; |
| | 0 | 1397 | | if (TryTimeToTicks(positive, new TimeSpanToken(dd), |
| | 0 | 1398 | | new TimeSpanToken(hh), |
| | 0 | 1399 | | new TimeSpanToken(mm), |
| | 0 | 1400 | | new TimeSpanToken(ss), |
| | 0 | 1401 | | new TimeSpanToken(ff, leadingZeroes), |
| | 0 | 1402 | | out long ticks)) |
| | | 1403 | | { |
| | 0 | 1404 | | if (!positive) |
| | | 1405 | | { |
| | 0 | 1406 | | ticks = -ticks; |
| | | 1407 | | } |
| | | 1408 | | |
| | 0 | 1409 | | result.parsedTimeSpan = new TimeSpan(ticks); |
| | 0 | 1410 | | return true; |
| | | 1411 | | } |
| | | 1412 | | else |
| | | 1413 | | { |
| | 0 | 1414 | | return result.SetOverflowFailure(); |
| | | 1415 | | } |
| | | 1416 | | } |
| | | 1417 | | |
| | | 1418 | | private static bool ParseExactDigits(ref TimeSpanTokenizer tokenizer, int minDigitLength, out int result) |
| | | 1419 | | { |
| | 0 | 1420 | | int maxDigitLength = (minDigitLength == 1) ? 2 : minDigitLength; |
| | 0 | 1421 | | return ParseExactDigits(ref tokenizer, minDigitLength, maxDigitLength, out _, out result); |
| | | 1422 | | } |
| | | 1423 | | |
| | | 1424 | | private static bool ParseExactDigits(ref TimeSpanTokenizer tokenizer, int minDigitLength, int maxDigitLength, ou |
| | | 1425 | | { |
| | 0 | 1426 | | int tmpResult = 0, tmpZeroes = 0; |
| | | 1427 | | |
| | 0 | 1428 | | int tokenLength = 0; |
| | 0 | 1429 | | while (tokenLength < maxDigitLength) |
| | | 1430 | | { |
| | 0 | 1431 | | char ch = tokenizer.NextChar(); |
| | 0 | 1432 | | if (!char.IsAsciiDigit(ch)) |
| | | 1433 | | { |
| | 0 | 1434 | | tokenizer.BackOne(); |
| | 0 | 1435 | | break; |
| | | 1436 | | } |
| | | 1437 | | |
| | 0 | 1438 | | tmpResult = tmpResult * 10 + (ch - '0'); |
| | 0 | 1439 | | if (tmpResult == 0) tmpZeroes++; |
| | 0 | 1440 | | tokenLength++; |
| | | 1441 | | } |
| | | 1442 | | |
| | 0 | 1443 | | zeroes = tmpZeroes; |
| | 0 | 1444 | | result = tmpResult; |
| | 0 | 1445 | | return tokenLength >= minDigitLength; |
| | | 1446 | | } |
| | | 1447 | | |
| | | 1448 | | private static bool ParseExactLiteral(ref TimeSpanTokenizer tokenizer, ref ValueStringBuilder enquotedString) |
| | | 1449 | | { |
| | 0 | 1450 | | ReadOnlySpan<char> span = enquotedString.AsSpan(); |
| | 0 | 1451 | | for (int i = 0; i < span.Length; i++) |
| | | 1452 | | { |
| | 0 | 1453 | | if (span[i] != tokenizer.NextChar()) |
| | | 1454 | | { |
| | 0 | 1455 | | return false; |
| | | 1456 | | } |
| | | 1457 | | } |
| | | 1458 | | |
| | 0 | 1459 | | return true; |
| | | 1460 | | } |
| | | 1461 | | |
| | | 1462 | | /// <summary> |
| | | 1463 | | /// Parses the "c" (constant) format. This code is 100% identical to the non-globalized v1.0-v3.5 TimeSpan.Pars |
| | | 1464 | | /// and exists for performance/appcompat with legacy callers who cannot move onto the globalized Parse overloads |
| | | 1465 | | /// </summary> |
| | | 1466 | | private static bool TryParseTimeSpanConstant(ReadOnlySpan<char> input, ref TimeSpanResult result) => |
| | 0 | 1467 | | default(StringParser).TryParse(input, ref result); |
| | | 1468 | | |
| | | 1469 | | private ref struct StringParser |
| | | 1470 | | { |
| | | 1471 | | private ReadOnlySpan<char> _str; |
| | | 1472 | | private char _ch; |
| | | 1473 | | private int _pos; |
| | | 1474 | | |
| | | 1475 | | internal void NextChar() |
| | | 1476 | | { |
| | 0 | 1477 | | ReadOnlySpan<char> str = _str; |
| | | 1478 | | |
| | 0 | 1479 | | if (_pos < str.Length) |
| | | 1480 | | { |
| | 0 | 1481 | | _pos++; |
| | | 1482 | | } |
| | | 1483 | | |
| | 0 | 1484 | | int pos = _pos; |
| | 0 | 1485 | | _ch = (uint)pos < (uint)str.Length ? |
| | 0 | 1486 | | str[pos] : |
| | 0 | 1487 | | (char)0; |
| | 0 | 1488 | | } |
| | | 1489 | | |
| | | 1490 | | internal char NextNonDigit() |
| | | 1491 | | { |
| | 0 | 1492 | | int i = _str.Slice(_pos).IndexOfAnyExceptInRange('0', '9'); |
| | 0 | 1493 | | return i < 0 ? (char)0 : _str[_pos + i]; |
| | | 1494 | | } |
| | | 1495 | | |
| | | 1496 | | internal bool TryParse(ReadOnlySpan<char> input, ref TimeSpanResult result) |
| | | 1497 | | { |
| | 0 | 1498 | | result.parsedTimeSpan = default; |
| | | 1499 | | |
| | 0 | 1500 | | _str = input; |
| | 0 | 1501 | | _pos = -1; |
| | 0 | 1502 | | NextChar(); |
| | 0 | 1503 | | SkipBlanks(); |
| | | 1504 | | |
| | 0 | 1505 | | bool negative = false; |
| | 0 | 1506 | | if (_ch == '-') |
| | | 1507 | | { |
| | 0 | 1508 | | negative = true; |
| | 0 | 1509 | | NextChar(); |
| | | 1510 | | } |
| | | 1511 | | |
| | | 1512 | | long time; |
| | 0 | 1513 | | if (NextNonDigit() == ':') |
| | | 1514 | | { |
| | 0 | 1515 | | if (!ParseTime(out time, ref result)) |
| | | 1516 | | { |
| | 0 | 1517 | | return false; |
| | | 1518 | | } |
| | | 1519 | | } |
| | | 1520 | | else |
| | | 1521 | | { |
| | 0 | 1522 | | if (!ParseInt((int)(0x7FFFFFFFFFFFFFFFL / TimeSpan.TicksPerDay), out int days, ref result)) |
| | | 1523 | | { |
| | 0 | 1524 | | return false; |
| | | 1525 | | } |
| | | 1526 | | |
| | 0 | 1527 | | time = days * TimeSpan.TicksPerDay; |
| | | 1528 | | |
| | 0 | 1529 | | if (_ch == '.') |
| | | 1530 | | { |
| | 0 | 1531 | | NextChar(); |
| | 0 | 1532 | | if (!ParseTime(out long remainingTime, ref result)) |
| | | 1533 | | { |
| | 0 | 1534 | | return false; |
| | | 1535 | | } |
| | 0 | 1536 | | time += remainingTime; |
| | | 1537 | | } |
| | | 1538 | | } |
| | | 1539 | | |
| | 0 | 1540 | | if (negative) |
| | | 1541 | | { |
| | 0 | 1542 | | time = -time; |
| | | 1543 | | // Allow -0 as well |
| | 0 | 1544 | | if (time > 0) |
| | | 1545 | | { |
| | 0 | 1546 | | return result.SetOverflowFailure(); |
| | | 1547 | | } |
| | | 1548 | | } |
| | | 1549 | | else |
| | | 1550 | | { |
| | 0 | 1551 | | if (time < 0) |
| | | 1552 | | { |
| | 0 | 1553 | | return result.SetOverflowFailure(); |
| | | 1554 | | } |
| | | 1555 | | } |
| | | 1556 | | |
| | 0 | 1557 | | SkipBlanks(); |
| | | 1558 | | |
| | 0 | 1559 | | if (_pos < _str.Length) |
| | | 1560 | | { |
| | 0 | 1561 | | return result.SetBadTimeSpanFailure(); |
| | | 1562 | | } |
| | | 1563 | | |
| | 0 | 1564 | | result.parsedTimeSpan = new TimeSpan(time); |
| | 0 | 1565 | | return true; |
| | | 1566 | | } |
| | | 1567 | | |
| | | 1568 | | internal bool ParseInt(int max, out int i, ref TimeSpanResult result) |
| | | 1569 | | { |
| | 0 | 1570 | | i = 0; |
| | 0 | 1571 | | int p = _pos; |
| | 0 | 1572 | | while (char.IsAsciiDigit(_ch)) |
| | | 1573 | | { |
| | 0 | 1574 | | if ((i & 0xF0000000) != 0) |
| | | 1575 | | { |
| | 0 | 1576 | | return result.SetOverflowFailure(); |
| | | 1577 | | } |
| | | 1578 | | |
| | 0 | 1579 | | i = i * 10 + _ch - '0'; |
| | 0 | 1580 | | if (i < 0) |
| | | 1581 | | { |
| | 0 | 1582 | | return result.SetOverflowFailure(); |
| | | 1583 | | } |
| | | 1584 | | |
| | 0 | 1585 | | NextChar(); |
| | | 1586 | | } |
| | | 1587 | | |
| | 0 | 1588 | | if (p == _pos) |
| | | 1589 | | { |
| | 0 | 1590 | | return result.SetBadTimeSpanFailure(); |
| | | 1591 | | } |
| | | 1592 | | |
| | 0 | 1593 | | if (i > max) |
| | | 1594 | | { |
| | 0 | 1595 | | return result.SetOverflowFailure(); |
| | | 1596 | | } |
| | | 1597 | | |
| | 0 | 1598 | | return true; |
| | | 1599 | | } |
| | | 1600 | | |
| | | 1601 | | internal bool ParseTime(out long time, ref TimeSpanResult result) |
| | | 1602 | | { |
| | 0 | 1603 | | time = 0; |
| | | 1604 | | |
| | 0 | 1605 | | if (!ParseInt(23, out int unit, ref result)) |
| | | 1606 | | { |
| | 0 | 1607 | | return false; |
| | | 1608 | | } |
| | | 1609 | | |
| | 0 | 1610 | | time = unit * TimeSpan.TicksPerHour; |
| | 0 | 1611 | | if (_ch != ':') |
| | | 1612 | | { |
| | 0 | 1613 | | return result.SetBadTimeSpanFailure(); |
| | | 1614 | | } |
| | | 1615 | | |
| | 0 | 1616 | | NextChar(); |
| | | 1617 | | |
| | 0 | 1618 | | if (!ParseInt(59, out unit, ref result)) |
| | | 1619 | | { |
| | 0 | 1620 | | return false; |
| | | 1621 | | } |
| | | 1622 | | |
| | 0 | 1623 | | time += unit * TimeSpan.TicksPerMinute; |
| | | 1624 | | |
| | 0 | 1625 | | if (_ch == ':') |
| | | 1626 | | { |
| | 0 | 1627 | | NextChar(); |
| | | 1628 | | |
| | | 1629 | | // allow seconds with the leading zero |
| | 0 | 1630 | | if (_ch != '.') |
| | | 1631 | | { |
| | 0 | 1632 | | if (!ParseInt(59, out unit, ref result)) |
| | | 1633 | | { |
| | 0 | 1634 | | return false; |
| | | 1635 | | } |
| | 0 | 1636 | | time += unit * TimeSpan.TicksPerSecond; |
| | | 1637 | | } |
| | | 1638 | | |
| | 0 | 1639 | | if (_ch == '.') |
| | | 1640 | | { |
| | 0 | 1641 | | NextChar(); |
| | 0 | 1642 | | int f = (int)TimeSpan.TicksPerSecond; |
| | 0 | 1643 | | while (f > 1 && char.IsAsciiDigit(_ch)) |
| | | 1644 | | { |
| | 0 | 1645 | | f /= 10; |
| | 0 | 1646 | | time += (_ch - '0') * f; |
| | 0 | 1647 | | NextChar(); |
| | | 1648 | | } |
| | | 1649 | | } |
| | | 1650 | | } |
| | | 1651 | | |
| | 0 | 1652 | | return true; |
| | | 1653 | | } |
| | | 1654 | | |
| | | 1655 | | internal void SkipBlanks() |
| | | 1656 | | { |
| | 0 | 1657 | | while (_ch == ' ' || _ch == '\t') NextChar(); |
| | 0 | 1658 | | } |
| | | 1659 | | } |
| | | 1660 | | |
| | | 1661 | | /// <summary>Common private ParseExactMultiple method called by both ParseExactMultiple and TryParseExactMultipl |
| | | 1662 | | private static bool TryParseExactMultipleTimeSpan(ReadOnlySpan<char> input, string?[]? formats, IFormatProvider? |
| | | 1663 | | { |
| | 0 | 1664 | | if (formats == null) |
| | | 1665 | | { |
| | 0 | 1666 | | return result.SetArgumentNullFailure(nameof(formats)); |
| | | 1667 | | } |
| | | 1668 | | |
| | 0 | 1669 | | if (input.Length == 0) |
| | | 1670 | | { |
| | 0 | 1671 | | return result.SetBadTimeSpanFailure(); |
| | | 1672 | | } |
| | | 1673 | | |
| | 0 | 1674 | | if (formats.Length == 0) |
| | | 1675 | | { |
| | 0 | 1676 | | return result.SetNoFormatSpecifierFailure(); |
| | | 1677 | | } |
| | | 1678 | | |
| | | 1679 | | // Do a loop through the provided formats and see if we can parse successfully in |
| | | 1680 | | // one of the formats. |
| | 0 | 1681 | | for (int i = 0; i < formats.Length; i++) |
| | | 1682 | | { |
| | 0 | 1683 | | string? format = formats[i]; |
| | | 1684 | | |
| | 0 | 1685 | | if (string.IsNullOrEmpty(format)) |
| | | 1686 | | { |
| | 0 | 1687 | | return result.SetBadFormatSpecifierFailure(); |
| | | 1688 | | } |
| | | 1689 | | |
| | | 1690 | | // Create a new non-throwing result each time to ensure the runs are independent. |
| | 0 | 1691 | | TimeSpanResult innerResult = new TimeSpanResult(throwOnFailure: false, originalTimeSpanString: input); |
| | | 1692 | | |
| | 0 | 1693 | | if (TryParseExactTimeSpan(input, format, formatProvider, styles, ref innerResult)) |
| | | 1694 | | { |
| | 0 | 1695 | | result.parsedTimeSpan = innerResult.parsedTimeSpan; |
| | 0 | 1696 | | return true; |
| | | 1697 | | } |
| | | 1698 | | } |
| | | 1699 | | |
| | 0 | 1700 | | return result.SetBadTimeSpanFailure(); |
| | | 1701 | | } |
| | | 1702 | | } |
| | | 1703 | | } |
| | | 1704 | | |