System.Xml.XmlConverter.TryParseSingle C# (CSharp) Méthode

TryParseSingle() private static méthode

private static TryParseSingle ( byte chars, int offset, int count, float &result ) : bool
chars byte
offset int
count int
result float
Résultat bool
        private static bool TryParseSingle(byte[] chars, int offset, int count, out float result)
        {
            result = 0;
            int offsetMax = offset + count;
            bool negative = false;
            if (offset < offsetMax && chars[offset] == '-')
            {
                negative = true;
                offset++;
                count--;
            }
            if (count < 1 || count > 10)
                return false;
            int value = 0;
            int ch;
            while (offset < offsetMax)
            {
                ch = (chars[offset] - '0');
                if (ch == ('.' - '0'))
                {
                    offset++;
                    int pow10 = 1;
                    while (offset < offsetMax)
                    {
                        ch = chars[offset] - '0';
                        if (((uint)ch) >= 10)
                            return false;
                        pow10 *= 10;
                        value = value * 10 + ch;
                        offset++;
                    }
                    // More than 8 characters (7 sig figs and a decimal) and int -> float conversion is lossy, so use double
                    if (count > 8)
                    {
                        result = (float)((double)value / (double)pow10);
                    }
                    else
                    {
                        result = (float)value / (float)pow10;
                    }
                    if (negative)
                        result = -result;
                    return true;
                }
                else if (((uint)ch) >= 10)
                    return false;
                value = value * 10 + ch;
                offset++;
            }
            // Ten digits w/out a decimal point might have overflowed the int
            if (count == 10)
                return false;
            if (negative)
                result = -value;
            else
                result = value;
            return true;
        }