BitOrchestra.Parser.AcceptInteger C# (CSharp) Method

AcceptInteger() public static method

Tries parsing an integer value given in binary, decimal or hexadecimal.
public static AcceptInteger ( string Text, int &Index, int &Value, int &ErrorIndex ) : bool
Text string
Index int
Value int
ErrorIndex int
return bool
        public static bool AcceptInteger(string Text, ref int Index, ref int Value, out int ErrorIndex)
        {
            ErrorIndex = Index;
            int cur = Index;

            // Determine base, if any
            int b = 10;
            if (Text.Length - Index >= 2)
            {
                cur += 2;
                switch (Text.Substring(Index, 2))
                {
                    case "0d":
                        b = 10;
                        break;
                    case "0b":
                        b = 2;
                        break;
                    case "0h":
                    case "0x":
                        b = 16;
                        break;
                    default:
                        cur -= 2;
                        break;
                }
            }

            // Find digits
            List<int> digs = new List<int>();
            bool found = false;
            while (cur < Text.Length)
            {
                int dig = 0;
                if (IsHexadecimalDigit(Text[cur], ref dig) && dig < b)
                {
                    found = true;
                    digs.Add(dig);
                    cur++;
                    continue;
                }
                else
                {
                    ErrorIndex = cur;
                    break;
                }
            }

            if (found)
            {
                Value = 0;
                int r = 1;
                for (int t = digs.Count - 1; t >= 0; t--)
                {
                    Value += digs[t] * r;
                    r *= b;
                }

                Index = cur;
                return true;
            }
            return false;
        }