Ejemplo n.º 1
0
        internal static unsafe double ParseDouble(string value, NumberStyles options, NumberFormatInfo numfmt)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            byte *stackBuffer = stackalloc byte[Number.NumberBuffer.NumberBufferBytes];

            Number.NumberBuffer number = new Number.NumberBuffer(stackBuffer);
            double num = 0.0;

            if (!Number.TryStringToNumber(value, options, ref number, numfmt, false))
            {
                string str = value.Trim();
                if (str.Equals(numfmt.PositiveInfinitySymbol))
                {
                    return(double.PositiveInfinity);
                }
                if (str.Equals(numfmt.NegativeInfinitySymbol))
                {
                    return(double.NegativeInfinity);
                }
                if (str.Equals(numfmt.NaNSymbol))
                {
                    return(double.NaN);
                }
                throw new FormatException(Environment.GetResourceString("Format_InvalidString"));
            }
            if (!Number.NumberBufferToDouble(number.PackForNative(), ref num))
            {
                throw new OverflowException(Environment.GetResourceString("Overflow_Double"));
            }
            return(num);
        }
 public static unsafe bool NumberBufferToDouble(ref Number.NumberBuffer number, ref double value)
 {
     fixed(Number.NumberBuffer *numberPtr = &number)
     {
         return(NumberBufferToDouble((byte *)numberPtr, ref value));
     }
 }
Ejemplo n.º 3
0
        internal unsafe static bool TryParseSingle(string value, NumberStyles options, NumberFormatInfo numfmt, out float result)
        {
            byte *stackBuffer = stackalloc byte[checked (unchecked ((UIntPtr)Number.NumberBuffer.NumberBufferBytes) * 1)];

            Number.NumberBuffer numberBuffer = new Number.NumberBuffer(stackBuffer);
            result = 0f;
            double num = 0.0;

            if (!Number.TryStringToNumber(value, options, ref numberBuffer, numfmt, false))
            {
                return(false);
            }
            if (!Number.NumberBufferToDouble(numberBuffer.PackForNative(), ref num))
            {
                return(false);
            }
            float num2 = (float)num;

            if (float.IsInfinity(num2))
            {
                return(false);
            }
            result = num2;
            return(true);
        }
Ejemplo n.º 4
0
        private static unsafe bool NumberToUInt64(ref Number.NumberBuffer number, ref ulong value)
        {
            int num1 = number.scale;

            if (num1 > 20 || num1 < number.precision || number.sign)
            {
                return(false);
            }
            char *chPtr = number.digits;
            ulong num2  = 0;

            while (--num1 >= 0)
            {
                if (num2 > 1844674407370955161UL)
                {
                    return(false);
                }
                num2 *= 10UL;
                if ((int)*chPtr != 0)
                {
                    ulong num3 = num2 + (ulong)((int)*chPtr++ - 48);
                    if (num3 < num2)
                    {
                        return(false);
                    }
                    num2 = num3;
                }
            }
            value = num2;
            return(true);
        }
Ejemplo n.º 5
0
        private static unsafe bool NumberToUInt32(ref Number.NumberBuffer number, ref uint value)
        {
            int num1 = number.scale;

            if (num1 > 10 || num1 < number.precision || number.sign)
            {
                return(false);
            }
            char *chPtr = number.digits;
            uint  num2  = 0;

            while (--num1 >= 0)
            {
                if (num2 > 429496729U)
                {
                    return(false);
                }
                num2 *= 10U;
                if ((int)*chPtr != 0)
                {
                    uint num3 = num2 + ((uint)*chPtr++ - 48U);
                    if (num3 < num2)
                    {
                        return(false);
                    }
                    num2 = num3;
                }
            }
            value = num2;
            return(true);
        }
Ejemplo n.º 6
0
        private unsafe static bool NumberToUInt64(ref Number.NumberBuffer number, ref ulong value)
        {
            int num = number.scale;

            if (num > 20 || num < number.precision || number.sign)
            {
                return(false);
            }
            char *digits = number.digits;
            ulong num2   = 0UL;

            while (--num >= 0)
            {
                if (num2 > 1844674407370955161UL)
                {
                    return(false);
                }
                num2 *= 10UL;
                if (*digits != '\0')
                {
                    ulong num3 = num2 + (ulong)((long)(*(digits++) - '0'));
                    if (num3 < num2)
                    {
                        return(false);
                    }
                    num2 = num3;
                }
            }
            value = num2;
            return(true);
        }
Ejemplo n.º 7
0
        private unsafe static bool NumberToUInt32(ref Number.NumberBuffer number, ref uint value)
        {
            int num = number.scale;

            if (num > 10 || num < number.precision || number.sign)
            {
                return(false);
            }
            char *digits = number.digits;
            uint  num2   = 0U;

            while (--num >= 0)
            {
                if (num2 > 429496729U)
                {
                    return(false);
                }
                num2 *= 10U;
                if (*digits != '\0')
                {
                    uint num3 = num2 + (uint)(*(digits++) - '0');
                    if (num3 < num2)
                    {
                        return(false);
                    }
                    num2 = num3;
                }
            }
            value = num2;
            return(true);
        }
Ejemplo n.º 8
0
        private static unsafe void DecimalToNumber(Decimal value, ref Number.NumberBuffer number)
        {
            Decimal d = value;

            char *buffer = number.digits;

            number.precision = DecimalPrecision;
            number.sign      = d.IsNegative;

            char *p = buffer + DecimalPrecision;

            while (d.Mid != 0 | d.High != 0)
            {
                p = Number.UInt32ToDecChars(p, Decimal.DecDivMod1E9(ref d), 9);
            }
            p = Number.UInt32ToDecChars(p, d.Low, 0);

            int i = (int)(buffer + DecimalPrecision - p);

            number.scale = i - d.Scale;

            char *dst = number.digits;

            while (--i >= 0)
            {
                *dst++ = *p++;
            }
            *dst = '\0';
        }
Ejemplo n.º 9
0
        internal unsafe static bool TryParseDecimal(string value, NumberStyles options, NumberFormatInfo numfmt, out decimal result)
        {
            byte *stackBuffer = stackalloc byte[checked (unchecked ((UIntPtr)Number.NumberBuffer.NumberBufferBytes) * 1)];

            Number.NumberBuffer numberBuffer = new Number.NumberBuffer(stackBuffer);
            result = 0m;
            return(Number.TryStringToNumber(value, options, ref numberBuffer, numfmt, true) && Number.NumberBufferToDecimal(numberBuffer.PackForNative(), ref result));
        }
Ejemplo n.º 10
0
        // Token: 0x06001096 RID: 4246 RVA: 0x000316C8 File Offset: 0x0002F8C8
        private static bool HexNumberToInt64(ref Number.NumberBuffer number, ref long value)
        {
            ulong num    = 0UL;
            bool  result = Number.HexNumberToUInt64(ref number, ref num);

            value = (long)num;
            return(result);
        }
Ejemplo n.º 11
0
        // Token: 0x06001095 RID: 4245 RVA: 0x000316AC File Offset: 0x0002F8AC
        private static bool HexNumberToInt32(ref Number.NumberBuffer number, ref int value)
        {
            uint num    = 0U;
            bool result = Number.HexNumberToUInt32(ref number, ref num);

            value = (int)num;
            return(result);
        }
Ejemplo n.º 12
0
        private static bool HexNumberToInt32(ref Number.NumberBuffer number, ref int value)
        {
            uint num1 = 0;
            int  num2 = Number.HexNumberToUInt32(ref number, ref num1) ? 1 : 0;

            value = (int)num1;
            return(num2 != 0);
        }
Ejemplo n.º 13
0
        private static bool HexNumberToInt64(ref Number.NumberBuffer number, ref long value)
        {
            ulong num1 = 0;
            int   num2 = Number.HexNumberToUInt64(ref number, ref num1) ? 1 : 0;

            value = (long)num1;
            return(num2 != 0);
        }
Ejemplo n.º 14
0
        internal static unsafe bool TryParseDouble(string value, NumberStyles options, NumberFormatInfo numfmt, out double result)
        {
            byte *stackBuffer = stackalloc byte[Number.NumberBuffer.NumberBufferBytes];

            Number.NumberBuffer number = new Number.NumberBuffer(stackBuffer);
            result = 0.0;
            return(Number.TryStringToNumber(value, options, ref number, numfmt, false) && Number.NumberBufferToDouble(number.PackForNative(), ref result));
        }
Ejemplo n.º 15
0
        internal static unsafe Decimal ParseDecimal(string value, NumberStyles options, NumberFormatInfo numfmt)
        {
            byte *stackBuffer = stackalloc byte[Number.NumberBuffer.NumberBufferBytes];

            Number.NumberBuffer number = new Number.NumberBuffer(stackBuffer);
            Decimal             num    = new Decimal();

            Number.StringToNumber(value, options, ref number, numfmt, true);
            if (!Number.NumberBufferToDecimal(number.PackForNative(), ref num))
            {
                throw new OverflowException(Environment.GetResourceString("Overflow_Decimal"));
            }
            return(num);
        }
Ejemplo n.º 16
0
        internal unsafe static decimal ParseDecimal(string value, NumberStyles options, NumberFormatInfo numfmt)
        {
            byte *stackBuffer = stackalloc byte[checked (unchecked ((UIntPtr)Number.NumberBuffer.NumberBufferBytes) * 1)];

            Number.NumberBuffer numberBuffer = new Number.NumberBuffer(stackBuffer);
            decimal             result       = 0m;

            Number.StringToNumber(value, options, ref numberBuffer, numfmt, true);
            if (!Number.NumberBufferToDecimal(numberBuffer.PackForNative(), ref result))
            {
                throw new OverflowException(Environment.GetResourceString("Overflow_Decimal"));
            }
            return(result);
        }
Ejemplo n.º 17
0
        private unsafe static bool HexNumberToUInt32(ref Number.NumberBuffer number, ref uint value)
        {
            int num = number.scale;

            if (num > 10 || num < number.precision)
            {
                return(false);
            }
            char *ptr  = number.digits;
            uint  num2 = 0U;

            while (--num >= 0)
            {
                if (num2 > 268435455U)
                {
                    return(false);
                }
                num2 *= 16U;
                if (*ptr != '\0')
                {
                    uint num3 = num2;
                    if (*ptr != '\0')
                    {
                        if (*ptr >= '0' && *ptr <= '9')
                        {
                            num3 += (uint)(*ptr - '0');
                        }
                        else if (*ptr >= 'A' && *ptr <= 'F')
                        {
                            num3 += (uint)(*ptr - 'A' + '\n');
                        }
                        else
                        {
                            num3 += (uint)(*ptr - 'a' + '\n');
                        }
                        ptr++;
                    }
                    if (num3 < num2)
                    {
                        return(false);
                    }
                    num2 = num3;
                }
            }
            value = num2;
            return(true);
        }
Ejemplo n.º 18
0
        private unsafe static bool HexNumberToUInt64(ref Number.NumberBuffer number, ref ulong value)
        {
            int num = number.scale;

            if (num > 20 || num < number.precision)
            {
                return(false);
            }
            char *ptr  = number.digits;
            ulong num2 = 0UL;

            while (--num >= 0)
            {
                if (num2 > 1152921504606846975UL)
                {
                    return(false);
                }
                num2 *= 16UL;
                if (*ptr != '\0')
                {
                    ulong num3 = num2;
                    if (*ptr != '\0')
                    {
                        if (*ptr >= '0' && *ptr <= '9')
                        {
                            num3 += (ulong)((long)(*ptr - '0'));
                        }
                        else if (*ptr >= 'A' && *ptr <= 'F')
                        {
                            num3 += (ulong)((long)(*ptr - 'A' + '\n'));
                        }
                        else
                        {
                            num3 += (ulong)((long)(*ptr - 'a' + '\n'));
                        }
                        ptr++;
                    }
                    if (num3 < num2)
                    {
                        return(false);
                    }
                    num2 = num3;
                }
            }
            value = num2;
            return(true);
        }
Ejemplo n.º 19
0
        private static unsafe bool HexNumberToUInt32(ref Number.NumberBuffer number, ref uint value)
        {
            int num1 = number.scale;

            if (num1 > 10 || num1 < number.precision)
            {
                return(false);
            }
            char *chPtr = number.digits;
            uint  num2  = 0;

            while (--num1 >= 0)
            {
                if (num2 > 268435455U)
                {
                    return(false);
                }
                num2 *= 16U;
                if ((int)*chPtr != 0)
                {
                    uint num3 = num2;
                    if ((int)*chPtr != 0)
                    {
                        if ((int)*chPtr >= 48 && (int)*chPtr <= 57)
                        {
                            num3 += (uint)*chPtr - 48U;
                        }
                        else if ((int)*chPtr >= 65 && (int)*chPtr <= 70)
                        {
                            num3 += (uint)((int)*chPtr - 65 + 10);
                        }
                        else
                        {
                            num3 += (uint)((int)*chPtr - 97 + 10);
                        }
                        chPtr += 2;
                    }
                    if (num3 < num2)
                    {
                        return(false);
                    }
                    num2 = num3;
                }
            }
            value = num2;
            return(true);
        }
Ejemplo n.º 20
0
        private static unsafe bool HexNumberToUInt64(ref Number.NumberBuffer number, ref ulong value)
        {
            int num1 = number.scale;

            if (num1 > 20 || num1 < number.precision)
            {
                return(false);
            }
            char *chPtr = number.digits;
            ulong num2  = 0;

            while (--num1 >= 0)
            {
                if (num2 > 1152921504606846975UL)
                {
                    return(false);
                }
                num2 *= 16UL;
                if ((int)*chPtr != 0)
                {
                    ulong num3 = num2;
                    if ((int)*chPtr != 0)
                    {
                        if ((int)*chPtr >= 48 && (int)*chPtr <= 57)
                        {
                            num3 += (ulong)((int)*chPtr - 48);
                        }
                        else if ((int)*chPtr >= 65 && (int)*chPtr <= 70)
                        {
                            num3 += (ulong)((int)*chPtr - 65 + 10);
                        }
                        else
                        {
                            num3 += (ulong)((int)*chPtr - 97 + 10);
                        }
                        chPtr += 2;
                    }
                    if (num3 < num2)
                    {
                        return(false);
                    }
                    num2 = num3;
                }
            }
            value = num2;
            return(true);
        }
Ejemplo n.º 21
0
        internal unsafe static float ParseSingle(string value, NumberStyles options, NumberFormatInfo numfmt)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            byte *stackBuffer = stackalloc byte[checked (unchecked ((UIntPtr)Number.NumberBuffer.NumberBufferBytes) * 1)];

            Number.NumberBuffer numberBuffer = new Number.NumberBuffer(stackBuffer);
            double num = 0.0;

            if (!Number.TryStringToNumber(value, options, ref numberBuffer, numfmt, false))
            {
                string text = value.Trim();
                if (text.Equals(numfmt.PositiveInfinitySymbol))
                {
                    return(float.PositiveInfinity);
                }
                if (text.Equals(numfmt.NegativeInfinitySymbol))
                {
                    return(float.NegativeInfinity);
                }
                if (text.Equals(numfmt.NaNSymbol))
                {
                    return(float.NaN);
                }
                throw new FormatException(Environment.GetResourceString("Format_InvalidString"));
            }
            else
            {
                if (!Number.NumberBufferToDouble(numberBuffer.PackForNative(), ref num))
                {
                    throw new OverflowException(Environment.GetResourceString("Overflow_Single"));
                }
                float num2 = (float)num;
                if (float.IsInfinity(num2))
                {
                    throw new OverflowException(Environment.GetResourceString("Overflow_Single"));
                }
                return(num2);
            }
        }
Ejemplo n.º 22
0
        internal unsafe static uint ParseUInt32(string value, NumberStyles options, NumberFormatInfo numfmt)
        {
            byte *stackBuffer = stackalloc byte[checked (unchecked ((UIntPtr)Number.NumberBuffer.NumberBufferBytes) * 1)];

            Number.NumberBuffer numberBuffer = new Number.NumberBuffer(stackBuffer);
            uint result = 0U;

            Number.StringToNumber(value, options, ref numberBuffer, numfmt, false);
            if ((options & NumberStyles.AllowHexSpecifier) != NumberStyles.None)
            {
                if (!Number.HexNumberToUInt32(ref numberBuffer, ref result))
                {
                    throw new OverflowException(Environment.GetResourceString("Overflow_UInt32"));
                }
            }
            else if (!Number.NumberToUInt32(ref numberBuffer, ref result))
            {
                throw new OverflowException(Environment.GetResourceString("Overflow_UInt32"));
            }
            return(result);
        }
Ejemplo n.º 23
0
        internal static unsafe int ParseInt32(string s, NumberStyles style, NumberFormatInfo info)
        {
            byte *stackBuffer = stackalloc byte[Number.NumberBuffer.NumberBufferBytes];

            Number.NumberBuffer number = new Number.NumberBuffer(stackBuffer);
            int num = 0;

            Number.StringToNumber(s, style, ref number, info, false);
            if ((style & NumberStyles.AllowHexSpecifier) != NumberStyles.None)
            {
                if (!Number.HexNumberToInt32(ref number, ref num))
                {
                    throw new OverflowException(Environment.GetResourceString("Overflow_Int32"));
                }
            }
            else if (!Number.NumberToInt32(ref number, ref num))
            {
                throw new OverflowException(Environment.GetResourceString("Overflow_Int32"));
            }
            return(num);
        }
Ejemplo n.º 24
0
        internal static unsafe bool TryParseSingle(string value, NumberStyles options, NumberFormatInfo numfmt, out float result)
        {
            byte *stackBuffer = stackalloc byte[Number.NumberBuffer.NumberBufferBytes];

            Number.NumberBuffer number = new Number.NumberBuffer(stackBuffer);
            result = 0.0f;
            double num = 0.0;

            if (!Number.TryStringToNumber(value, options, ref number, numfmt, false) || !Number.NumberBufferToDouble(number.PackForNative(), ref num))
            {
                return(false);
            }
            float f = (float)num;

            if (float.IsInfinity(f))
            {
                return(false);
            }
            result = f;
            return(true);
        }
Ejemplo n.º 25
0
        private static unsafe void StringToNumber(string str, NumberStyles options, ref Number.NumberBuffer number, NumberFormatInfo info, bool parseDecimal)
        {
            if (str == null)
            {
                throw new ArgumentNullException("String");
            }
            string str1  = str;
            char * chPtr = (char *)str1;

            if ((IntPtr)chPtr != IntPtr.Zero)
            {
                chPtr += RuntimeHelpers.OffsetToStringData;
            }
            char *str2 = chPtr;

            if (!Number.ParseNumber(ref str2, options, ref number, (StringBuilder)null, info, parseDecimal) || str2 - chPtr < (long)str.Length && !Number.TrailingZeros(str, (int)(str2 - chPtr)))
            {
                throw new FormatException(Environment.GetResourceString("Format_InvalidString"));
            }
            str1 = (string)null;
        }
Ejemplo n.º 26
0
        internal static unsafe ulong ParseUInt64(string value, NumberStyles options, NumberFormatInfo numfmt)
        {
            byte *stackBuffer = stackalloc byte[Number.NumberBuffer.NumberBufferBytes];

            Number.NumberBuffer number = new Number.NumberBuffer(stackBuffer);
            ulong num = 0;

            Number.StringToNumber(value, options, ref number, numfmt, false);
            if ((options & NumberStyles.AllowHexSpecifier) != NumberStyles.None)
            {
                if (!Number.HexNumberToUInt64(ref number, ref num))
                {
                    throw new OverflowException(Environment.GetResourceString("Overflow_UInt64"));
                }
            }
            else if (!Number.NumberToUInt64(ref number, ref num))
            {
                throw new OverflowException(Environment.GetResourceString("Overflow_UInt64"));
            }
            return(num);
        }
Ejemplo n.º 27
0
        private static unsafe bool NumberToInt32(ref Number.NumberBuffer number, ref int value)
        {
            int num1 = number.scale;

            if (num1 > 10 || num1 < number.precision)
            {
                return(false);
            }
            char *chPtr = number.digits;
            int   num2  = 0;

            while (--num1 >= 0)
            {
                if ((uint)num2 > 214748364U)
                {
                    return(false);
                }
                num2 *= 10;
                if ((int)*chPtr != 0)
                {
                    num2 += (int)*chPtr++ - 48;
                }
            }
            if (number.sign)
            {
                num2 = -num2;
                if (num2 > 0)
                {
                    return(false);
                }
            }
            else if (num2 < 0)
            {
                return(false);
            }
            value = num2;
            return(true);
        }
Ejemplo n.º 28
0
        private static unsafe bool NumberToInt64(ref Number.NumberBuffer number, ref long value)
        {
            int num1 = number.scale;

            if (num1 > 19 || num1 < number.precision)
            {
                return(false);
            }
            char *chPtr = number.digits;
            long  num2  = 0;

            while (--num1 >= 0)
            {
                if ((ulong)num2 > 922337203685477580UL)
                {
                    return(false);
                }
                num2 *= 10L;
                if ((int)*chPtr != 0)
                {
                    num2 += (long)((int)*chPtr++ - 48);
                }
            }
            if (number.sign)
            {
                num2 = -num2;
                if (num2 > 0L)
                {
                    return(false);
                }
            }
            else if (num2 < 0L)
            {
                return(false);
            }
            value = num2;
            return(true);
        }
Ejemplo n.º 29
0
        private unsafe static bool NumberToInt32(ref Number.NumberBuffer number, ref int value)
        {
            int num = number.scale;

            if (num > 10 || num < number.precision)
            {
                return(false);
            }
            char *digits = number.digits;
            int   num2   = 0;

            while (--num >= 0)
            {
                if (num2 > 214748364)
                {
                    return(false);
                }
                num2 *= 10;
                if (*digits != '\0')
                {
                    num2 += (int)(*(digits++) - '0');
                }
            }
            if (number.sign)
            {
                num2 = -num2;
                if (num2 > 0)
                {
                    return(false);
                }
            }
            else if (num2 < 0)
            {
                return(false);
            }
            value = num2;
            return(true);
        }
Ejemplo n.º 30
0
        private unsafe static bool NumberToInt64(ref Number.NumberBuffer number, ref long value)
        {
            int num = number.scale;

            if (num > 19 || num < number.precision)
            {
                return(false);
            }
            char *digits = number.digits;
            long  num2   = 0L;

            while (--num >= 0)
            {
                if (num2 > 922337203685477580L)
                {
                    return(false);
                }
                num2 *= 10L;
                if (*digits != '\0')
                {
                    num2 += (long)(*(digits++) - '0');
                }
            }
            if (number.sign)
            {
                num2 = -num2;
                if (num2 > 0L)
                {
                    return(false);
                }
            }
            else if (num2 < 0L)
            {
                return(false);
            }
            value = num2;
            return(true);
        }
Ejemplo n.º 31
0
                        static String FormatBigInteger(BigInteger value, String format, NumberFormatInfo info) {
            int digits = 0;
            char fmt = ParseFormatSpecifier(format, out digits);
            if (fmt == 'x' || fmt == 'X')
                return FormatBigIntegerToHexString(value, fmt, digits, info);

            bool decimalFmt = (fmt == 'g' || fmt == 'G' || fmt == 'd' || fmt == 'D' || fmt == 'r' || fmt == 'R');           

#if SILVERLIGHT ||FEATURE_NETCORE
            if (!decimalFmt) {
                // Silverlight supports invariant formats only
                throw new FormatException(SR.GetString(SR.Format_InvalidFormatSpecifier));
            }
#endif //SILVERLIGHT ||FEATURE_NETCORE

            if (value._bits == null) {
                if (fmt == 'g' || fmt == 'G' || fmt == 'r' || fmt == 'R') {
                    if (digits > 0)
                        format = String.Format(CultureInfo.InvariantCulture, "D{0}", digits.ToString(CultureInfo.InvariantCulture));
                    else
                        format = "D";
                }
                return value._sign.ToString(format, info);
            }


            // First convert to base 10^9.
            const uint kuBase = 1000000000; // 10^9
            const int kcchBase = 9;

            int cuSrc = BigInteger.Length(value._bits);
            int cuMax;
            try {
                cuMax = checked(cuSrc * 10 / 9 + 2);
            }
            catch (OverflowException e) { throw new FormatException(SR.GetString(SR.Format_TooLarge), e); }
            uint[] rguDst = new uint[cuMax];
            int cuDst = 0;

            for (int iuSrc = cuSrc; --iuSrc >= 0; ) {
                uint uCarry = value._bits[iuSrc];
                for (int iuDst = 0; iuDst < cuDst; iuDst++) {
                    Contract.Assert(rguDst[iuDst] < kuBase);
                    ulong uuRes = NumericsHelpers.MakeUlong(rguDst[iuDst], uCarry);
                    rguDst[iuDst] = (uint)(uuRes % kuBase);
                    uCarry = (uint)(uuRes / kuBase);
                }
                if (uCarry != 0) {
                    rguDst[cuDst++] = uCarry % kuBase;
                    uCarry /= kuBase;
                    if (uCarry != 0)
                        rguDst[cuDst++] = uCarry;
                }
            }

            int cchMax;
            try {
                // Each uint contributes at most 9 digits to the decimal representation.
                cchMax = checked(cuDst * kcchBase);
            }
            catch (OverflowException e) { throw new FormatException(SR.GetString(SR.Format_TooLarge), e); }

            if (decimalFmt) {
                if (digits > 0 && digits > cchMax)
                    cchMax = digits;
                if (value._sign < 0) {
                    try {
                        // Leave an extra slot for a minus sign.
                        cchMax = checked(cchMax + info.NegativeSign.Length);
                    }
                    catch (OverflowException e) { throw new FormatException(SR.GetString(SR.Format_TooLarge), e); }
                }
            }

            int rgchBufSize;

            try {
                // We'll pass the rgch buffer to native code, which is going to treat it like a string of digits, so it needs
                // to be null terminated.  Let's ensure that we can allocate a buffer of that size.
                rgchBufSize = checked(cchMax + 1);
            } catch (OverflowException e) { throw new FormatException(SR.GetString(SR.Format_TooLarge), e); }

            char[] rgch = new char[rgchBufSize];

            int ichDst = cchMax;

            for (int iuDst = 0; iuDst < cuDst - 1; iuDst++) {
                uint uDig = rguDst[iuDst];
                Contract.Assert(uDig < kuBase);
                for (int cch = kcchBase; --cch >= 0; ) {
                    rgch[--ichDst] = (char)('0' + uDig % 10);
                    uDig /= 10;
                }
            }
            for (uint uDig = rguDst[cuDst - 1]; uDig != 0; ) {
                rgch[--ichDst] = (char)('0' + uDig % 10);
                uDig /= 10;
            }

#if !SILVERLIGHT ||FEATURE_NETCORE
            if (!decimalFmt) {
                //
                // Go to the VM for GlobLoc aware formatting
                //    
                Byte * numberBufferBytes = stackalloc Byte[Number.NumberBuffer.NumberBufferBytes];
                Number.NumberBuffer number = new Number.NumberBuffer(numberBufferBytes);
                // sign = true for negative and false for 0 and positive values
                number.sign = (value._sign < 0);
                // the cut-off point to switch (G)eneral from (F)ixed-point to (E)xponential form
                number.precision = 29;
                number.digits[0] = '\0';    
                number.scale = cchMax - ichDst;

                int maxDigits = Math.Min(ichDst + 50, cchMax);
                for (int i = ichDst; i < maxDigits; i++) {
                    number.digits[i - ichDst] = rgch[i];
                }
              
                fixed(char* pinnedExtraDigits = rgch) {
                    return Number.FormatNumberBuffer(number.PackForNative(), format, info, pinnedExtraDigits + ichDst);
                }
            }
#endif //!SILVERLIGHT ||FEATURE_NETCORE

            // Format Round-trip decimal
            // This format is supported for integral types only. The number is converted to a string of
            // decimal digits (0-9), prefixed by a minus sign if the number is negative. The precision
            // specifier indicates the minimum number of digits desired in the resulting string. If required,
            // the number is padded with zeros to its left to produce the number of digits given by the
            // precision specifier.
            int numDigitsPrinted = cchMax - ichDst;               
            while (digits > 0 && digits > numDigitsPrinted) {
                // pad leading zeros
                rgch[--ichDst] = '0';
                digits--;                    
            }
            if (value._sign < 0) {
                String negativeSign = info.NegativeSign;
                for (int i = info.NegativeSign.Length - 1; i > -1; i--)
                    rgch[--ichDst] = info.NegativeSign[i];
            }
            return new String(rgch, ichDst, cchMax - ichDst);
        }
Ejemplo n.º 32
0
		internal unsafe static ulong ParseUInt64(string value, NumberStyles options, NumberFormatInfo numfmt)
		{
			byte* stackBuffer = stackalloc byte[(UIntPtr)114 / 1];
			Number.NumberBuffer numberBuffer = new Number.NumberBuffer(stackBuffer);
			ulong result = 0uL;
			Number.StringToNumber(value, options, ref numberBuffer, numfmt, false);
			if ((options & NumberStyles.AllowHexSpecifier) != NumberStyles.None)
			{
				if (!Number.HexNumberToUInt64(ref numberBuffer, ref result))
				{
					throw new OverflowException(Environment.GetResourceString("Overflow_UInt64"));
				}
			}
			else
			{
				if (!Number.NumberToUInt64(ref numberBuffer, ref result))
				{
					throw new OverflowException(Environment.GetResourceString("Overflow_UInt64"));
				}
			}
			return result;
		}
 internal static unsafe bool TryParseBigInteger(string value, NumberStyles style, NumberFormatInfo info, out BigInteger result)
 {
     ArgumentException exception;
     result = BigInteger.Zero;
     if (!TryValidateParseStyleInteger(style, out exception))
     {
         throw exception;
     }
     BigNumberBuffer number = BigNumberBuffer.Create();
     byte* stackBuffer = stackalloc byte[0x72];
     Number.NumberBuffer buffer2 = new Number.NumberBuffer(stackBuffer);
     result = 0;
     if (!Number.TryStringToNumber(value, style, ref buffer2, number.digits, info, false))
     {
         return false;
     }
     number.precision = buffer2.precision;
     number.scale = buffer2.scale;
     number.sign = buffer2.sign;
     if ((style & NumberStyles.AllowHexSpecifier) != NumberStyles.None)
     {
         if (!HexNumberToBigInteger(ref number, ref result))
         {
             return false;
         }
     }
     else if (!NumberToBigInteger(ref number, ref result))
     {
         return false;
     }
     return true;
 }
Ejemplo n.º 34
0
        internal unsafe static Boolean TryParseBigInteger(String value, NumberStyles style, NumberFormatInfo info, out BigInteger result) {
            result = BigInteger.Zero;
            ArgumentException e;
            if (!TryValidateParseStyleInteger(style, out e))
                throw e; // TryParse still throws ArgumentException on invalid NumberStyles

            BigNumberBuffer bignumber = BigNumberBuffer.Create();
            Byte * numberBufferBytes = stackalloc Byte[Number.NumberBuffer.NumberBufferBytes];
            Number.NumberBuffer number = new Number.NumberBuffer(numberBufferBytes);
            result = 0;
    
            if (!Number.TryStringToNumber(value, style, ref number, bignumber.digits, info, false)) {
                return false;
            }
            bignumber.precision = number.precision;
            bignumber.scale = number.scale;
            bignumber.sign = number.sign;

            if ((style & NumberStyles.AllowHexSpecifier) != 0) {
                if (!HexNumberToBigInteger(ref bignumber, ref result)) { 
                    return false;
                }
            }
            else {
                if (!NumberToBigInteger(ref bignumber, ref result)) {
                    return false;
                }
            }
            return true;           
        }
Ejemplo n.º 35
0
		internal unsafe static bool TryParseUInt64(string s, NumberStyles style, NumberFormatInfo info, out ulong result)
		{
			byte* stackBuffer = stackalloc byte[(UIntPtr)114 / 1];
			Number.NumberBuffer numberBuffer = new Number.NumberBuffer(stackBuffer);
			result = 0uL;
			if (!Number.TryStringToNumber(s, style, ref numberBuffer, info, false))
			{
				return false;
			}
			if ((style & NumberStyles.AllowHexSpecifier) != NumberStyles.None)
			{
				if (!Number.HexNumberToUInt64(ref numberBuffer, ref result))
				{
					return false;
				}
			}
			else
			{
				if (!Number.NumberToUInt64(ref numberBuffer, ref result))
				{
					return false;
				}
			}
			return true;
		}
 internal static unsafe string FormatBigInteger(BigInteger value, string format, NumberFormatInfo info)
 {
     int num3;
     int num9;
     int digits = 0;
     char ch = ParseFormatSpecifier(format, out digits);
     switch (ch)
     {
         case 'x':
         case 'X':
             return FormatBigIntegerToHexString(value, ch, digits, info);
     }
     bool flag = ((((ch == 'g') || (ch == 'G')) || ((ch == 'd') || (ch == 'D'))) || (ch == 'r')) || (ch == 'R');
     if (value._bits == null)
     {
         switch (ch)
         {
             case 'g':
             case 'G':
             case 'r':
             case 'R':
                 if (digits > 0)
                 {
                     format = string.Format(CultureInfo.InvariantCulture, "D{0}", new object[] { digits.ToString(CultureInfo.InvariantCulture) });
                 }
                 else
                 {
                     format = "D";
                 }
                 break;
         }
         return value._sign.ToString(format, info);
     }
     int num2 = BigInteger.Length(value._bits);
     try
     {
         num3 = ((num2 * 10) / 9) + 2;
     }
     catch (OverflowException exception)
     {
         throw new FormatException(SR.GetString("Format_TooLarge"), exception);
     }
     uint[] numArray = new uint[num3];
     int num4 = 0;
     int index = num2;
     while (--index >= 0)
     {
         uint uLo = value._bits[index];
         for (int k = 0; k < num4; k++)
         {
             ulong num8 = NumericsHelpers.MakeUlong(numArray[k], uLo);
             numArray[k] = (uint) (num8 % ((ulong) 0x3b9aca00L));
             uLo = (uint) (num8 / ((ulong) 0x3b9aca00L));
         }
         if (uLo != 0)
         {
             numArray[num4++] = uLo % 0x3b9aca00;
             uLo /= 0x3b9aca00;
             if (uLo != 0)
             {
                 numArray[num4++] = uLo;
             }
         }
     }
     try
     {
         num9 = num4 * 9;
     }
     catch (OverflowException exception2)
     {
         throw new FormatException(SR.GetString("Format_TooLarge"), exception2);
     }
     if (flag)
     {
         if ((digits > 0) && (digits > num9))
         {
             num9 = digits;
         }
         if (value._sign < 0)
         {
             try
             {
                 num9 += info.NegativeSign.Length;
             }
             catch (OverflowException exception3)
             {
                 throw new FormatException(SR.GetString("Format_TooLarge"), exception3);
             }
         }
     }
     char[] chArray = new char[num9];
     int startIndex = num9;
     for (int i = 0; i < (num4 - 1); i++)
     {
         uint num12 = numArray[i];
         int num13 = 9;
         while (--num13 >= 0)
         {
             chArray[--startIndex] = (char) (0x30 + (num12 % 10));
             num12 /= 10;
         }
     }
     for (uint j = numArray[num4 - 1]; j != 0; j /= 10)
     {
         chArray[--startIndex] = (char) (0x30 + (j % 10));
     }
     if (!flag)
     {
         byte* stackBuffer = stackalloc byte[0x72];
         Number.NumberBuffer buffer = new Number.NumberBuffer(stackBuffer) {
             sign = value._sign < 0,
             precision = 0x1d
         };
         buffer.digits[0] = '\0';
         buffer.scale = num9 - startIndex;
         int num15 = Math.Min(startIndex + 50, num9);
         for (int m = startIndex; m < num15; m++)
         {
             buffer.digits[m - startIndex] = chArray[m];
         }
         return Number.FormatNumberBuffer(buffer.PackForNative(), format, info);
     }
     int num17 = num9 - startIndex;
     while ((digits > 0) && (digits > num17))
     {
         chArray[--startIndex] = '0';
         digits--;
     }
     if (value._sign < 0)
     {
         string negativeSign = info.NegativeSign;
         for (int n = info.NegativeSign.Length - 1; n > -1; n--)
         {
             chArray[--startIndex] = info.NegativeSign[n];
         }
     }
     return new string(chArray, startIndex, num9 - startIndex);
 }
Ejemplo n.º 37
0
		internal unsafe static bool TryParseSingle(string value, NumberStyles options, NumberFormatInfo numfmt, out float result)
		{
			byte* stackBuffer = stackalloc byte[(UIntPtr)114 / 1];
			Number.NumberBuffer numberBuffer = new Number.NumberBuffer(stackBuffer);
			result = 0f;
			double num = 0.0;
			if (!Number.TryStringToNumber(value, options, ref numberBuffer, numfmt, false))
			{
				return false;
			}
			if (!Number.NumberBufferToDouble(numberBuffer.PackForNative(), ref num))
			{
				return false;
			}
			float num2 = (float)num;
			if (float.IsInfinity(num2))
			{
				return false;
			}
			result = num2;
			return true;
		}
Ejemplo n.º 38
0
		internal unsafe static bool TryParseDouble(string value, NumberStyles options, NumberFormatInfo numfmt, out double result)
		{
			byte* stackBuffer = stackalloc byte[(UIntPtr)114 / 1];
			Number.NumberBuffer numberBuffer = new Number.NumberBuffer(stackBuffer);
			result = 0.0;
			return Number.TryStringToNumber(value, options, ref numberBuffer, numfmt, false) && Number.NumberBufferToDouble(numberBuffer.PackForNative(), ref result);
		}
Ejemplo n.º 39
0
		internal unsafe static decimal ParseDecimal(string value, NumberStyles options, NumberFormatInfo numfmt)
		{
			byte* stackBuffer = stackalloc byte[(UIntPtr)114 / 1];
			Number.NumberBuffer numberBuffer = new Number.NumberBuffer(stackBuffer);
			decimal result = 0m;
			Number.StringToNumber(value, options, ref numberBuffer, numfmt, true);
			if (!Number.NumberBufferToDecimal(numberBuffer.PackForNative(), ref result))
			{
				throw new OverflowException(Environment.GetResourceString("Overflow_Decimal"));
			}
			return result;
		}
Ejemplo n.º 40
0
		internal unsafe static float ParseSingle(string value, NumberStyles options, NumberFormatInfo numfmt)
		{
			if (value == null)
			{
				throw new ArgumentNullException("value");
			}
			byte* stackBuffer = stackalloc byte[(UIntPtr)114 / 1];
			Number.NumberBuffer numberBuffer = new Number.NumberBuffer(stackBuffer);
			double num = 0.0;
			if (!Number.TryStringToNumber(value, options, ref numberBuffer, numfmt, false))
			{
				string text = value.Trim();
				if (text.Equals(numfmt.PositiveInfinitySymbol))
				{
					return float.PositiveInfinity;
				}
				if (text.Equals(numfmt.NegativeInfinitySymbol))
				{
					return float.NegativeInfinity;
				}
				if (text.Equals(numfmt.NaNSymbol))
				{
					return float.NaN;
				}
				throw new FormatException(Environment.GetResourceString("Format_InvalidString"));
			}
			else
			{
				if (!Number.NumberBufferToDouble(numberBuffer.PackForNative(), ref num))
				{
					throw new OverflowException(Environment.GetResourceString("Overflow_Single"));
				}
				float num2 = (float)num;
				if (float.IsInfinity(num2))
				{
					throw new OverflowException(Environment.GetResourceString("Overflow_Single"));
				}
				return num2;
			}
		}
Ejemplo n.º 41
0
		internal unsafe static int ParseInt32(string s, NumberStyles style, NumberFormatInfo info)
		{
			byte* stackBuffer = stackalloc byte[(UIntPtr)114 / 1];
			Number.NumberBuffer numberBuffer = new Number.NumberBuffer(stackBuffer);
			int result = 0;
			Number.StringToNumber(s, style, ref numberBuffer, info, false);
			if ((style & NumberStyles.AllowHexSpecifier) != NumberStyles.None)
			{
				if (!Number.HexNumberToInt32(ref numberBuffer, ref result))
				{
					throw new OverflowException(Environment.GetResourceString("Overflow_Int32"));
				}
			}
			else
			{
				if (!Number.NumberToInt32(ref numberBuffer, ref result))
				{
					throw new OverflowException(Environment.GetResourceString("Overflow_Int32"));
				}
			}
			return result;
		}