コード例 #1
0
        public static void StandardFormatParseString(string formatString, char expectedSymbol, byte expectedPrecision)
        {
            StandardFormat format = StandardFormat.Parse(formatString);

            Assert.Equal(expectedSymbol, format.Symbol);
            Assert.Equal(expectedPrecision, format.Precision);
        }
コード例 #2
0
        public static void StandardFormatParseSpan(string formatString, char expectedSymbol, byte expectedPrecision)
        {
            ReadOnlySpan <char> span   = formatString.AsSpan();
            StandardFormat      format = StandardFormat.Parse(span);

            Assert.Equal(expectedSymbol, format.Symbol);
            Assert.Equal(expectedPrecision, format.Precision);
        }
コード例 #3
0
        public void CheckTimeSpan(TimeSpan value, string format)
        {
            var parsed    = StandardFormat.Parse(format);
            var formatter = new StringFormatter();

            formatter.Append(value, parsed);
            var result    = formatter.ToString();
            var clrResult = value.ToString(format, CultureInfo.InvariantCulture);

            Assert.Equal(clrResult, result);
        }
コード例 #4
0
        private void CheckInt64(long value, string format, string expected, StringFormatter formatter)
        {
            var parsed = StandardFormat.Parse(format);

            formatter.Clear();
            formatter.Append(value, parsed);
            var result = formatter.ToString();

            Assert.Equal(expected, result);

            var clrResult = value.ToString(format, CultureInfo.InvariantCulture);

            Assert.Equal(clrResult, result);
        }
コード例 #5
0
 public FormatSegment(int offset, int count, bool isFormatArgument, int formatIndex, string formatString, bool utf8)
 {
     Offset           = offset;
     Count            = count;
     IsFormatArgument = isFormatArgument;
     FormatIndex      = formatIndex;
     FormatString     = formatString;
     if (utf8)
     {
         StandardFormat = (formatString != null) ? StandardFormat.Parse(formatString) : default;
     }
     else
     {
         StandardFormat = default;
     }
 }
コード例 #6
0
        public void FormatNPrecision()
        {
            var format = StandardFormat.Parse("N1");
            var sb     = new StringFormatter();

            sb.Append((sbyte)-10, format);
            sb.Append((byte)99, format);
            sb.Append((short)-10, format);
            sb.Append((ushort)99, format);
            sb.Append((int)-10, format);
            sb.Append((uint)99, format);
            sb.Append((long)-10, format);
            sb.Append((ulong)99, format);
            var result = sb.ToString();

            Assert.Equal("-10.099.0-10.099.0-10.099.0-10.099.0", result);
        }
コード例 #7
0
        public void FormatXPrecision()
        {
            var x = StandardFormat.Parse("x10");
            var X = StandardFormat.Parse("X10");

            var sb = new StringFormatter();

            sb.Append((ulong)255, x);
            sb.Append((uint)255, X);
            Assert.Equal("00000000ff00000000FF", sb.ToString());

            sb.Clear();
            sb.Append((int)-1, X);
            Assert.Equal("00FFFFFFFF", sb.ToString());

            sb.Clear();
            sb.Append((int)-2, X);
            Assert.Equal("00FFFFFFFE", sb.ToString());
        }
コード例 #8
0
        public void FormatX()
        {
            var x = StandardFormat.Parse("x");
            var X = StandardFormat.Parse("X");

            var sb = new StringFormatter();

            sb.Append((ulong)255, x);
            sb.Append((uint)255, X);
            Assert.Equal("ffFF", sb.ToString());

            sb.Clear();
            sb.Append((int)-1, X);
            Assert.Equal("FFFFFFFF", sb.ToString());

            sb.Clear();
            sb.Append((int)-2, X);
            Assert.Equal("FFFFFFFE", sb.ToString());
        }
コード例 #9
0
        public void FormatXUtf8()
        {
            var x = StandardFormat.Parse("x");
            var X = StandardFormat.Parse("X");

            var sb = new ArrayFormatter(256, SymbolTable.InvariantUtf8);

            sb.Append((ulong)255, x);
            sb.Append((uint)255, X);

            Assert.Equal("ffFF", new Utf8Span(sb.Formatted.AsSpan()).ToString());

            sb.Clear();
            sb.Append((int)-1, X);
            Assert.Equal("FFFFFFFF", new Utf8Span(sb.Formatted.AsSpan()).ToString());

            sb.Clear();
            sb.Append((int)-2, X);
            Assert.Equal("FFFFFFFE", new Utf8Span(sb.Formatted.AsSpan()).ToString());
        }
コード例 #10
0
        public unsafe void GuidParsingUtf16(string format)
        {
            var parsedFormat = StandardFormat.Parse(format);
            var random       = new Random(1000);
            var guidBytes    = new byte[16];

            var expected = guidWithAllNonZeroDigits;

            for (int i = 0; i < 100; i++)
            {
                string expectedString = expected.ToString(format, CultureInfo.InvariantCulture);

                Assert.True(Utf16Parser.TryParseGuid(expectedString.ToCharArray(), out Guid parsed, out int charactersConsumed, parsedFormat));
                Assert.Equal(expected, parsed);
                Assert.Equal(expectedString.Length, charactersConsumed);

                random.NextBytes(guidBytes);
                expected = new Guid(guidBytes);
            }
        }
コード例 #11
0
        public void ParsePrecisionFormats()
        {
            StandardFormat parsed;

            parsed = StandardFormat.Parse("D1");
            Verify(parsed, 1, 'D');

            parsed = StandardFormat.Parse("d2");
            Verify(parsed, 2, 'd');

            parsed = StandardFormat.Parse("E3");
            Verify(parsed, 3, 'E');

            parsed = StandardFormat.Parse("e4");
            Verify(parsed, 4, 'e');

            parsed = StandardFormat.Parse("F5");
            Verify(parsed, 5, 'F');

            parsed = StandardFormat.Parse("f6");
            Verify(parsed, 6, 'f');

            parsed = StandardFormat.Parse("G7");
            Verify(parsed, 7, 'G');

            parsed = StandardFormat.Parse("g99");
            Verify(parsed, 99, 'g');

            parsed = StandardFormat.Parse("N98");
            Verify(parsed, 98, 'N');

            parsed = StandardFormat.Parse("n10");
            Verify(parsed, 10, 'n');

            parsed = StandardFormat.Parse("X11");
            Verify(parsed, 11, 'X');

            parsed = StandardFormat.Parse("x10");
            Verify(parsed, 10, 'x');
        }
コード例 #12
0
 public static void StandardFormatParseNegative(string badFormatString)
 {
     Assert.Throws <FormatException>(() => StandardFormat.Parse(badFormatString));
 }
コード例 #13
0
        protected override string InvariantToStringSingle(float f, string format)
        {
            Span <byte> s = stackalloc byte[SingleNumberBufferLength];
            bool        formatSucceeded = Utf8Formatter.TryFormat(f, s, out int bytesWritten, StandardFormat.Parse(format));

            Assert.True(formatSucceeded, $"Failed to format '{f.ToString(CultureInfo.InvariantCulture)}' using '{format}'.");
            return(s.Slice(0, bytesWritten).ToUtf16String());
        }
コード例 #14
0
        private const int SingleNumberBufferLength = 112 + 1 + 1;  // 112 for the longest input + 1 for rounding: 1.40129846E-45

        protected override string InvariantToStringDouble(double d)
        {
            Span <byte> s = stackalloc byte[DoubleNumberBufferLength];
            bool        formatSucceeded = Utf8Formatter.TryFormat(d, s, out int bytesWritten, StandardFormat.Parse("E17"));

            Assert.True(formatSucceeded, $"Failed to format {d.ToString("G17", CultureInfo.InvariantCulture)}");
            return(s.ToUtf16String());
        }
コード例 #15
0
 public void ParseThrowsExceptionWhenParsedPrecisionExceedsMaxPrecision()
 {
     var ex = Assert.Throws <FormatException>(() => StandardFormat.Parse($"x{100}"));
 }
コード例 #16
0
        public void ParseSimpleFormats()
        {
            StandardFormat parsed;

            parsed = StandardFormat.Parse("B");
            Verify(parsed, StandardFormat.NoPrecision, 'B');

            parsed = StandardFormat.Parse("b");
            Verify(parsed, StandardFormat.NoPrecision, 'b');

            parsed = StandardFormat.Parse("D");
            Verify(parsed, StandardFormat.NoPrecision, 'D');

            parsed = StandardFormat.Parse("d");
            Verify(parsed, StandardFormat.NoPrecision, 'd');

            parsed = StandardFormat.Parse("E");
            Verify(parsed, StandardFormat.NoPrecision, 'E');

            parsed = StandardFormat.Parse("e");
            Verify(parsed, StandardFormat.NoPrecision, 'e');

            parsed = StandardFormat.Parse("F");
            Verify(parsed, StandardFormat.NoPrecision, 'F');

            parsed = StandardFormat.Parse("f");
            Verify(parsed, StandardFormat.NoPrecision, 'f');

            parsed = StandardFormat.Parse("G");
            Verify(parsed, StandardFormat.NoPrecision, 'G');

            parsed = StandardFormat.Parse("g");
            Verify(parsed, StandardFormat.NoPrecision, 'g');

            parsed = StandardFormat.Parse("N");
            Verify(parsed, StandardFormat.NoPrecision, 'N');

            parsed = StandardFormat.Parse("n");
            Verify(parsed, StandardFormat.NoPrecision, 'n');

            parsed = StandardFormat.Parse("O");
            Verify(parsed, StandardFormat.NoPrecision, 'O');

            parsed = StandardFormat.Parse("o");
            Verify(parsed, StandardFormat.NoPrecision, 'o');

            parsed = StandardFormat.Parse("P");
            Verify(parsed, StandardFormat.NoPrecision, 'P');

            parsed = StandardFormat.Parse("p");
            Verify(parsed, StandardFormat.NoPrecision, 'p');

            parsed = StandardFormat.Parse("R");
            Verify(parsed, StandardFormat.NoPrecision, 'R');

            parsed = StandardFormat.Parse("r");
            Verify(parsed, StandardFormat.NoPrecision, 'r');

            parsed = StandardFormat.Parse("t");
            Verify(parsed, StandardFormat.NoPrecision, 't');

            parsed = StandardFormat.Parse("X");
            Verify(parsed, StandardFormat.NoPrecision, 'X');

            parsed = StandardFormat.Parse("x");
            Verify(parsed, StandardFormat.NoPrecision, 'x');
        }