private static void FormatToRightJustify <TBufferWriter, T>(ref TBufferWriter sb, T arg, int width, StandardFormat format, string argName)
            where TBufferWriter : IBufferWriter <byte>
        {
            if (typeof(T) == typeof(string))
            {
                var s       = Unsafe.As <string>(arg);
                int padding = width - s.Length;
                if (padding > 0)
                {
                    var paddingSpan = sb.GetSpan(padding);
                    paddingSpan.Fill(sp);
                    sb.Advance(padding);
                }

                ZString.AppendChars(ref sb, s.AsSpan());
            }
            else
            {
                Span <byte> s = stackalloc byte[typeof(T).IsValueType ? Unsafe.SizeOf <T>() * 8 : 1024];

                if (!Utf8ValueStringBuilder.FormatterCache <T> .TryFormatDelegate(arg, s, out var charsWritten, format))
                {
                    s = stackalloc byte[s.Length * 2];
                    if (!Utf8ValueStringBuilder.FormatterCache <T> .TryFormatDelegate(arg, s, out charsWritten, format))
                    {
                        ExceptionUtil.ThrowArgumentException(argName);
                    }
                }

                int padding = width - charsWritten;
                if (padding > 0)
                {
                    var paddingSpan = sb.GetSpan(padding);
                    paddingSpan.Fill(sp);
                    sb.Advance(padding);
                }

                var span = sb.GetSpan(charsWritten);
                s.CopyTo(span);
                sb.Advance(charsWritten);
            }
        }
Beispiel #2
0
 /// <summary>
 /// Creates a new instance with given format provider.
 /// </summary>
 public ZStringWriter(IFormatProvider formatProvider) : base(formatProvider)
 {
     sb     = ZString.CreateStringBuilder();
     isOpen = true;
 }