return t;
}
+ enum ParseError {
+ None,
+ Format,
+ Overflow
+ }
+
// Class Parser implements parser for TimeSpan.Parse
private class Parser
{
private string _src;
private int _cur = 0;
private int _length;
- private bool formatError;
- private bool overflowError;
+ ParseError parse_error;
public Parser (string src)
{
while (!AtEnd && Char.IsDigit (_src, _cur)) {
res = res * 10 + _src[_cur] - '0';
if (res > Int32.MaxValue) {
- overflowError = true;
+ SetParseError (ParseError.Overflow);
break;
}
_cur++;
}
if (!optional && (count == 0))
- formatError = true;
+ SetParseError (ParseError.Format);
return (int)res;
}
if (_src[_cur] == ':')
_cur++;
else if (!optional)
- formatError = true;
+ SetParseError (ParseError.Format);
}
}
}
if (!digitseen)
- formatError = true;
+ SetParseError (ParseError.Format);
return res;
}
+ void SetParseError (ParseError error)
+ {
+ // We preserve the very first error.
+ if (parse_error != ParseError.None)
+ return;
+
+ parse_error = error;
+ }
+
+ bool CheckParseSuccess (int hours, int minutes, int seconds, bool tryParse)
+ {
+ // FormatException has precedence over OverflowException starting with 4.0
+ // so put the block before/after properly.
+#if NET_4_0
+ if (parse_error == ParseError.Format) {
+ if (tryParse)
+ return false;
+ throw new FormatException (
+ Locale.GetText ("Invalid format for TimeSpan.Parse."));
+ }
+#endif
+ if (parse_error == ParseError.Overflow || hours > 23 || minutes > 59 || seconds > 59) {
+ if (tryParse)
+ return false;
+ throw new OverflowException (
+ Locale.GetText ("Invalid time data."));
+ }
+#if !NET_4_0
+ // Respect the Overflow precedence for 2.0, putting the format check last.
+ if (parse_error == ParseError.Format) {
+ if (tryParse)
+ return false;
+ throw new FormatException (
+ Locale.GetText ("Invalid format for TimeSpan.Parse."));
+ }
+#endif
+
+ return true;
+ }
+
public bool Execute (bool tryParse, out TimeSpan result)
{
bool sign;
ParseWhiteSpace ();
if (!AtEnd)
- formatError = true;
+ SetParseError (ParseError.Format);
- // Overflow has presceance over FormatException
- if (overflowError || hours > 23 || minutes > 59 || seconds > 59) {
- if (tryParse)
- return false;
- throw new OverflowException (
- Locale.GetText ("Invalid time data."));
- }
- else if (formatError) {
- if (tryParse)
- return false;
- throw new FormatException (
- Locale.GetText ("Invalid format for TimeSpan.Parse."));
- }
+ if (!CheckParseSuccess (hours, minutes, seconds, tryParse))
+ return false;
long t;
if (!TimeSpan.CalculateTicks (days, hours, minutes, seconds, 0, false, out t))