diff options
Diffstat (limited to 'src/mscorlib/src/System/Globalization/TimeSpanParse.cs')
-rw-r--r-- | src/mscorlib/src/System/Globalization/TimeSpanParse.cs | 1074 |
1 files changed, 666 insertions, 408 deletions
diff --git a/src/mscorlib/src/System/Globalization/TimeSpanParse.cs b/src/mscorlib/src/System/Globalization/TimeSpanParse.cs index d83c5fa151..a29e6c2e29 100644 --- a/src/mscorlib/src/System/Globalization/TimeSpanParse.cs +++ b/src/mscorlib/src/System/Globalization/TimeSpanParse.cs @@ -50,83 +50,95 @@ // TimeSpanTokenizer.NextChar and TimeSpanTokenizer.BackOne() are called directly. // //////////////////////////////////////////////////////////////////////////// -namespace System.Globalization { - using System.Text; - using System; - using System.Diagnostics; - using System.Diagnostics.Contracts; - using System.Globalization; - - internal static class TimeSpanParse { + +using System.Text; +using System; +using System.Diagnostics; +using System.Diagnostics.Contracts; +using System.Globalization; + +namespace System.Globalization +{ + internal static class TimeSpanParse + { // ---- SECTION: members for internal support ---------* - internal static void ValidateStyles(TimeSpanStyles style, String parameterName) { + internal static void ValidateStyles(TimeSpanStyles style, String parameterName) + { if (style != TimeSpanStyles.None && style != TimeSpanStyles.AssumeNegative) - throw new ArgumentException(Environment.GetResourceString("Argument_InvalidTimeSpanStyles"), parameterName); + throw new ArgumentException(SR.Argument_InvalidTimeSpanStyles, parameterName); } internal const int unlimitedDigits = -1; internal const int maxFractionDigits = 7; - internal const int maxDays = 10675199; - internal const int maxHours = 23; - internal const int maxMinutes = 59; - internal const int maxSeconds = 59; + internal const int maxDays = 10675199; + internal const int maxHours = 23; + internal const int maxMinutes = 59; + internal const int maxSeconds = 59; internal const int maxFraction = 9999999; #region InternalSupport - enum TimeSpanThrowStyle { - None = 0, - All = 1, + private enum TimeSpanThrowStyle + { + None = 0, + All = 1, } - private enum ParseFailureKind { - None = 0, - ArgumentNull = 1, - Format = 2, - FormatWithParameter = 3, - Overflow = 4, + private enum ParseFailureKind + { + None = 0, + ArgumentNull = 1, + Format = 2, + FormatWithParameter = 3, + Overflow = 4, } [Flags] - enum TimeSpanStandardStyles { // Standard Format Styles - None = 0x00000000, - Invariant = 0x00000001, //Allow Invariant Culture - Localized = 0x00000002, //Allow Localized Culture - RequireFull = 0x00000004, //Require the input to be in DHMSF format - Any = Invariant | Localized, + private enum TimeSpanStandardStyles + { // Standard Format Styles + None = 0x00000000, + Invariant = 0x00000001, //Allow Invariant Culture + Localized = 0x00000002, //Allow Localized Culture + RequireFull = 0x00000004, //Require the input to be in DHMSF format + Any = Invariant | Localized, } // TimeSpan Token Types - private enum TTT { - None = 0, // None of the TimeSpanToken fields are set - End = 1, // '\0' - Num = 2, // Number - Sep = 3, // literal - NumOverflow = 4, // Number that overflowed + private enum TTT + { + None = 0, // None of the TimeSpanToken fields are set + End = 1, // '\0' + Num = 2, // Number + Sep = 3, // literal + NumOverflow = 4, // Number that overflowed } private static readonly TimeSpanToken zero = new TimeSpanToken(0); - struct TimeSpanToken { + private struct TimeSpanToken + { internal TTT ttt; internal int num; // Store the number that we are parsing (if any) internal int zeroes; // Store the number of leading zeroes (if any) internal String sep; // Store the literal that we are parsing (if any) - public TimeSpanToken(int number) { + public TimeSpanToken(int number) + { ttt = TTT.Num; num = number; zeroes = 0; sep = null; } - public TimeSpanToken(int leadingZeroes, int number) { + public TimeSpanToken(int leadingZeroes, int number) + { ttt = TTT.Num; num = number; zeroes = leadingZeroes; sep = null; } - public bool IsInvalidNumber(int maxValue, int maxPrecision) { + public bool IsInvalidNumber(int maxValue, int maxPrecision) + { Debug.Assert(ttt == TTT.Num); Debug.Assert(num > -1); Debug.Assert(maxValue > 0); @@ -138,12 +150,12 @@ namespace System.Globalization { return false; // all validation past this point applies only to fields with precision limits if (zeroes > maxPrecision) return true; - if (num == 0 || zeroes == 0) + if (num == 0 || zeroes == 0) return false; // num > 0 && zeroes > 0 && num <= maxValue && zeroes <= maxPrecision - return (num >= (maxValue/(long)Math.Pow(10, zeroes-1))); - } + return (num >= (maxValue / (long)Math.Pow(10, zeroes - 1))); + } } // @@ -151,41 +163,50 @@ namespace System.Globalization { // // Actions: TimeSpanTokenizer.GetNextToken() returns the next token in the input string. // - struct TimeSpanTokenizer { + private struct TimeSpanTokenizer + { private int m_pos; private String m_value; - internal void Init(String input) { + internal void Init(String input) + { Init(input, 0); } - internal void Init(String input, int startPosition) { + internal void Init(String input, int startPosition) + { m_pos = startPosition; m_value = input; } // used by the parsing routines that operate on standard-formats - internal TimeSpanToken GetNextToken() { + internal TimeSpanToken GetNextToken() + { Debug.Assert(m_pos > -1); TimeSpanToken tok = new TimeSpanToken(); char ch = CurrentChar; - if (ch == (char)0) { + if (ch == (char)0) + { tok.ttt = TTT.End; return tok; } - if (ch >= '0' && ch <= '9') { + if (ch >= '0' && ch <= '9') + { tok.ttt = TTT.Num; tok.num = 0; tok.zeroes = 0; - do { - if ((tok.num & 0xF0000000) != 0) { + do + { + if ((tok.num & 0xF0000000) != 0) + { tok.ttt = TTT.NumOverflow; return tok; } tok.num = tok.num * 10 + ch - '0'; if (tok.num == 0) tok.zeroes++; - if (tok.num < 0) { + if (tok.num < 0) + { tok.ttt = TTT.NumOverflow; return tok; } @@ -193,12 +214,14 @@ namespace System.Globalization { } while (ch >= '0' && ch <= '9'); return tok; } - else { + else + { tok.ttt = TTT.Sep; int startIndex = m_pos; int length = 0; - while (ch != (char)0 && (ch < '0' || '9' < ch)) { + while (ch != (char)0 && (ch < '0' || '9' < ch)) + { ch = NextChar; length++; } @@ -207,53 +230,70 @@ namespace System.Globalization { } } - internal Boolean EOL { - get { - return m_pos >= (m_value.Length-1); + internal Boolean EOL + { + get + { + return m_pos >= (m_value.Length - 1); } } // BackOne, NextChar, CurrentChar - used by ParseExact (ParseByFormat) to operate // on custom-formats where exact character-by-character control is allowed - internal void BackOne() { + internal void BackOne() + { if (m_pos > 0) --m_pos; } - internal char NextChar { - get { + internal char NextChar + { + get + { m_pos++; return CurrentChar; } } - internal char CurrentChar { - get { - if (m_pos > -1 && m_pos < m_value.Length) { + internal char CurrentChar + { + get + { + if (m_pos > -1 && m_pos < m_value.Length) + { return m_value[m_pos]; } - else { - return (char) 0; + else + { + return (char)0; } } } } - + // This stores intermediary parsing state for the standard formats - struct TimeSpanRawInfo { - internal TimeSpanFormat.FormatLiterals PositiveInvariant { - get { + private struct TimeSpanRawInfo + { + internal TimeSpanFormat.FormatLiterals PositiveInvariant + { + get + { return TimeSpanFormat.PositiveInvariantFormatLiterals; } } - internal TimeSpanFormat.FormatLiterals NegativeInvariant { - get { + internal TimeSpanFormat.FormatLiterals NegativeInvariant + { + get + { return TimeSpanFormat.NegativeInvariantFormatLiterals; } } - internal TimeSpanFormat.FormatLiterals PositiveLocalized { - get { - if (!m_posLocInit) { + internal TimeSpanFormat.FormatLiterals PositiveLocalized + { + get + { + if (!m_posLocInit) + { m_posLoc = new TimeSpanFormat.FormatLiterals(); m_posLoc.Init(m_fullPosPattern, false); m_posLocInit = true; @@ -261,99 +301,110 @@ namespace System.Globalization { return m_posLoc; } } - internal TimeSpanFormat.FormatLiterals NegativeLocalized { - get { - if (!m_negLocInit) { + internal TimeSpanFormat.FormatLiterals NegativeLocalized + { + get + { + if (!m_negLocInit) + { m_negLoc = new TimeSpanFormat.FormatLiterals(); - m_negLoc.Init(m_fullNegPattern, false); - m_negLocInit = true; + m_negLoc.Init(m_fullNegPattern, false); + m_negLocInit = true; } return m_negLoc; } } - internal Boolean FullAppCompatMatch(TimeSpanFormat.FormatLiterals pattern) { - return SepCount == 5 - && NumCount == 4 - && pattern.Start == literals[0] - && pattern.DayHourSep == literals[1] - && pattern.HourMinuteSep == literals[2] - && pattern.AppCompatLiteral == literals[3] - && pattern.End == literals[4]; + internal Boolean FullAppCompatMatch(TimeSpanFormat.FormatLiterals pattern) + { + return SepCount == 5 + && NumCount == 4 + && pattern.Start == literals[0] + && pattern.DayHourSep == literals[1] + && pattern.HourMinuteSep == literals[2] + && pattern.AppCompatLiteral == literals[3] + && pattern.End == literals[4]; } - internal Boolean PartialAppCompatMatch(TimeSpanFormat.FormatLiterals pattern) { - return SepCount == 4 - && NumCount == 3 - && pattern.Start == literals[0] - && pattern.HourMinuteSep == literals[1] - && pattern.AppCompatLiteral == literals[2] - && pattern.End == literals[3]; + internal Boolean PartialAppCompatMatch(TimeSpanFormat.FormatLiterals pattern) + { + return SepCount == 4 + && NumCount == 3 + && pattern.Start == literals[0] + && pattern.HourMinuteSep == literals[1] + && pattern.AppCompatLiteral == literals[2] + && pattern.End == literals[3]; } // DHMSF (all values matched) - internal Boolean FullMatch(TimeSpanFormat.FormatLiterals pattern) { - return SepCount == MaxLiteralTokens - && NumCount == MaxNumericTokens - && pattern.Start == literals[0] - && pattern.DayHourSep == literals[1] - && pattern.HourMinuteSep == literals[2] - && pattern.MinuteSecondSep == literals[3] + internal Boolean FullMatch(TimeSpanFormat.FormatLiterals pattern) + { + return SepCount == MaxLiteralTokens + && NumCount == MaxNumericTokens + && pattern.Start == literals[0] + && pattern.DayHourSep == literals[1] + && pattern.HourMinuteSep == literals[2] + && pattern.MinuteSecondSep == literals[3] && pattern.SecondFractionSep == literals[4] - && pattern.End == literals[5]; + && pattern.End == literals[5]; } // D (no hours, minutes, seconds, or fractions) - internal Boolean FullDMatch(TimeSpanFormat.FormatLiterals pattern) { - return SepCount == 2 - && NumCount == 1 - && pattern.Start == literals[0] - && pattern.End == literals[1]; + internal Boolean FullDMatch(TimeSpanFormat.FormatLiterals pattern) + { + return SepCount == 2 + && NumCount == 1 + && pattern.Start == literals[0] + && pattern.End == literals[1]; } // HM (no days, seconds, or fractions) - internal Boolean FullHMMatch(TimeSpanFormat.FormatLiterals pattern) { - return SepCount == 3 - && NumCount == 2 - && pattern.Start == literals[0] - && pattern.HourMinuteSep == literals[1] - && pattern.End == literals[2]; + internal Boolean FullHMMatch(TimeSpanFormat.FormatLiterals pattern) + { + return SepCount == 3 + && NumCount == 2 + && pattern.Start == literals[0] + && pattern.HourMinuteSep == literals[1] + && pattern.End == literals[2]; } // DHM (no seconds or fraction) - internal Boolean FullDHMMatch(TimeSpanFormat.FormatLiterals pattern) { - return SepCount == 4 - && NumCount == 3 - && pattern.Start == literals[0] - && pattern.DayHourSep == literals[1] - && pattern.HourMinuteSep == literals[2] - && pattern.End == literals[3]; - + internal Boolean FullDHMMatch(TimeSpanFormat.FormatLiterals pattern) + { + return SepCount == 4 + && NumCount == 3 + && pattern.Start == literals[0] + && pattern.DayHourSep == literals[1] + && pattern.HourMinuteSep == literals[2] + && pattern.End == literals[3]; } // HMS (no days or fraction) - internal Boolean FullHMSMatch(TimeSpanFormat.FormatLiterals pattern) { - return SepCount == 4 - && NumCount == 3 - && pattern.Start == literals[0] - && pattern.HourMinuteSep == literals[1] - && pattern.MinuteSecondSep == literals[2] - && pattern.End == literals[3]; + internal Boolean FullHMSMatch(TimeSpanFormat.FormatLiterals pattern) + { + return SepCount == 4 + && NumCount == 3 + && pattern.Start == literals[0] + && pattern.HourMinuteSep == literals[1] + && pattern.MinuteSecondSep == literals[2] + && pattern.End == literals[3]; } // DHMS (no fraction) - internal Boolean FullDHMSMatch(TimeSpanFormat.FormatLiterals pattern) { - return SepCount == 5 - && NumCount == 4 - && pattern.Start == literals[0] - && pattern.DayHourSep == literals[1] - && pattern.HourMinuteSep == literals[2] - && pattern.MinuteSecondSep == literals[3] - && pattern.End == literals[4]; + internal Boolean FullDHMSMatch(TimeSpanFormat.FormatLiterals pattern) + { + return SepCount == 5 + && NumCount == 4 + && pattern.Start == literals[0] + && pattern.DayHourSep == literals[1] + && pattern.HourMinuteSep == literals[2] + && pattern.MinuteSecondSep == literals[3] + && pattern.End == literals[4]; } // HMSF (no days) - internal Boolean FullHMSFMatch(TimeSpanFormat.FormatLiterals pattern) { - return SepCount == 5 - && NumCount == 4 - && pattern.Start == literals[0] - && pattern.HourMinuteSep == literals[1] - && pattern.MinuteSecondSep == literals[2] + internal Boolean FullHMSFMatch(TimeSpanFormat.FormatLiterals pattern) + { + return SepCount == 5 + && NumCount == 4 + && pattern.Start == literals[0] + && pattern.HourMinuteSep == literals[1] + && pattern.MinuteSecondSep == literals[2] && pattern.SecondFractionSep == literals[3] - && pattern.End == literals[4]; + && pattern.End == literals[4]; } internal TTT lastSeenTTT; @@ -374,7 +425,8 @@ namespace System.Globalization { private const int MaxLiteralTokens = 6; private const int MaxNumericTokens = 5; - internal void Init(DateTimeFormatInfo dtfi) { + internal void Init(DateTimeFormatInfo dtfi) + { Debug.Assert(dtfi != null); lastSeenTTT = TTT.None; @@ -383,7 +435,7 @@ namespace System.Globalization { NumCount = 0; literals = new String[MaxLiteralTokens]; - numbers = new TimeSpanToken[MaxNumericTokens]; + numbers = new TimeSpanToken[MaxNumericTokens]; m_fullPosPattern = dtfi.FullTimeSpanPositivePattern; m_fullNegPattern = dtfi.FullTimeSpanNegativePattern; @@ -391,23 +443,28 @@ namespace System.Globalization { m_negLocInit = false; } - internal Boolean ProcessToken(ref TimeSpanToken tok, ref TimeSpanResult result) { - if (tok.ttt == TTT.NumOverflow) { + internal Boolean ProcessToken(ref TimeSpanToken tok, ref TimeSpanResult result) + { + if (tok.ttt == TTT.NumOverflow) + { result.SetFailure(ParseFailureKind.Overflow, "Overflow_TimeSpanElementTooLarge", null); return false; } - if (tok.ttt != TTT.Sep && tok.ttt != TTT.Num) { + if (tok.ttt != TTT.Sep && tok.ttt != TTT.Num) + { // Some unknown token or a repeat token type in the input result.SetFailure(ParseFailureKind.Format, "Format_BadTimeSpan", null); return false; } - switch (tok.ttt) { + switch (tok.ttt) + { case TTT.Sep: if (!AddSep(tok.sep, ref result)) return false; break; case TTT.Num: - if (tokenCount == 0) { + if (tokenCount == 0) + { if (!AddSep(String.Empty, ref result)) return false; } if (!AddNum(tok, ref result)) return false; @@ -421,8 +478,10 @@ namespace System.Globalization { return true; } - private bool AddSep(String sep, ref TimeSpanResult result) { - if (SepCount >= MaxLiteralTokens || tokenCount >= MaxTokens) { + private bool AddSep(String sep, ref TimeSpanResult result) + { + if (SepCount >= MaxLiteralTokens || tokenCount >= MaxTokens) + { result.SetFailure(ParseFailureKind.Format, "Format_BadTimeSpan", null); return false; } @@ -430,19 +489,22 @@ namespace System.Globalization { tokenCount++; return true; } - private bool AddNum(TimeSpanToken num, ref TimeSpanResult result) { - if (NumCount >= MaxNumericTokens || tokenCount >= MaxTokens) { + private bool AddNum(TimeSpanToken num, ref TimeSpanResult result) + { + if (NumCount >= MaxNumericTokens || tokenCount >= MaxTokens) + { result.SetFailure(ParseFailureKind.Format, "Format_BadTimeSpan", null); return false; } - numbers[NumCount++] = num; + numbers[NumCount++] = num; tokenCount++; return true; } } // This will store the result of the parsing. And it will eventually be used to construct a TimeSpan instance. - struct TimeSpanResult { + private struct TimeSpanResult + { internal TimeSpan parsedTimeSpan; internal TimeSpanThrowStyle throwStyle; @@ -451,60 +513,70 @@ namespace System.Globalization { internal object m_failureMessageFormatArgument; internal string m_failureArgumentName; - internal void Init(TimeSpanThrowStyle canThrow) { + internal void Init(TimeSpanThrowStyle canThrow) + { parsedTimeSpan = default(TimeSpan); - throwStyle = canThrow; + throwStyle = canThrow; } - internal void SetFailure(ParseFailureKind failure, string failureMessageID) { + internal void SetFailure(ParseFailureKind failure, string failureMessageID) + { SetFailure(failure, failureMessageID, null, null); } - internal void SetFailure(ParseFailureKind failure, string failureMessageID, object failureMessageFormatArgument) { + internal void SetFailure(ParseFailureKind failure, string failureMessageID, object failureMessageFormatArgument) + { SetFailure(failure, failureMessageID, failureMessageFormatArgument, null); } internal void SetFailure(ParseFailureKind failure, string failureMessageID, object failureMessageFormatArgument, - string failureArgumentName) { + string failureArgumentName) + { m_failure = failure; m_failureMessageID = failureMessageID; m_failureMessageFormatArgument = failureMessageFormatArgument; m_failureArgumentName = failureArgumentName; - if (throwStyle != TimeSpanThrowStyle.None) { + if (throwStyle != TimeSpanThrowStyle.None) + { throw GetTimeSpanParseException(); } } - internal Exception GetTimeSpanParseException() { - switch (m_failure) { - case ParseFailureKind.ArgumentNull: - return new ArgumentNullException(m_failureArgumentName, Environment.GetResourceString(m_failureMessageID)); + internal Exception GetTimeSpanParseException() + { + switch (m_failure) + { + case ParseFailureKind.ArgumentNull: + return new ArgumentNullException(m_failureArgumentName, SR.GetResourceString(m_failureMessageID)); - case ParseFailureKind.FormatWithParameter: - return new FormatException(Environment.GetResourceString(m_failureMessageID, m_failureMessageFormatArgument)); + case ParseFailureKind.FormatWithParameter: + return new FormatException(SR.Format(SR.GetResourceString(m_failureMessageID), m_failureMessageFormatArgument)); - case ParseFailureKind.Format: - return new FormatException(Environment.GetResourceString(m_failureMessageID)); + case ParseFailureKind.Format: + return new FormatException(SR.GetResourceString(m_failureMessageID)); - case ParseFailureKind.Overflow: - return new OverflowException(Environment.GetResourceString(m_failureMessageID)); + case ParseFailureKind.Overflow: + return new OverflowException(SR.GetResourceString(m_failureMessageID)); - default: - Debug.Assert(false, "Unknown TimeSpanParseFailure: " + m_failure); - return new FormatException(Environment.GetResourceString("Format_InvalidString")); + default: + Debug.Assert(false, "Unknown TimeSpanParseFailure: " + m_failure); + return new FormatException(SR.Format_InvalidString); } } } - static bool TryTimeToTicks(bool positive, TimeSpanToken days, TimeSpanToken hours, TimeSpanToken minutes, TimeSpanToken seconds, TimeSpanToken fraction, out long result) { + private static bool TryTimeToTicks(bool positive, TimeSpanToken days, TimeSpanToken hours, TimeSpanToken minutes, TimeSpanToken seconds, TimeSpanToken fraction, out long result) + { if (days.IsInvalidNumber(maxDays, unlimitedDigits) || hours.IsInvalidNumber(maxHours, unlimitedDigits) || minutes.IsInvalidNumber(maxMinutes, unlimitedDigits) || seconds.IsInvalidNumber(maxSeconds, unlimitedDigits) - || fraction.IsInvalidNumber(maxFraction, maxFractionDigits)) { + || fraction.IsInvalidNumber(maxFraction, maxFractionDigits)) + { result = 0; return false; } Int64 ticks = ((Int64)days.num * 3600 * 24 + (Int64)hours.num * 3600 + (Int64)minutes.num * 60 + seconds.num) * 1000; - if (ticks > TimeSpan.MaxMilliSeconds || ticks < TimeSpan.MinMilliSeconds) { + if (ticks > TimeSpan.MaxMilliSeconds || ticks < TimeSpan.MinMilliSeconds) + { result = 0; return false; } @@ -518,18 +590,22 @@ namespace System.Globalization { // ".01" => (1,1) => 100,000 ticks // ".001" => (2,1) => 10,000 ticks long f = fraction.num; - if (f != 0) { + if (f != 0) + { long lowerLimit = TimeSpan.TicksPerTenthSecond; - if (fraction.zeroes > 0) { + if (fraction.zeroes > 0) + { long divisor = (long)Math.Pow(10, fraction.zeroes); lowerLimit = lowerLimit / divisor; } - while (f < lowerLimit) { + while (f < lowerLimit) + { f *= 10; } } result = ((long)ticks * TimeSpan.TicksPerMillisecond) + f; - if (positive && result < 0) { + if (positive && result < 0) + { result = 0; return false; } @@ -544,74 +620,92 @@ namespace System.Globalization { // // Actions: Main methods called from TimeSpan.Parse #region ParseMethods - internal static TimeSpan Parse(String input, IFormatProvider formatProvider) { + internal static TimeSpan Parse(String input, IFormatProvider formatProvider) + { TimeSpanResult parseResult = new TimeSpanResult(); parseResult.Init(TimeSpanThrowStyle.All); - if (TryParseTimeSpan(input, TimeSpanStandardStyles.Any, formatProvider, ref parseResult)) { + if (TryParseTimeSpan(input, TimeSpanStandardStyles.Any, formatProvider, ref parseResult)) + { return parseResult.parsedTimeSpan; } - else { + else + { throw parseResult.GetTimeSpanParseException(); } } - internal static Boolean TryParse(String input, IFormatProvider formatProvider, out TimeSpan result) { + internal static Boolean TryParse(String input, IFormatProvider formatProvider, out TimeSpan result) + { TimeSpanResult parseResult = new TimeSpanResult(); parseResult.Init(TimeSpanThrowStyle.None); - if (TryParseTimeSpan(input, TimeSpanStandardStyles.Any, formatProvider, ref parseResult)) { + if (TryParseTimeSpan(input, TimeSpanStandardStyles.Any, formatProvider, ref parseResult)) + { result = parseResult.parsedTimeSpan; return true; } - else { + else + { result = default(TimeSpan); return false; } } - internal static TimeSpan ParseExact(String input, String format, IFormatProvider formatProvider, TimeSpanStyles styles) { + internal static TimeSpan ParseExact(String input, String format, IFormatProvider formatProvider, TimeSpanStyles styles) + { TimeSpanResult parseResult = new TimeSpanResult(); parseResult.Init(TimeSpanThrowStyle.All); - if (TryParseExactTimeSpan(input, format, formatProvider, styles, ref parseResult)) { + if (TryParseExactTimeSpan(input, format, formatProvider, styles, ref parseResult)) + { return parseResult.parsedTimeSpan; } - else { + else + { throw parseResult.GetTimeSpanParseException(); } } - internal static Boolean TryParseExact(String input, String format, IFormatProvider formatProvider, TimeSpanStyles styles, out TimeSpan result) { + internal static Boolean TryParseExact(String input, String format, IFormatProvider formatProvider, TimeSpanStyles styles, out TimeSpan result) + { TimeSpanResult parseResult = new TimeSpanResult(); parseResult.Init(TimeSpanThrowStyle.None); - if (TryParseExactTimeSpan(input, format, formatProvider, styles, ref parseResult)) { + if (TryParseExactTimeSpan(input, format, formatProvider, styles, ref parseResult)) + { result = parseResult.parsedTimeSpan; return true; } - else { + else + { result = default(TimeSpan); return false; } } - internal static TimeSpan ParseExactMultiple(String input, String[] formats, IFormatProvider formatProvider, TimeSpanStyles styles) { + internal static TimeSpan ParseExactMultiple(String input, String[] formats, IFormatProvider formatProvider, TimeSpanStyles styles) + { TimeSpanResult parseResult = new TimeSpanResult(); parseResult.Init(TimeSpanThrowStyle.All); - if (TryParseExactMultipleTimeSpan(input, formats, formatProvider, styles, ref parseResult)) { + if (TryParseExactMultipleTimeSpan(input, formats, formatProvider, styles, ref parseResult)) + { return parseResult.parsedTimeSpan; } - else { + else + { throw parseResult.GetTimeSpanParseException(); } } - internal static Boolean TryParseExactMultiple(String input, String[] formats, IFormatProvider formatProvider, TimeSpanStyles styles, out TimeSpan result) { + internal static Boolean TryParseExactMultiple(String input, String[] formats, IFormatProvider formatProvider, TimeSpanStyles styles, out TimeSpan result) + { TimeSpanResult parseResult = new TimeSpanResult(); parseResult.Init(TimeSpanThrowStyle.None); - if (TryParseExactMultipleTimeSpan(input, formats, formatProvider, styles, ref parseResult)) { + if (TryParseExactMultipleTimeSpan(input, formats, formatProvider, styles, ref parseResult)) + { result = parseResult.parsedTimeSpan; return true; } - else { + else + { result = default(TimeSpan); return false; } @@ -626,14 +720,17 @@ namespace System.Globalization { // // Actions: Common private Parse method called by both Parse and TryParse // - private static Boolean TryParseTimeSpan(String input, TimeSpanStandardStyles style, IFormatProvider formatProvider, ref TimeSpanResult result) { - if (input == null) { + private static Boolean TryParseTimeSpan(String input, TimeSpanStandardStyles style, IFormatProvider formatProvider, ref TimeSpanResult result) + { + if (input == null) + { result.SetFailure(ParseFailureKind.ArgumentNull, "ArgumentNull_String", null, nameof(input)); return false; } input = input.Trim(); - if (input == String.Empty) { + if (input == String.Empty) + { result.SetFailure(ParseFailureKind.Format, "Format_BadTimeSpan"); return false; } @@ -648,19 +745,23 @@ namespace System.Globalization { /* The following loop will break out when we reach the end of the str or * when we can determine that the input is invalid. */ - while (tok.ttt != TTT.End) { - if (!raw.ProcessToken(ref tok, ref result)) { + while (tok.ttt != TTT.End) + { + if (!raw.ProcessToken(ref tok, ref result)) + { result.SetFailure(ParseFailureKind.Format, "Format_BadTimeSpan"); return false; } tok = tokenizer.GetNextToken(); } - if (!tokenizer.EOL) { + if (!tokenizer.EOL) + { // embedded nulls in the input string result.SetFailure(ParseFailureKind.Format, "Format_BadTimeSpan"); return false; } - if (!ProcessTerminalState(ref raw, style, ref result)) { + if (!ProcessTerminalState(ref raw, style, ref result)) + { result.SetFailure(ParseFailureKind.Format, "Format_BadTimeSpan"); return false; } @@ -685,18 +786,22 @@ namespace System.Globalization { // 3 numbers => h:m:s | d.h:m | h:m:.f // 4 numbers => h:m:s.f | d.h:m:s | d.h:m:.f // 5 numbers => d.h:m:s.f - private static Boolean ProcessTerminalState(ref TimeSpanRawInfo raw, TimeSpanStandardStyles style, ref TimeSpanResult result) { - if (raw.lastSeenTTT == TTT.Num) { + private static Boolean ProcessTerminalState(ref TimeSpanRawInfo raw, TimeSpanStandardStyles style, ref TimeSpanResult result) + { + if (raw.lastSeenTTT == TTT.Num) + { TimeSpanToken tok = new TimeSpanToken(); tok.ttt = TTT.Sep; tok.sep = String.Empty; - if (!raw.ProcessToken(ref tok, ref result)) { + if (!raw.ProcessToken(ref tok, ref result)) + { result.SetFailure(ParseFailureKind.Format, "Format_BadTimeSpan"); return false; } } - switch (raw.NumCount) { + switch (raw.NumCount) + { case 1: return ProcessTerminal_D(ref raw, style, ref result); case 2: @@ -711,7 +816,7 @@ namespace System.Globalization { result.SetFailure(ParseFailureKind.Format, "Format_BadTimeSpan"); return false; } - } + } // // ProcessTerminal_DHMSF @@ -719,8 +824,10 @@ namespace System.Globalization { // Actions: Validate the 5-number "Days.Hours:Minutes:Seconds.Fraction" terminal case. // Sets result.parsedTimeSpan on success. // - private static Boolean ProcessTerminal_DHMSF(ref TimeSpanRawInfo raw, TimeSpanStandardStyles style, ref TimeSpanResult result) { - if (raw.SepCount != 6 || raw.NumCount != 5) { + private static Boolean ProcessTerminal_DHMSF(ref TimeSpanRawInfo raw, TimeSpanStandardStyles style, ref TimeSpanResult result) + { + if (raw.SepCount != 6 || raw.NumCount != 5) + { result.SetFailure(ParseFailureKind.Format, "Format_BadTimeSpan"); return false; } @@ -731,42 +838,52 @@ namespace System.Globalization { bool positive = false; bool match = false; - if (inv) { - if (raw.FullMatch(raw.PositiveInvariant)) { + if (inv) + { + if (raw.FullMatch(raw.PositiveInvariant)) + { match = true; - positive = true; + positive = true; } - if (!match && raw.FullMatch(raw.NegativeInvariant)) { + if (!match && raw.FullMatch(raw.NegativeInvariant)) + { match = true; - positive = false; + positive = false; } } - if (loc) { - if (!match && raw.FullMatch(raw.PositiveLocalized)) { + if (loc) + { + if (!match && raw.FullMatch(raw.PositiveLocalized)) + { match = true; - positive = true; + positive = true; } - if (!match && raw.FullMatch(raw.NegativeLocalized)) { + if (!match && raw.FullMatch(raw.NegativeLocalized)) + { match = true; - positive = false; + positive = false; } } long ticks; - if (match) { - if (!TryTimeToTicks(positive, raw.numbers[0], raw.numbers[1], raw.numbers[2], raw.numbers[3], raw.numbers[4], out ticks)) { + if (match) + { + if (!TryTimeToTicks(positive, raw.numbers[0], raw.numbers[1], raw.numbers[2], raw.numbers[3], raw.numbers[4], out ticks)) + { result.SetFailure(ParseFailureKind.Overflow, "Overflow_TimeSpanElementTooLarge"); return false; - } - if (!positive) { + } + if (!positive) + { ticks = -ticks; - if (ticks > 0) { + if (ticks > 0) + { result.SetFailure(ParseFailureKind.Overflow, "Overflow_TimeSpanElementTooLarge"); return false; } } result.parsedTimeSpan._ticks = ticks; return true; - } + } result.SetFailure(ParseFailureKind.Format, "Format_BadTimeSpan"); return false; @@ -778,8 +895,10 @@ namespace System.Globalization { // Actions: Validate the ambiguous 4-number "Hours:Minutes:Seconds.Fraction", "Days.Hours:Minutes:Seconds", or "Days.Hours:Minutes:.Fraction" terminal case. // Sets result.parsedTimeSpan on success. // - private static Boolean ProcessTerminal_HMS_F_D(ref TimeSpanRawInfo raw, TimeSpanStandardStyles style, ref TimeSpanResult result) { - if (raw.SepCount != 5 || raw.NumCount != 4 || (style & TimeSpanStandardStyles.RequireFull) != 0) { + private static Boolean ProcessTerminal_HMS_F_D(ref TimeSpanRawInfo raw, TimeSpanStandardStyles style, ref TimeSpanResult result) + { + if (raw.SepCount != 5 || raw.NumCount != 4 || (style & TimeSpanStandardStyles.RequireFull) != 0) + { result.SetFailure(ParseFailureKind.Format, "Format_BadTimeSpan"); return false; } @@ -792,75 +911,92 @@ namespace System.Globalization { bool match = false; bool overflow = false; - if (inv) { - if (raw.FullHMSFMatch(raw.PositiveInvariant)) { - positive = true; + if (inv) + { + if (raw.FullHMSFMatch(raw.PositiveInvariant)) + { + positive = true; match = TryTimeToTicks(positive, zero, raw.numbers[0], raw.numbers[1], raw.numbers[2], raw.numbers[3], out ticks); overflow = overflow || !match; } - if (!match && raw.FullDHMSMatch(raw.PositiveInvariant)) { - positive = true; + if (!match && raw.FullDHMSMatch(raw.PositiveInvariant)) + { + positive = true; match = TryTimeToTicks(positive, raw.numbers[0], raw.numbers[1], raw.numbers[2], raw.numbers[3], zero, out ticks); overflow = overflow || !match; } - if (!match && raw.FullAppCompatMatch(raw.PositiveInvariant)) { + if (!match && raw.FullAppCompatMatch(raw.PositiveInvariant)) + { positive = true; match = TryTimeToTicks(positive, raw.numbers[0], raw.numbers[1], raw.numbers[2], zero, raw.numbers[3], out ticks); overflow = overflow || !match; } - if (!match && raw.FullHMSFMatch(raw.NegativeInvariant)) { - positive = false; + if (!match && raw.FullHMSFMatch(raw.NegativeInvariant)) + { + positive = false; match = TryTimeToTicks(positive, zero, raw.numbers[0], raw.numbers[1], raw.numbers[2], raw.numbers[3], out ticks); overflow = overflow || !match; } - if (!match && raw.FullDHMSMatch(raw.NegativeInvariant)) { - positive = false; + if (!match && raw.FullDHMSMatch(raw.NegativeInvariant)) + { + positive = false; match = TryTimeToTicks(positive, raw.numbers[0], raw.numbers[1], raw.numbers[2], raw.numbers[3], zero, out ticks); overflow = overflow || !match; } - if (!match && raw.FullAppCompatMatch(raw.NegativeInvariant)) { + if (!match && raw.FullAppCompatMatch(raw.NegativeInvariant)) + { positive = false; match = TryTimeToTicks(positive, raw.numbers[0], raw.numbers[1], raw.numbers[2], zero, raw.numbers[3], out ticks); overflow = overflow || !match; } } - if (loc) { - if (!match && raw.FullHMSFMatch(raw.PositiveLocalized)) { - positive = true; + if (loc) + { + if (!match && raw.FullHMSFMatch(raw.PositiveLocalized)) + { + positive = true; match = TryTimeToTicks(positive, zero, raw.numbers[0], raw.numbers[1], raw.numbers[2], raw.numbers[3], out ticks); overflow = overflow || !match; } - if (!match && raw.FullDHMSMatch(raw.PositiveLocalized)) { - positive = true; + if (!match && raw.FullDHMSMatch(raw.PositiveLocalized)) + { + positive = true; match = TryTimeToTicks(positive, raw.numbers[0], raw.numbers[1], raw.numbers[2], raw.numbers[3], zero, out ticks); overflow = overflow || !match; } - if (!match && raw.FullAppCompatMatch(raw.PositiveLocalized)) { + if (!match && raw.FullAppCompatMatch(raw.PositiveLocalized)) + { positive = true; match = TryTimeToTicks(positive, raw.numbers[0], raw.numbers[1], raw.numbers[2], zero, raw.numbers[3], out ticks); overflow = overflow || !match; } - if (!match && raw.FullHMSFMatch(raw.NegativeLocalized)) { - positive = false; + if (!match && raw.FullHMSFMatch(raw.NegativeLocalized)) + { + positive = false; match = TryTimeToTicks(positive, zero, raw.numbers[0], raw.numbers[1], raw.numbers[2], raw.numbers[3], out ticks); overflow = overflow || !match; } - if (!match && raw.FullDHMSMatch(raw.NegativeLocalized)) { - positive = false; + if (!match && raw.FullDHMSMatch(raw.NegativeLocalized)) + { + positive = false; match = TryTimeToTicks(positive, raw.numbers[0], raw.numbers[1], raw.numbers[2], raw.numbers[3], zero, out ticks); overflow = overflow || !match; } - if (!match && raw.FullAppCompatMatch(raw.NegativeLocalized)) { + if (!match && raw.FullAppCompatMatch(raw.NegativeLocalized)) + { positive = false; match = TryTimeToTicks(positive, raw.numbers[0], raw.numbers[1], raw.numbers[2], zero, raw.numbers[3], out ticks); overflow = overflow || !match; } } - - if (match) { - if (!positive) { + + if (match) + { + if (!positive) + { ticks = -ticks; - if (ticks > 0) { + if (ticks > 0) + { result.SetFailure(ParseFailureKind.Overflow, "Overflow_TimeSpanElementTooLarge"); return false; } @@ -869,12 +1005,14 @@ namespace System.Globalization { return true; } - if (overflow) { + if (overflow) + { // we found at least one literal pattern match but the numbers just didn't fit result.SetFailure(ParseFailureKind.Overflow, "Overflow_TimeSpanElementTooLarge"); return false; } - else { + else + { // we couldn't find a thing result.SetFailure(ParseFailureKind.Format, "Format_BadTimeSpan"); return false; @@ -886,8 +1024,10 @@ namespace System.Globalization { // // Actions: Validate the ambiguous 3-number "Hours:Minutes:Seconds", "Days.Hours:Minutes", or "Hours:Minutes:.Fraction" terminal case // - private static Boolean ProcessTerminal_HM_S_D(ref TimeSpanRawInfo raw, TimeSpanStandardStyles style, ref TimeSpanResult result) { - if (raw.SepCount != 4 || raw.NumCount != 3 || (style & TimeSpanStandardStyles.RequireFull) != 0) { + private static Boolean ProcessTerminal_HM_S_D(ref TimeSpanRawInfo raw, TimeSpanStandardStyles style, ref TimeSpanResult result) + { + if (raw.SepCount != 4 || raw.NumCount != 3 || (style & TimeSpanStandardStyles.RequireFull) != 0) + { result.SetFailure(ParseFailureKind.Format, "Format_BadTimeSpan"); return false; } @@ -901,89 +1041,108 @@ namespace System.Globalization { long ticks = 0; - if (inv) { - if (raw.FullHMSMatch(raw.PositiveInvariant)) { - positive = true; + if (inv) + { + if (raw.FullHMSMatch(raw.PositiveInvariant)) + { + positive = true; match = TryTimeToTicks(positive, zero, raw.numbers[0], raw.numbers[1], raw.numbers[2], zero, out ticks); overflow = overflow || !match; } - if (!match && raw.FullDHMMatch(raw.PositiveInvariant)) { + if (!match && raw.FullDHMMatch(raw.PositiveInvariant)) + { positive = true; match = TryTimeToTicks(positive, raw.numbers[0], raw.numbers[1], raw.numbers[2], zero, zero, out ticks); overflow = overflow || !match; - } - if (!match && raw.PartialAppCompatMatch(raw.PositiveInvariant)) { + } + if (!match && raw.PartialAppCompatMatch(raw.PositiveInvariant)) + { positive = true; match = TryTimeToTicks(positive, zero, raw.numbers[0], raw.numbers[1], zero, raw.numbers[2], out ticks); overflow = overflow || !match; } - if (!match && raw.FullHMSMatch(raw.NegativeInvariant)) { + if (!match && raw.FullHMSMatch(raw.NegativeInvariant)) + { positive = false; match = TryTimeToTicks(positive, zero, raw.numbers[0], raw.numbers[1], raw.numbers[2], zero, out ticks); overflow = overflow || !match; } - if (!match && raw.FullDHMMatch(raw.NegativeInvariant)) { + if (!match && raw.FullDHMMatch(raw.NegativeInvariant)) + { positive = false; match = TryTimeToTicks(positive, raw.numbers[0], raw.numbers[1], raw.numbers[2], zero, zero, out ticks); overflow = overflow || !match; - } - if (!match && raw.PartialAppCompatMatch(raw.NegativeInvariant)) { + } + if (!match && raw.PartialAppCompatMatch(raw.NegativeInvariant)) + { positive = false; match = TryTimeToTicks(positive, zero, raw.numbers[0], raw.numbers[1], zero, raw.numbers[2], out ticks); overflow = overflow || !match; } } - if (loc) { - if (!match && raw.FullHMSMatch(raw.PositiveLocalized)) { - positive = true; + if (loc) + { + if (!match && raw.FullHMSMatch(raw.PositiveLocalized)) + { + positive = true; match = TryTimeToTicks(positive, zero, raw.numbers[0], raw.numbers[1], raw.numbers[2], zero, out ticks); overflow = overflow || !match; } - if (!match && raw.FullDHMMatch(raw.PositiveLocalized)) { + if (!match && raw.FullDHMMatch(raw.PositiveLocalized)) + { positive = true; match = TryTimeToTicks(positive, raw.numbers[0], raw.numbers[1], raw.numbers[2], zero, zero, out ticks); overflow = overflow || !match; } - if (!match && raw.PartialAppCompatMatch(raw.PositiveLocalized)) { + if (!match && raw.PartialAppCompatMatch(raw.PositiveLocalized)) + { positive = true; match = TryTimeToTicks(positive, zero, raw.numbers[0], raw.numbers[1], zero, raw.numbers[2], out ticks); overflow = overflow || !match; } - if (!match && raw.FullHMSMatch(raw.NegativeLocalized)) { + if (!match && raw.FullHMSMatch(raw.NegativeLocalized)) + { positive = false; match = TryTimeToTicks(positive, zero, raw.numbers[0], raw.numbers[1], raw.numbers[2], zero, out ticks); overflow = overflow || !match; } - if (!match && raw.FullDHMMatch(raw.NegativeLocalized)) { + if (!match && raw.FullDHMMatch(raw.NegativeLocalized)) + { positive = false; match = TryTimeToTicks(positive, raw.numbers[0], raw.numbers[1], raw.numbers[2], zero, zero, out ticks); overflow = overflow || !match; - } - if (!match && raw.PartialAppCompatMatch(raw.NegativeLocalized)) { + } + if (!match && raw.PartialAppCompatMatch(raw.NegativeLocalized)) + { positive = false; match = TryTimeToTicks(positive, zero, raw.numbers[0], raw.numbers[1], zero, raw.numbers[2], out ticks); overflow = overflow || !match; } } - if (match) { - if (!positive) { + if (match) + { + if (!positive) + { ticks = -ticks; - if (ticks > 0) { + if (ticks > 0) + { result.SetFailure(ParseFailureKind.Overflow, "Overflow_TimeSpanElementTooLarge"); return false; } } result.parsedTimeSpan._ticks = ticks; return true; - } + } - if (overflow) { + if (overflow) + { // we found at least one literal pattern match but the numbers just didn't fit result.SetFailure(ParseFailureKind.Overflow, "Overflow_TimeSpanElementTooLarge"); return false; } - else { + else + { // we couldn't find a thing result.SetFailure(ParseFailureKind.Format, "Format_BadTimeSpan"); return false; @@ -995,8 +1154,10 @@ namespace System.Globalization { // // Actions: Validate the 2-number "Hours:Minutes" terminal case // - private static Boolean ProcessTerminal_HM(ref TimeSpanRawInfo raw, TimeSpanStandardStyles style, ref TimeSpanResult result) { - if (raw.SepCount != 3 || raw.NumCount != 2 || (style & TimeSpanStandardStyles.RequireFull) != 0) { + private static Boolean ProcessTerminal_HM(ref TimeSpanRawInfo raw, TimeSpanStandardStyles style, ref TimeSpanResult result) + { + if (raw.SepCount != 3 || raw.NumCount != 2 || (style & TimeSpanStandardStyles.RequireFull) != 0) + { result.SetFailure(ParseFailureKind.Format, "Format_BadTimeSpan"); return false; } @@ -1007,43 +1168,53 @@ namespace System.Globalization { bool positive = false; bool match = false; - if (inv) { - if (raw.FullHMMatch(raw.PositiveInvariant)) { + if (inv) + { + if (raw.FullHMMatch(raw.PositiveInvariant)) + { match = true; - positive = true; + positive = true; } - if (!match && raw.FullHMMatch(raw.NegativeInvariant)) { + if (!match && raw.FullHMMatch(raw.NegativeInvariant)) + { match = true; positive = false; } } - if (loc) { - if (!match && raw.FullHMMatch(raw.PositiveLocalized)) { + if (loc) + { + if (!match && raw.FullHMMatch(raw.PositiveLocalized)) + { match = true; - positive = true; + positive = true; } - if (!match && raw.FullHMMatch(raw.NegativeLocalized)) { + if (!match && raw.FullHMMatch(raw.NegativeLocalized)) + { match = true; positive = false; } } long ticks = 0; - if (match) { - if (!TryTimeToTicks(positive, zero, raw.numbers[0], raw.numbers[1], zero, zero, out ticks)) { + if (match) + { + if (!TryTimeToTicks(positive, zero, raw.numbers[0], raw.numbers[1], zero, zero, out ticks)) + { result.SetFailure(ParseFailureKind.Overflow, "Overflow_TimeSpanElementTooLarge"); return false; } - if (!positive) { + if (!positive) + { ticks = -ticks; - if (ticks > 0) { + if (ticks > 0) + { result.SetFailure(ParseFailureKind.Overflow, "Overflow_TimeSpanElementTooLarge"); return false; } } result.parsedTimeSpan._ticks = ticks; return true; - } + } result.SetFailure(ParseFailureKind.Format, "Format_BadTimeSpan"); return false; @@ -1055,8 +1226,10 @@ namespace System.Globalization { // // Actions: Validate the 1-number "Days" terminal case // - private static Boolean ProcessTerminal_D(ref TimeSpanRawInfo raw, TimeSpanStandardStyles style, ref TimeSpanResult result) { - if (raw.SepCount != 2 || raw.NumCount != 1 || (style & TimeSpanStandardStyles.RequireFull) != 0) { + private static Boolean ProcessTerminal_D(ref TimeSpanRawInfo raw, TimeSpanStandardStyles style, ref TimeSpanResult result) + { + if (raw.SepCount != 2 || raw.NumCount != 1 || (style & TimeSpanStandardStyles.RequireFull) != 0) + { result.SetFailure(ParseFailureKind.Format, "Format_BadTimeSpan"); return false; } @@ -1067,43 +1240,53 @@ namespace System.Globalization { bool positive = false; bool match = false; - if (inv) { - if (raw.FullDMatch(raw.PositiveInvariant)) { + if (inv) + { + if (raw.FullDMatch(raw.PositiveInvariant)) + { match = true; - positive = true; + positive = true; } - if (!match && raw.FullDMatch(raw.NegativeInvariant)) { + if (!match && raw.FullDMatch(raw.NegativeInvariant)) + { match = true; positive = false; } } - if (loc) { - if (!match && raw.FullDMatch(raw.PositiveLocalized)) { + if (loc) + { + if (!match && raw.FullDMatch(raw.PositiveLocalized)) + { match = true; - positive = true; + positive = true; } - if (!match && raw.FullDMatch(raw.NegativeLocalized)) { + if (!match && raw.FullDMatch(raw.NegativeLocalized)) + { match = true; positive = false; } } long ticks = 0; - if (match) { - if (!TryTimeToTicks(positive, raw.numbers[0], zero, zero, zero, zero, out ticks)) { + if (match) + { + if (!TryTimeToTicks(positive, raw.numbers[0], zero, zero, zero, zero, out ticks)) + { result.SetFailure(ParseFailureKind.Overflow, "Overflow_TimeSpanElementTooLarge"); return false; } - if (!positive) { + if (!positive) + { ticks = -ticks; - if (ticks > 0) { + if (ticks > 0) + { result.SetFailure(ParseFailureKind.Overflow, "Overflow_TimeSpanElementTooLarge"); return false; } } result.parsedTimeSpan._ticks = ticks; return true; - } + } result.SetFailure(ParseFailureKind.Format, "Format_BadTimeSpan"); return false; @@ -1116,40 +1299,49 @@ namespace System.Globalization { // // Actions: Common private ParseExact method called by both ParseExact and TryParseExact // - private static Boolean TryParseExactTimeSpan(String input, String format, IFormatProvider formatProvider, TimeSpanStyles styles, ref TimeSpanResult result) { - if (input == null) { + private static Boolean TryParseExactTimeSpan(String input, String format, IFormatProvider formatProvider, TimeSpanStyles styles, ref TimeSpanResult result) + { + if (input == null) + { result.SetFailure(ParseFailureKind.ArgumentNull, "ArgumentNull_String", null, nameof(input)); return false; } - if (format == null) { + if (format == null) + { result.SetFailure(ParseFailureKind.ArgumentNull, "ArgumentNull_String", null, nameof(format)); return false; } - if (format.Length == 0) { + if (format.Length == 0) + { result.SetFailure(ParseFailureKind.Format, "Format_BadFormatSpecifier"); return false; } - if (format.Length == 1) { + if (format.Length == 1) + { TimeSpanStandardStyles style = TimeSpanStandardStyles.None; - if (format[0] == 'c' || format[0] == 't' || format[0] == 'T') { + if (format[0] == 'c' || format[0] == 't' || format[0] == 'T') + { // fast path for legacy style TimeSpan formats. return TryParseTimeSpanConstant(input, ref result); } - else if (format[0] == 'g') { + else if (format[0] == 'g') + { style = TimeSpanStandardStyles.Localized; } - else if (format[0] == 'G') { + else if (format[0] == 'G') + { style = TimeSpanStandardStyles.Localized | TimeSpanStandardStyles.RequireFull; } - else { + else + { result.SetFailure(ParseFailureKind.Format, "Format_BadFormatSpecifier"); return false; } return TryParseTimeSpan(input, style, formatProvider, ref result); } - + return TryParseByFormat(input, format, styles, ref result); } @@ -1158,7 +1350,8 @@ namespace System.Globalization { // // Actions: Parse the TimeSpan instance using the specified format. Used by TryParseExactTimeSpan. // - private static Boolean TryParseByFormat(String input, String format, TimeSpanStyles styles, ref TimeSpanResult result) { + private static Boolean TryParseByFormat(String input, String format, TimeSpanStyles styles, ref TimeSpanResult result) + { Debug.Assert(input != null, "input != null"); Debug.Assert(format != null, "format != null"); @@ -1179,13 +1372,16 @@ namespace System.Globalization { TimeSpanTokenizer tokenizer = new TimeSpanTokenizer(); tokenizer.Init(input, -1); - while (i < format.Length) { + while (i < format.Length) + { char ch = format[i]; int nextFormatChar; - switch (ch) { + switch (ch) + { case 'h': tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch); - if (tokenLen > 2 || seenHH || !ParseExactDigits(ref tokenizer, tokenLen, out hh)) { + if (tokenLen > 2 || seenHH || !ParseExactDigits(ref tokenizer, tokenLen, out hh)) + { result.SetFailure(ParseFailureKind.Format, "Format_InvalidString"); return false; } @@ -1193,7 +1389,8 @@ namespace System.Globalization { break; case 'm': tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch); - if (tokenLen > 2 || seenMM || !ParseExactDigits(ref tokenizer, tokenLen, out mm)) { + if (tokenLen > 2 || seenMM || !ParseExactDigits(ref tokenizer, tokenLen, out mm)) + { result.SetFailure(ParseFailureKind.Format, "Format_InvalidString"); return false; } @@ -1201,7 +1398,8 @@ namespace System.Globalization { break; case 's': tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch); - if (tokenLen > 2 || seenSS || !ParseExactDigits(ref tokenizer, tokenLen, out ss)) { + if (tokenLen > 2 || seenSS || !ParseExactDigits(ref tokenizer, tokenLen, out ss)) + { result.SetFailure(ParseFailureKind.Format, "Format_InvalidString"); return false; } @@ -1209,7 +1407,8 @@ namespace System.Globalization { break; case 'f': tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch); - if (tokenLen > DateTimeFormat.MaxSecondsFractionDigits || seenFF || !ParseExactDigits(ref tokenizer, tokenLen, tokenLen, out leadingZeroes, out ff)) { + if (tokenLen > DateTimeFormat.MaxSecondsFractionDigits || seenFF || !ParseExactDigits(ref tokenizer, tokenLen, tokenLen, out leadingZeroes, out ff)) + { result.SetFailure(ParseFailureKind.Format, "Format_InvalidString"); return false; } @@ -1217,7 +1416,8 @@ namespace System.Globalization { break; case 'F': tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch); - if (tokenLen > DateTimeFormat.MaxSecondsFractionDigits || seenFF) { + if (tokenLen > DateTimeFormat.MaxSecondsFractionDigits || seenFF) + { result.SetFailure(ParseFailureKind.Format, "Format_InvalidString"); return false; } @@ -1227,7 +1427,8 @@ namespace System.Globalization { case 'd': tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch); int tmp = 0; - if (tokenLen > 8 || seenDD || !ParseExactDigits(ref tokenizer, (tokenLen<2) ? 1 : tokenLen, (tokenLen<2) ? 8 : tokenLen, out tmp, out dd)) { + if (tokenLen > 8 || seenDD || !ParseExactDigits(ref tokenizer, (tokenLen < 2) ? 1 : tokenLen, (tokenLen < 2) ? 8 : tokenLen, out tmp, out dd)) + { result.SetFailure(ParseFailureKind.Format, "Format_InvalidString"); return false; } @@ -1236,11 +1437,13 @@ namespace System.Globalization { case '\'': case '\"': StringBuilder enquotedString = new StringBuilder(); - if (!DateTimeParse.TryParseQuoteString(format, i, enquotedString, out tokenLen)) { + if (!DateTimeParse.TryParseQuoteString(format, i, enquotedString, out tokenLen)) + { result.SetFailure(ParseFailureKind.FormatWithParameter, "Format_BadQuote", ch); return false; } - if (!ParseExactLiteral(ref tokenizer, enquotedString)) { + if (!ParseExactLiteral(ref tokenizer, enquotedString)) + { result.SetFailure(ParseFailureKind.Format, "Format_InvalidString"); return false; } @@ -1252,11 +1455,13 @@ namespace System.Globalization { nextFormatChar = DateTimeFormat.ParseNextChar(format, i); // nextFormatChar will be -1 if we already reach the end of the format string. // Besides, we will not allow "%%" appear in the pattern. - if (nextFormatChar >= 0 && nextFormatChar != (int)'%') { + if (nextFormatChar >= 0 && nextFormatChar != (int)'%') + { tokenLen = 1; // skip the '%' and process the format character break; } - else { + else + { // This means that '%' is at the end of the format string or // "%%" appears in the format string. result.SetFailure(ParseFailureKind.Format, "Format_InvalidString"); @@ -1267,10 +1472,12 @@ namespace System.Globalization { // For example, "\d" will insert the character 'd' into the string. // nextFormatChar = DateTimeFormat.ParseNextChar(format, i); - if (nextFormatChar >= 0 && tokenizer.NextChar == (char)nextFormatChar) { + if (nextFormatChar >= 0 && tokenizer.NextChar == (char)nextFormatChar) + { tokenLen = 2; - } - else { + } + else + { // This means that '\' is at the end of the format string or the literal match failed. result.SetFailure(ParseFailureKind.Format, "Format_InvalidString"); return false; @@ -1284,12 +1491,13 @@ namespace System.Globalization { } - if (!tokenizer.EOL) { + if (!tokenizer.EOL) + { // the custom format didn't consume the entire input result.SetFailure(ParseFailureKind.Format, "Format_BadTimeSpan"); return false; } - + long ticks = 0; bool positive = (styles & TimeSpanStyles.AssumeNegative) == 0; if (TryTimeToTicks(positive, new TimeSpanToken(dd), @@ -1297,33 +1505,38 @@ namespace System.Globalization { new TimeSpanToken(mm), new TimeSpanToken(ss), new TimeSpanToken(leadingZeroes, ff), - out ticks)) { + out ticks)) + { if (!positive) ticks = -ticks; result.parsedTimeSpan._ticks = ticks; return true; } - else { + else + { result.SetFailure(ParseFailureKind.Overflow, "Overflow_TimeSpanElementTooLarge"); return false; - } } - private static Boolean ParseExactDigits(ref TimeSpanTokenizer tokenizer, int minDigitLength, out int result) { + private static Boolean ParseExactDigits(ref TimeSpanTokenizer tokenizer, int minDigitLength, out int result) + { result = 0; int zeroes = 0; int maxDigitLength = (minDigitLength == 1) ? 2 : minDigitLength; return ParseExactDigits(ref tokenizer, minDigitLength, maxDigitLength, out zeroes, out result); } - private static Boolean ParseExactDigits(ref TimeSpanTokenizer tokenizer, int minDigitLength, int maxDigitLength, out int zeroes, out int result) { + private static Boolean ParseExactDigits(ref TimeSpanTokenizer tokenizer, int minDigitLength, int maxDigitLength, out int zeroes, out int result) + { result = 0; zeroes = 0; int tokenLength = 0; - while (tokenLength < maxDigitLength) { + while (tokenLength < maxDigitLength) + { char ch = tokenizer.NextChar; - if (ch < '0' || ch > '9') { - tokenizer.BackOne(); + if (ch < '0' || ch > '9') + { + tokenizer.BackOne(); break; } result = result * 10 + (ch - '0'); @@ -1332,8 +1545,10 @@ namespace System.Globalization { } return (tokenLength >= minDigitLength); } - private static Boolean ParseExactLiteral(ref TimeSpanTokenizer tokenizer, StringBuilder enquotedString) { - for (int i = 0; i < enquotedString.Length; i++) { + private static Boolean ParseExactLiteral(ref TimeSpanTokenizer tokenizer, StringBuilder enquotedString) + { + for (int i = 0; i < enquotedString.Length; i++) + { if (enquotedString[i] != tokenizer.NextChar) return false; } @@ -1348,35 +1563,42 @@ namespace System.Globalization { // Actions: Parses the "c" (constant) format. This code is 100% identical to the non-globalized v1.0-v3.5 TimeSpan.Parse() routine // and exists for performance/appcompat with legacy callers who cannot move onto the globalized Parse overloads. // - private static Boolean TryParseTimeSpanConstant(String input, ref TimeSpanResult result) { + private static Boolean TryParseTimeSpanConstant(String input, ref TimeSpanResult result) + { return (new StringParser().TryParse(input, ref result)); } - private struct StringParser { + private struct StringParser + { private String str; private char ch; private int pos; private int len; - internal void NextChar() { + internal void NextChar() + { if (pos < len) pos++; - ch = pos < len? str[pos]: (char) 0; + ch = pos < len ? str[pos] : (char)0; } - internal char NextNonDigit() { + internal char NextNonDigit() + { int i = pos; - while (i < len) { + while (i < len) + { char ch = str[i]; if (ch < '0' || ch > '9') return ch; i++; } - return (char) 0; + return (char)0; } - - internal bool TryParse(String input, ref TimeSpanResult result) { + + internal bool TryParse(String input, ref TimeSpanResult result) + { result.parsedTimeSpan._ticks = 0; - if (input == null) { + if (input == null) + { result.SetFailure(ParseFailureKind.ArgumentNull, "ArgumentNull_String", null, nameof(input)); return false; } @@ -1386,109 +1608,136 @@ namespace System.Globalization { NextChar(); SkipBlanks(); bool negative = false; - if (ch == '-') { + if (ch == '-') + { negative = true; NextChar(); } long time; - if (NextNonDigit() == ':') { - if (!ParseTime(out time, ref result)) { + if (NextNonDigit() == ':') + { + if (!ParseTime(out time, ref result)) + { return false; }; } - else { + else + { int days; - if (!ParseInt((int)(0x7FFFFFFFFFFFFFFFL / TimeSpan.TicksPerDay), out days, ref result)) { + if (!ParseInt((int)(0x7FFFFFFFFFFFFFFFL / TimeSpan.TicksPerDay), out days, ref result)) + { return false; } time = days * TimeSpan.TicksPerDay; - if (ch == '.') { + if (ch == '.') + { NextChar(); long remainingTime; - if (!ParseTime(out remainingTime, ref result)) { + if (!ParseTime(out remainingTime, ref result)) + { return false; }; time += remainingTime; } } - if (negative) { + if (negative) + { time = -time; // Allow -0 as well - if (time > 0) { + if (time > 0) + { result.SetFailure(ParseFailureKind.Overflow, "Overflow_TimeSpanElementTooLarge"); - return false; + return false; } } - else { - if (time < 0) { + else + { + if (time < 0) + { result.SetFailure(ParseFailureKind.Overflow, "Overflow_TimeSpanElementTooLarge"); - return false; + return false; } } SkipBlanks(); - if (pos < len) { + if (pos < len) + { result.SetFailure(ParseFailureKind.Format, "Format_BadTimeSpan"); - return false; + return false; } result.parsedTimeSpan._ticks = time; return true; } - internal bool ParseInt(int max, out int i, ref TimeSpanResult result) { + internal bool ParseInt(int max, out int i, ref TimeSpanResult result) + { i = 0; int p = pos; - while (ch >= '0' && ch <= '9') { - if ((i & 0xF0000000) != 0) { + while (ch >= '0' && ch <= '9') + { + if ((i & 0xF0000000) != 0) + { result.SetFailure(ParseFailureKind.Overflow, "Overflow_TimeSpanElementTooLarge"); return false; } i = i * 10 + ch - '0'; - if (i < 0) { + if (i < 0) + { result.SetFailure(ParseFailureKind.Overflow, "Overflow_TimeSpanElementTooLarge"); return false; } NextChar(); } - if (p == pos) { + if (p == pos) + { result.SetFailure(ParseFailureKind.Format, "Format_BadTimeSpan"); return false; } - if (i > max) { + if (i > max) + { result.SetFailure(ParseFailureKind.Overflow, "Overflow_TimeSpanElementTooLarge"); return false; } return true; } - internal bool ParseTime(out long time, ref TimeSpanResult result) { + internal bool ParseTime(out long time, ref TimeSpanResult result) + { time = 0; int unit; - if (!ParseInt(23, out unit, ref result)) { + if (!ParseInt(23, out unit, ref result)) + { return false; } time = unit * TimeSpan.TicksPerHour; - if (ch != ':') { + if (ch != ':') + { result.SetFailure(ParseFailureKind.Format, "Format_BadTimeSpan"); - return false; + return false; } - NextChar(); - if (!ParseInt(59, out unit, ref result)) { + NextChar(); + if (!ParseInt(59, out unit, ref result)) + { return false; - } + } time += unit * TimeSpan.TicksPerMinute; - if (ch == ':') { + if (ch == ':') + { NextChar(); // allow seconds with the leading zero - if (ch != '.') { - if (!ParseInt(59, out unit, ref result)) { + if (ch != '.') + { + if (!ParseInt(59, out unit, ref result)) + { return false; - } + } time += unit * TimeSpan.TicksPerSecond; } - if (ch == '.') { + if (ch == '.') + { NextChar(); int f = (int)TimeSpan.TicksPerSecond; - while (f > 1 && ch >= '0' && ch <= '9') { + while (f > 1 && ch >= '0' && ch <= '9') + { f /= 10; time += (ch - '0') * f; NextChar(); @@ -1498,10 +1747,11 @@ namespace System.Globalization { return true; } - internal void SkipBlanks() { + internal void SkipBlanks() + { while (ch == ' ' || ch == '\t') NextChar(); } - } + } #endregion #region TryParseExactMultipleTimeSpan @@ -1510,22 +1760,27 @@ namespace System.Globalization { // // Actions: Common private ParseExactMultiple method called by both ParseExactMultiple and TryParseExactMultiple // - private static Boolean TryParseExactMultipleTimeSpan(String input, String[] formats, IFormatProvider formatProvider, TimeSpanStyles styles, ref TimeSpanResult result) { - if (input == null) { + private static Boolean TryParseExactMultipleTimeSpan(String input, String[] formats, IFormatProvider formatProvider, TimeSpanStyles styles, ref TimeSpanResult result) + { + if (input == null) + { result.SetFailure(ParseFailureKind.ArgumentNull, "ArgumentNull_String", null, nameof(input)); return false; } - if (formats == null) { + if (formats == null) + { result.SetFailure(ParseFailureKind.ArgumentNull, "ArgumentNull_String", null, nameof(formats)); return false; } - if (input.Length == 0) { + if (input.Length == 0) + { result.SetFailure(ParseFailureKind.Format, "Format_BadTimeSpan"); return false; } - if (formats.Length == 0) { + if (formats.Length == 0) + { result.SetFailure(ParseFailureKind.Format, "Format_BadFormatSpecifier"); return false; } @@ -1534,8 +1789,10 @@ namespace System.Globalization { // Do a loop through the provided formats and see if we can parse succesfully in // one of the formats. // - for (int i = 0; i < formats.Length; i++) { - if (formats[i] == null || formats[i].Length == 0) { + for (int i = 0; i < formats.Length; i++) + { + if (formats[i] == null || formats[i].Length == 0) + { result.SetFailure(ParseFailureKind.Format, "Format_BadFormatSpecifier"); return false; } @@ -1544,7 +1801,8 @@ namespace System.Globalization { TimeSpanResult innerResult = new TimeSpanResult(); innerResult.Init(TimeSpanThrowStyle.None); - if(TryParseExactTimeSpan(input, formats[i], formatProvider, styles, ref innerResult)) { + if (TryParseExactTimeSpan(input, formats[i], formatProvider, styles, ref innerResult)) + { result.parsedTimeSpan = innerResult.parsedTimeSpan; return true; } |