public unsafe static int StringToInt (string value, int fromBase, int flags, int* parsePos) { if ((flags & (IsTight | NoSpace)) == 0) throw new NotImplementedException (flags.ToString ()); if (value == null) return 0; int chars = 0; uint result = 0; int digitValue; int len = value.Length; bool negative = false; if (len == 0) { // Mimic broken .net behaviour throw new ArgumentOutOfRangeException ("Empty string"); } int i = parsePos == null ? 0 : *parsePos; //Check for a sign if (value [i] == '-') { if (fromBase != 10) throw new ArgumentException ("String cannot contain a minus sign if the base is not 10."); if ((flags & TreatAsUnsigned) != 0) throw new OverflowException ("Negative number"); negative = true; i++; } else if (value [i] == '+') { i++; } if (fromBase == 16 && i + 1 < len && value [i] =='0' && (value [i + 1] == 'x' || value [i + 1] == 'X')) { i += 2; } uint max_value; if ((flags & TreatAsI1) != 0) { max_value = Byte.MaxValue; } else if ((flags & TreatAsI2) != 0) { max_value = UInt16.MaxValue; } else { max_value = UInt32.MaxValue; } while (i < len) { char c = value [i]; if (Char.IsNumber (c)) { digitValue = c - '0'; } else if (Char.IsLetter (c)) { digitValue = Char.ToLowerInvariant (c) - 'a' + 10; } else { if (i == 0) throw new FormatException ("Could not find any parsable digits."); if ((flags & IsTight) != 0) throw new FormatException ("Additional unparsable characters are at the end of the string."); break; } if (digitValue >= fromBase) { if (chars > 0) { throw new FormatException ("Additional unparsable characters are at the end of the string."); } throw new FormatException ("Could not find any parsable digits."); } var res = (uint) fromBase * result + (uint) digitValue; if (res < result || res > max_value) throw new OverflowException (); result = res; chars++; ++i; } if (chars == 0) throw new FormatException ("Could not find any parsable digits."); if (parsePos != null) *parsePos = i; return negative ? -(int)result : (int)result; }
public unsafe static int StringToInt(string s, int radix, int flags, ref int currPos) { fixed(int *ptr = &currPos) { return(ParseNumbers.StringToInt(s, radix, flags, ptr)); } }