diff --git a/ExceptionUtil.cs b/ExceptionUtil.cs index 6441369..401e1c6 100644 --- a/ExceptionUtil.cs +++ b/ExceptionUtil.cs @@ -15,5 +15,10 @@ internal static void ThrowFormatException() { throw new FormatException("Index (zero based) must be greater than or equal to zero and less than the size of the argument list."); } + + internal static void ThrowFormatError() + { + throw new FormatException("Input string was not in a correct format."); + } } } diff --git a/FormatHelper.cs b/FormatHelper.cs new file mode 100644 index 0000000..de9846c --- /dev/null +++ b/FormatHelper.cs @@ -0,0 +1,172 @@ +using System; +using System.Text; +using System.Buffers; +using System.Runtime.CompilerServices; + +namespace Cysharp.Text +{ + internal static partial class Utf16FormatHelper + { + const char sp = (char)' '; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void FormatTo(ref TBufferWriter sb, T arg, int width, ReadOnlySpan format, string argName) + where TBufferWriter : IBufferWriter + { + if (width <= 0) // leftJustify + { + var span = sb.GetSpan(0); + if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg, span, out var argWritten, format)) + { + sb.Advance(0); + span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); + if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg, span, out argWritten, format)) + { + ExceptionUtil.ThrowArgumentException(argName); + } + } + sb.Advance(argWritten); + + width *= -1; + int padding = width - argWritten; + if (width > 0 && padding > 0) + { + var paddingSpan = sb.GetSpan(padding); + paddingSpan.Fill(sp); + sb.Advance(padding); + } + } + else + { + FormatToRightJustify(ref sb, arg, width, format, argName); + } + } + + private static void FormatToRightJustify(ref TBufferWriter sb, T arg, int width, ReadOnlySpan format, string argName) + where TBufferWriter : IBufferWriter + { + if (typeof(T) == typeof(string)) + { + var s = Unsafe.As(arg); + int padding = width - s.Length; + if (padding > 0) + { + var paddingSpan = sb.GetSpan(padding); + paddingSpan.Fill(sp); + sb.Advance(padding); + } + + var span = sb.GetSpan(s.Length); + s.AsSpan().CopyTo(span); + sb.Advance(s.Length); + } + else + { + Span s = stackalloc char[typeof(T).IsValueType ? Unsafe.SizeOf() * 8 : 1024]; + + if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg, s, out var charsWritten, format)) + { + s = stackalloc char[s.Length * 2]; + if (!Utf16ValueStringBuilder.FormatterCache.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); + } + } + } + + internal static partial class Utf8FormatHelper + { + const byte sp = (byte)' '; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void FormatTo(ref TBufferWriter sb, T arg, int width, StandardFormat format, string argName) + where TBufferWriter : IBufferWriter + { + if (width <= 0) // leftJustify + { + var span = sb.GetSpan(0); + if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg, span, out var argWritten, format)) + { + sb.Advance(0); + span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); + if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg, span, out argWritten, format)) + { + ExceptionUtil.ThrowArgumentException(argName); + } + } + sb.Advance(argWritten); + + width *= -1; + int padding = width - argWritten; + if (width > 0 && padding > 0) + { + var paddingSpan = sb.GetSpan(padding); + paddingSpan.Fill(sp); + sb.Advance(padding); + } + } + else + { + FormatToRightJustify(ref sb, arg, width, format, argName); + } + } + + private static void FormatToRightJustify(ref TBufferWriter sb, T arg, int width, StandardFormat format, string argName) + where TBufferWriter : IBufferWriter + { + if (typeof(T) == typeof(string)) + { + var s = Unsafe.As(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 s = stackalloc byte[typeof(T).IsValueType ? Unsafe.SizeOf() * 8 : 1024]; + + if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg, s, out var charsWritten, format)) + { + s = stackalloc byte[s.Length * 2]; + if (!Utf8ValueStringBuilder.FormatterCache.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); + } + } + } + +} \ No newline at end of file diff --git a/FormatParser.cs b/FormatParser.cs index eed6286..a955281 100644 --- a/FormatParser.cs +++ b/FormatParser.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.CompilerServices; namespace Cysharp.Text { @@ -11,45 +12,207 @@ public readonly ref struct ParseResult public readonly int Index; public readonly ReadOnlySpan FormatString; public readonly int LastIndex; + public readonly int Alignment; - public ParseResult(int index, ReadOnlySpan formatString, int lastIndex) + public ParseResult(int index, ReadOnlySpan formatString, int lastIndex, int alignment) { Index = index; FormatString = formatString; LastIndex = lastIndex; + Alignment = alignment; } } - public static ParseResult Parse(ReadOnlySpan format) + internal const int ArgLengthLimit = 16; + internal const int WidthLimit = 1000; // Note: -WidthLimit < ArgAlign < WidthLimit + + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ParserScanResult ScanFormatString(ReadOnlySpan format, ref int i) { - int index = -1; - var formatStart = -1; + var len = format.Length; + char c = format[i]; - // ignore start '{' - for (int i = 1; i < format.Length; i++) + i++; // points netxt char + if (c == '}') { - if (format[i] == '}') + // skip escaped '}' + if (i < len && format[i] == '}') + { + i++; + return ParserScanResult.EscapedChar; + } + else + { + ExceptionUtil.ThrowFormatError(); + return ParserScanResult.NormalChar; // NOTE Don't reached + } + } + else if (c == '{') + { + // skip escaped '{' + if (i < len && format[i] == '{') + { + i++; + return ParserScanResult.EscapedChar; + } + else { - if (index == -1) + i--; + return ParserScanResult.BraceOpen; + } + } + else + { + // ch is the normal char OR end of text + return ParserScanResult.NormalChar; + } + } + + // Accept only non-unicode numbers + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static bool IsDigit(char c) => '0' <= c && c <= '9'; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ParseResult Parse(string format, int i) + { + char c = default; + var len = format.Length; + + i++; // Skip `{` + + // === Index Component === + // ('0'-'9')+ WS* + + if (i == len || !IsDigit(c = format[i])) + { + ExceptionUtil.ThrowFormatError(); + } + + int paramIndex = 0; + do + { + paramIndex = (paramIndex * 10) + c - '0'; + + if (++i == len) + ExceptionUtil.ThrowFormatError(); + + c = format[i]; + } + while (IsDigit(c) && paramIndex < ArgLengthLimit); + + if (paramIndex >= ArgLengthLimit) + { + ExceptionUtil.ThrowFormatException(); + } + + // skip whitespace. + while (i < len && (c = format[i]) == ' ') + i++; + + // === Alignment Component === + // comma WS* minus? ('0'-'9')+ WS* + + int alignment = 0; + + if (c == ',') + { + i++; + + // skip whitespace. + while (i < len && (c = format[i]) == ' ') + i++; + + if (i == len) + { + ExceptionUtil.ThrowFormatError(); + } + + var leftJustify = false; + if (c == '-') + { + leftJustify = true; + + if (++i == len) + ExceptionUtil.ThrowFormatError(); + + c = format[i]; + } + + if (!IsDigit(c)) + { + ExceptionUtil.ThrowFormatError(); + } + + do + { + alignment = (alignment * 10) + c - '0'; + + if (++i == len) + ExceptionUtil.ThrowFormatError(); + + c = format[i]; + } + while (IsDigit(c) && alignment < WidthLimit); + + if (leftJustify) + alignment *= -1; + } + + // skip whitespace. + while (i < len && (c = format[i]) == ' ') + i++; + + // === Format String Component === + + ReadOnlySpan itemFormatSpan = default; + + if (c == ':') + { + i++; + int formatStart = i; + + while (true) + { + if (i == len) { - index = Int32.Parse(format.Slice(1, i - 1)); - return new ParseResult(index, default, i); + ExceptionUtil.ThrowFormatError(); } - else + c = format[i]; + + if (c == '}') { - var formatString = format.Slice(formatStart, i - formatStart); - return new ParseResult(index, formatString, i); + break; } + else if (c == '{') + { + ExceptionUtil.ThrowFormatError(); + } + + i++; } - if (index == -1 && (format[i] == ',' && format[i - 1] != '\\') || (format[i] == ':' && format[i - 1] != '\\')) + // has format + if (i > formatStart) { - index = Int32.Parse(format.Slice(1, i - 1)); - formatStart = i + 1; + itemFormatSpan = format.AsSpan(formatStart, i - formatStart); } } + else if (c != '}') + { + // Unexpected character + ExceptionUtil.ThrowFormatError(); + } - throw new FormatException("Invalid format string. format:" + format.ToString()); + i++; // Skip `}` + return new ParseResult(paramIndex, itemFormatSpan, i, alignment); } } + internal enum ParserScanResult + { + BraceOpen, + EscapedChar, + NormalChar, + } + } diff --git a/PreparedFormat.cs b/PreparedFormat.cs index c343a57..955c5ca 100644 --- a/PreparedFormat.cs +++ b/PreparedFormat.cs @@ -9,12 +9,12 @@ public sealed partial class Utf16PreparedFormat public string FormatString { get; } public int MinSize { get; } - readonly FormatSegment[] segments; + readonly Utf16FormatSegment[] segments; public Utf16PreparedFormat(string format) { this.FormatString = format; - this.segments = PreparedFormatHelper.Parse(format, false); + this.segments = PreparedFormatHelper.Utf16Parse(format); var size = 0; foreach (var item in segments) @@ -44,53 +44,42 @@ public string Format(T1 arg1) public void FormatTo(ref TBufferWriter sb, T1 arg1) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { - if (!item.IsFormatArgument) - { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var span = sb.GetSpan(item.Count); - strSpan.TryCopyTo(span); - sb.Advance(item.Count); - } - else + switch (item.FormatIndex) { - switch (item.FormatIndex) - { - case 0: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - sb.Advance(argWritten); - break; - } - default: - break; - } + case Utf16FormatSegment.NotFormatIndex: + { + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + break; + } + case 0: + { + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); + break; + } + default: + break; } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } public int MinSize { get; } - readonly FormatSegment[] segments; + readonly Utf16FormatSegment[] segments; public Utf16PreparedFormat(string format) { this.FormatString = format; - this.segments = PreparedFormatHelper.Parse(format, false); + this.segments = PreparedFormatHelper.Utf16Parse(format); var size = 0; foreach (var item in segments) @@ -120,68 +109,47 @@ public string Format(T1 arg1, T2 arg2) public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { - if (!item.IsFormatArgument) - { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var span = sb.GetSpan(item.Count); - strSpan.TryCopyTo(span); - sb.Advance(item.Count); - } - else + switch (item.FormatIndex) { - switch (item.FormatIndex) - { - case 0: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - sb.Advance(argWritten); - break; - } - case 1: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - sb.Advance(argWritten); - break; - } - default: - break; - } + case Utf16FormatSegment.NotFormatIndex: + { + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + break; + } + case 0: + { + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); + break; + } + case 1: + { + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); + break; + } + default: + break; } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } public int MinSize { get; } - readonly FormatSegment[] segments; + readonly Utf16FormatSegment[] segments; public Utf16PreparedFormat(string format) { this.FormatString = format; - this.segments = PreparedFormatHelper.Parse(format, false); + this.segments = PreparedFormatHelper.Utf16Parse(format); var size = 0; foreach (var item in segments) @@ -211,83 +179,52 @@ public string Format(T1 arg1, T2 arg2, T3 arg3) public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 arg3) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { - if (!item.IsFormatArgument) - { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var span = sb.GetSpan(item.Count); - strSpan.TryCopyTo(span); - sb.Advance(item.Count); - } - else + switch (item.FormatIndex) { - switch (item.FormatIndex) - { - case 0: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - sb.Advance(argWritten); - break; - } - case 1: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - sb.Advance(argWritten); - break; - } - case 2: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - sb.Advance(argWritten); - break; - } - default: - break; - } + case Utf16FormatSegment.NotFormatIndex: + { + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + break; + } + case 0: + { + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); + break; + } + case 1: + { + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); + break; + } + case 2: + { + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); + break; + } + default: + break; } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } public int MinSize { get; } - readonly FormatSegment[] segments; + readonly Utf16FormatSegment[] segments; public Utf16PreparedFormat(string format) { this.FormatString = format; - this.segments = PreparedFormatHelper.Parse(format, false); + this.segments = PreparedFormatHelper.Utf16Parse(format); var size = 0; foreach (var item in segments) @@ -317,98 +254,57 @@ public string Format(T1 arg1, T2 arg2, T3 arg3, T4 arg4) public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 arg3, T4 arg4) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { - if (!item.IsFormatArgument) + switch (item.FormatIndex) { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var span = sb.GetSpan(item.Count); - strSpan.TryCopyTo(span); - sb.Advance(item.Count); - } - else - { - switch (item.FormatIndex) - { - case 0: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - sb.Advance(argWritten); - break; - } - case 1: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - sb.Advance(argWritten); - break; - } - case 2: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - sb.Advance(argWritten); - break; - } - case 3: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - sb.Advance(argWritten); - break; - } - default: - break; - } + case Utf16FormatSegment.NotFormatIndex: + { + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + break; + } + case 0: + { + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); + break; + } + case 1: + { + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); + break; + } + case 2: + { + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); + break; + } + case 3: + { + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); + break; + } + default: + break; } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } public int MinSize { get; } - readonly FormatSegment[] segments; + readonly Utf16FormatSegment[] segments; public Utf16PreparedFormat(string format) { this.FormatString = format; - this.segments = PreparedFormatHelper.Parse(format, false); + this.segments = PreparedFormatHelper.Utf16Parse(format); var size = 0; foreach (var item in segments) @@ -438,113 +334,62 @@ public string Format(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5) public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { - if (!item.IsFormatArgument) + switch (item.FormatIndex) { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var span = sb.GetSpan(item.Count); - strSpan.TryCopyTo(span); - sb.Advance(item.Count); - } - else - { - switch (item.FormatIndex) - { - case 0: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - sb.Advance(argWritten); - break; - } - case 1: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - sb.Advance(argWritten); - break; - } - case 2: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - sb.Advance(argWritten); - break; - } - case 3: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - sb.Advance(argWritten); - break; - } - case 4: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg5)); - } - } - sb.Advance(argWritten); - break; - } - default: - break; - } + case Utf16FormatSegment.NotFormatIndex: + { + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + break; + } + case 0: + { + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); + break; + } + case 1: + { + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); + break; + } + case 2: + { + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); + break; + } + case 3: + { + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); + break; + } + case 4: + { + Utf16FormatHelper.FormatTo(ref sb, arg5, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg5)); + break; + } + default: + break; } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } public int MinSize { get; } - readonly FormatSegment[] segments; + readonly Utf16FormatSegment[] segments; public Utf16PreparedFormat(string format) { this.FormatString = format; - this.segments = PreparedFormatHelper.Parse(format, false); + this.segments = PreparedFormatHelper.Utf16Parse(format); var size = 0; foreach (var item in segments) @@ -574,128 +419,67 @@ public string Format(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6) public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { - if (!item.IsFormatArgument) + switch (item.FormatIndex) { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var span = sb.GetSpan(item.Count); - strSpan.TryCopyTo(span); - sb.Advance(item.Count); - } - else - { - switch (item.FormatIndex) - { - case 0: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - sb.Advance(argWritten); - break; - } - case 1: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - sb.Advance(argWritten); - break; - } - case 2: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - sb.Advance(argWritten); - break; - } - case 3: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - sb.Advance(argWritten); - break; - } - case 4: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg5)); - } - } - sb.Advance(argWritten); - break; - } - case 5: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg6)); - } - } - sb.Advance(argWritten); - break; - } - default: - break; - } + case Utf16FormatSegment.NotFormatIndex: + { + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + break; + } + case 0: + { + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); + break; + } + case 1: + { + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); + break; + } + case 2: + { + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); + break; + } + case 3: + { + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); + break; + } + case 4: + { + Utf16FormatHelper.FormatTo(ref sb, arg5, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg5)); + break; + } + case 5: + { + Utf16FormatHelper.FormatTo(ref sb, arg6, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg6)); + break; + } + default: + break; } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } public int MinSize { get; } - readonly FormatSegment[] segments; + readonly Utf16FormatSegment[] segments; public Utf16PreparedFormat(string format) { this.FormatString = format; - this.segments = PreparedFormatHelper.Parse(format, false); + this.segments = PreparedFormatHelper.Utf16Parse(format); var size = 0; foreach (var item in segments) @@ -725,143 +509,72 @@ public string Format(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 ar public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { - if (!item.IsFormatArgument) - { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var span = sb.GetSpan(item.Count); - strSpan.TryCopyTo(span); - sb.Advance(item.Count); - } - else + switch (item.FormatIndex) { - switch (item.FormatIndex) - { - case 0: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - sb.Advance(argWritten); - break; - } - case 1: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - sb.Advance(argWritten); - break; - } - case 2: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - sb.Advance(argWritten); - break; - } - case 3: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - sb.Advance(argWritten); - break; - } - case 4: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg5)); - } - } - sb.Advance(argWritten); - break; - } - case 5: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg6)); - } - } - sb.Advance(argWritten); - break; - } - case 6: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg7)); - } - } - sb.Advance(argWritten); - break; - } - default: - break; - } + case Utf16FormatSegment.NotFormatIndex: + { + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + break; + } + case 0: + { + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); + break; + } + case 1: + { + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); + break; + } + case 2: + { + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); + break; + } + case 3: + { + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); + break; + } + case 4: + { + Utf16FormatHelper.FormatTo(ref sb, arg5, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg5)); + break; + } + case 5: + { + Utf16FormatHelper.FormatTo(ref sb, arg6, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg6)); + break; + } + case 6: + { + Utf16FormatHelper.FormatTo(ref sb, arg7, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg7)); + break; + } + default: + break; } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } public int MinSize { get; } - readonly FormatSegment[] segments; + readonly Utf16FormatSegment[] segments; public Utf16PreparedFormat(string format) { this.FormatString = format; - this.segments = PreparedFormatHelper.Parse(format, false); + this.segments = PreparedFormatHelper.Utf16Parse(format); var size = 0; foreach (var item in segments) @@ -891,158 +604,77 @@ public string Format(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 ar public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { - if (!item.IsFormatArgument) + switch (item.FormatIndex) { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var span = sb.GetSpan(item.Count); - strSpan.TryCopyTo(span); - sb.Advance(item.Count); - } - else - { - switch (item.FormatIndex) - { - case 0: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - sb.Advance(argWritten); - break; - } - case 1: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - sb.Advance(argWritten); - break; - } - case 2: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - sb.Advance(argWritten); - break; - } - case 3: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - sb.Advance(argWritten); - break; - } - case 4: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg5)); - } - } - sb.Advance(argWritten); - break; - } - case 5: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg6)); - } - } - sb.Advance(argWritten); - break; - } - case 6: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg7)); - } - } - sb.Advance(argWritten); - break; - } - case 7: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg8)); - } - } - sb.Advance(argWritten); - break; - } - default: - break; - } + case Utf16FormatSegment.NotFormatIndex: + { + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + break; + } + case 0: + { + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); + break; + } + case 1: + { + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); + break; + } + case 2: + { + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); + break; + } + case 3: + { + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); + break; + } + case 4: + { + Utf16FormatHelper.FormatTo(ref sb, arg5, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg5)); + break; + } + case 5: + { + Utf16FormatHelper.FormatTo(ref sb, arg6, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg6)); + break; + } + case 6: + { + Utf16FormatHelper.FormatTo(ref sb, arg7, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg7)); + break; + } + case 7: + { + Utf16FormatHelper.FormatTo(ref sb, arg8, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg8)); + break; + } + default: + break; } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } public int MinSize { get; } - readonly FormatSegment[] segments; + readonly Utf16FormatSegment[] segments; public Utf16PreparedFormat(string format) { this.FormatString = format; - this.segments = PreparedFormatHelper.Parse(format, false); + this.segments = PreparedFormatHelper.Utf16Parse(format); var size = 0; foreach (var item in segments) @@ -1072,173 +704,82 @@ public string Format(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 ar public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { - if (!item.IsFormatArgument) - { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var span = sb.GetSpan(item.Count); - strSpan.TryCopyTo(span); - sb.Advance(item.Count); - } - else + switch (item.FormatIndex) { - switch (item.FormatIndex) - { - case 0: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - sb.Advance(argWritten); - break; - } - case 1: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - sb.Advance(argWritten); - break; - } - case 2: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - sb.Advance(argWritten); - break; - } - case 3: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - sb.Advance(argWritten); - break; - } - case 4: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg5)); - } - } - sb.Advance(argWritten); - break; - } - case 5: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg6)); - } - } - sb.Advance(argWritten); - break; - } - case 6: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg7)); - } - } - sb.Advance(argWritten); - break; - } - case 7: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg8)); - } - } - sb.Advance(argWritten); - break; - } - case 8: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg9)); - } - } - sb.Advance(argWritten); - break; - } - default: - break; - } + case Utf16FormatSegment.NotFormatIndex: + { + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + break; + } + case 0: + { + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); + break; + } + case 1: + { + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); + break; + } + case 2: + { + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); + break; + } + case 3: + { + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); + break; + } + case 4: + { + Utf16FormatHelper.FormatTo(ref sb, arg5, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg5)); + break; + } + case 5: + { + Utf16FormatHelper.FormatTo(ref sb, arg6, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg6)); + break; + } + case 6: + { + Utf16FormatHelper.FormatTo(ref sb, arg7, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg7)); + break; + } + case 7: + { + Utf16FormatHelper.FormatTo(ref sb, arg8, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg8)); + break; + } + case 8: + { + Utf16FormatHelper.FormatTo(ref sb, arg9, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg9)); + break; + } + default: + break; } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } public int MinSize { get; } - readonly FormatSegment[] segments; + readonly Utf16FormatSegment[] segments; public Utf16PreparedFormat(string format) { this.FormatString = format; - this.segments = PreparedFormatHelper.Parse(format, false); + this.segments = PreparedFormatHelper.Utf16Parse(format); var size = 0; foreach (var item in segments) @@ -1268,188 +809,87 @@ public string Format(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 ar public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { - if (!item.IsFormatArgument) + switch (item.FormatIndex) { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var span = sb.GetSpan(item.Count); - strSpan.TryCopyTo(span); - sb.Advance(item.Count); - } - else - { - switch (item.FormatIndex) - { - case 0: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - sb.Advance(argWritten); - break; - } - case 1: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - sb.Advance(argWritten); - break; - } - case 2: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - sb.Advance(argWritten); - break; - } - case 3: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - sb.Advance(argWritten); - break; - } - case 4: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg5)); - } - } - sb.Advance(argWritten); - break; - } - case 5: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg6)); - } - } - sb.Advance(argWritten); - break; - } - case 6: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg7)); - } - } - sb.Advance(argWritten); - break; - } - case 7: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg8)); - } - } - sb.Advance(argWritten); - break; - } - case 8: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg9)); - } - } - sb.Advance(argWritten); - break; - } - case 9: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg10)); - } - } - sb.Advance(argWritten); - break; - } - default: - break; - } + case Utf16FormatSegment.NotFormatIndex: + { + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + break; + } + case 0: + { + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); + break; + } + case 1: + { + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); + break; + } + case 2: + { + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); + break; + } + case 3: + { + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); + break; + } + case 4: + { + Utf16FormatHelper.FormatTo(ref sb, arg5, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg5)); + break; + } + case 5: + { + Utf16FormatHelper.FormatTo(ref sb, arg6, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg6)); + break; + } + case 6: + { + Utf16FormatHelper.FormatTo(ref sb, arg7, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg7)); + break; + } + case 7: + { + Utf16FormatHelper.FormatTo(ref sb, arg8, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg8)); + break; + } + case 8: + { + Utf16FormatHelper.FormatTo(ref sb, arg9, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg9)); + break; + } + case 9: + { + Utf16FormatHelper.FormatTo(ref sb, arg10, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg10)); + break; + } + default: + break; } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } public int MinSize { get; } - readonly FormatSegment[] segments; + readonly Utf16FormatSegment[] segments; public Utf16PreparedFormat(string format) { this.FormatString = format; - this.segments = PreparedFormatHelper.Parse(format, false); + this.segments = PreparedFormatHelper.Utf16Parse(format); var size = 0; foreach (var item in segments) @@ -1479,203 +919,92 @@ public string Format(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 ar public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { - if (!item.IsFormatArgument) - { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var span = sb.GetSpan(item.Count); - strSpan.TryCopyTo(span); - sb.Advance(item.Count); - } - else + switch (item.FormatIndex) { - switch (item.FormatIndex) - { - case 0: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - sb.Advance(argWritten); - break; - } - case 1: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - sb.Advance(argWritten); - break; - } - case 2: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - sb.Advance(argWritten); - break; - } - case 3: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - sb.Advance(argWritten); - break; - } - case 4: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg5)); - } - } - sb.Advance(argWritten); - break; - } - case 5: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg6)); - } - } - sb.Advance(argWritten); - break; - } - case 6: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg7)); - } - } - sb.Advance(argWritten); - break; - } - case 7: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg8)); - } - } - sb.Advance(argWritten); - break; - } - case 8: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg9)); - } - } - sb.Advance(argWritten); - break; - } - case 9: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg10)); - } - } - sb.Advance(argWritten); - break; - } - case 10: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg11, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg11, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg11)); - } - } - sb.Advance(argWritten); - break; - } - default: - break; - } + case Utf16FormatSegment.NotFormatIndex: + { + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + break; + } + case 0: + { + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); + break; + } + case 1: + { + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); + break; + } + case 2: + { + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); + break; + } + case 3: + { + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); + break; + } + case 4: + { + Utf16FormatHelper.FormatTo(ref sb, arg5, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg5)); + break; + } + case 5: + { + Utf16FormatHelper.FormatTo(ref sb, arg6, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg6)); + break; + } + case 6: + { + Utf16FormatHelper.FormatTo(ref sb, arg7, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg7)); + break; + } + case 7: + { + Utf16FormatHelper.FormatTo(ref sb, arg8, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg8)); + break; + } + case 8: + { + Utf16FormatHelper.FormatTo(ref sb, arg9, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg9)); + break; + } + case 9: + { + Utf16FormatHelper.FormatTo(ref sb, arg10, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg10)); + break; + } + case 10: + { + Utf16FormatHelper.FormatTo(ref sb, arg11, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg11)); + break; + } + default: + break; } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } public int MinSize { get; } - readonly FormatSegment[] segments; + readonly Utf16FormatSegment[] segments; public Utf16PreparedFormat(string format) { this.FormatString = format; - this.segments = PreparedFormatHelper.Parse(format, false); + this.segments = PreparedFormatHelper.Utf16Parse(format); var size = 0; foreach (var item in segments) @@ -1705,218 +1034,97 @@ public string Format(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 ar public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { - if (!item.IsFormatArgument) + switch (item.FormatIndex) { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var span = sb.GetSpan(item.Count); - strSpan.TryCopyTo(span); - sb.Advance(item.Count); - } - else - { - switch (item.FormatIndex) - { - case 0: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - sb.Advance(argWritten); - break; - } - case 1: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - sb.Advance(argWritten); - break; - } - case 2: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - sb.Advance(argWritten); - break; - } - case 3: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - sb.Advance(argWritten); - break; - } - case 4: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg5)); - } - } - sb.Advance(argWritten); - break; - } - case 5: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg6)); - } - } - sb.Advance(argWritten); - break; - } - case 6: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg7)); - } - } - sb.Advance(argWritten); - break; - } - case 7: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg8)); - } - } - sb.Advance(argWritten); - break; - } - case 8: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg9)); - } - } - sb.Advance(argWritten); - break; - } - case 9: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg10)); - } - } - sb.Advance(argWritten); - break; - } - case 10: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg11, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg11, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg11)); - } - } - sb.Advance(argWritten); - break; - } - case 11: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg12, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg12, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg12)); - } - } - sb.Advance(argWritten); - break; - } - default: - break; - } + case Utf16FormatSegment.NotFormatIndex: + { + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + break; + } + case 0: + { + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); + break; + } + case 1: + { + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); + break; + } + case 2: + { + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); + break; + } + case 3: + { + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); + break; + } + case 4: + { + Utf16FormatHelper.FormatTo(ref sb, arg5, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg5)); + break; + } + case 5: + { + Utf16FormatHelper.FormatTo(ref sb, arg6, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg6)); + break; + } + case 6: + { + Utf16FormatHelper.FormatTo(ref sb, arg7, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg7)); + break; + } + case 7: + { + Utf16FormatHelper.FormatTo(ref sb, arg8, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg8)); + break; + } + case 8: + { + Utf16FormatHelper.FormatTo(ref sb, arg9, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg9)); + break; + } + case 9: + { + Utf16FormatHelper.FormatTo(ref sb, arg10, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg10)); + break; + } + case 10: + { + Utf16FormatHelper.FormatTo(ref sb, arg11, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg11)); + break; + } + case 11: + { + Utf16FormatHelper.FormatTo(ref sb, arg12, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg12)); + break; + } + default: + break; } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } public int MinSize { get; } - readonly FormatSegment[] segments; + readonly Utf16FormatSegment[] segments; public Utf16PreparedFormat(string format) { this.FormatString = format; - this.segments = PreparedFormatHelper.Parse(format, false); + this.segments = PreparedFormatHelper.Utf16Parse(format); var size = 0; foreach (var item in segments) @@ -1946,233 +1154,102 @@ public string Format(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 ar public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { - if (!item.IsFormatArgument) + switch (item.FormatIndex) { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var span = sb.GetSpan(item.Count); - strSpan.TryCopyTo(span); - sb.Advance(item.Count); - } - else - { - switch (item.FormatIndex) - { - case 0: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - sb.Advance(argWritten); - break; - } - case 1: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - sb.Advance(argWritten); - break; - } - case 2: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - sb.Advance(argWritten); - break; - } - case 3: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - sb.Advance(argWritten); - break; - } - case 4: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg5)); - } - } - sb.Advance(argWritten); - break; - } - case 5: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg6)); - } - } - sb.Advance(argWritten); - break; - } - case 6: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg7)); - } - } - sb.Advance(argWritten); - break; - } - case 7: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg8)); - } - } - sb.Advance(argWritten); - break; - } - case 8: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg9)); - } - } - sb.Advance(argWritten); - break; - } - case 9: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg10)); - } - } - sb.Advance(argWritten); - break; - } - case 10: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg11, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg11, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg11)); - } - } - sb.Advance(argWritten); - break; - } - case 11: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg12, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg12, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg12)); - } - } - sb.Advance(argWritten); - break; - } - case 12: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg13, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg13, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg13)); - } - } - sb.Advance(argWritten); - break; - } - default: - break; - } + case Utf16FormatSegment.NotFormatIndex: + { + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + break; + } + case 0: + { + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); + break; + } + case 1: + { + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); + break; + } + case 2: + { + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); + break; + } + case 3: + { + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); + break; + } + case 4: + { + Utf16FormatHelper.FormatTo(ref sb, arg5, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg5)); + break; + } + case 5: + { + Utf16FormatHelper.FormatTo(ref sb, arg6, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg6)); + break; + } + case 6: + { + Utf16FormatHelper.FormatTo(ref sb, arg7, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg7)); + break; + } + case 7: + { + Utf16FormatHelper.FormatTo(ref sb, arg8, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg8)); + break; + } + case 8: + { + Utf16FormatHelper.FormatTo(ref sb, arg9, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg9)); + break; + } + case 9: + { + Utf16FormatHelper.FormatTo(ref sb, arg10, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg10)); + break; + } + case 10: + { + Utf16FormatHelper.FormatTo(ref sb, arg11, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg11)); + break; + } + case 11: + { + Utf16FormatHelper.FormatTo(ref sb, arg12, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg12)); + break; + } + case 12: + { + Utf16FormatHelper.FormatTo(ref sb, arg13, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg13)); + break; + } + default: + break; } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } public int MinSize { get; } - readonly FormatSegment[] segments; + readonly Utf16FormatSegment[] segments; public Utf16PreparedFormat(string format) { this.FormatString = format; - this.segments = PreparedFormatHelper.Parse(format, false); + this.segments = PreparedFormatHelper.Utf16Parse(format); var size = 0; foreach (var item in segments) @@ -2202,248 +1279,107 @@ public string Format(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 ar public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { - if (!item.IsFormatArgument) + switch (item.FormatIndex) { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var span = sb.GetSpan(item.Count); - strSpan.TryCopyTo(span); - sb.Advance(item.Count); - } - else - { - switch (item.FormatIndex) - { - case 0: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - sb.Advance(argWritten); - break; - } - case 1: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - sb.Advance(argWritten); - break; - } - case 2: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - sb.Advance(argWritten); - break; - } - case 3: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - sb.Advance(argWritten); - break; - } - case 4: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg5)); - } - } - sb.Advance(argWritten); - break; - } - case 5: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg6)); - } - } - sb.Advance(argWritten); - break; - } - case 6: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg7)); - } - } - sb.Advance(argWritten); - break; - } - case 7: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg8)); - } - } - sb.Advance(argWritten); - break; - } - case 8: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg9)); - } - } - sb.Advance(argWritten); - break; - } - case 9: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg10)); - } - } - sb.Advance(argWritten); - break; - } - case 10: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg11, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg11, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg11)); - } - } - sb.Advance(argWritten); - break; - } - case 11: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg12, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg12, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg12)); - } - } - sb.Advance(argWritten); - break; - } - case 12: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg13, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg13, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg13)); - } - } - sb.Advance(argWritten); - break; - } - case 13: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg14, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg14, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg14)); - } - } - sb.Advance(argWritten); - break; - } - default: - break; - } + case Utf16FormatSegment.NotFormatIndex: + { + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + break; + } + case 0: + { + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); + break; + } + case 1: + { + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); + break; + } + case 2: + { + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); + break; + } + case 3: + { + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); + break; + } + case 4: + { + Utf16FormatHelper.FormatTo(ref sb, arg5, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg5)); + break; + } + case 5: + { + Utf16FormatHelper.FormatTo(ref sb, arg6, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg6)); + break; + } + case 6: + { + Utf16FormatHelper.FormatTo(ref sb, arg7, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg7)); + break; + } + case 7: + { + Utf16FormatHelper.FormatTo(ref sb, arg8, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg8)); + break; + } + case 8: + { + Utf16FormatHelper.FormatTo(ref sb, arg9, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg9)); + break; + } + case 9: + { + Utf16FormatHelper.FormatTo(ref sb, arg10, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg10)); + break; + } + case 10: + { + Utf16FormatHelper.FormatTo(ref sb, arg11, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg11)); + break; + } + case 11: + { + Utf16FormatHelper.FormatTo(ref sb, arg12, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg12)); + break; + } + case 12: + { + Utf16FormatHelper.FormatTo(ref sb, arg13, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg13)); + break; + } + case 13: + { + Utf16FormatHelper.FormatTo(ref sb, arg14, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg14)); + break; + } + default: + break; } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } public int MinSize { get; } - readonly FormatSegment[] segments; + readonly Utf16FormatSegment[] segments; public Utf16PreparedFormat(string format) { this.FormatString = format; - this.segments = PreparedFormatHelper.Parse(format, false); + this.segments = PreparedFormatHelper.Utf16Parse(format); var size = 0; foreach (var item in segments) @@ -2473,263 +1409,112 @@ public string Format(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 ar public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { - if (!item.IsFormatArgument) - { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var span = sb.GetSpan(item.Count); - strSpan.TryCopyTo(span); - sb.Advance(item.Count); - } - else + switch (item.FormatIndex) { - switch (item.FormatIndex) - { - case 0: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - sb.Advance(argWritten); - break; - } - case 1: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - sb.Advance(argWritten); - break; - } - case 2: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - sb.Advance(argWritten); - break; - } - case 3: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - sb.Advance(argWritten); - break; - } - case 4: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg5)); - } - } - sb.Advance(argWritten); - break; - } - case 5: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg6)); - } - } - sb.Advance(argWritten); - break; - } - case 6: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg7)); - } - } - sb.Advance(argWritten); - break; - } - case 7: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg8)); - } - } - sb.Advance(argWritten); - break; - } - case 8: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg9)); - } - } - sb.Advance(argWritten); - break; - } - case 9: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg10)); - } - } - sb.Advance(argWritten); - break; - } - case 10: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg11, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg11, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg11)); - } - } - sb.Advance(argWritten); - break; - } - case 11: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg12, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg12, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg12)); - } - } - sb.Advance(argWritten); - break; - } - case 12: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg13, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg13, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg13)); - } - } - sb.Advance(argWritten); - break; - } - case 13: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg14, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg14, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg14)); - } - } - sb.Advance(argWritten); - break; - } - case 14: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg15, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg15, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg15)); - } - } - sb.Advance(argWritten); - break; - } - default: - break; - } + case Utf16FormatSegment.NotFormatIndex: + { + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + break; + } + case 0: + { + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); + break; + } + case 1: + { + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); + break; + } + case 2: + { + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); + break; + } + case 3: + { + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); + break; + } + case 4: + { + Utf16FormatHelper.FormatTo(ref sb, arg5, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg5)); + break; + } + case 5: + { + Utf16FormatHelper.FormatTo(ref sb, arg6, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg6)); + break; + } + case 6: + { + Utf16FormatHelper.FormatTo(ref sb, arg7, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg7)); + break; + } + case 7: + { + Utf16FormatHelper.FormatTo(ref sb, arg8, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg8)); + break; + } + case 8: + { + Utf16FormatHelper.FormatTo(ref sb, arg9, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg9)); + break; + } + case 9: + { + Utf16FormatHelper.FormatTo(ref sb, arg10, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg10)); + break; + } + case 10: + { + Utf16FormatHelper.FormatTo(ref sb, arg11, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg11)); + break; + } + case 11: + { + Utf16FormatHelper.FormatTo(ref sb, arg12, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg12)); + break; + } + case 12: + { + Utf16FormatHelper.FormatTo(ref sb, arg13, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg13)); + break; + } + case 13: + { + Utf16FormatHelper.FormatTo(ref sb, arg14, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg14)); + break; + } + case 14: + { + Utf16FormatHelper.FormatTo(ref sb, arg15, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg15)); + break; + } + default: + break; } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } public int MinSize { get; } - readonly FormatSegment[] segments; + readonly Utf16FormatSegment[] segments; public Utf16PreparedFormat(string format) { this.FormatString = format; - this.segments = PreparedFormatHelper.Parse(format, false); + this.segments = PreparedFormatHelper.Utf16Parse(format); var size = 0; foreach (var item in segments) @@ -2759,278 +1544,118 @@ public string Format(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 ar public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { - if (!item.IsFormatArgument) - { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var span = sb.GetSpan(item.Count); - strSpan.TryCopyTo(span); - sb.Advance(item.Count); - } - else + switch (item.FormatIndex) { - switch (item.FormatIndex) - { - case 0: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - sb.Advance(argWritten); - break; - } - case 1: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - sb.Advance(argWritten); - break; - } - case 2: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - sb.Advance(argWritten); - break; - } - case 3: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - sb.Advance(argWritten); - break; - } - case 4: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg5)); - } - } - sb.Advance(argWritten); - break; - } - case 5: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg6)); - } - } - sb.Advance(argWritten); - break; - } - case 6: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg7)); - } - } - sb.Advance(argWritten); - break; - } - case 7: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg8)); - } - } - sb.Advance(argWritten); - break; - } - case 8: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg9)); - } - } - sb.Advance(argWritten); - break; - } - case 9: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg10)); - } - } - sb.Advance(argWritten); - break; - } - case 10: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg11, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg11, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg11)); - } - } - sb.Advance(argWritten); - break; - } - case 11: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg12, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg12, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg12)); - } - } - sb.Advance(argWritten); - break; - } - case 12: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg13, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg13, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg13)); - } - } - sb.Advance(argWritten); - break; - } - case 13: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg14, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg14, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg14)); - } - } - sb.Advance(argWritten); - break; - } - case 14: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg15, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg15, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg15)); - } - } - sb.Advance(argWritten); - break; - } - case 15: - { - var span = sb.GetSpan(0); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg16, span, out var argWritten, item.FormatString.AsSpan())) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg16, span, out argWritten, item.FormatString.AsSpan())) - { - ExceptionUtil.ThrowArgumentException(nameof(arg16)); - } - } - sb.Advance(argWritten); - break; - } - default: - break; - } + case Utf16FormatSegment.NotFormatIndex: + { + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + break; + } + case 0: + { + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); + break; + } + case 1: + { + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); + break; + } + case 2: + { + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); + break; + } + case 3: + { + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); + break; + } + case 4: + { + Utf16FormatHelper.FormatTo(ref sb, arg5, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg5)); + break; + } + case 5: + { + Utf16FormatHelper.FormatTo(ref sb, arg6, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg6)); + break; + } + case 6: + { + Utf16FormatHelper.FormatTo(ref sb, arg7, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg7)); + break; + } + case 7: + { + Utf16FormatHelper.FormatTo(ref sb, arg8, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg8)); + break; + } + case 8: + { + Utf16FormatHelper.FormatTo(ref sb, arg9, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg9)); + break; + } + case 9: + { + Utf16FormatHelper.FormatTo(ref sb, arg10, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg10)); + break; + } + case 10: + { + Utf16FormatHelper.FormatTo(ref sb, arg11, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg11)); + break; + } + case 11: + { + Utf16FormatHelper.FormatTo(ref sb, arg12, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg12)); + break; + } + case 12: + { + Utf16FormatHelper.FormatTo(ref sb, arg13, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg13)); + break; + } + case 13: + { + Utf16FormatHelper.FormatTo(ref sb, arg14, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg14)); + break; + } + case 14: + { + Utf16FormatHelper.FormatTo(ref sb, arg15, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg15)); + break; + } + case 15: + { + Utf16FormatHelper.FormatTo(ref sb, arg16, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg16)); + break; + } + default: + break; } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } public int MinSize { get; } - readonly FormatSegment[] segments; + readonly Utf8FormatSegment[] segments; + readonly byte[] utf8PreEncodedbuffer; public Utf8PreparedFormat(string format) { this.FormatString = format; - this.segments = PreparedFormatHelper.Parse(format, true); + this.segments = PreparedFormatHelper.Utf8Parse(format, out utf8PreEncodedbuffer); var size = 0; foreach (var item in segments) @@ -3060,54 +1685,43 @@ public string Format(T1 arg1) public void FormatTo(ref TBufferWriter sb, T1 arg1) where TBufferWriter : IBufferWriter { + var formatSpan = utf8PreEncodedbuffer.AsSpan(); + foreach (var item in segments) { - if (!item.IsFormatArgument) - { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Encoding.UTF8.GetMaxByteCount(item.Count); - var span = sb.GetSpan(size); - var count = Encoding.UTF8.GetBytes(strSpan, span); - sb.Advance(count); - } - else + switch (item.FormatIndex) { - switch (item.FormatIndex) - { - case 0: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - sb.Advance(argWritten); - break; - } - default: - break; - } + case Utf8FormatSegment.NotFormatIndex: + { + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + break; + } + case 0: + { + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); + break; + } + default: + break; } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } public int MinSize { get; } - readonly FormatSegment[] segments; + readonly Utf8FormatSegment[] segments; + readonly byte[] utf8PreEncodedbuffer; public Utf8PreparedFormat(string format) { this.FormatString = format; - this.segments = PreparedFormatHelper.Parse(format, true); + this.segments = PreparedFormatHelper.Utf8Parse(format, out utf8PreEncodedbuffer); var size = 0; foreach (var item in segments) @@ -3137,69 +1751,48 @@ public string Format(T1 arg1, T2 arg2) public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2) where TBufferWriter : IBufferWriter { + var formatSpan = utf8PreEncodedbuffer.AsSpan(); + foreach (var item in segments) { - if (!item.IsFormatArgument) - { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Encoding.UTF8.GetMaxByteCount(item.Count); - var span = sb.GetSpan(size); - var count = Encoding.UTF8.GetBytes(strSpan, span); - sb.Advance(count); - } - else + switch (item.FormatIndex) { - switch (item.FormatIndex) - { - case 0: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - sb.Advance(argWritten); - break; - } - case 1: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - sb.Advance(argWritten); - break; - } - default: - break; - } + case Utf8FormatSegment.NotFormatIndex: + { + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + break; + } + case 0: + { + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); + break; + } + case 1: + { + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); + break; + } + default: + break; } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } public int MinSize { get; } - readonly FormatSegment[] segments; + readonly Utf8FormatSegment[] segments; + readonly byte[] utf8PreEncodedbuffer; public Utf8PreparedFormat(string format) { this.FormatString = format; - this.segments = PreparedFormatHelper.Parse(format, true); + this.segments = PreparedFormatHelper.Utf8Parse(format, out utf8PreEncodedbuffer); var size = 0; foreach (var item in segments) @@ -3229,84 +1822,53 @@ public string Format(T1 arg1, T2 arg2, T3 arg3) public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 arg3) where TBufferWriter : IBufferWriter { + var formatSpan = utf8PreEncodedbuffer.AsSpan(); + foreach (var item in segments) { - if (!item.IsFormatArgument) - { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Encoding.UTF8.GetMaxByteCount(item.Count); - var span = sb.GetSpan(size); - var count = Encoding.UTF8.GetBytes(strSpan, span); - sb.Advance(count); - } - else + switch (item.FormatIndex) { - switch (item.FormatIndex) - { - case 0: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - sb.Advance(argWritten); - break; - } - case 1: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - sb.Advance(argWritten); - break; - } - case 2: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - sb.Advance(argWritten); - break; - } - default: - break; - } + case Utf8FormatSegment.NotFormatIndex: + { + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + break; + } + case 0: + { + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); + break; + } + case 1: + { + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); + break; + } + case 2: + { + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); + break; + } + default: + break; } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } public int MinSize { get; } - readonly FormatSegment[] segments; + readonly Utf8FormatSegment[] segments; + readonly byte[] utf8PreEncodedbuffer; public Utf8PreparedFormat(string format) { this.FormatString = format; - this.segments = PreparedFormatHelper.Parse(format, true); + this.segments = PreparedFormatHelper.Utf8Parse(format, out utf8PreEncodedbuffer); var size = 0; foreach (var item in segments) @@ -3336,99 +1898,58 @@ public string Format(T1 arg1, T2 arg2, T3 arg3, T4 arg4) public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 arg3, T4 arg4) where TBufferWriter : IBufferWriter { + var formatSpan = utf8PreEncodedbuffer.AsSpan(); + foreach (var item in segments) { - if (!item.IsFormatArgument) + switch (item.FormatIndex) { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Encoding.UTF8.GetMaxByteCount(item.Count); - var span = sb.GetSpan(size); - var count = Encoding.UTF8.GetBytes(strSpan, span); - sb.Advance(count); - } - else - { - switch (item.FormatIndex) - { - case 0: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - sb.Advance(argWritten); - break; - } - case 1: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - sb.Advance(argWritten); - break; - } - case 2: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - sb.Advance(argWritten); - break; - } - case 3: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - sb.Advance(argWritten); - break; - } - default: - break; - } + case Utf8FormatSegment.NotFormatIndex: + { + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + break; + } + case 0: + { + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); + break; + } + case 1: + { + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); + break; + } + case 2: + { + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); + break; + } + case 3: + { + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); + break; + } + default: + break; } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } public int MinSize { get; } - readonly FormatSegment[] segments; + readonly Utf8FormatSegment[] segments; + readonly byte[] utf8PreEncodedbuffer; public Utf8PreparedFormat(string format) { this.FormatString = format; - this.segments = PreparedFormatHelper.Parse(format, true); + this.segments = PreparedFormatHelper.Utf8Parse(format, out utf8PreEncodedbuffer); var size = 0; foreach (var item in segments) @@ -3458,114 +1979,63 @@ public string Format(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5) public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5) where TBufferWriter : IBufferWriter { + var formatSpan = utf8PreEncodedbuffer.AsSpan(); + foreach (var item in segments) { - if (!item.IsFormatArgument) + switch (item.FormatIndex) { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Encoding.UTF8.GetMaxByteCount(item.Count); - var span = sb.GetSpan(size); - var count = Encoding.UTF8.GetBytes(strSpan, span); - sb.Advance(count); - } - else - { - switch (item.FormatIndex) - { - case 0: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - sb.Advance(argWritten); - break; - } - case 1: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - sb.Advance(argWritten); - break; - } - case 2: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - sb.Advance(argWritten); - break; - } - case 3: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - sb.Advance(argWritten); - break; - } - case 4: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg5)); - } - } - sb.Advance(argWritten); - break; - } - default: - break; - } + case Utf8FormatSegment.NotFormatIndex: + { + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + break; + } + case 0: + { + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); + break; + } + case 1: + { + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); + break; + } + case 2: + { + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); + break; + } + case 3: + { + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); + break; + } + case 4: + { + Utf8FormatHelper.FormatTo(ref sb, arg5, item.Alignment, item.StandardFormat, nameof(arg5)); + break; + } + default: + break; } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } public int MinSize { get; } - readonly FormatSegment[] segments; + readonly Utf8FormatSegment[] segments; + readonly byte[] utf8PreEncodedbuffer; public Utf8PreparedFormat(string format) { this.FormatString = format; - this.segments = PreparedFormatHelper.Parse(format, true); + this.segments = PreparedFormatHelper.Utf8Parse(format, out utf8PreEncodedbuffer); var size = 0; foreach (var item in segments) @@ -3595,129 +2065,68 @@ public string Format(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6) public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6) where TBufferWriter : IBufferWriter { + var formatSpan = utf8PreEncodedbuffer.AsSpan(); + foreach (var item in segments) { - if (!item.IsFormatArgument) + switch (item.FormatIndex) { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Encoding.UTF8.GetMaxByteCount(item.Count); - var span = sb.GetSpan(size); - var count = Encoding.UTF8.GetBytes(strSpan, span); - sb.Advance(count); - } - else - { - switch (item.FormatIndex) - { - case 0: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - sb.Advance(argWritten); - break; - } - case 1: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - sb.Advance(argWritten); - break; - } - case 2: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - sb.Advance(argWritten); - break; - } - case 3: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - sb.Advance(argWritten); - break; - } - case 4: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg5)); - } - } - sb.Advance(argWritten); - break; - } - case 5: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg6)); - } - } - sb.Advance(argWritten); - break; - } - default: - break; - } + case Utf8FormatSegment.NotFormatIndex: + { + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + break; + } + case 0: + { + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); + break; + } + case 1: + { + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); + break; + } + case 2: + { + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); + break; + } + case 3: + { + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); + break; + } + case 4: + { + Utf8FormatHelper.FormatTo(ref sb, arg5, item.Alignment, item.StandardFormat, nameof(arg5)); + break; + } + case 5: + { + Utf8FormatHelper.FormatTo(ref sb, arg6, item.Alignment, item.StandardFormat, nameof(arg6)); + break; + } + default: + break; } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } public int MinSize { get; } - readonly FormatSegment[] segments; + readonly Utf8FormatSegment[] segments; + readonly byte[] utf8PreEncodedbuffer; public Utf8PreparedFormat(string format) { this.FormatString = format; - this.segments = PreparedFormatHelper.Parse(format, true); + this.segments = PreparedFormatHelper.Utf8Parse(format, out utf8PreEncodedbuffer); var size = 0; foreach (var item in segments) @@ -3747,144 +2156,73 @@ public string Format(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 ar public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7) where TBufferWriter : IBufferWriter { + var formatSpan = utf8PreEncodedbuffer.AsSpan(); + foreach (var item in segments) { - if (!item.IsFormatArgument) - { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Encoding.UTF8.GetMaxByteCount(item.Count); - var span = sb.GetSpan(size); - var count = Encoding.UTF8.GetBytes(strSpan, span); - sb.Advance(count); - } - else + switch (item.FormatIndex) { - switch (item.FormatIndex) - { - case 0: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - sb.Advance(argWritten); - break; - } - case 1: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - sb.Advance(argWritten); - break; - } - case 2: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - sb.Advance(argWritten); - break; - } - case 3: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - sb.Advance(argWritten); - break; - } - case 4: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg5)); - } - } - sb.Advance(argWritten); - break; - } - case 5: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg6)); - } - } - sb.Advance(argWritten); - break; - } - case 6: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg7)); - } - } - sb.Advance(argWritten); - break; - } - default: - break; - } + case Utf8FormatSegment.NotFormatIndex: + { + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + break; + } + case 0: + { + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); + break; + } + case 1: + { + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); + break; + } + case 2: + { + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); + break; + } + case 3: + { + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); + break; + } + case 4: + { + Utf8FormatHelper.FormatTo(ref sb, arg5, item.Alignment, item.StandardFormat, nameof(arg5)); + break; + } + case 5: + { + Utf8FormatHelper.FormatTo(ref sb, arg6, item.Alignment, item.StandardFormat, nameof(arg6)); + break; + } + case 6: + { + Utf8FormatHelper.FormatTo(ref sb, arg7, item.Alignment, item.StandardFormat, nameof(arg7)); + break; + } + default: + break; } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } public int MinSize { get; } - readonly FormatSegment[] segments; + readonly Utf8FormatSegment[] segments; + readonly byte[] utf8PreEncodedbuffer; public Utf8PreparedFormat(string format) { this.FormatString = format; - this.segments = PreparedFormatHelper.Parse(format, true); + this.segments = PreparedFormatHelper.Utf8Parse(format, out utf8PreEncodedbuffer); var size = 0; foreach (var item in segments) @@ -3914,159 +2252,78 @@ public string Format(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 ar public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8) where TBufferWriter : IBufferWriter { + var formatSpan = utf8PreEncodedbuffer.AsSpan(); + foreach (var item in segments) { - if (!item.IsFormatArgument) + switch (item.FormatIndex) { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Encoding.UTF8.GetMaxByteCount(item.Count); - var span = sb.GetSpan(size); - var count = Encoding.UTF8.GetBytes(strSpan, span); - sb.Advance(count); - } - else - { - switch (item.FormatIndex) - { - case 0: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - sb.Advance(argWritten); - break; - } - case 1: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - sb.Advance(argWritten); - break; - } - case 2: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - sb.Advance(argWritten); - break; - } - case 3: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - sb.Advance(argWritten); - break; - } - case 4: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg5)); - } - } - sb.Advance(argWritten); - break; - } - case 5: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg6)); - } - } - sb.Advance(argWritten); - break; - } - case 6: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg7)); - } - } - sb.Advance(argWritten); - break; - } - case 7: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg8)); - } - } - sb.Advance(argWritten); - break; - } - default: - break; - } + case Utf8FormatSegment.NotFormatIndex: + { + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + break; + } + case 0: + { + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); + break; + } + case 1: + { + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); + break; + } + case 2: + { + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); + break; + } + case 3: + { + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); + break; + } + case 4: + { + Utf8FormatHelper.FormatTo(ref sb, arg5, item.Alignment, item.StandardFormat, nameof(arg5)); + break; + } + case 5: + { + Utf8FormatHelper.FormatTo(ref sb, arg6, item.Alignment, item.StandardFormat, nameof(arg6)); + break; + } + case 6: + { + Utf8FormatHelper.FormatTo(ref sb, arg7, item.Alignment, item.StandardFormat, nameof(arg7)); + break; + } + case 7: + { + Utf8FormatHelper.FormatTo(ref sb, arg8, item.Alignment, item.StandardFormat, nameof(arg8)); + break; + } + default: + break; } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } public int MinSize { get; } - readonly FormatSegment[] segments; + readonly Utf8FormatSegment[] segments; + readonly byte[] utf8PreEncodedbuffer; public Utf8PreparedFormat(string format) { this.FormatString = format; - this.segments = PreparedFormatHelper.Parse(format, true); + this.segments = PreparedFormatHelper.Utf8Parse(format, out utf8PreEncodedbuffer); var size = 0; foreach (var item in segments) @@ -4096,174 +2353,83 @@ public string Format(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 ar public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9) where TBufferWriter : IBufferWriter { + var formatSpan = utf8PreEncodedbuffer.AsSpan(); + foreach (var item in segments) { - if (!item.IsFormatArgument) - { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Encoding.UTF8.GetMaxByteCount(item.Count); - var span = sb.GetSpan(size); - var count = Encoding.UTF8.GetBytes(strSpan, span); - sb.Advance(count); - } - else + switch (item.FormatIndex) { - switch (item.FormatIndex) - { - case 0: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - sb.Advance(argWritten); - break; - } - case 1: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - sb.Advance(argWritten); - break; - } - case 2: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - sb.Advance(argWritten); - break; - } - case 3: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - sb.Advance(argWritten); - break; - } - case 4: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg5)); - } - } - sb.Advance(argWritten); - break; - } - case 5: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg6)); - } - } - sb.Advance(argWritten); - break; - } - case 6: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg7)); - } - } - sb.Advance(argWritten); - break; - } - case 7: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg8)); - } - } - sb.Advance(argWritten); - break; - } - case 8: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg9)); - } - } - sb.Advance(argWritten); - break; - } - default: - break; - } + case Utf8FormatSegment.NotFormatIndex: + { + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + break; + } + case 0: + { + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); + break; + } + case 1: + { + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); + break; + } + case 2: + { + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); + break; + } + case 3: + { + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); + break; + } + case 4: + { + Utf8FormatHelper.FormatTo(ref sb, arg5, item.Alignment, item.StandardFormat, nameof(arg5)); + break; + } + case 5: + { + Utf8FormatHelper.FormatTo(ref sb, arg6, item.Alignment, item.StandardFormat, nameof(arg6)); + break; + } + case 6: + { + Utf8FormatHelper.FormatTo(ref sb, arg7, item.Alignment, item.StandardFormat, nameof(arg7)); + break; + } + case 7: + { + Utf8FormatHelper.FormatTo(ref sb, arg8, item.Alignment, item.StandardFormat, nameof(arg8)); + break; + } + case 8: + { + Utf8FormatHelper.FormatTo(ref sb, arg9, item.Alignment, item.StandardFormat, nameof(arg9)); + break; + } + default: + break; } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } public int MinSize { get; } - readonly FormatSegment[] segments; + readonly Utf8FormatSegment[] segments; + readonly byte[] utf8PreEncodedbuffer; public Utf8PreparedFormat(string format) { this.FormatString = format; - this.segments = PreparedFormatHelper.Parse(format, true); + this.segments = PreparedFormatHelper.Utf8Parse(format, out utf8PreEncodedbuffer); var size = 0; foreach (var item in segments) @@ -4293,189 +2459,88 @@ public string Format(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 ar public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10) where TBufferWriter : IBufferWriter { + var formatSpan = utf8PreEncodedbuffer.AsSpan(); + foreach (var item in segments) { - if (!item.IsFormatArgument) + switch (item.FormatIndex) { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Encoding.UTF8.GetMaxByteCount(item.Count); - var span = sb.GetSpan(size); - var count = Encoding.UTF8.GetBytes(strSpan, span); - sb.Advance(count); - } - else - { - switch (item.FormatIndex) - { - case 0: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - sb.Advance(argWritten); - break; - } - case 1: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - sb.Advance(argWritten); - break; - } - case 2: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - sb.Advance(argWritten); - break; - } - case 3: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - sb.Advance(argWritten); - break; - } - case 4: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg5)); - } - } - sb.Advance(argWritten); - break; - } - case 5: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg6)); - } - } - sb.Advance(argWritten); - break; - } - case 6: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg7)); - } - } - sb.Advance(argWritten); - break; - } - case 7: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg8)); - } - } - sb.Advance(argWritten); - break; - } - case 8: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg9)); - } - } - sb.Advance(argWritten); - break; - } - case 9: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg10)); - } - } - sb.Advance(argWritten); - break; - } - default: - break; - } + case Utf8FormatSegment.NotFormatIndex: + { + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + break; + } + case 0: + { + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); + break; + } + case 1: + { + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); + break; + } + case 2: + { + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); + break; + } + case 3: + { + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); + break; + } + case 4: + { + Utf8FormatHelper.FormatTo(ref sb, arg5, item.Alignment, item.StandardFormat, nameof(arg5)); + break; + } + case 5: + { + Utf8FormatHelper.FormatTo(ref sb, arg6, item.Alignment, item.StandardFormat, nameof(arg6)); + break; + } + case 6: + { + Utf8FormatHelper.FormatTo(ref sb, arg7, item.Alignment, item.StandardFormat, nameof(arg7)); + break; + } + case 7: + { + Utf8FormatHelper.FormatTo(ref sb, arg8, item.Alignment, item.StandardFormat, nameof(arg8)); + break; + } + case 8: + { + Utf8FormatHelper.FormatTo(ref sb, arg9, item.Alignment, item.StandardFormat, nameof(arg9)); + break; + } + case 9: + { + Utf8FormatHelper.FormatTo(ref sb, arg10, item.Alignment, item.StandardFormat, nameof(arg10)); + break; + } + default: + break; } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } public int MinSize { get; } - readonly FormatSegment[] segments; + readonly Utf8FormatSegment[] segments; + readonly byte[] utf8PreEncodedbuffer; public Utf8PreparedFormat(string format) { this.FormatString = format; - this.segments = PreparedFormatHelper.Parse(format, true); + this.segments = PreparedFormatHelper.Utf8Parse(format, out utf8PreEncodedbuffer); var size = 0; foreach (var item in segments) @@ -4505,204 +2570,93 @@ public string Format(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 ar public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11) where TBufferWriter : IBufferWriter { + var formatSpan = utf8PreEncodedbuffer.AsSpan(); + foreach (var item in segments) { - if (!item.IsFormatArgument) - { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Encoding.UTF8.GetMaxByteCount(item.Count); - var span = sb.GetSpan(size); - var count = Encoding.UTF8.GetBytes(strSpan, span); - sb.Advance(count); - } - else + switch (item.FormatIndex) { - switch (item.FormatIndex) - { - case 0: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - sb.Advance(argWritten); - break; - } - case 1: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - sb.Advance(argWritten); - break; - } - case 2: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - sb.Advance(argWritten); - break; - } - case 3: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - sb.Advance(argWritten); - break; - } - case 4: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg5)); - } - } - sb.Advance(argWritten); - break; - } - case 5: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg6)); - } - } - sb.Advance(argWritten); - break; - } - case 6: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg7)); - } - } - sb.Advance(argWritten); - break; - } - case 7: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg8)); - } - } - sb.Advance(argWritten); - break; - } - case 8: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg9)); - } - } - sb.Advance(argWritten); - break; - } - case 9: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg10)); - } - } - sb.Advance(argWritten); - break; - } - case 10: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg11, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg11, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg11)); - } - } - sb.Advance(argWritten); - break; - } - default: - break; - } + case Utf8FormatSegment.NotFormatIndex: + { + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + break; + } + case 0: + { + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); + break; + } + case 1: + { + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); + break; + } + case 2: + { + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); + break; + } + case 3: + { + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); + break; + } + case 4: + { + Utf8FormatHelper.FormatTo(ref sb, arg5, item.Alignment, item.StandardFormat, nameof(arg5)); + break; + } + case 5: + { + Utf8FormatHelper.FormatTo(ref sb, arg6, item.Alignment, item.StandardFormat, nameof(arg6)); + break; + } + case 6: + { + Utf8FormatHelper.FormatTo(ref sb, arg7, item.Alignment, item.StandardFormat, nameof(arg7)); + break; + } + case 7: + { + Utf8FormatHelper.FormatTo(ref sb, arg8, item.Alignment, item.StandardFormat, nameof(arg8)); + break; + } + case 8: + { + Utf8FormatHelper.FormatTo(ref sb, arg9, item.Alignment, item.StandardFormat, nameof(arg9)); + break; + } + case 9: + { + Utf8FormatHelper.FormatTo(ref sb, arg10, item.Alignment, item.StandardFormat, nameof(arg10)); + break; + } + case 10: + { + Utf8FormatHelper.FormatTo(ref sb, arg11, item.Alignment, item.StandardFormat, nameof(arg11)); + break; + } + default: + break; } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } public int MinSize { get; } - readonly FormatSegment[] segments; + readonly Utf8FormatSegment[] segments; + readonly byte[] utf8PreEncodedbuffer; public Utf8PreparedFormat(string format) { this.FormatString = format; - this.segments = PreparedFormatHelper.Parse(format, true); + this.segments = PreparedFormatHelper.Utf8Parse(format, out utf8PreEncodedbuffer); var size = 0; foreach (var item in segments) @@ -4732,219 +2686,98 @@ public string Format(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 ar public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12) where TBufferWriter : IBufferWriter { + var formatSpan = utf8PreEncodedbuffer.AsSpan(); + foreach (var item in segments) { - if (!item.IsFormatArgument) + switch (item.FormatIndex) { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Encoding.UTF8.GetMaxByteCount(item.Count); - var span = sb.GetSpan(size); - var count = Encoding.UTF8.GetBytes(strSpan, span); - sb.Advance(count); - } - else - { - switch (item.FormatIndex) - { - case 0: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - sb.Advance(argWritten); - break; - } - case 1: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - sb.Advance(argWritten); - break; - } - case 2: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - sb.Advance(argWritten); - break; - } - case 3: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - sb.Advance(argWritten); - break; - } - case 4: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg5)); - } - } - sb.Advance(argWritten); - break; - } - case 5: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg6)); - } - } - sb.Advance(argWritten); - break; - } - case 6: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg7)); - } - } - sb.Advance(argWritten); - break; - } - case 7: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg8)); - } - } - sb.Advance(argWritten); - break; - } - case 8: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg9)); - } - } - sb.Advance(argWritten); - break; - } - case 9: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg10)); - } - } - sb.Advance(argWritten); - break; - } - case 10: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg11, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg11, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg11)); - } - } - sb.Advance(argWritten); - break; - } - case 11: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg12, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg12, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg12)); - } - } - sb.Advance(argWritten); - break; - } - default: - break; - } + case Utf8FormatSegment.NotFormatIndex: + { + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + break; + } + case 0: + { + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); + break; + } + case 1: + { + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); + break; + } + case 2: + { + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); + break; + } + case 3: + { + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); + break; + } + case 4: + { + Utf8FormatHelper.FormatTo(ref sb, arg5, item.Alignment, item.StandardFormat, nameof(arg5)); + break; + } + case 5: + { + Utf8FormatHelper.FormatTo(ref sb, arg6, item.Alignment, item.StandardFormat, nameof(arg6)); + break; + } + case 6: + { + Utf8FormatHelper.FormatTo(ref sb, arg7, item.Alignment, item.StandardFormat, nameof(arg7)); + break; + } + case 7: + { + Utf8FormatHelper.FormatTo(ref sb, arg8, item.Alignment, item.StandardFormat, nameof(arg8)); + break; + } + case 8: + { + Utf8FormatHelper.FormatTo(ref sb, arg9, item.Alignment, item.StandardFormat, nameof(arg9)); + break; + } + case 9: + { + Utf8FormatHelper.FormatTo(ref sb, arg10, item.Alignment, item.StandardFormat, nameof(arg10)); + break; + } + case 10: + { + Utf8FormatHelper.FormatTo(ref sb, arg11, item.Alignment, item.StandardFormat, nameof(arg11)); + break; + } + case 11: + { + Utf8FormatHelper.FormatTo(ref sb, arg12, item.Alignment, item.StandardFormat, nameof(arg12)); + break; + } + default: + break; } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } public int MinSize { get; } - readonly FormatSegment[] segments; + readonly Utf8FormatSegment[] segments; + readonly byte[] utf8PreEncodedbuffer; public Utf8PreparedFormat(string format) { this.FormatString = format; - this.segments = PreparedFormatHelper.Parse(format, true); + this.segments = PreparedFormatHelper.Utf8Parse(format, out utf8PreEncodedbuffer); var size = 0; foreach (var item in segments) @@ -4974,234 +2807,103 @@ public string Format(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 ar public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13) where TBufferWriter : IBufferWriter { + var formatSpan = utf8PreEncodedbuffer.AsSpan(); + foreach (var item in segments) { - if (!item.IsFormatArgument) + switch (item.FormatIndex) { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Encoding.UTF8.GetMaxByteCount(item.Count); - var span = sb.GetSpan(size); - var count = Encoding.UTF8.GetBytes(strSpan, span); - sb.Advance(count); - } - else - { - switch (item.FormatIndex) - { - case 0: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - sb.Advance(argWritten); - break; - } - case 1: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - sb.Advance(argWritten); - break; - } - case 2: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - sb.Advance(argWritten); - break; - } - case 3: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - sb.Advance(argWritten); - break; - } - case 4: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg5)); - } - } - sb.Advance(argWritten); - break; - } - case 5: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg6)); - } - } - sb.Advance(argWritten); - break; - } - case 6: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg7)); - } - } - sb.Advance(argWritten); - break; - } - case 7: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg8)); - } - } - sb.Advance(argWritten); - break; - } - case 8: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg9)); - } - } - sb.Advance(argWritten); - break; - } - case 9: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg10)); - } - } - sb.Advance(argWritten); - break; - } - case 10: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg11, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg11, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg11)); - } - } - sb.Advance(argWritten); - break; - } - case 11: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg12, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg12, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg12)); - } - } - sb.Advance(argWritten); - break; - } - case 12: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg13, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg13, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg13)); - } - } - sb.Advance(argWritten); - break; - } - default: - break; - } + case Utf8FormatSegment.NotFormatIndex: + { + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + break; + } + case 0: + { + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); + break; + } + case 1: + { + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); + break; + } + case 2: + { + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); + break; + } + case 3: + { + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); + break; + } + case 4: + { + Utf8FormatHelper.FormatTo(ref sb, arg5, item.Alignment, item.StandardFormat, nameof(arg5)); + break; + } + case 5: + { + Utf8FormatHelper.FormatTo(ref sb, arg6, item.Alignment, item.StandardFormat, nameof(arg6)); + break; + } + case 6: + { + Utf8FormatHelper.FormatTo(ref sb, arg7, item.Alignment, item.StandardFormat, nameof(arg7)); + break; + } + case 7: + { + Utf8FormatHelper.FormatTo(ref sb, arg8, item.Alignment, item.StandardFormat, nameof(arg8)); + break; + } + case 8: + { + Utf8FormatHelper.FormatTo(ref sb, arg9, item.Alignment, item.StandardFormat, nameof(arg9)); + break; + } + case 9: + { + Utf8FormatHelper.FormatTo(ref sb, arg10, item.Alignment, item.StandardFormat, nameof(arg10)); + break; + } + case 10: + { + Utf8FormatHelper.FormatTo(ref sb, arg11, item.Alignment, item.StandardFormat, nameof(arg11)); + break; + } + case 11: + { + Utf8FormatHelper.FormatTo(ref sb, arg12, item.Alignment, item.StandardFormat, nameof(arg12)); + break; + } + case 12: + { + Utf8FormatHelper.FormatTo(ref sb, arg13, item.Alignment, item.StandardFormat, nameof(arg13)); + break; + } + default: + break; } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } public int MinSize { get; } - readonly FormatSegment[] segments; + readonly Utf8FormatSegment[] segments; + readonly byte[] utf8PreEncodedbuffer; public Utf8PreparedFormat(string format) { this.FormatString = format; - this.segments = PreparedFormatHelper.Parse(format, true); + this.segments = PreparedFormatHelper.Utf8Parse(format, out utf8PreEncodedbuffer); var size = 0; foreach (var item in segments) @@ -5231,249 +2933,108 @@ public string Format(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 ar public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14) where TBufferWriter : IBufferWriter { + var formatSpan = utf8PreEncodedbuffer.AsSpan(); + foreach (var item in segments) { - if (!item.IsFormatArgument) + switch (item.FormatIndex) { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Encoding.UTF8.GetMaxByteCount(item.Count); - var span = sb.GetSpan(size); - var count = Encoding.UTF8.GetBytes(strSpan, span); - sb.Advance(count); - } - else - { - switch (item.FormatIndex) - { - case 0: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - sb.Advance(argWritten); - break; - } - case 1: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - sb.Advance(argWritten); - break; - } - case 2: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - sb.Advance(argWritten); - break; - } - case 3: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - sb.Advance(argWritten); - break; - } - case 4: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg5)); - } - } - sb.Advance(argWritten); - break; - } - case 5: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg6)); - } - } - sb.Advance(argWritten); - break; - } - case 6: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg7)); - } - } - sb.Advance(argWritten); - break; - } - case 7: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg8)); - } - } - sb.Advance(argWritten); - break; - } - case 8: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg9)); - } - } - sb.Advance(argWritten); - break; - } - case 9: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg10)); - } - } - sb.Advance(argWritten); - break; - } - case 10: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg11, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg11, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg11)); - } - } - sb.Advance(argWritten); - break; - } - case 11: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg12, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg12, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg12)); - } - } - sb.Advance(argWritten); - break; - } - case 12: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg13, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg13, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg13)); - } - } - sb.Advance(argWritten); - break; - } - case 13: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg14, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg14, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg14)); - } - } - sb.Advance(argWritten); - break; - } - default: - break; - } + case Utf8FormatSegment.NotFormatIndex: + { + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + break; + } + case 0: + { + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); + break; + } + case 1: + { + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); + break; + } + case 2: + { + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); + break; + } + case 3: + { + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); + break; + } + case 4: + { + Utf8FormatHelper.FormatTo(ref sb, arg5, item.Alignment, item.StandardFormat, nameof(arg5)); + break; + } + case 5: + { + Utf8FormatHelper.FormatTo(ref sb, arg6, item.Alignment, item.StandardFormat, nameof(arg6)); + break; + } + case 6: + { + Utf8FormatHelper.FormatTo(ref sb, arg7, item.Alignment, item.StandardFormat, nameof(arg7)); + break; + } + case 7: + { + Utf8FormatHelper.FormatTo(ref sb, arg8, item.Alignment, item.StandardFormat, nameof(arg8)); + break; + } + case 8: + { + Utf8FormatHelper.FormatTo(ref sb, arg9, item.Alignment, item.StandardFormat, nameof(arg9)); + break; + } + case 9: + { + Utf8FormatHelper.FormatTo(ref sb, arg10, item.Alignment, item.StandardFormat, nameof(arg10)); + break; + } + case 10: + { + Utf8FormatHelper.FormatTo(ref sb, arg11, item.Alignment, item.StandardFormat, nameof(arg11)); + break; + } + case 11: + { + Utf8FormatHelper.FormatTo(ref sb, arg12, item.Alignment, item.StandardFormat, nameof(arg12)); + break; + } + case 12: + { + Utf8FormatHelper.FormatTo(ref sb, arg13, item.Alignment, item.StandardFormat, nameof(arg13)); + break; + } + case 13: + { + Utf8FormatHelper.FormatTo(ref sb, arg14, item.Alignment, item.StandardFormat, nameof(arg14)); + break; + } + default: + break; } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } public int MinSize { get; } - readonly FormatSegment[] segments; + readonly Utf8FormatSegment[] segments; + readonly byte[] utf8PreEncodedbuffer; public Utf8PreparedFormat(string format) { this.FormatString = format; - this.segments = PreparedFormatHelper.Parse(format, true); + this.segments = PreparedFormatHelper.Utf8Parse(format, out utf8PreEncodedbuffer); var size = 0; foreach (var item in segments) @@ -5503,264 +3064,113 @@ public string Format(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 ar public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15) where TBufferWriter : IBufferWriter { + var formatSpan = utf8PreEncodedbuffer.AsSpan(); + foreach (var item in segments) { - if (!item.IsFormatArgument) - { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Encoding.UTF8.GetMaxByteCount(item.Count); - var span = sb.GetSpan(size); - var count = Encoding.UTF8.GetBytes(strSpan, span); - sb.Advance(count); - } - else + switch (item.FormatIndex) { - switch (item.FormatIndex) - { - case 0: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - sb.Advance(argWritten); - break; - } - case 1: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - sb.Advance(argWritten); - break; - } - case 2: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - sb.Advance(argWritten); - break; - } - case 3: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - sb.Advance(argWritten); - break; - } - case 4: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg5)); - } - } - sb.Advance(argWritten); - break; - } - case 5: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg6)); - } - } - sb.Advance(argWritten); - break; - } - case 6: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg7)); - } - } - sb.Advance(argWritten); - break; - } - case 7: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg8)); - } - } - sb.Advance(argWritten); - break; - } - case 8: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg9)); - } - } - sb.Advance(argWritten); - break; - } - case 9: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg10)); - } - } - sb.Advance(argWritten); - break; - } - case 10: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg11, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg11, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg11)); - } - } - sb.Advance(argWritten); - break; - } - case 11: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg12, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg12, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg12)); - } - } - sb.Advance(argWritten); - break; - } - case 12: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg13, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg13, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg13)); - } - } - sb.Advance(argWritten); - break; - } - case 13: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg14, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg14, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg14)); - } - } - sb.Advance(argWritten); - break; - } - case 14: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg15, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg15, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg15)); - } - } - sb.Advance(argWritten); - break; - } - default: - break; - } + case Utf8FormatSegment.NotFormatIndex: + { + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + break; + } + case 0: + { + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); + break; + } + case 1: + { + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); + break; + } + case 2: + { + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); + break; + } + case 3: + { + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); + break; + } + case 4: + { + Utf8FormatHelper.FormatTo(ref sb, arg5, item.Alignment, item.StandardFormat, nameof(arg5)); + break; + } + case 5: + { + Utf8FormatHelper.FormatTo(ref sb, arg6, item.Alignment, item.StandardFormat, nameof(arg6)); + break; + } + case 6: + { + Utf8FormatHelper.FormatTo(ref sb, arg7, item.Alignment, item.StandardFormat, nameof(arg7)); + break; + } + case 7: + { + Utf8FormatHelper.FormatTo(ref sb, arg8, item.Alignment, item.StandardFormat, nameof(arg8)); + break; + } + case 8: + { + Utf8FormatHelper.FormatTo(ref sb, arg9, item.Alignment, item.StandardFormat, nameof(arg9)); + break; + } + case 9: + { + Utf8FormatHelper.FormatTo(ref sb, arg10, item.Alignment, item.StandardFormat, nameof(arg10)); + break; + } + case 10: + { + Utf8FormatHelper.FormatTo(ref sb, arg11, item.Alignment, item.StandardFormat, nameof(arg11)); + break; + } + case 11: + { + Utf8FormatHelper.FormatTo(ref sb, arg12, item.Alignment, item.StandardFormat, nameof(arg12)); + break; + } + case 12: + { + Utf8FormatHelper.FormatTo(ref sb, arg13, item.Alignment, item.StandardFormat, nameof(arg13)); + break; + } + case 13: + { + Utf8FormatHelper.FormatTo(ref sb, arg14, item.Alignment, item.StandardFormat, nameof(arg14)); + break; + } + case 14: + { + Utf8FormatHelper.FormatTo(ref sb, arg15, item.Alignment, item.StandardFormat, nameof(arg15)); + break; + } + default: + break; } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } public int MinSize { get; } - readonly FormatSegment[] segments; + readonly Utf8FormatSegment[] segments; + readonly byte[] utf8PreEncodedbuffer; public Utf8PreparedFormat(string format) { this.FormatString = format; - this.segments = PreparedFormatHelper.Parse(format, true); + this.segments = PreparedFormatHelper.Utf8Parse(format, out utf8PreEncodedbuffer); var size = 0; foreach (var item in segments) @@ -5790,266 +3200,104 @@ public string Format(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 ar public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16) where TBufferWriter : IBufferWriter { + var formatSpan = utf8PreEncodedbuffer.AsSpan(); + foreach (var item in segments) { - if (!item.IsFormatArgument) - { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Encoding.UTF8.GetMaxByteCount(item.Count); - var span = sb.GetSpan(size); - var count = Encoding.UTF8.GetBytes(strSpan, span); - sb.Advance(count); - } - else + switch (item.FormatIndex) { - switch (item.FormatIndex) - { - case 0: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - sb.Advance(argWritten); - break; - } - case 1: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - sb.Advance(argWritten); - break; - } - case 2: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - sb.Advance(argWritten); - break; - } - case 3: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - sb.Advance(argWritten); - break; - } - case 4: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg5)); - } - } - sb.Advance(argWritten); - break; - } - case 5: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg6)); - } - } - sb.Advance(argWritten); - break; - } - case 6: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg7)); - } - } - sb.Advance(argWritten); - break; - } - case 7: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg8)); - } - } - sb.Advance(argWritten); - break; - } - case 8: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg9)); - } - } - sb.Advance(argWritten); - break; - } - case 9: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg10)); - } - } - sb.Advance(argWritten); - break; - } - case 10: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg11, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg11, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg11)); - } - } - sb.Advance(argWritten); - break; - } - case 11: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg12, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg12, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg12)); - } - } - sb.Advance(argWritten); - break; - } - case 12: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg13, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg13, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg13)); - } - } - sb.Advance(argWritten); - break; - } - case 13: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg14, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg14, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg14)); - } - } - sb.Advance(argWritten); - break; - } - case 14: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg15, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg15, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg15)); - } - } - sb.Advance(argWritten); - break; - } - case 15: - { - var span = sb.GetSpan(0); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg16, span, out var argWritten, item.StandardFormat)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg16, span, out argWritten, item.StandardFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg16)); - } - } - sb.Advance(argWritten); - break; - } - default: - break; - } + case Utf8FormatSegment.NotFormatIndex: + { + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + break; + } + case 0: + { + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); + break; + } + case 1: + { + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); + break; + } + case 2: + { + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); + break; + } + case 3: + { + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); + break; + } + case 4: + { + Utf8FormatHelper.FormatTo(ref sb, arg5, item.Alignment, item.StandardFormat, nameof(arg5)); + break; + } + case 5: + { + Utf8FormatHelper.FormatTo(ref sb, arg6, item.Alignment, item.StandardFormat, nameof(arg6)); + break; + } + case 6: + { + Utf8FormatHelper.FormatTo(ref sb, arg7, item.Alignment, item.StandardFormat, nameof(arg7)); + break; + } + case 7: + { + Utf8FormatHelper.FormatTo(ref sb, arg8, item.Alignment, item.StandardFormat, nameof(arg8)); + break; + } + case 8: + { + Utf8FormatHelper.FormatTo(ref sb, arg9, item.Alignment, item.StandardFormat, nameof(arg9)); + break; + } + case 9: + { + Utf8FormatHelper.FormatTo(ref sb, arg10, item.Alignment, item.StandardFormat, nameof(arg10)); + break; + } + case 10: + { + Utf8FormatHelper.FormatTo(ref sb, arg11, item.Alignment, item.StandardFormat, nameof(arg11)); + break; + } + case 11: + { + Utf8FormatHelper.FormatTo(ref sb, arg12, item.Alignment, item.StandardFormat, nameof(arg12)); + break; + } + case 12: + { + Utf8FormatHelper.FormatTo(ref sb, arg13, item.Alignment, item.StandardFormat, nameof(arg13)); + break; + } + case 13: + { + Utf8FormatHelper.FormatTo(ref sb, arg14, item.Alignment, item.StandardFormat, nameof(arg14)); + break; + } + case 14: + { + Utf8FormatHelper.FormatTo(ref sb, arg15, item.Alignment, item.StandardFormat, nameof(arg15)); + break; + } + case 15: + { + Utf8FormatHelper.FormatTo(ref sb, arg16, item.Alignment, item.StandardFormat, nameof(arg16)); + break; + } + default: + break; } } } } - -} \ No newline at end of file +} diff --git a/PreparedFormatHelper.cs b/PreparedFormatHelper.cs index 60bdcfd..18d808d 100644 --- a/PreparedFormatHelper.cs +++ b/PreparedFormatHelper.cs @@ -1,109 +1,179 @@ using System; using System.Buffers; using System.Collections.Generic; +using System.Text; namespace Cysharp.Text { internal static class PreparedFormatHelper { - internal static FormatSegment[] Parse(string format, bool withStandardFormat) + internal static Utf16FormatSegment[] Utf16Parse(string format) { - var list = new List(); + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + + var list = new List(); + + int i = 0; + int len = format.Length; var copyFrom = 0; - for (int i = 0; i < format.Length; i++) + var formatSpan = format.AsSpan(); + + while (true) { - if (format[i] == '{') + while (i < len) { - // escape. - if (i == format.Length - 1) + var parserScanResult = FormatParser.ScanFormatString(formatSpan, ref i); + + if (ParserScanResult.NormalChar == parserScanResult && i < len) { - throw new FormatException("invalid format"); + // skip normal char + continue; } - if (i != format.Length && format[i + 1] == '{') + var size = i - copyFrom; + if (ParserScanResult.EscapedChar == parserScanResult) { - var size = i - copyFrom; - if (size != 0) - { - list.Add(new FormatSegment(copyFrom, size, false, 0, null, withStandardFormat)); - } - i = i + 1; // skip escaped '{' - copyFrom = i; - continue; + size--; } - else + + if (size != 0) { - var size = i - copyFrom; - if (size != 0) - { - list.Add(new FormatSegment(copyFrom, size, false, 0, null, withStandardFormat)); - } + list.Add(new Utf16FormatSegment(copyFrom, size, Utf16FormatSegment.NotFormatIndex, 0)); } - // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - list.Add(new FormatSegment(0, 0, true, indexParse.Index, indexParse.FormatString.ToString(), withStandardFormat)); + copyFrom = i; + + if (ParserScanResult.BraceOpen == parserScanResult) + { + break; + } + } - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + if (i >= len) + { + break; } - else if (format[i] == '}') + + // Here it is before `{`. + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; // continue after '}' + i = indexParse.LastIndex; + + list.Add(new Utf16FormatSegment(indexParse.LastIndex - indexParse.FormatString.Length - 1, indexParse.FormatString.Length, indexParse.Index, indexParse.Alignment)); + } + + return list.ToArray(); + } + + internal static Utf8FormatSegment[] Utf8Parse(string format, out byte[] utf8buffer) + { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + + var list = new List(); + utf8buffer = new byte[Encoding.UTF8.GetMaxByteCount(format.Length)]; + var bufOffset = 0; + + int i = 0; + int len = format.Length; + + var copyFrom = 0; + var formatSpan = format.AsSpan(); + + while (true) + { + while (i < len) { - if (i != format.Length && format[i + 1] == '}') + var parserScanResult = FormatParser.ScanFormatString(formatSpan, ref i); + + if (ParserScanResult.NormalChar == parserScanResult && i < len) { - var size = i - copyFrom; - if (size != 0) - { - list.Add(new FormatSegment(copyFrom, size, false, 0, null, withStandardFormat)); - } - i = i + 1; // skip escaped '}' - copyFrom = i; + // skip normal char continue; } + + var size = i - copyFrom; + if (ParserScanResult.EscapedChar == parserScanResult) + { + size--; + } + + if (size != 0) + { + var utf8size = Encoding.UTF8.GetBytes(format, copyFrom, size, utf8buffer, bufOffset); + list.Add(new Utf8FormatSegment(bufOffset, utf8size, Utf8FormatSegment.NotFormatIndex, default, 0)); + bufOffset += utf8size; + } + + copyFrom = i; + + if (ParserScanResult.BraceOpen == parserScanResult) + { + break; + } } - } - { - // final string - var copyLength = format.Length - copyFrom; - if (copyLength > 0) + if (i >= len) { - list.Add(new FormatSegment(copyFrom, copyLength, false, 0, null, withStandardFormat)); + break; } + + // Here it is before `{`. + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; // continue after '}' + i = indexParse.LastIndex; + list.Add(new Utf8FormatSegment(0, 0, indexParse.Index, StandardFormat.Parse(indexParse.FormatString), indexParse.Alignment)); } return list.ToArray(); } } + internal readonly struct Utf8FormatSegment + { + public const int NotFormatIndex = -1; + + public readonly int Offset; + public readonly int Count; + public readonly int FormatIndex; + public readonly StandardFormat StandardFormat; + public readonly int Alignment; + + public bool IsFormatArgument => FormatIndex != NotFormatIndex; + + public Utf8FormatSegment(int offset, int count, int formatIndex, StandardFormat format, int alignment) + { + Offset = offset; + Count = count; + FormatIndex = formatIndex; + StandardFormat = format; + Alignment = alignment; + } + } - internal readonly struct FormatSegment + internal readonly struct Utf16FormatSegment { + public const int NotFormatIndex = -1; + public readonly int Offset; public readonly int Count; - public readonly bool IsFormatArgument; public readonly int FormatIndex; - public readonly string FormatString; + public readonly int Alignment; - // Utf8 - public readonly StandardFormat StandardFormat; + public bool IsFormatArgument => FormatIndex != NotFormatIndex; - public FormatSegment(int offset, int count, bool isFormatArgument, int formatIndex, string formatString, bool utf8) + public Utf16FormatSegment(int offset, int count, int formatIndex, int alignment) { Offset = offset; Count = count; - IsFormatArgument = isFormatArgument; FormatIndex = formatIndex; - FormatString = formatString; - if (utf8) - { - StandardFormat = (formatString != null) ? StandardFormat.Parse(formatString) : default; - } - else - { - StandardFormat = default; - } + Alignment = alignment; } } } diff --git a/Utf16/Utf16ValueStringBuilder.AppendFormat.cs b/Utf16/Utf16ValueStringBuilder.AppendFormat.cs index 515186d..717464b 100644 --- a/Utf16/Utf16ValueStringBuilder.AppendFormat.cs +++ b/Utf16/Utf16ValueStringBuilder.AppendFormat.cs @@ -7,10 +7,16 @@ public partial struct Utf16ValueStringBuilder /// Appends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. public void AppendFormat(string format, T1 arg1) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -33,22 +39,22 @@ public void AppendFormat(string format, T1 arg1) } // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; + i = indexParse.LastIndex - 1; switch (indexParse.Index) { case 0: - AppendFormatInternal(arg1, indexParse.FormatString, nameof(arg1)); + AppendFormatInternal(arg1, indexParse.Alignment, indexParse.FormatString, nameof(arg1)); continue; default: ThrowFormatException(); break; } } - else if (format[i] == '}') + else if (c == '}') { - if (i != format.Length && format[i + 1] == '}') + if (i + 1 < format.Length && format[i + 1] == '}') { var size = i - copyFrom; Append(format.AsSpan(copyFrom, size)); @@ -56,6 +62,10 @@ public void AppendFormat(string format, T1 arg1) copyFrom = i; continue; } + else + { + ThrowFormatException(); + } } } @@ -68,14 +78,19 @@ public void AppendFormat(string format, T1 arg1) } } } - /// Appends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. public void AppendFormat(string format, T1 arg1, T2 arg2) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -98,25 +113,25 @@ public void AppendFormat(string format, T1 arg1, T2 arg2) } // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; + i = indexParse.LastIndex - 1; switch (indexParse.Index) { case 0: - AppendFormatInternal(arg1, indexParse.FormatString, nameof(arg1)); + AppendFormatInternal(arg1, indexParse.Alignment, indexParse.FormatString, nameof(arg1)); continue; case 1: - AppendFormatInternal(arg2, indexParse.FormatString, nameof(arg2)); + AppendFormatInternal(arg2, indexParse.Alignment, indexParse.FormatString, nameof(arg2)); continue; default: ThrowFormatException(); break; } } - else if (format[i] == '}') + else if (c == '}') { - if (i != format.Length && format[i + 1] == '}') + if (i + 1 < format.Length && format[i + 1] == '}') { var size = i - copyFrom; Append(format.AsSpan(copyFrom, size)); @@ -124,6 +139,10 @@ public void AppendFormat(string format, T1 arg1, T2 arg2) copyFrom = i; continue; } + else + { + ThrowFormatException(); + } } } @@ -136,14 +155,19 @@ public void AppendFormat(string format, T1 arg1, T2 arg2) } } } - /// Appends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg3) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -166,28 +190,28 @@ public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg3) } // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; + i = indexParse.LastIndex - 1; switch (indexParse.Index) { case 0: - AppendFormatInternal(arg1, indexParse.FormatString, nameof(arg1)); + AppendFormatInternal(arg1, indexParse.Alignment, indexParse.FormatString, nameof(arg1)); continue; case 1: - AppendFormatInternal(arg2, indexParse.FormatString, nameof(arg2)); + AppendFormatInternal(arg2, indexParse.Alignment, indexParse.FormatString, nameof(arg2)); continue; case 2: - AppendFormatInternal(arg3, indexParse.FormatString, nameof(arg3)); + AppendFormatInternal(arg3, indexParse.Alignment, indexParse.FormatString, nameof(arg3)); continue; default: ThrowFormatException(); break; } } - else if (format[i] == '}') + else if (c == '}') { - if (i != format.Length && format[i + 1] == '}') + if (i + 1 < format.Length && format[i + 1] == '}') { var size = i - copyFrom; Append(format.AsSpan(copyFrom, size)); @@ -195,6 +219,10 @@ public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg3) copyFrom = i; continue; } + else + { + ThrowFormatException(); + } } } @@ -207,14 +235,19 @@ public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg3) } } } - /// Appends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -237,31 +270,31 @@ public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg } // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; + i = indexParse.LastIndex - 1; switch (indexParse.Index) { case 0: - AppendFormatInternal(arg1, indexParse.FormatString, nameof(arg1)); + AppendFormatInternal(arg1, indexParse.Alignment, indexParse.FormatString, nameof(arg1)); continue; case 1: - AppendFormatInternal(arg2, indexParse.FormatString, nameof(arg2)); + AppendFormatInternal(arg2, indexParse.Alignment, indexParse.FormatString, nameof(arg2)); continue; case 2: - AppendFormatInternal(arg3, indexParse.FormatString, nameof(arg3)); + AppendFormatInternal(arg3, indexParse.Alignment, indexParse.FormatString, nameof(arg3)); continue; case 3: - AppendFormatInternal(arg4, indexParse.FormatString, nameof(arg4)); + AppendFormatInternal(arg4, indexParse.Alignment, indexParse.FormatString, nameof(arg4)); continue; default: ThrowFormatException(); break; } } - else if (format[i] == '}') + else if (c == '}') { - if (i != format.Length && format[i + 1] == '}') + if (i + 1 < format.Length && format[i + 1] == '}') { var size = i - copyFrom; Append(format.AsSpan(copyFrom, size)); @@ -269,6 +302,10 @@ public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg copyFrom = i; continue; } + else + { + ThrowFormatException(); + } } } @@ -281,14 +318,19 @@ public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg } } } - /// Appends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -311,34 +353,34 @@ public void AppendFormat(string format, T1 arg1, T2 arg2, T3 } // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; + i = indexParse.LastIndex - 1; switch (indexParse.Index) { case 0: - AppendFormatInternal(arg1, indexParse.FormatString, nameof(arg1)); + AppendFormatInternal(arg1, indexParse.Alignment, indexParse.FormatString, nameof(arg1)); continue; case 1: - AppendFormatInternal(arg2, indexParse.FormatString, nameof(arg2)); + AppendFormatInternal(arg2, indexParse.Alignment, indexParse.FormatString, nameof(arg2)); continue; case 2: - AppendFormatInternal(arg3, indexParse.FormatString, nameof(arg3)); + AppendFormatInternal(arg3, indexParse.Alignment, indexParse.FormatString, nameof(arg3)); continue; case 3: - AppendFormatInternal(arg4, indexParse.FormatString, nameof(arg4)); + AppendFormatInternal(arg4, indexParse.Alignment, indexParse.FormatString, nameof(arg4)); continue; case 4: - AppendFormatInternal(arg5, indexParse.FormatString, nameof(arg5)); + AppendFormatInternal(arg5, indexParse.Alignment, indexParse.FormatString, nameof(arg5)); continue; default: ThrowFormatException(); break; } } - else if (format[i] == '}') + else if (c == '}') { - if (i != format.Length && format[i + 1] == '}') + if (i + 1 < format.Length && format[i + 1] == '}') { var size = i - copyFrom; Append(format.AsSpan(copyFrom, size)); @@ -346,6 +388,10 @@ public void AppendFormat(string format, T1 arg1, T2 arg2, T3 copyFrom = i; continue; } + else + { + ThrowFormatException(); + } } } @@ -358,14 +404,19 @@ public void AppendFormat(string format, T1 arg1, T2 arg2, T3 } } } - /// Appends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -388,37 +439,37 @@ public void AppendFormat(string format, T1 arg1, T2 arg2 } // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; + i = indexParse.LastIndex - 1; switch (indexParse.Index) { case 0: - AppendFormatInternal(arg1, indexParse.FormatString, nameof(arg1)); + AppendFormatInternal(arg1, indexParse.Alignment, indexParse.FormatString, nameof(arg1)); continue; case 1: - AppendFormatInternal(arg2, indexParse.FormatString, nameof(arg2)); + AppendFormatInternal(arg2, indexParse.Alignment, indexParse.FormatString, nameof(arg2)); continue; case 2: - AppendFormatInternal(arg3, indexParse.FormatString, nameof(arg3)); + AppendFormatInternal(arg3, indexParse.Alignment, indexParse.FormatString, nameof(arg3)); continue; case 3: - AppendFormatInternal(arg4, indexParse.FormatString, nameof(arg4)); + AppendFormatInternal(arg4, indexParse.Alignment, indexParse.FormatString, nameof(arg4)); continue; case 4: - AppendFormatInternal(arg5, indexParse.FormatString, nameof(arg5)); + AppendFormatInternal(arg5, indexParse.Alignment, indexParse.FormatString, nameof(arg5)); continue; case 5: - AppendFormatInternal(arg6, indexParse.FormatString, nameof(arg6)); + AppendFormatInternal(arg6, indexParse.Alignment, indexParse.FormatString, nameof(arg6)); continue; default: ThrowFormatException(); break; } } - else if (format[i] == '}') + else if (c == '}') { - if (i != format.Length && format[i + 1] == '}') + if (i + 1 < format.Length && format[i + 1] == '}') { var size = i - copyFrom; Append(format.AsSpan(copyFrom, size)); @@ -426,6 +477,10 @@ public void AppendFormat(string format, T1 arg1, T2 arg2 copyFrom = i; continue; } + else + { + ThrowFormatException(); + } } } @@ -438,14 +493,19 @@ public void AppendFormat(string format, T1 arg1, T2 arg2 } } } - /// Appends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -468,40 +528,40 @@ public void AppendFormat(string format, T1 arg1, T2 } // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; + i = indexParse.LastIndex - 1; switch (indexParse.Index) { case 0: - AppendFormatInternal(arg1, indexParse.FormatString, nameof(arg1)); + AppendFormatInternal(arg1, indexParse.Alignment, indexParse.FormatString, nameof(arg1)); continue; case 1: - AppendFormatInternal(arg2, indexParse.FormatString, nameof(arg2)); + AppendFormatInternal(arg2, indexParse.Alignment, indexParse.FormatString, nameof(arg2)); continue; case 2: - AppendFormatInternal(arg3, indexParse.FormatString, nameof(arg3)); + AppendFormatInternal(arg3, indexParse.Alignment, indexParse.FormatString, nameof(arg3)); continue; case 3: - AppendFormatInternal(arg4, indexParse.FormatString, nameof(arg4)); + AppendFormatInternal(arg4, indexParse.Alignment, indexParse.FormatString, nameof(arg4)); continue; case 4: - AppendFormatInternal(arg5, indexParse.FormatString, nameof(arg5)); + AppendFormatInternal(arg5, indexParse.Alignment, indexParse.FormatString, nameof(arg5)); continue; case 5: - AppendFormatInternal(arg6, indexParse.FormatString, nameof(arg6)); + AppendFormatInternal(arg6, indexParse.Alignment, indexParse.FormatString, nameof(arg6)); continue; case 6: - AppendFormatInternal(arg7, indexParse.FormatString, nameof(arg7)); + AppendFormatInternal(arg7, indexParse.Alignment, indexParse.FormatString, nameof(arg7)); continue; default: ThrowFormatException(); break; } } - else if (format[i] == '}') + else if (c == '}') { - if (i != format.Length && format[i + 1] == '}') + if (i + 1 < format.Length && format[i + 1] == '}') { var size = i - copyFrom; Append(format.AsSpan(copyFrom, size)); @@ -509,6 +569,10 @@ public void AppendFormat(string format, T1 arg1, T2 copyFrom = i; continue; } + else + { + ThrowFormatException(); + } } } @@ -521,14 +585,19 @@ public void AppendFormat(string format, T1 arg1, T2 } } } - /// Appends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -551,43 +620,43 @@ public void AppendFormat(string format, T1 arg1, } // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; + i = indexParse.LastIndex - 1; switch (indexParse.Index) { case 0: - AppendFormatInternal(arg1, indexParse.FormatString, nameof(arg1)); + AppendFormatInternal(arg1, indexParse.Alignment, indexParse.FormatString, nameof(arg1)); continue; case 1: - AppendFormatInternal(arg2, indexParse.FormatString, nameof(arg2)); + AppendFormatInternal(arg2, indexParse.Alignment, indexParse.FormatString, nameof(arg2)); continue; case 2: - AppendFormatInternal(arg3, indexParse.FormatString, nameof(arg3)); + AppendFormatInternal(arg3, indexParse.Alignment, indexParse.FormatString, nameof(arg3)); continue; case 3: - AppendFormatInternal(arg4, indexParse.FormatString, nameof(arg4)); + AppendFormatInternal(arg4, indexParse.Alignment, indexParse.FormatString, nameof(arg4)); continue; case 4: - AppendFormatInternal(arg5, indexParse.FormatString, nameof(arg5)); + AppendFormatInternal(arg5, indexParse.Alignment, indexParse.FormatString, nameof(arg5)); continue; case 5: - AppendFormatInternal(arg6, indexParse.FormatString, nameof(arg6)); + AppendFormatInternal(arg6, indexParse.Alignment, indexParse.FormatString, nameof(arg6)); continue; case 6: - AppendFormatInternal(arg7, indexParse.FormatString, nameof(arg7)); + AppendFormatInternal(arg7, indexParse.Alignment, indexParse.FormatString, nameof(arg7)); continue; case 7: - AppendFormatInternal(arg8, indexParse.FormatString, nameof(arg8)); + AppendFormatInternal(arg8, indexParse.Alignment, indexParse.FormatString, nameof(arg8)); continue; default: ThrowFormatException(); break; } } - else if (format[i] == '}') + else if (c == '}') { - if (i != format.Length && format[i + 1] == '}') + if (i + 1 < format.Length && format[i + 1] == '}') { var size = i - copyFrom; Append(format.AsSpan(copyFrom, size)); @@ -595,6 +664,10 @@ public void AppendFormat(string format, T1 arg1, copyFrom = i; continue; } + else + { + ThrowFormatException(); + } } } @@ -607,14 +680,19 @@ public void AppendFormat(string format, T1 arg1, } } } - /// Appends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -637,46 +715,46 @@ public void AppendFormat(string format, T1 a } // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; + i = indexParse.LastIndex - 1; switch (indexParse.Index) { case 0: - AppendFormatInternal(arg1, indexParse.FormatString, nameof(arg1)); + AppendFormatInternal(arg1, indexParse.Alignment, indexParse.FormatString, nameof(arg1)); continue; case 1: - AppendFormatInternal(arg2, indexParse.FormatString, nameof(arg2)); + AppendFormatInternal(arg2, indexParse.Alignment, indexParse.FormatString, nameof(arg2)); continue; case 2: - AppendFormatInternal(arg3, indexParse.FormatString, nameof(arg3)); + AppendFormatInternal(arg3, indexParse.Alignment, indexParse.FormatString, nameof(arg3)); continue; case 3: - AppendFormatInternal(arg4, indexParse.FormatString, nameof(arg4)); + AppendFormatInternal(arg4, indexParse.Alignment, indexParse.FormatString, nameof(arg4)); continue; case 4: - AppendFormatInternal(arg5, indexParse.FormatString, nameof(arg5)); + AppendFormatInternal(arg5, indexParse.Alignment, indexParse.FormatString, nameof(arg5)); continue; case 5: - AppendFormatInternal(arg6, indexParse.FormatString, nameof(arg6)); + AppendFormatInternal(arg6, indexParse.Alignment, indexParse.FormatString, nameof(arg6)); continue; case 6: - AppendFormatInternal(arg7, indexParse.FormatString, nameof(arg7)); + AppendFormatInternal(arg7, indexParse.Alignment, indexParse.FormatString, nameof(arg7)); continue; case 7: - AppendFormatInternal(arg8, indexParse.FormatString, nameof(arg8)); + AppendFormatInternal(arg8, indexParse.Alignment, indexParse.FormatString, nameof(arg8)); continue; case 8: - AppendFormatInternal(arg9, indexParse.FormatString, nameof(arg9)); + AppendFormatInternal(arg9, indexParse.Alignment, indexParse.FormatString, nameof(arg9)); continue; default: ThrowFormatException(); break; } } - else if (format[i] == '}') + else if (c == '}') { - if (i != format.Length && format[i + 1] == '}') + if (i + 1 < format.Length && format[i + 1] == '}') { var size = i - copyFrom; Append(format.AsSpan(copyFrom, size)); @@ -684,6 +762,10 @@ public void AppendFormat(string format, T1 a copyFrom = i; continue; } + else + { + ThrowFormatException(); + } } } @@ -696,14 +778,19 @@ public void AppendFormat(string format, T1 a } } } - /// Appends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -726,49 +813,49 @@ public void AppendFormat(string format, } // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; + i = indexParse.LastIndex - 1; switch (indexParse.Index) { case 0: - AppendFormatInternal(arg1, indexParse.FormatString, nameof(arg1)); + AppendFormatInternal(arg1, indexParse.Alignment, indexParse.FormatString, nameof(arg1)); continue; case 1: - AppendFormatInternal(arg2, indexParse.FormatString, nameof(arg2)); + AppendFormatInternal(arg2, indexParse.Alignment, indexParse.FormatString, nameof(arg2)); continue; case 2: - AppendFormatInternal(arg3, indexParse.FormatString, nameof(arg3)); + AppendFormatInternal(arg3, indexParse.Alignment, indexParse.FormatString, nameof(arg3)); continue; case 3: - AppendFormatInternal(arg4, indexParse.FormatString, nameof(arg4)); + AppendFormatInternal(arg4, indexParse.Alignment, indexParse.FormatString, nameof(arg4)); continue; case 4: - AppendFormatInternal(arg5, indexParse.FormatString, nameof(arg5)); + AppendFormatInternal(arg5, indexParse.Alignment, indexParse.FormatString, nameof(arg5)); continue; case 5: - AppendFormatInternal(arg6, indexParse.FormatString, nameof(arg6)); + AppendFormatInternal(arg6, indexParse.Alignment, indexParse.FormatString, nameof(arg6)); continue; case 6: - AppendFormatInternal(arg7, indexParse.FormatString, nameof(arg7)); + AppendFormatInternal(arg7, indexParse.Alignment, indexParse.FormatString, nameof(arg7)); continue; case 7: - AppendFormatInternal(arg8, indexParse.FormatString, nameof(arg8)); + AppendFormatInternal(arg8, indexParse.Alignment, indexParse.FormatString, nameof(arg8)); continue; case 8: - AppendFormatInternal(arg9, indexParse.FormatString, nameof(arg9)); + AppendFormatInternal(arg9, indexParse.Alignment, indexParse.FormatString, nameof(arg9)); continue; case 9: - AppendFormatInternal(arg10, indexParse.FormatString, nameof(arg10)); + AppendFormatInternal(arg10, indexParse.Alignment, indexParse.FormatString, nameof(arg10)); continue; default: ThrowFormatException(); break; } } - else if (format[i] == '}') + else if (c == '}') { - if (i != format.Length && format[i + 1] == '}') + if (i + 1 < format.Length && format[i + 1] == '}') { var size = i - copyFrom; Append(format.AsSpan(copyFrom, size)); @@ -776,6 +863,10 @@ public void AppendFormat(string format, copyFrom = i; continue; } + else + { + ThrowFormatException(); + } } } @@ -788,14 +879,19 @@ public void AppendFormat(string format, } } } - /// Appends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -818,52 +914,52 @@ public void AppendFormat(string fo } // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; + i = indexParse.LastIndex - 1; switch (indexParse.Index) { case 0: - AppendFormatInternal(arg1, indexParse.FormatString, nameof(arg1)); + AppendFormatInternal(arg1, indexParse.Alignment, indexParse.FormatString, nameof(arg1)); continue; case 1: - AppendFormatInternal(arg2, indexParse.FormatString, nameof(arg2)); + AppendFormatInternal(arg2, indexParse.Alignment, indexParse.FormatString, nameof(arg2)); continue; case 2: - AppendFormatInternal(arg3, indexParse.FormatString, nameof(arg3)); + AppendFormatInternal(arg3, indexParse.Alignment, indexParse.FormatString, nameof(arg3)); continue; case 3: - AppendFormatInternal(arg4, indexParse.FormatString, nameof(arg4)); + AppendFormatInternal(arg4, indexParse.Alignment, indexParse.FormatString, nameof(arg4)); continue; case 4: - AppendFormatInternal(arg5, indexParse.FormatString, nameof(arg5)); + AppendFormatInternal(arg5, indexParse.Alignment, indexParse.FormatString, nameof(arg5)); continue; case 5: - AppendFormatInternal(arg6, indexParse.FormatString, nameof(arg6)); + AppendFormatInternal(arg6, indexParse.Alignment, indexParse.FormatString, nameof(arg6)); continue; case 6: - AppendFormatInternal(arg7, indexParse.FormatString, nameof(arg7)); + AppendFormatInternal(arg7, indexParse.Alignment, indexParse.FormatString, nameof(arg7)); continue; case 7: - AppendFormatInternal(arg8, indexParse.FormatString, nameof(arg8)); + AppendFormatInternal(arg8, indexParse.Alignment, indexParse.FormatString, nameof(arg8)); continue; case 8: - AppendFormatInternal(arg9, indexParse.FormatString, nameof(arg9)); + AppendFormatInternal(arg9, indexParse.Alignment, indexParse.FormatString, nameof(arg9)); continue; case 9: - AppendFormatInternal(arg10, indexParse.FormatString, nameof(arg10)); + AppendFormatInternal(arg10, indexParse.Alignment, indexParse.FormatString, nameof(arg10)); continue; case 10: - AppendFormatInternal(arg11, indexParse.FormatString, nameof(arg11)); + AppendFormatInternal(arg11, indexParse.Alignment, indexParse.FormatString, nameof(arg11)); continue; default: ThrowFormatException(); break; } } - else if (format[i] == '}') + else if (c == '}') { - if (i != format.Length && format[i + 1] == '}') + if (i + 1 < format.Length && format[i + 1] == '}') { var size = i - copyFrom; Append(format.AsSpan(copyFrom, size)); @@ -871,6 +967,10 @@ public void AppendFormat(string fo copyFrom = i; continue; } + else + { + ThrowFormatException(); + } } } @@ -883,14 +983,19 @@ public void AppendFormat(string fo } } } - /// Appends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -913,55 +1018,55 @@ public void AppendFormat(stri } // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; + i = indexParse.LastIndex - 1; switch (indexParse.Index) { case 0: - AppendFormatInternal(arg1, indexParse.FormatString, nameof(arg1)); + AppendFormatInternal(arg1, indexParse.Alignment, indexParse.FormatString, nameof(arg1)); continue; case 1: - AppendFormatInternal(arg2, indexParse.FormatString, nameof(arg2)); + AppendFormatInternal(arg2, indexParse.Alignment, indexParse.FormatString, nameof(arg2)); continue; case 2: - AppendFormatInternal(arg3, indexParse.FormatString, nameof(arg3)); + AppendFormatInternal(arg3, indexParse.Alignment, indexParse.FormatString, nameof(arg3)); continue; case 3: - AppendFormatInternal(arg4, indexParse.FormatString, nameof(arg4)); + AppendFormatInternal(arg4, indexParse.Alignment, indexParse.FormatString, nameof(arg4)); continue; case 4: - AppendFormatInternal(arg5, indexParse.FormatString, nameof(arg5)); + AppendFormatInternal(arg5, indexParse.Alignment, indexParse.FormatString, nameof(arg5)); continue; case 5: - AppendFormatInternal(arg6, indexParse.FormatString, nameof(arg6)); + AppendFormatInternal(arg6, indexParse.Alignment, indexParse.FormatString, nameof(arg6)); continue; case 6: - AppendFormatInternal(arg7, indexParse.FormatString, nameof(arg7)); + AppendFormatInternal(arg7, indexParse.Alignment, indexParse.FormatString, nameof(arg7)); continue; case 7: - AppendFormatInternal(arg8, indexParse.FormatString, nameof(arg8)); + AppendFormatInternal(arg8, indexParse.Alignment, indexParse.FormatString, nameof(arg8)); continue; case 8: - AppendFormatInternal(arg9, indexParse.FormatString, nameof(arg9)); + AppendFormatInternal(arg9, indexParse.Alignment, indexParse.FormatString, nameof(arg9)); continue; case 9: - AppendFormatInternal(arg10, indexParse.FormatString, nameof(arg10)); + AppendFormatInternal(arg10, indexParse.Alignment, indexParse.FormatString, nameof(arg10)); continue; case 10: - AppendFormatInternal(arg11, indexParse.FormatString, nameof(arg11)); + AppendFormatInternal(arg11, indexParse.Alignment, indexParse.FormatString, nameof(arg11)); continue; case 11: - AppendFormatInternal(arg12, indexParse.FormatString, nameof(arg12)); + AppendFormatInternal(arg12, indexParse.Alignment, indexParse.FormatString, nameof(arg12)); continue; default: ThrowFormatException(); break; } } - else if (format[i] == '}') + else if (c == '}') { - if (i != format.Length && format[i + 1] == '}') + if (i + 1 < format.Length && format[i + 1] == '}') { var size = i - copyFrom; Append(format.AsSpan(copyFrom, size)); @@ -969,6 +1074,10 @@ public void AppendFormat(stri copyFrom = i; continue; } + else + { + ThrowFormatException(); + } } } @@ -981,14 +1090,19 @@ public void AppendFormat(stri } } } - /// Appends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -1011,58 +1125,58 @@ public void AppendFormat } // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; + i = indexParse.LastIndex - 1; switch (indexParse.Index) { case 0: - AppendFormatInternal(arg1, indexParse.FormatString, nameof(arg1)); + AppendFormatInternal(arg1, indexParse.Alignment, indexParse.FormatString, nameof(arg1)); continue; case 1: - AppendFormatInternal(arg2, indexParse.FormatString, nameof(arg2)); + AppendFormatInternal(arg2, indexParse.Alignment, indexParse.FormatString, nameof(arg2)); continue; case 2: - AppendFormatInternal(arg3, indexParse.FormatString, nameof(arg3)); + AppendFormatInternal(arg3, indexParse.Alignment, indexParse.FormatString, nameof(arg3)); continue; case 3: - AppendFormatInternal(arg4, indexParse.FormatString, nameof(arg4)); + AppendFormatInternal(arg4, indexParse.Alignment, indexParse.FormatString, nameof(arg4)); continue; case 4: - AppendFormatInternal(arg5, indexParse.FormatString, nameof(arg5)); + AppendFormatInternal(arg5, indexParse.Alignment, indexParse.FormatString, nameof(arg5)); continue; case 5: - AppendFormatInternal(arg6, indexParse.FormatString, nameof(arg6)); + AppendFormatInternal(arg6, indexParse.Alignment, indexParse.FormatString, nameof(arg6)); continue; case 6: - AppendFormatInternal(arg7, indexParse.FormatString, nameof(arg7)); + AppendFormatInternal(arg7, indexParse.Alignment, indexParse.FormatString, nameof(arg7)); continue; case 7: - AppendFormatInternal(arg8, indexParse.FormatString, nameof(arg8)); + AppendFormatInternal(arg8, indexParse.Alignment, indexParse.FormatString, nameof(arg8)); continue; case 8: - AppendFormatInternal(arg9, indexParse.FormatString, nameof(arg9)); + AppendFormatInternal(arg9, indexParse.Alignment, indexParse.FormatString, nameof(arg9)); continue; case 9: - AppendFormatInternal(arg10, indexParse.FormatString, nameof(arg10)); + AppendFormatInternal(arg10, indexParse.Alignment, indexParse.FormatString, nameof(arg10)); continue; case 10: - AppendFormatInternal(arg11, indexParse.FormatString, nameof(arg11)); + AppendFormatInternal(arg11, indexParse.Alignment, indexParse.FormatString, nameof(arg11)); continue; case 11: - AppendFormatInternal(arg12, indexParse.FormatString, nameof(arg12)); + AppendFormatInternal(arg12, indexParse.Alignment, indexParse.FormatString, nameof(arg12)); continue; case 12: - AppendFormatInternal(arg13, indexParse.FormatString, nameof(arg13)); + AppendFormatInternal(arg13, indexParse.Alignment, indexParse.FormatString, nameof(arg13)); continue; default: ThrowFormatException(); break; } } - else if (format[i] == '}') + else if (c == '}') { - if (i != format.Length && format[i + 1] == '}') + if (i + 1 < format.Length && format[i + 1] == '}') { var size = i - copyFrom; Append(format.AsSpan(copyFrom, size)); @@ -1070,6 +1184,10 @@ public void AppendFormat copyFrom = i; continue; } + else + { + ThrowFormatException(); + } } } @@ -1082,14 +1200,19 @@ public void AppendFormat } } } - /// Appends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -1112,61 +1235,61 @@ public void AppendFormatAppends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -1216,64 +1348,64 @@ public void AppendFormatAppends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -1323,67 +1464,67 @@ public void AppendFormat(T arg, int width, ReadOnlySpan format, string argName) + { + if (width <= 0) // leftJustify + { + width *= -1; + + if (!FormatterCache.TryFormatDelegate(arg, buffer.AsSpan(index), out var charsWritten, format)) + { + Grow(charsWritten); + if (!FormatterCache.TryFormatDelegate(arg, buffer.AsSpan(index), out charsWritten, format)) + { + ThrowArgumentException(argName); + } + } + + index += charsWritten; + + int padding = width - charsWritten; + if (width > 0 && padding > 0) + { + Append(' ', padding); // TODO Fill Method is too slow. + } + } + else // rightJustify + { + if (typeof(T) == typeof(string)) + { + var s = Unsafe.As(arg); + int padding = width - s.Length; + if (padding > 0) + { + Append(' ', padding); // TODO Fill Method is too slow. + } + + Append(s); + } + else + { + Span s = stackalloc char[typeof(T).IsValueType ? Unsafe.SizeOf() * 8 : 1024]; + + if (!FormatterCache.TryFormatDelegate(arg, s, out var charsWritten, format)) + { + s = stackalloc char[s.Length * 2]; + if (!FormatterCache.TryFormatDelegate(arg, s, out charsWritten, format)) + { + ThrowArgumentException(argName); + } + } + + int padding = width - charsWritten; + if (padding > 0) + { + Append(' ', padding); // TODO Fill Method is too slow. + } + + Append(s.Slice(0, charsWritten)); + } + } + } static void ThrowNestedException() { @@ -665,4 +728,4 @@ static bool TryFormatDefault(T value, Span dest, out int written, ReadOnly } } } -} \ No newline at end of file +} diff --git a/Utf8/Utf8ValueStringBuilder.AppendFormat.cs b/Utf8/Utf8ValueStringBuilder.AppendFormat.cs index 8fd66bc..4f0b1ad 100644 --- a/Utf8/Utf8ValueStringBuilder.AppendFormat.cs +++ b/Utf8/Utf8ValueStringBuilder.AppendFormat.cs @@ -8,10 +8,16 @@ public partial struct Utf8ValueStringBuilder /// Appends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. public void AppendFormat(string format, T1 arg1) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -34,28 +40,33 @@ public void AppendFormat(string format, T1 arg1) } // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; + i = indexParse.LastIndex - 1; var writeFormat = StandardFormat.Parse(indexParse.FormatString); switch (indexParse.Index) { case 0: - AppendFormatInternal(arg1, writeFormat, nameof(arg1)); + AppendFormatInternal(arg1, indexParse.Alignment, writeFormat, nameof(arg1)); continue; default: ThrowFormatException(); break; } } - else if (format[i] == '}') + else if (c == '}') { - if (i != format.Length && format[i + 1] == '}') + if (i + 1 < format.Length && format[i + 1] == '}') { var size = i - copyFrom; Append(format.AsSpan(copyFrom, size)); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ThrowFormatException(); } } @@ -74,10 +85,16 @@ public void AppendFormat(string format, T1 arg1) /// Appends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. public void AppendFormat(string format, T1 arg1, T2 arg2) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -100,31 +117,36 @@ public void AppendFormat(string format, T1 arg1, T2 arg2) } // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; + i = indexParse.LastIndex - 1; var writeFormat = StandardFormat.Parse(indexParse.FormatString); switch (indexParse.Index) { case 0: - AppendFormatInternal(arg1, writeFormat, nameof(arg1)); + AppendFormatInternal(arg1, indexParse.Alignment, writeFormat, nameof(arg1)); continue; case 1: - AppendFormatInternal(arg2, writeFormat, nameof(arg2)); + AppendFormatInternal(arg2, indexParse.Alignment, writeFormat, nameof(arg2)); continue; default: ThrowFormatException(); break; } } - else if (format[i] == '}') + else if (c == '}') { - if (i != format.Length && format[i + 1] == '}') + if (i + 1 < format.Length && format[i + 1] == '}') { var size = i - copyFrom; Append(format.AsSpan(copyFrom, size)); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ThrowFormatException(); } } @@ -143,10 +165,16 @@ public void AppendFormat(string format, T1 arg1, T2 arg2) /// Appends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg3) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -169,34 +197,39 @@ public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg3) } // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; + i = indexParse.LastIndex - 1; var writeFormat = StandardFormat.Parse(indexParse.FormatString); switch (indexParse.Index) { case 0: - AppendFormatInternal(arg1, writeFormat, nameof(arg1)); + AppendFormatInternal(arg1, indexParse.Alignment, writeFormat, nameof(arg1)); continue; case 1: - AppendFormatInternal(arg2, writeFormat, nameof(arg2)); + AppendFormatInternal(arg2, indexParse.Alignment, writeFormat, nameof(arg2)); continue; case 2: - AppendFormatInternal(arg3, writeFormat, nameof(arg3)); + AppendFormatInternal(arg3, indexParse.Alignment, writeFormat, nameof(arg3)); continue; default: ThrowFormatException(); break; } } - else if (format[i] == '}') + else if (c == '}') { - if (i != format.Length && format[i + 1] == '}') + if (i + 1 < format.Length && format[i + 1] == '}') { var size = i - copyFrom; Append(format.AsSpan(copyFrom, size)); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ThrowFormatException(); } } @@ -215,10 +248,16 @@ public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg3) /// Appends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -241,37 +280,42 @@ public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg } // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; + i = indexParse.LastIndex - 1; var writeFormat = StandardFormat.Parse(indexParse.FormatString); switch (indexParse.Index) { case 0: - AppendFormatInternal(arg1, writeFormat, nameof(arg1)); + AppendFormatInternal(arg1, indexParse.Alignment, writeFormat, nameof(arg1)); continue; case 1: - AppendFormatInternal(arg2, writeFormat, nameof(arg2)); + AppendFormatInternal(arg2, indexParse.Alignment, writeFormat, nameof(arg2)); continue; case 2: - AppendFormatInternal(arg3, writeFormat, nameof(arg3)); + AppendFormatInternal(arg3, indexParse.Alignment, writeFormat, nameof(arg3)); continue; case 3: - AppendFormatInternal(arg4, writeFormat, nameof(arg4)); + AppendFormatInternal(arg4, indexParse.Alignment, writeFormat, nameof(arg4)); continue; default: ThrowFormatException(); break; } } - else if (format[i] == '}') + else if (c == '}') { - if (i != format.Length && format[i + 1] == '}') + if (i + 1 < format.Length && format[i + 1] == '}') { var size = i - copyFrom; Append(format.AsSpan(copyFrom, size)); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ThrowFormatException(); } } @@ -290,10 +334,16 @@ public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg /// Appends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -316,40 +366,45 @@ public void AppendFormat(string format, T1 arg1, T2 arg2, T3 } // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; + i = indexParse.LastIndex - 1; var writeFormat = StandardFormat.Parse(indexParse.FormatString); switch (indexParse.Index) { case 0: - AppendFormatInternal(arg1, writeFormat, nameof(arg1)); + AppendFormatInternal(arg1, indexParse.Alignment, writeFormat, nameof(arg1)); continue; case 1: - AppendFormatInternal(arg2, writeFormat, nameof(arg2)); + AppendFormatInternal(arg2, indexParse.Alignment, writeFormat, nameof(arg2)); continue; case 2: - AppendFormatInternal(arg3, writeFormat, nameof(arg3)); + AppendFormatInternal(arg3, indexParse.Alignment, writeFormat, nameof(arg3)); continue; case 3: - AppendFormatInternal(arg4, writeFormat, nameof(arg4)); + AppendFormatInternal(arg4, indexParse.Alignment, writeFormat, nameof(arg4)); continue; case 4: - AppendFormatInternal(arg5, writeFormat, nameof(arg5)); + AppendFormatInternal(arg5, indexParse.Alignment, writeFormat, nameof(arg5)); continue; default: ThrowFormatException(); break; } } - else if (format[i] == '}') + else if (c == '}') { - if (i != format.Length && format[i + 1] == '}') + if (i + 1 < format.Length && format[i + 1] == '}') { var size = i - copyFrom; Append(format.AsSpan(copyFrom, size)); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ThrowFormatException(); } } @@ -368,10 +423,16 @@ public void AppendFormat(string format, T1 arg1, T2 arg2, T3 /// Appends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -394,43 +455,48 @@ public void AppendFormat(string format, T1 arg1, T2 arg2 } // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; + i = indexParse.LastIndex - 1; var writeFormat = StandardFormat.Parse(indexParse.FormatString); switch (indexParse.Index) { case 0: - AppendFormatInternal(arg1, writeFormat, nameof(arg1)); + AppendFormatInternal(arg1, indexParse.Alignment, writeFormat, nameof(arg1)); continue; case 1: - AppendFormatInternal(arg2, writeFormat, nameof(arg2)); + AppendFormatInternal(arg2, indexParse.Alignment, writeFormat, nameof(arg2)); continue; case 2: - AppendFormatInternal(arg3, writeFormat, nameof(arg3)); + AppendFormatInternal(arg3, indexParse.Alignment, writeFormat, nameof(arg3)); continue; case 3: - AppendFormatInternal(arg4, writeFormat, nameof(arg4)); + AppendFormatInternal(arg4, indexParse.Alignment, writeFormat, nameof(arg4)); continue; case 4: - AppendFormatInternal(arg5, writeFormat, nameof(arg5)); + AppendFormatInternal(arg5, indexParse.Alignment, writeFormat, nameof(arg5)); continue; case 5: - AppendFormatInternal(arg6, writeFormat, nameof(arg6)); + AppendFormatInternal(arg6, indexParse.Alignment, writeFormat, nameof(arg6)); continue; default: ThrowFormatException(); break; } } - else if (format[i] == '}') + else if (c == '}') { - if (i != format.Length && format[i + 1] == '}') + if (i + 1 < format.Length && format[i + 1] == '}') { var size = i - copyFrom; Append(format.AsSpan(copyFrom, size)); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ThrowFormatException(); } } @@ -449,10 +515,16 @@ public void AppendFormat(string format, T1 arg1, T2 arg2 /// Appends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -475,46 +547,51 @@ public void AppendFormat(string format, T1 arg1, T2 } // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; + i = indexParse.LastIndex - 1; var writeFormat = StandardFormat.Parse(indexParse.FormatString); switch (indexParse.Index) { case 0: - AppendFormatInternal(arg1, writeFormat, nameof(arg1)); + AppendFormatInternal(arg1, indexParse.Alignment, writeFormat, nameof(arg1)); continue; case 1: - AppendFormatInternal(arg2, writeFormat, nameof(arg2)); + AppendFormatInternal(arg2, indexParse.Alignment, writeFormat, nameof(arg2)); continue; case 2: - AppendFormatInternal(arg3, writeFormat, nameof(arg3)); + AppendFormatInternal(arg3, indexParse.Alignment, writeFormat, nameof(arg3)); continue; case 3: - AppendFormatInternal(arg4, writeFormat, nameof(arg4)); + AppendFormatInternal(arg4, indexParse.Alignment, writeFormat, nameof(arg4)); continue; case 4: - AppendFormatInternal(arg5, writeFormat, nameof(arg5)); + AppendFormatInternal(arg5, indexParse.Alignment, writeFormat, nameof(arg5)); continue; case 5: - AppendFormatInternal(arg6, writeFormat, nameof(arg6)); + AppendFormatInternal(arg6, indexParse.Alignment, writeFormat, nameof(arg6)); continue; case 6: - AppendFormatInternal(arg7, writeFormat, nameof(arg7)); + AppendFormatInternal(arg7, indexParse.Alignment, writeFormat, nameof(arg7)); continue; default: ThrowFormatException(); break; } } - else if (format[i] == '}') + else if (c == '}') { - if (i != format.Length && format[i + 1] == '}') + if (i + 1 < format.Length && format[i + 1] == '}') { var size = i - copyFrom; Append(format.AsSpan(copyFrom, size)); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ThrowFormatException(); } } @@ -533,10 +610,16 @@ public void AppendFormat(string format, T1 arg1, T2 /// Appends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -559,49 +642,54 @@ public void AppendFormat(string format, T1 arg1, } // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; + i = indexParse.LastIndex - 1; var writeFormat = StandardFormat.Parse(indexParse.FormatString); switch (indexParse.Index) { case 0: - AppendFormatInternal(arg1, writeFormat, nameof(arg1)); + AppendFormatInternal(arg1, indexParse.Alignment, writeFormat, nameof(arg1)); continue; case 1: - AppendFormatInternal(arg2, writeFormat, nameof(arg2)); + AppendFormatInternal(arg2, indexParse.Alignment, writeFormat, nameof(arg2)); continue; case 2: - AppendFormatInternal(arg3, writeFormat, nameof(arg3)); + AppendFormatInternal(arg3, indexParse.Alignment, writeFormat, nameof(arg3)); continue; case 3: - AppendFormatInternal(arg4, writeFormat, nameof(arg4)); + AppendFormatInternal(arg4, indexParse.Alignment, writeFormat, nameof(arg4)); continue; case 4: - AppendFormatInternal(arg5, writeFormat, nameof(arg5)); + AppendFormatInternal(arg5, indexParse.Alignment, writeFormat, nameof(arg5)); continue; case 5: - AppendFormatInternal(arg6, writeFormat, nameof(arg6)); + AppendFormatInternal(arg6, indexParse.Alignment, writeFormat, nameof(arg6)); continue; case 6: - AppendFormatInternal(arg7, writeFormat, nameof(arg7)); + AppendFormatInternal(arg7, indexParse.Alignment, writeFormat, nameof(arg7)); continue; case 7: - AppendFormatInternal(arg8, writeFormat, nameof(arg8)); + AppendFormatInternal(arg8, indexParse.Alignment, writeFormat, nameof(arg8)); continue; default: ThrowFormatException(); break; } } - else if (format[i] == '}') + else if (c == '}') { - if (i != format.Length && format[i + 1] == '}') + if (i + 1 < format.Length && format[i + 1] == '}') { var size = i - copyFrom; Append(format.AsSpan(copyFrom, size)); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ThrowFormatException(); } } @@ -620,10 +708,16 @@ public void AppendFormat(string format, T1 arg1, /// Appends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -646,52 +740,57 @@ public void AppendFormat(string format, T1 a } // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; + i = indexParse.LastIndex - 1; var writeFormat = StandardFormat.Parse(indexParse.FormatString); switch (indexParse.Index) { case 0: - AppendFormatInternal(arg1, writeFormat, nameof(arg1)); + AppendFormatInternal(arg1, indexParse.Alignment, writeFormat, nameof(arg1)); continue; case 1: - AppendFormatInternal(arg2, writeFormat, nameof(arg2)); + AppendFormatInternal(arg2, indexParse.Alignment, writeFormat, nameof(arg2)); continue; case 2: - AppendFormatInternal(arg3, writeFormat, nameof(arg3)); + AppendFormatInternal(arg3, indexParse.Alignment, writeFormat, nameof(arg3)); continue; case 3: - AppendFormatInternal(arg4, writeFormat, nameof(arg4)); + AppendFormatInternal(arg4, indexParse.Alignment, writeFormat, nameof(arg4)); continue; case 4: - AppendFormatInternal(arg5, writeFormat, nameof(arg5)); + AppendFormatInternal(arg5, indexParse.Alignment, writeFormat, nameof(arg5)); continue; case 5: - AppendFormatInternal(arg6, writeFormat, nameof(arg6)); + AppendFormatInternal(arg6, indexParse.Alignment, writeFormat, nameof(arg6)); continue; case 6: - AppendFormatInternal(arg7, writeFormat, nameof(arg7)); + AppendFormatInternal(arg7, indexParse.Alignment, writeFormat, nameof(arg7)); continue; case 7: - AppendFormatInternal(arg8, writeFormat, nameof(arg8)); + AppendFormatInternal(arg8, indexParse.Alignment, writeFormat, nameof(arg8)); continue; case 8: - AppendFormatInternal(arg9, writeFormat, nameof(arg9)); + AppendFormatInternal(arg9, indexParse.Alignment, writeFormat, nameof(arg9)); continue; default: ThrowFormatException(); break; } } - else if (format[i] == '}') + else if (c == '}') { - if (i != format.Length && format[i + 1] == '}') + if (i + 1 < format.Length && format[i + 1] == '}') { var size = i - copyFrom; Append(format.AsSpan(copyFrom, size)); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ThrowFormatException(); } } @@ -710,10 +809,16 @@ public void AppendFormat(string format, T1 a /// Appends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -736,55 +841,60 @@ public void AppendFormat(string format, } // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; + i = indexParse.LastIndex - 1; var writeFormat = StandardFormat.Parse(indexParse.FormatString); switch (indexParse.Index) { case 0: - AppendFormatInternal(arg1, writeFormat, nameof(arg1)); + AppendFormatInternal(arg1, indexParse.Alignment, writeFormat, nameof(arg1)); continue; case 1: - AppendFormatInternal(arg2, writeFormat, nameof(arg2)); + AppendFormatInternal(arg2, indexParse.Alignment, writeFormat, nameof(arg2)); continue; case 2: - AppendFormatInternal(arg3, writeFormat, nameof(arg3)); + AppendFormatInternal(arg3, indexParse.Alignment, writeFormat, nameof(arg3)); continue; case 3: - AppendFormatInternal(arg4, writeFormat, nameof(arg4)); + AppendFormatInternal(arg4, indexParse.Alignment, writeFormat, nameof(arg4)); continue; case 4: - AppendFormatInternal(arg5, writeFormat, nameof(arg5)); + AppendFormatInternal(arg5, indexParse.Alignment, writeFormat, nameof(arg5)); continue; case 5: - AppendFormatInternal(arg6, writeFormat, nameof(arg6)); + AppendFormatInternal(arg6, indexParse.Alignment, writeFormat, nameof(arg6)); continue; case 6: - AppendFormatInternal(arg7, writeFormat, nameof(arg7)); + AppendFormatInternal(arg7, indexParse.Alignment, writeFormat, nameof(arg7)); continue; case 7: - AppendFormatInternal(arg8, writeFormat, nameof(arg8)); + AppendFormatInternal(arg8, indexParse.Alignment, writeFormat, nameof(arg8)); continue; case 8: - AppendFormatInternal(arg9, writeFormat, nameof(arg9)); + AppendFormatInternal(arg9, indexParse.Alignment, writeFormat, nameof(arg9)); continue; case 9: - AppendFormatInternal(arg10, writeFormat, nameof(arg10)); + AppendFormatInternal(arg10, indexParse.Alignment, writeFormat, nameof(arg10)); continue; default: ThrowFormatException(); break; } } - else if (format[i] == '}') + else if (c == '}') { - if (i != format.Length && format[i + 1] == '}') + if (i + 1 < format.Length && format[i + 1] == '}') { var size = i - copyFrom; Append(format.AsSpan(copyFrom, size)); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ThrowFormatException(); } } @@ -803,10 +913,16 @@ public void AppendFormat(string format, /// Appends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -829,58 +945,63 @@ public void AppendFormat(string fo } // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; + i = indexParse.LastIndex - 1; var writeFormat = StandardFormat.Parse(indexParse.FormatString); switch (indexParse.Index) { case 0: - AppendFormatInternal(arg1, writeFormat, nameof(arg1)); + AppendFormatInternal(arg1, indexParse.Alignment, writeFormat, nameof(arg1)); continue; case 1: - AppendFormatInternal(arg2, writeFormat, nameof(arg2)); + AppendFormatInternal(arg2, indexParse.Alignment, writeFormat, nameof(arg2)); continue; case 2: - AppendFormatInternal(arg3, writeFormat, nameof(arg3)); + AppendFormatInternal(arg3, indexParse.Alignment, writeFormat, nameof(arg3)); continue; case 3: - AppendFormatInternal(arg4, writeFormat, nameof(arg4)); + AppendFormatInternal(arg4, indexParse.Alignment, writeFormat, nameof(arg4)); continue; case 4: - AppendFormatInternal(arg5, writeFormat, nameof(arg5)); + AppendFormatInternal(arg5, indexParse.Alignment, writeFormat, nameof(arg5)); continue; case 5: - AppendFormatInternal(arg6, writeFormat, nameof(arg6)); + AppendFormatInternal(arg6, indexParse.Alignment, writeFormat, nameof(arg6)); continue; case 6: - AppendFormatInternal(arg7, writeFormat, nameof(arg7)); + AppendFormatInternal(arg7, indexParse.Alignment, writeFormat, nameof(arg7)); continue; case 7: - AppendFormatInternal(arg8, writeFormat, nameof(arg8)); + AppendFormatInternal(arg8, indexParse.Alignment, writeFormat, nameof(arg8)); continue; case 8: - AppendFormatInternal(arg9, writeFormat, nameof(arg9)); + AppendFormatInternal(arg9, indexParse.Alignment, writeFormat, nameof(arg9)); continue; case 9: - AppendFormatInternal(arg10, writeFormat, nameof(arg10)); + AppendFormatInternal(arg10, indexParse.Alignment, writeFormat, nameof(arg10)); continue; case 10: - AppendFormatInternal(arg11, writeFormat, nameof(arg11)); + AppendFormatInternal(arg11, indexParse.Alignment, writeFormat, nameof(arg11)); continue; default: ThrowFormatException(); break; } } - else if (format[i] == '}') + else if (c == '}') { - if (i != format.Length && format[i + 1] == '}') + if (i + 1 < format.Length && format[i + 1] == '}') { var size = i - copyFrom; Append(format.AsSpan(copyFrom, size)); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ThrowFormatException(); } } @@ -899,10 +1020,16 @@ public void AppendFormat(string fo /// Appends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -925,61 +1052,66 @@ public void AppendFormat(stri } // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; + i = indexParse.LastIndex - 1; var writeFormat = StandardFormat.Parse(indexParse.FormatString); switch (indexParse.Index) { case 0: - AppendFormatInternal(arg1, writeFormat, nameof(arg1)); + AppendFormatInternal(arg1, indexParse.Alignment, writeFormat, nameof(arg1)); continue; case 1: - AppendFormatInternal(arg2, writeFormat, nameof(arg2)); + AppendFormatInternal(arg2, indexParse.Alignment, writeFormat, nameof(arg2)); continue; case 2: - AppendFormatInternal(arg3, writeFormat, nameof(arg3)); + AppendFormatInternal(arg3, indexParse.Alignment, writeFormat, nameof(arg3)); continue; case 3: - AppendFormatInternal(arg4, writeFormat, nameof(arg4)); + AppendFormatInternal(arg4, indexParse.Alignment, writeFormat, nameof(arg4)); continue; case 4: - AppendFormatInternal(arg5, writeFormat, nameof(arg5)); + AppendFormatInternal(arg5, indexParse.Alignment, writeFormat, nameof(arg5)); continue; case 5: - AppendFormatInternal(arg6, writeFormat, nameof(arg6)); + AppendFormatInternal(arg6, indexParse.Alignment, writeFormat, nameof(arg6)); continue; case 6: - AppendFormatInternal(arg7, writeFormat, nameof(arg7)); + AppendFormatInternal(arg7, indexParse.Alignment, writeFormat, nameof(arg7)); continue; case 7: - AppendFormatInternal(arg8, writeFormat, nameof(arg8)); + AppendFormatInternal(arg8, indexParse.Alignment, writeFormat, nameof(arg8)); continue; case 8: - AppendFormatInternal(arg9, writeFormat, nameof(arg9)); + AppendFormatInternal(arg9, indexParse.Alignment, writeFormat, nameof(arg9)); continue; case 9: - AppendFormatInternal(arg10, writeFormat, nameof(arg10)); + AppendFormatInternal(arg10, indexParse.Alignment, writeFormat, nameof(arg10)); continue; case 10: - AppendFormatInternal(arg11, writeFormat, nameof(arg11)); + AppendFormatInternal(arg11, indexParse.Alignment, writeFormat, nameof(arg11)); continue; case 11: - AppendFormatInternal(arg12, writeFormat, nameof(arg12)); + AppendFormatInternal(arg12, indexParse.Alignment, writeFormat, nameof(arg12)); continue; default: ThrowFormatException(); break; } } - else if (format[i] == '}') + else if (c == '}') { - if (i != format.Length && format[i + 1] == '}') + if (i + 1 < format.Length && format[i + 1] == '}') { var size = i - copyFrom; Append(format.AsSpan(copyFrom, size)); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ThrowFormatException(); } } @@ -998,10 +1130,16 @@ public void AppendFormat(stri /// Appends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -1024,64 +1162,69 @@ public void AppendFormat } // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; + i = indexParse.LastIndex - 1; var writeFormat = StandardFormat.Parse(indexParse.FormatString); switch (indexParse.Index) { case 0: - AppendFormatInternal(arg1, writeFormat, nameof(arg1)); + AppendFormatInternal(arg1, indexParse.Alignment, writeFormat, nameof(arg1)); continue; case 1: - AppendFormatInternal(arg2, writeFormat, nameof(arg2)); + AppendFormatInternal(arg2, indexParse.Alignment, writeFormat, nameof(arg2)); continue; case 2: - AppendFormatInternal(arg3, writeFormat, nameof(arg3)); + AppendFormatInternal(arg3, indexParse.Alignment, writeFormat, nameof(arg3)); continue; case 3: - AppendFormatInternal(arg4, writeFormat, nameof(arg4)); + AppendFormatInternal(arg4, indexParse.Alignment, writeFormat, nameof(arg4)); continue; case 4: - AppendFormatInternal(arg5, writeFormat, nameof(arg5)); + AppendFormatInternal(arg5, indexParse.Alignment, writeFormat, nameof(arg5)); continue; case 5: - AppendFormatInternal(arg6, writeFormat, nameof(arg6)); + AppendFormatInternal(arg6, indexParse.Alignment, writeFormat, nameof(arg6)); continue; case 6: - AppendFormatInternal(arg7, writeFormat, nameof(arg7)); + AppendFormatInternal(arg7, indexParse.Alignment, writeFormat, nameof(arg7)); continue; case 7: - AppendFormatInternal(arg8, writeFormat, nameof(arg8)); + AppendFormatInternal(arg8, indexParse.Alignment, writeFormat, nameof(arg8)); continue; case 8: - AppendFormatInternal(arg9, writeFormat, nameof(arg9)); + AppendFormatInternal(arg9, indexParse.Alignment, writeFormat, nameof(arg9)); continue; case 9: - AppendFormatInternal(arg10, writeFormat, nameof(arg10)); + AppendFormatInternal(arg10, indexParse.Alignment, writeFormat, nameof(arg10)); continue; case 10: - AppendFormatInternal(arg11, writeFormat, nameof(arg11)); + AppendFormatInternal(arg11, indexParse.Alignment, writeFormat, nameof(arg11)); continue; case 11: - AppendFormatInternal(arg12, writeFormat, nameof(arg12)); + AppendFormatInternal(arg12, indexParse.Alignment, writeFormat, nameof(arg12)); continue; case 12: - AppendFormatInternal(arg13, writeFormat, nameof(arg13)); + AppendFormatInternal(arg13, indexParse.Alignment, writeFormat, nameof(arg13)); continue; default: ThrowFormatException(); break; } } - else if (format[i] == '}') + else if (c == '}') { - if (i != format.Length && format[i + 1] == '}') + if (i + 1 < format.Length && format[i + 1] == '}') { var size = i - copyFrom; Append(format.AsSpan(copyFrom, size)); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ThrowFormatException(); } } @@ -1100,10 +1243,16 @@ public void AppendFormat /// Appends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -1126,67 +1275,72 @@ public void AppendFormatAppends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -1231,70 +1391,75 @@ public void AppendFormatAppends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. public void AppendFormat(string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -1339,73 +1510,78 @@ public void AppendFormat(T arg, StandardFormat writeFormat, string argName) + private void AppendFormatInternal(T arg, int width, StandardFormat format, string argName) { - if (!FormatterCache.TryFormatDelegate(arg, buffer.AsSpan(index), out var written, writeFormat)) + if (width <= 0) // leftJustify { - Grow(written); - if (!FormatterCache.TryFormatDelegate(arg, buffer.AsSpan(index), out written, writeFormat)) + width *= -1; + + if (!FormatterCache.TryFormatDelegate(arg, buffer.AsSpan(index), out var charsWritten, format)) { - ThrowArgumentException(argName); + Grow(charsWritten); + if (!FormatterCache.TryFormatDelegate(arg, buffer.AsSpan(index), out charsWritten, format)) + { + ThrowArgumentException(argName); + } + } + + index += charsWritten; + + int padding = width - charsWritten; + if (width > 0 && padding > 0) + { + Append(' ', padding); // TODO Fill Method is too slow. } } - index += written; - } + else // rightJustify + { + if (typeof(T) == typeof(string)) + { + var s = Unsafe.As(arg); + int padding = width - s.Length; + if (padding > 0) + { + Append(' ', padding); // TODO Fill Method is too slow. + } + + Append(s); + } + else + { + Span s = stackalloc byte[typeof(T).IsValueType ? Unsafe.SizeOf() * 8 : 1024]; + + if (!FormatterCache.TryFormatDelegate(arg, s, out var charsWritten, format)) + { + s = stackalloc byte[s.Length * 2]; + if (!FormatterCache.TryFormatDelegate(arg, s, out charsWritten, format)) + { + ThrowArgumentException(argName); + } + } + int padding = width - charsWritten; + if (padding > 0) + { + Append(' ', padding); // TODO Fill Method is too slow. + } + + s.CopyTo(GetSpan(charsWritten)); + Advance(charsWritten); + } + } + } /// /// Register custom formatter @@ -455,4 +502,4 @@ static bool TryFormatDefault(T value, Span dest, out int written, Standard } } } -} \ No newline at end of file +} diff --git a/ZString.Utf8Format.cs b/ZString.Utf8Format.cs index dcf7f39..ce267b9 100644 --- a/ZString.Utf8Format.cs +++ b/ZString.Utf8Format.cs @@ -1,6 +1,8 @@ -using System.Runtime.CompilerServices; +using System; using System.Buffers; -using System; +using System.Runtime.CompilerServices; + +using static Cysharp.Text.Utf8ValueStringBuilder; namespace Cysharp.Text { @@ -9,10 +11,16 @@ public static partial class ZString /// Replaces one or more format items in a string with the string representation of some specified values. public static void Utf8Format(IBufferWriter bufferWriter, string format, T1 arg1) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -39,27 +47,15 @@ public static void Utf8Format(IBufferWriter bufferWriter, string forma } // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; + i = indexParse.LastIndex - 1; var writeFormat = StandardFormat.Parse(indexParse.FormatString); switch (indexParse.Index) { case 0: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -67,9 +63,9 @@ public static void Utf8Format(IBufferWriter bufferWriter, string forma ExceptionUtil.ThrowFormatException(); } - else if (format[i] == '}') + else if (c == '}') { - if (i != format.Length && format[i + 1] == '}') + if (i + 1 < format.Length && format[i + 1] == '}') { var size = i - copyFrom; var buffer = bufferWriter.GetSpan(UTF8NoBom.GetMaxByteCount(size)); @@ -77,11 +73,13 @@ public static void Utf8Format(IBufferWriter bufferWriter, string forma bufferWriter.Advance(written); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ExceptionUtil.ThrowFormatException(); } } - - NEXT_LOOP: - continue; } { @@ -95,14 +93,19 @@ public static void Utf8Format(IBufferWriter bufferWriter, string forma } } } - /// Replaces one or more format items in a string with the string representation of some specified values. public static void Utf8Format(IBufferWriter bufferWriter, string format, T1 arg1, T2 arg2) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -129,42 +132,18 @@ public static void Utf8Format(IBufferWriter bufferWriter, string f } // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; + i = indexParse.LastIndex - 1; var writeFormat = StandardFormat.Parse(indexParse.FormatString); switch (indexParse.Index) { case 0: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + continue; case 1: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -172,9 +151,9 @@ public static void Utf8Format(IBufferWriter bufferWriter, string f ExceptionUtil.ThrowFormatException(); } - else if (format[i] == '}') + else if (c == '}') { - if (i != format.Length && format[i + 1] == '}') + if (i + 1 < format.Length && format[i + 1] == '}') { var size = i - copyFrom; var buffer = bufferWriter.GetSpan(UTF8NoBom.GetMaxByteCount(size)); @@ -182,11 +161,13 @@ public static void Utf8Format(IBufferWriter bufferWriter, string f bufferWriter.Advance(written); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ExceptionUtil.ThrowFormatException(); } } - - NEXT_LOOP: - continue; } { @@ -200,14 +181,19 @@ public static void Utf8Format(IBufferWriter bufferWriter, string f } } } - /// Replaces one or more format items in a string with the string representation of some specified values. public static void Utf8Format(IBufferWriter bufferWriter, string format, T1 arg1, T2 arg2, T3 arg3) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -234,57 +220,21 @@ public static void Utf8Format(IBufferWriter bufferWriter, stri } // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; + i = indexParse.LastIndex - 1; var writeFormat = StandardFormat.Parse(indexParse.FormatString); switch (indexParse.Index) { case 0: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + continue; case 1: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); + continue; case 2: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -292,9 +242,9 @@ public static void Utf8Format(IBufferWriter bufferWriter, stri ExceptionUtil.ThrowFormatException(); } - else if (format[i] == '}') + else if (c == '}') { - if (i != format.Length && format[i + 1] == '}') + if (i + 1 < format.Length && format[i + 1] == '}') { var size = i - copyFrom; var buffer = bufferWriter.GetSpan(UTF8NoBom.GetMaxByteCount(size)); @@ -302,11 +252,13 @@ public static void Utf8Format(IBufferWriter bufferWriter, stri bufferWriter.Advance(written); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ExceptionUtil.ThrowFormatException(); } } - - NEXT_LOOP: - continue; } { @@ -320,14 +272,19 @@ public static void Utf8Format(IBufferWriter bufferWriter, stri } } } - /// Replaces one or more format items in a string with the string representation of some specified values. public static void Utf8Format(IBufferWriter bufferWriter, string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -354,72 +311,24 @@ public static void Utf8Format(IBufferWriter bufferWriter, } // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; + i = indexParse.LastIndex - 1; var writeFormat = StandardFormat.Parse(indexParse.FormatString); switch (indexParse.Index) { case 0: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + continue; case 1: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); + continue; case 2: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); + continue; case 3: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -427,9 +336,9 @@ public static void Utf8Format(IBufferWriter bufferWriter, ExceptionUtil.ThrowFormatException(); } - else if (format[i] == '}') + else if (c == '}') { - if (i != format.Length && format[i + 1] == '}') + if (i + 1 < format.Length && format[i + 1] == '}') { var size = i - copyFrom; var buffer = bufferWriter.GetSpan(UTF8NoBom.GetMaxByteCount(size)); @@ -437,11 +346,13 @@ public static void Utf8Format(IBufferWriter bufferWriter, bufferWriter.Advance(written); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ExceptionUtil.ThrowFormatException(); } } - - NEXT_LOOP: - continue; } { @@ -455,14 +366,19 @@ public static void Utf8Format(IBufferWriter bufferWriter, } } } - /// Replaces one or more format items in a string with the string representation of some specified values. public static void Utf8Format(IBufferWriter bufferWriter, string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -489,87 +405,27 @@ public static void Utf8Format(IBufferWriter bufferWrit } // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; + i = indexParse.LastIndex - 1; var writeFormat = StandardFormat.Parse(indexParse.FormatString); switch (indexParse.Index) { case 0: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + continue; case 1: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); + continue; case 2: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); + continue; case 3: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); + continue; case 4: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg5)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -577,9 +433,9 @@ public static void Utf8Format(IBufferWriter bufferWrit ExceptionUtil.ThrowFormatException(); } - else if (format[i] == '}') + else if (c == '}') { - if (i != format.Length && format[i + 1] == '}') + if (i + 1 < format.Length && format[i + 1] == '}') { var size = i - copyFrom; var buffer = bufferWriter.GetSpan(UTF8NoBom.GetMaxByteCount(size)); @@ -587,11 +443,13 @@ public static void Utf8Format(IBufferWriter bufferWrit bufferWriter.Advance(written); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ExceptionUtil.ThrowFormatException(); } } - - NEXT_LOOP: - continue; } { @@ -605,14 +463,19 @@ public static void Utf8Format(IBufferWriter bufferWrit } } } - /// Replaces one or more format items in a string with the string representation of some specified values. public static void Utf8Format(IBufferWriter bufferWriter, string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -639,102 +502,30 @@ public static void Utf8Format(IBufferWriter buffer } // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; + i = indexParse.LastIndex - 1; var writeFormat = StandardFormat.Parse(indexParse.FormatString); switch (indexParse.Index) { case 0: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + continue; case 1: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); + continue; case 2: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); + continue; case 3: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); + continue; case 4: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg5)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); + continue; case 5: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg6)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg6, indexParse.Alignment, writeFormat, nameof(arg6)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -742,9 +533,9 @@ public static void Utf8Format(IBufferWriter buffer ExceptionUtil.ThrowFormatException(); } - else if (format[i] == '}') + else if (c == '}') { - if (i != format.Length && format[i + 1] == '}') + if (i + 1 < format.Length && format[i + 1] == '}') { var size = i - copyFrom; var buffer = bufferWriter.GetSpan(UTF8NoBom.GetMaxByteCount(size)); @@ -752,11 +543,13 @@ public static void Utf8Format(IBufferWriter buffer bufferWriter.Advance(written); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ExceptionUtil.ThrowFormatException(); } } - - NEXT_LOOP: - continue; } { @@ -770,14 +563,19 @@ public static void Utf8Format(IBufferWriter buffer } } } - /// Replaces one or more format items in a string with the string representation of some specified values. public static void Utf8Format(IBufferWriter bufferWriter, string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -804,117 +602,33 @@ public static void Utf8Format(IBufferWriter bu } // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; + i = indexParse.LastIndex - 1; var writeFormat = StandardFormat.Parse(indexParse.FormatString); switch (indexParse.Index) { case 0: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + continue; case 1: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); + continue; case 2: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); + continue; case 3: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); + continue; case 4: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg5)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); + continue; case 5: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg6)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg6, indexParse.Alignment, writeFormat, nameof(arg6)); + continue; case 6: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg7)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg7, indexParse.Alignment, writeFormat, nameof(arg7)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -922,9 +636,9 @@ public static void Utf8Format(IBufferWriter bu ExceptionUtil.ThrowFormatException(); } - else if (format[i] == '}') + else if (c == '}') { - if (i != format.Length && format[i + 1] == '}') + if (i + 1 < format.Length && format[i + 1] == '}') { var size = i - copyFrom; var buffer = bufferWriter.GetSpan(UTF8NoBom.GetMaxByteCount(size)); @@ -932,11 +646,13 @@ public static void Utf8Format(IBufferWriter bu bufferWriter.Advance(written); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ExceptionUtil.ThrowFormatException(); } } - - NEXT_LOOP: - continue; } { @@ -950,14 +666,19 @@ public static void Utf8Format(IBufferWriter bu } } } - /// Replaces one or more format items in a string with the string representation of some specified values. public static void Utf8Format(IBufferWriter bufferWriter, string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -984,132 +705,36 @@ public static void Utf8Format(IBufferWriter.TryFormatDelegate(arg1, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + continue; case 1: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); + continue; case 2: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); + continue; case 3: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); + continue; case 4: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg5)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); + continue; case 5: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg6)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg6, indexParse.Alignment, writeFormat, nameof(arg6)); + continue; case 6: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg7)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg7, indexParse.Alignment, writeFormat, nameof(arg7)); + continue; case 7: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg8)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg8, indexParse.Alignment, writeFormat, nameof(arg8)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -1117,9 +742,9 @@ public static void Utf8Format(IBufferWriter(IBufferWriter(IBufferWriterReplaces one or more format items in a string with the string representation of some specified values. public static void Utf8Format(IBufferWriter bufferWriter, string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -1179,147 +811,39 @@ public static void Utf8Format(IBufferWriter< } // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; + i = indexParse.LastIndex - 1; var writeFormat = StandardFormat.Parse(indexParse.FormatString); switch (indexParse.Index) { case 0: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + continue; case 1: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); + continue; case 2: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); + continue; case 3: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); + continue; case 4: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg5)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); + continue; case 5: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg6)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg6, indexParse.Alignment, writeFormat, nameof(arg6)); + continue; case 6: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg7)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg7, indexParse.Alignment, writeFormat, nameof(arg7)); + continue; case 7: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg8)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg8, indexParse.Alignment, writeFormat, nameof(arg8)); + continue; case 8: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg9)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg9, indexParse.Alignment, writeFormat, nameof(arg9)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -1327,9 +851,9 @@ public static void Utf8Format(IBufferWriter< ExceptionUtil.ThrowFormatException(); } - else if (format[i] == '}') + else if (c == '}') { - if (i != format.Length && format[i + 1] == '}') + if (i + 1 < format.Length && format[i + 1] == '}') { var size = i - copyFrom; var buffer = bufferWriter.GetSpan(UTF8NoBom.GetMaxByteCount(size)); @@ -1337,11 +861,13 @@ public static void Utf8Format(IBufferWriter< bufferWriter.Advance(written); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ExceptionUtil.ThrowFormatException(); } } - - NEXT_LOOP: - continue; } { @@ -1355,14 +881,19 @@ public static void Utf8Format(IBufferWriter< } } } - /// Replaces one or more format items in a string with the string representation of some specified values. public static void Utf8Format(IBufferWriter bufferWriter, string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -1389,162 +920,42 @@ public static void Utf8Format(IBufferWr } // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; + i = indexParse.LastIndex - 1; var writeFormat = StandardFormat.Parse(indexParse.FormatString); switch (indexParse.Index) { case 0: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + continue; case 1: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); + continue; case 2: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); + continue; case 3: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); + continue; case 4: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg5)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); + continue; case 5: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg6)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg6, indexParse.Alignment, writeFormat, nameof(arg6)); + continue; case 6: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg7)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg7, indexParse.Alignment, writeFormat, nameof(arg7)); + continue; case 7: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg8)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg8, indexParse.Alignment, writeFormat, nameof(arg8)); + continue; case 8: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg9)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg9, indexParse.Alignment, writeFormat, nameof(arg9)); + continue; case 9: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg10)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg10, indexParse.Alignment, writeFormat, nameof(arg10)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -1552,9 +963,9 @@ public static void Utf8Format(IBufferWr ExceptionUtil.ThrowFormatException(); } - else if (format[i] == '}') + else if (c == '}') { - if (i != format.Length && format[i + 1] == '}') + if (i + 1 < format.Length && format[i + 1] == '}') { var size = i - copyFrom; var buffer = bufferWriter.GetSpan(UTF8NoBom.GetMaxByteCount(size)); @@ -1562,11 +973,13 @@ public static void Utf8Format(IBufferWr bufferWriter.Advance(written); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ExceptionUtil.ThrowFormatException(); } } - - NEXT_LOOP: - continue; } { @@ -1580,14 +993,19 @@ public static void Utf8Format(IBufferWr } } } - /// Replaces one or more format items in a string with the string representation of some specified values. public static void Utf8Format(IBufferWriter bufferWriter, string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -1614,177 +1032,45 @@ public static void Utf8Format(IBuf } // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; + i = indexParse.LastIndex - 1; var writeFormat = StandardFormat.Parse(indexParse.FormatString); switch (indexParse.Index) { case 0: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + continue; case 1: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); + continue; case 2: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); + continue; case 3: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); + continue; case 4: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg5)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); + continue; case 5: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg6)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg6, indexParse.Alignment, writeFormat, nameof(arg6)); + continue; case 6: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg7)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg7, indexParse.Alignment, writeFormat, nameof(arg7)); + continue; case 7: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg8)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg8, indexParse.Alignment, writeFormat, nameof(arg8)); + continue; case 8: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg9)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg9, indexParse.Alignment, writeFormat, nameof(arg9)); + continue; case 9: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg10)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg10, indexParse.Alignment, writeFormat, nameof(arg10)); + continue; case 10: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg11, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg11, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg11)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg11, indexParse.Alignment, writeFormat, nameof(arg11)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -1792,9 +1078,9 @@ public static void Utf8Format(IBuf ExceptionUtil.ThrowFormatException(); } - else if (format[i] == '}') + else if (c == '}') { - if (i != format.Length && format[i + 1] == '}') + if (i + 1 < format.Length && format[i + 1] == '}') { var size = i - copyFrom; var buffer = bufferWriter.GetSpan(UTF8NoBom.GetMaxByteCount(size)); @@ -1802,11 +1088,13 @@ public static void Utf8Format(IBuf bufferWriter.Advance(written); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ExceptionUtil.ThrowFormatException(); } } - - NEXT_LOOP: - continue; } { @@ -1820,14 +1108,19 @@ public static void Utf8Format(IBuf } } } - /// Replaces one or more format items in a string with the string representation of some specified values. public static void Utf8Format(IBufferWriter bufferWriter, string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -1854,192 +1147,48 @@ public static void Utf8Format } // try to find range - var indexParse = FormatParser.Parse(format.AsSpan(i)); - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; + var indexParse = FormatParser.Parse(format, i); + copyFrom = indexParse.LastIndex; + i = indexParse.LastIndex - 1; var writeFormat = StandardFormat.Parse(indexParse.FormatString); switch (indexParse.Index) { case 0: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + continue; case 1: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); + continue; case 2: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); + continue; case 3: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); + continue; case 4: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg5)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); + continue; case 5: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg6)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg6, indexParse.Alignment, writeFormat, nameof(arg6)); + continue; case 6: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg7)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg7, indexParse.Alignment, writeFormat, nameof(arg7)); + continue; case 7: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg8)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg8, indexParse.Alignment, writeFormat, nameof(arg8)); + continue; case 8: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg9)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg9, indexParse.Alignment, writeFormat, nameof(arg9)); + continue; case 9: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg10)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg10, indexParse.Alignment, writeFormat, nameof(arg10)); + continue; case 10: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg11, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg11, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg11)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg11, indexParse.Alignment, writeFormat, nameof(arg11)); + continue; case 11: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg12, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg12, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg12)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg12, indexParse.Alignment, writeFormat, nameof(arg12)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -2047,9 +1196,9 @@ public static void Utf8Format ExceptionUtil.ThrowFormatException(); } - else if (format[i] == '}') + else if (c == '}') { - if (i != format.Length && format[i + 1] == '}') + if (i + 1 < format.Length && format[i + 1] == '}') { var size = i - copyFrom; var buffer = bufferWriter.GetSpan(UTF8NoBom.GetMaxByteCount(size)); @@ -2057,11 +1206,13 @@ public static void Utf8Format bufferWriter.Advance(written); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ExceptionUtil.ThrowFormatException(); } } - - NEXT_LOOP: - continue; } { @@ -2075,14 +1226,19 @@ public static void Utf8Format } } } - /// Replaces one or more format items in a string with the string representation of some specified values. public static void Utf8Format(IBufferWriter bufferWriter, string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -2109,207 +1265,51 @@ public static void Utf8Format.TryFormatDelegate(arg1, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + continue; case 1: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); + continue; case 2: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); + continue; case 3: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); + continue; case 4: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg5)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); + continue; case 5: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg6)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg6, indexParse.Alignment, writeFormat, nameof(arg6)); + continue; case 6: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg7)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg7, indexParse.Alignment, writeFormat, nameof(arg7)); + continue; case 7: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg8)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg8, indexParse.Alignment, writeFormat, nameof(arg8)); + continue; case 8: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg9)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg9, indexParse.Alignment, writeFormat, nameof(arg9)); + continue; case 9: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg10)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg10, indexParse.Alignment, writeFormat, nameof(arg10)); + continue; case 10: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg11, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg11, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg11)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg11, indexParse.Alignment, writeFormat, nameof(arg11)); + continue; case 11: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg12, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg12, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg12)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg12, indexParse.Alignment, writeFormat, nameof(arg12)); + continue; case 12: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg13, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg13, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg13)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg13, indexParse.Alignment, writeFormat, nameof(arg13)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -2317,9 +1317,9 @@ public static void Utf8FormatReplaces one or more format items in a string with the string representation of some specified values. public static void Utf8Format(IBufferWriter bufferWriter, string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -2379,222 +1386,54 @@ public static void Utf8Format.TryFormatDelegate(arg1, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + continue; case 1: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); + continue; case 2: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); + continue; case 3: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); + continue; case 4: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg5)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); + continue; case 5: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg6)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg6, indexParse.Alignment, writeFormat, nameof(arg6)); + continue; case 6: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg7)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg7, indexParse.Alignment, writeFormat, nameof(arg7)); + continue; case 7: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg8)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg8, indexParse.Alignment, writeFormat, nameof(arg8)); + continue; case 8: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg9)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg9, indexParse.Alignment, writeFormat, nameof(arg9)); + continue; case 9: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg10)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg10, indexParse.Alignment, writeFormat, nameof(arg10)); + continue; case 10: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg11, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg11, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg11)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg11, indexParse.Alignment, writeFormat, nameof(arg11)); + continue; case 11: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg12, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg12, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg12)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg12, indexParse.Alignment, writeFormat, nameof(arg12)); + continue; case 12: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg13, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg13, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg13)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg13, indexParse.Alignment, writeFormat, nameof(arg13)); + continue; case 13: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg14, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg14, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg14)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg14, indexParse.Alignment, writeFormat, nameof(arg14)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -2602,9 +1441,9 @@ public static void Utf8FormatReplaces one or more format items in a string with the string representation of some specified values. public static void Utf8Format(IBufferWriter bufferWriter, string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -2664,237 +1510,57 @@ public static void Utf8Format.TryFormatDelegate(arg1, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + continue; case 1: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); + continue; case 2: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); + continue; case 3: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); + continue; case 4: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg5)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); + continue; case 5: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg6)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg6, indexParse.Alignment, writeFormat, nameof(arg6)); + continue; case 6: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg7)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg7, indexParse.Alignment, writeFormat, nameof(arg7)); + continue; case 7: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg8)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg8, indexParse.Alignment, writeFormat, nameof(arg8)); + continue; case 8: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg9)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg9, indexParse.Alignment, writeFormat, nameof(arg9)); + continue; case 9: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg10)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg10, indexParse.Alignment, writeFormat, nameof(arg10)); + continue; case 10: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg11, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg11, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg11)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg11, indexParse.Alignment, writeFormat, nameof(arg11)); + continue; case 11: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg12, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg12, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg12)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg12, indexParse.Alignment, writeFormat, nameof(arg12)); + continue; case 12: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg13, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg13, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg13)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg13, indexParse.Alignment, writeFormat, nameof(arg13)); + continue; case 13: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg14, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg14, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg14)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg14, indexParse.Alignment, writeFormat, nameof(arg14)); + continue; case 14: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg15, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg15, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg15)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg15, indexParse.Alignment, writeFormat, nameof(arg15)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -2902,9 +1568,9 @@ public static void Utf8FormatReplaces one or more format items in a string with the string representation of some specified values. public static void Utf8Format(IBufferWriter bufferWriter, string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16) { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + var copyFrom = 0; for (int i = 0; i < format.Length; i++) { - if (format[i] == '{') + var c = format[i]; + if (c == '{') { // escape. if (i == format.Length - 1) @@ -2964,252 +1637,60 @@ public static void Utf8Format.TryFormatDelegate(arg1, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg1, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg1)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + continue; case 1: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg2, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg2)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); + continue; case 2: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg3, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg3)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); + continue; case 3: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg4, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg4)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); + continue; case 4: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg5, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg5)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); + continue; case 5: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg6, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg6)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg6, indexParse.Alignment, writeFormat, nameof(arg6)); + continue; case 6: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg7, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg7)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg7, indexParse.Alignment, writeFormat, nameof(arg7)); + continue; case 7: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg8, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg8)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg8, indexParse.Alignment, writeFormat, nameof(arg8)); + continue; case 8: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg9, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg9)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg9, indexParse.Alignment, writeFormat, nameof(arg9)); + continue; case 9: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg10, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg10)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg10, indexParse.Alignment, writeFormat, nameof(arg10)); + continue; case 10: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg11, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg11, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg11)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg11, indexParse.Alignment, writeFormat, nameof(arg11)); + continue; case 11: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg12, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg12, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg12)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg12, indexParse.Alignment, writeFormat, nameof(arg12)); + continue; case 12: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg13, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg13, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg13)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg13, indexParse.Alignment, writeFormat, nameof(arg13)); + continue; case 13: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg14, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg14, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg14)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg14, indexParse.Alignment, writeFormat, nameof(arg14)); + continue; case 14: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg15, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg15, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg15)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg15, indexParse.Alignment, writeFormat, nameof(arg15)); + continue; case 15: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg16, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg16, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg16)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatHelper.FormatTo(ref bufferWriter, arg16, indexParse.Alignment, writeFormat, nameof(arg16)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -3217,9 +1698,9 @@ public static void Utf8Format(ref TBufferWriter sb, ReadOnlySpan chars) + where TBufferWriter : System.Buffers.IBufferWriter + { + var span = sb.GetSpan(UTF8NoBom.GetMaxByteCount(chars.Length)); + sb.Advance(UTF8NoBom.GetBytes(chars, span)); + } + /// Create the Utf16 string StringBuilder. public static Utf16ValueStringBuilder CreateStringBuilder() {