private static int StringToInt(string str)
{
var neg = false;
var i = 0;
if (str.Length > 0 && str[0] == '-') {
neg = true;
++i;
}
if (i == str.Length) {
throw new FormatException();
}
var ret = 0;
while (i < str.Length) {
int c = str[i];
++i;
if (c >= '0' && c <= '9') {
int x = c - '0';
if (ret > 214748364) {
throw new FormatException();
}
ret *= 10;
if (ret == 2147483640) {
if (neg && x == 8) {
if (i != str.Length) {
throw new FormatException();
}
return Int32.MinValue;
}
if (x > 7) {
throw new FormatException();
}
}
ret += x;
} else {
throw new FormatException();
}
}
return neg ? -ret : ret;
}