- int i = s.IndexOfAny(new char [] {'e', 'E'}, pos);
- if (i < 0)
- return false;
- if (++i == s.Length)
- return false;
- if (s [i] == '+' || s [i] == '-')
- if (++i == s.Length)
- return false;
- if (!Char.IsDigit (s [i]))
- return false;
- for (; i < s.Length; ++i)
- if (!Char.IsDigit (s [i]))
- break;
+ exponent = 0;
+
+ if (pos >= s.Length || (s [pos] != 'e' && s[pos] != 'E')) {
+ exc = null;
+ return false;
+ }
+
+ var i = pos + 1;
+ if (i == s.Length) {
+ exc = tryParse ? null : GetFormatException ();
+ return true;
+ }
+
+ // negative exponent not valid for Int32
+ if (s [i] == '-') {
+ exc = tryParse ? null : new OverflowException ("Value too large or too small.");
+ return true;
+ }
+
+ if (s [i] == '+' && ++i == s.Length) {
+ exc = tryParse ? null : GetFormatException ();
+ return true;
+ }
+
+ long exp = 0; // temp long value
+ for (; i < s.Length; i++) {
+ if (!Char.IsDigit (s [i])) {
+ exc = tryParse ? null : GetFormatException ();
+ return true;
+ }
+
+ // Reduce the risk of throwing an overflow exc
+ exp = checked (exp * 10 - (int) (s [i] - '0'));
+ if (exp < Int32.MinValue || exp > Int32.MaxValue) {
+ exc = tryParse ? null : new OverflowException ("Value too large or too small.");
+ return true;
+ }
+ }
+
+ // exp value saved as negative
+ exp = -exp;
+
+ exc = null;
+ exponent = (int)exp;