コード例 #1
0
    void IConverter <string> .Convert(ref char *dst, char *end, string value, FormatSpec formatSpec)
    {
        int lpadding = 0, rpadding = 0;

        if (formatSpec.argWidth < 0)
        {
            rpadding = -formatSpec.argWidth - value.Length;
        }
        else
        {
            lpadding = formatSpec.argWidth - value.Length;
        }

        while (lpadding-- > 0 && dst < end)
        {
            *dst++ = ' ';
        }

        for (int i = 0, l = value.Length; i < l && dst < end; i++)
        {
            *dst++ = value[i];
        }

        while (rpadding-- > 0 && dst < end)
        {
            *dst++ = ' ';
        }
    }
コード例 #2
0
 public void Format(ref char *dst, char *end, int argIndex, FormatSpec formatSpec)
 {
     switch (argIndex)
     {
     case 0: (Converter.instance as IConverter <T0>).Convert(ref dst, end, t0, formatSpec); break;
     }
 }
コード例 #3
0
    void IConverter <float> .Convert(ref char *dst, char *end, float value, FormatSpec formatSpec)
    {
        if (formatSpec.fractWidth == 0)
        {
            formatSpec.fractWidth = 2;
        }

        var intWidth = formatSpec.argWidth - formatSpec.fractWidth - 1;
        // Very crappy version for now
        bool neg = false;

        if (value < 0.0f)
        {
            neg   = true;
            value = -value;
        }
        int   v1        = Mathf.FloorToInt(value);
        float fractMult = (int)Mathf.Pow(10.0f, formatSpec.fractWidth);
        int   v2        = Mathf.FloorToInt(value * fractMult) % (int)(fractMult);

        ConvertInt(ref dst, end, neg ? -v1 : v1, intWidth, formatSpec.integerWidth, formatSpec.leadingZero);
        if (dst < end)
        {
            *dst++ = '.';
        }
        ConvertInt(ref dst, end, v2, formatSpec.fractWidth, formatSpec.fractWidth, true);
    }
コード例 #4
0
        public void TestParseDateTime(string f0, int y0, int m0, int d0, int h0, int mn0, int s0)
        {
            string     format = f0.Length > 0 ? "{0:" + f0 + "}" : "{0}";
            FormatSpec fs     = new FormatSpec(format, 0, format.Length);
            DateTime   dtnow  = DateTime.Now;

            if (y0 == 0)
            {
                y0 = dtnow.Year;
            }
            if (m0 == 0)
            {
                m0 = dtnow.Month;
            }
            if (d0 == 0)
            {
                d0 = dtnow.Day;
            }
            DateTime dt00 = new DateTime(y0, m0, d0, h0, mn0, s0);
            DateTime dt0  = DateTime.SpecifyKind(dt00, DateTimeKind.Utc);
            String   l0   = String.Format("***" + format + "***", dt0);
            int      j1   = fs.Parse(l0, 3, typeof(DateTime), out object d1);

            _output.WriteLine($"dt0 = {dt0}, d1 = {d1}, f0 = {f0}, l0 = {l0}");
            Assert.Equal(l0.Length - 3, j1);
            Assert.Equal(d1, dt0);
        }
コード例 #5
0
        public void TestParse(string s0, int i0, int l0, string t0, int k0, Type y0, int j0, object d0)
        {
            FormatSpec fs = new FormatSpec(s0, i0, l0);
            int        j1 = fs.Parse(t0, k0, y0, out object d1);

            Assert.Equal(j0, j1);
            Assert.Equal(d0, d1);
        }
コード例 #6
0
        public static string CreateTable(string format, IEnumerable <IEnumerable <string> > rows, params string[] headers)
        {
            var table = rows.Select(x => x.Select(y => y ?? "").ToList()).ToList();

            if (table.Count == 0)
            {
                return("");
            }
            if (headers == null || headers.Length == 0)
            {
                headers = table[0].ToArray(); table.RemoveAt(0);
            }

            var allData = new List <IEnumerable <string> > {
                headers
            }.Concat(table).ToList();
            var columns = headers.Length;

            if (columns == 0 || table.Any(x => x.Count != columns))
            {
                return("Invalid table");
            }

            var longestValue = allData.SelectMany(x => x).Max(x => x.Length);
            var spec         = new FormatSpec();

            switch (format)
            {
            case "csv":
                spec.Spacer     = "";
                spec.SpacerLine = "";
                spec.LineFormat = string.Join(",", headers.Select((_, idx) => "{" + idx + "}")) + "\r\n";
                spec.EmptyLine  = "";
                break;

            case "tsv":
                spec.Spacer     = "";
                spec.SpacerLine = "";
                spec.LineFormat = string.Join("\t", headers.Select((_, idx) => "{" + idx + "}")) + "\r\n";
                spec.EmptyLine  = "";
                break;

            default:
                spec.PadValues  = true;
                spec.Spacer     = new string('*', (columns * (longestValue + 3)) + 1);
                spec.SpacerLine = $"{spec.Spacer}\r\n";
                spec.LineFormat = "* " + string.Join(" * ", headers.Select((_, idx) => "{" + idx + ",-" + longestValue + "}")) + " *\r\n";
                spec.EmptyLine  = string.Format(spec.LineFormat, headers.Select(_ => "").ToArray());
                break;
            }

            return
                (spec.SpacerLine +
                 spec.EmptyLine +
                 string.Join(spec.EmptyLine + spec.SpacerLine + spec.EmptyLine, allData.Select(x => string.Format(spec.LineFormat, x.Select(y => (spec.PadValues && y.Length < (longestValue - 1) ? new string(' ', (longestValue - y.Length) / 2) : "") + y).ToArray()))) +
                 spec.EmptyLine +
                 spec.Spacer);
        }
コード例 #7
0
        public void TestCtor(string s0, int i0, int l0, int x0, int a0, string f0, bool v0)
        {
            FormatSpec fs = new FormatSpec(s0, i0, l0);

            Assert.Equal(x0, fs.Index);
            Assert.Equal(a0, fs.Alignment);
            Assert.Equal(f0, fs.Format);
            Assert.Equal(v0, fs.IsValid);
        }
コード例 #8
0
        public void TestParseTimeSpan(string f0, int d0, int h0, int m0, int ms0)
        {
            string     format = f0.Length > 0 ? "{0:" + f0 + "}" : "{0}";
            FormatSpec fs     = new FormatSpec(format, 0, format.Length);
            TimeSpan   ts0    = new TimeSpan(d0, h0, m0, ms0);
            String     l0     = String.Format("aaa" + format + "bbb", ts0);
            int        j1     = fs.Parse(l0, 3, typeof(TimeSpan), out object d1);

            Assert.Equal(l0.Length - 3, j1);
            Assert.Equal(d1, ts0);
        }
コード例 #9
0
 void IConverter <byte> .Convert(ref char *dst, char *end, byte value, FormatSpec formatSpec)
 {
     ConvertInt(ref dst, end, value, formatSpec.argWidth, formatSpec.integerWidth, formatSpec.leadingZero);
 }
コード例 #10
0
 public void Format(ref char *dst, char *end, int argIndex, FormatSpec formatSpec)
 {
 }
コード例 #11
0
    unsafe void IConverter <CharBufView> .Convert(ref char *dst, char *end, CharBufView value, FormatSpec formatSpec)
    {
        int lpadding = 0, rpadding = 0;

        if (formatSpec.argWidth < 0)
        {
            rpadding = -formatSpec.argWidth - value.length;
        }
        else
        {
            lpadding = formatSpec.argWidth - value.length;
        }

        while (lpadding-- > 0 && dst < end)
        {
            *dst++ = ' ';
        }

        for (int i = 0, l = value.length; i < l && dst < end; i++)
        {
            *dst++ = value.buf[i + value.start];
        }

        while (rpadding-- > 0 && dst < end)
        {
            *dst++ = ' ';
        }
    }
コード例 #12
0
 unsafe void IConverter <int> .Convert(ref char *dst, char *end, int value, FormatSpec formatSpec)
 {
     ConvertInt(ref dst, end, value, formatSpec.argWidth, formatSpec.numberWidth, formatSpec.leadingZero);
 }