コード例 #1
0
        public static void StandardFormatToString(string expected, char symbol, byte precision)
        {
            StandardFormat format = new StandardFormat(symbol, precision);
            string         actual = format.ToString();

            Assert.Equal(expected, actual);
        }
コード例 #2
0
        //
        // Common handler for TryFormat(Double) and TryFormat(Single). You may notice that this particular routine isn't getting into the "no allocation" spirit
        // of things. The DoubleToNumber() code is incredibly complex and is one of the few pieces of Number formatting never C#-ized. It would be really
        // be preferable not to have another version of that lying around. Until we really hit a scenario where floating point formatting needs the perf, we'll
        // make do with this.
        //
        private static bool TryFormatFloatingPoint <T>(T value, Span <byte> buffer, out int bytesWritten, StandardFormat format) where T : IFormattable
        {
            if (format.IsDefault)
            {
                format = 'G';
            }

            switch (format.Symbol)
            {
            case 'g':
            case 'G':
                if (format.Precision != StandardFormat.NoPrecision)
                {
                    throw new NotSupportedException(SR.Argument_GWithPrecisionNotSupported);
                }
                break;

            case 'f':
            case 'F':
            case 'e':
            case 'E':
                break;

            default:
                return(ThrowHelper.TryFormatThrowFormatException(out bytesWritten));
            }

            string formatString = format.ToString();
            string utf16Text    = value.ToString(formatString, CultureInfo.InvariantCulture);
            int    length       = utf16Text.Length;

            if (length > buffer.Length)
            {
                bytesWritten = 0;
                return(false);
            }

            for (int i = 0; i < length; i++)
            {
                Debug.Assert(utf16Text[i] < 128, "A culture-invariant ToString() of a floating point expected to produce ASCII characters only.");
                buffer[i] = (byte)(utf16Text[i]);
            }

            bytesWritten = length;
            return(true);
        }
コード例 #3
0
        public static void StandardFormatToStringOversizedPrecision()
        {
            // Code coverage: Precision of 100 is not legal but ToString() isn't allowed to throw an exception for that.
            // Make sure it doesn't.

            const byte BadPrecision = 100;

            StandardFormat format = default;

            unsafe
            {
                // We're aiming for the Precision field but we don't know where it is so nuke 'em all.
                new Span <byte>(&format, sizeof(StandardFormat)).Fill(BadPrecision);
            }

            string s = format.ToString();

            Assert.NotNull(s);
        }