Ejemplo n.º 1
0
        private void CoreFmt <T0, T1, T2>(ref StringMaker sm, IFormatProvider?provider, T0 arg0, T1 arg1, T2 arg2, ReadOnlySpan <object?> args)
        {
            var literalIndex = 0;

            foreach (var segment in _segments)
            {
                int literalCount = segment.LiteralCount;
                if (literalCount > 0)
                {
                    // the segment has some literal text
                    sm.Append(LiteralString.AsSpan(literalIndex, literalCount));
                    literalIndex += literalCount;
                }

                var argIndex = segment.ArgIndex;
                if (argIndex >= 0)
                {
                    // the segment has an arg to format
                    switch (argIndex)
                    {
                    case 0:
                        AppendArg(ref sm, arg0, segment.ArgFormat, provider, segment.ArgWidth);
                        break;

                    case 1:
                        AppendArg(ref sm, arg1, segment.ArgFormat, provider, segment.ArgWidth);
                        break;

                    case 2:
                        AppendArg(ref sm, arg2, segment.ArgFormat, provider, segment.ArgWidth);
                        break;

                    default:
                        AppendArg(ref sm, args[argIndex - 3], segment.ArgFormat, provider, segment.ArgWidth);
                        break;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private static void AppendArg <T>(ref StringMaker sm, T arg, string argFormat, IFormatProvider?provider, int argWidth)
        {
            switch (arg)
            {
            case int a:
                sm.Append(a, argFormat, provider, argWidth);
                break;

            case long a:
                sm.Append(a, argFormat, provider, argWidth);
                break;

            case string a:
                sm.Append(a, argWidth);
                break;

            case double a:
                sm.Append(a, argFormat, provider, argWidth);
                break;

            case float a:
                sm.Append(a, argFormat, provider, argWidth);
                break;

            case uint a:
                sm.Append(a, argFormat, provider, argWidth);
                break;

            case ulong a:
                sm.Append(a, argFormat, provider, argWidth);
                break;

            case short a:
                sm.Append(a, argFormat, provider, argWidth);
                break;

            case ushort a:
                sm.Append(a, argFormat, provider, argWidth);
                break;

            case byte a:
                sm.Append(a, argFormat, provider, argWidth);
                break;

            case sbyte a:
                sm.Append(a, argFormat, provider, argWidth);
                break;

            case bool a:
                sm.Append(a, argWidth);
                break;

            case char a:
                sm.Append(a, argWidth);
                break;

            case decimal a:
                sm.Append(a, argFormat, provider, argWidth);
                break;

            case DateTime a:
                sm.Append(a, argFormat, provider, argWidth);
                break;

            case TimeSpan a:
                sm.Append(a, argFormat, provider, argWidth);
                break;

            case ISpanFormattable a:
                sm.Append(a, argFormat, provider, argWidth);
                break;

            case IFormattable a:
                sm.Append(a, argFormat, provider, argWidth);
                break;

            case object a:
                sm.Append(a, argWidth);
                break;

            default:
                // when arg == null
                sm.Append(string.Empty, argWidth);
                break;
            }
        }