From 85703105778fba9c41db19c03e5627f554888b4f Mon Sep 17 00:00:00 2001 From: udaken Date: Mon, 20 Jul 2020 00:43:23 +0900 Subject: [PATCH 1/6] add BCL-Compatibility tests --- tests/ZString.Tests/CompositeFormatTest.cs | 127 +++++++++++++++++++++ tests/ZString.Tests/FormatTest.cs | 66 ++++++++++- 2 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 tests/ZString.Tests/CompositeFormatTest.cs diff --git a/tests/ZString.Tests/CompositeFormatTest.cs b/tests/ZString.Tests/CompositeFormatTest.cs new file mode 100644 index 0000000..bf0614e --- /dev/null +++ b/tests/ZString.Tests/CompositeFormatTest.cs @@ -0,0 +1,127 @@ +using Cysharp.Text; +using FluentAssertions; +using System; +using System.Buffers; +using System.Text; +using Xunit; +using static FluentAssertions.FluentActions; + +using static ZStringTests.FormatTest; +namespace ZStringTests +{ + public class CompositeFormatTest + { + [Theory] + [InlineData("{1}")] + [InlineData("{-0}")] + [InlineData("{-1}")] + [InlineData("}")] + [InlineData("{")] + [InlineData("{}")] + [InlineData("{A}")] + [InlineData("{1A}")] + [InlineData("{0x0}")] + [InlineData("{\uff11}")] // Full-Width One + [InlineData("{ }")] + [InlineData("{ 1}")] + [InlineData("{0 0}")] + [InlineData("{0+0}")] + [InlineData("{0")] + [InlineData("{foo")] + [InlineData("{{0}")] + [InlineData("{{{0")] + [InlineData("0}")] + [InlineData("bar}")] + [InlineData("{0}}")] + [InlineData("0}}}")] + [InlineData("{:0}")] + [InlineData("{0{}")] + [InlineData("{0{1}}")] + [InlineData("{,0}")] + [InlineData("{ 0,0}")] + [InlineData("{,-0}")] + [InlineData("{0,-}")] + [InlineData("{0,- 0}")] + [InlineData("{0,--0}")] + [InlineData("{:}")] + [InlineData("{,:}")] + [InlineData(" { , : } ")] + [InlineData("{:,}")] + [InlineData("{::}")] + [InlineData("{,,}")] + [InlineData(@"{\0}")] + [InlineData(@"{0\,0}")] + [InlineData(@"{0,0\:}")] + [InlineData(@"{0:\}}")] + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "<•Û—¯’†>")] + public void IncorrectFormat(string format) + { + var value = 9999; + var expected = CatchException(() => string.Format(format, value)); + var actual = CatchException(() => ZString.Format(format, value)); + + expected.Should().NotBeNull(); // test miss! + actual.Should().BeOfType(expected.GetType()); + + Exception CatchException(Func func) + { + try + { + _ = func(); + return null; + } + catch (Exception e) + { + return e; + } + } + } + + [Fact] + public void AlignmentComponentInt() + { + Test("{1,-1}{0,1}", Int64.MinValue, Int64.MaxValue); + Test("{0,1}{1,-1}", 1, 1); + Test("{0,10}{1,-10}", 1, 1); + } + [Fact] + public void AlignmentComponentString() + { + Test("{1,0}{0,0}", "right", "left"); + Test("{0,3}{1,-3}", "Foo", "Foo"); + Test("{0,4}{1,-4}", "Foo", "Foo"); + } + [Fact] + public void AlignmentComponent() + { + Test("{1,-" + 1000 + "}{0," + 1000 + "}", + "", Int64.MaxValue); + var guid = Guid.NewGuid(); + Test(testUtf8: false, "{0,10:X}{{0}}{1,-10:c}", Guid.NewGuid(), DateTime.Now.TimeOfDay.Negate()); + + string[] names = { "Adam", "Bridgette", "Carla", "Daniel", "Ebenezer", "Francine", "George" }; + decimal[] hours = { 40, 6.667m, 40.39m, 82, 40.333m, 80, 16.75m }; + + for (int ctr = 0; ctr < names.Length; ctr++) + Test("{0,-20} {1,5:F}", names[ctr], hours[ctr]); + } + + + [Fact] + public void Spaces() + { + var format = "Prime numbers less than 10: {00 , 01 }, {01 ,02 }, {2 ,3 :D }, {3 ,4: X }"; + var expected = string.Format(format, 2, 3, 5, 7); + var actual = ZString.Format(format, 2, 3, 5, 7); + actual.Should().Be(expected); + + } + + [Fact] + public void CompsiteFormats() + { + Test("{{Name = {0}, {1:f}({1:E})}}", "Fred", 500_0000_0000_0000m); + } + + } +} \ No newline at end of file diff --git a/tests/ZString.Tests/FormatTest.cs b/tests/ZString.Tests/FormatTest.cs index 8223b82..c7fcdb5 100644 --- a/tests/ZString.Tests/FormatTest.cs +++ b/tests/ZString.Tests/FormatTest.cs @@ -10,13 +10,19 @@ namespace ZStringTests { public class FormatTest { - void Test(string format, T0 t0, T1 t1) + internal static void Test(string format, T0 t0, T1 t1) + { + Test(true, format, t0, t1); + } + + internal static void Test(bool testUtf8, string format, T0 t0, T1 t1) { { var actual = ZString.Format(format, t0, t1); var expected = string.Format(format, t0, t1); actual.Should().Be(expected); } + if (testUtf8) { var sb = ZString.CreateUtf8StringBuilder(); sb.AppendFormat(format, t0, t1); @@ -31,6 +37,7 @@ void Test(string format, T0 t0, T1 t1) var expected = string.Format(format, t0, t1); actual.Should().Be(expected); } + if (testUtf8) { var sb = ZString.PrepareUtf8(format); var actual = sb.Format(t0, t1); @@ -39,6 +46,7 @@ void Test(string format, T0 t0, T1 t1) } // Direct + if (testUtf8) { #if NETCOREAPP3_1 var writer = new ArrayBufferWriter(); @@ -50,6 +58,46 @@ void Test(string format, T0 t0, T1 t1) } } + void Test(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) + { + { + var actual = ZString.Format(format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, arg16); + var expected = string.Format(format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, arg16); + actual.Should().Be(expected); + } + { + var sb = ZString.CreateUtf8StringBuilder(); + sb.AppendFormat(format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, arg16); + var actual = sb.ToString(); + var expected = string.Format(format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, arg16); + actual.Should().Be(expected); + } + + // Prepare + { + var actual = ZString.PrepareUtf16(format).Format(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, arg16); + var expected = string.Format(format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, arg16); + actual.Should().Be(expected); + } + { + var sb = ZString.PrepareUtf8(format); + var actual = sb.Format(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, arg16); + var expected = string.Format(format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, arg16); + actual.Should().Be(expected); + } + + // Direct + { +#if NETCOREAPP3_1 + var writer = new ArrayBufferWriter(); + ZString.Utf8Format(writer, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, arg16); + var actual = Encoding.UTF8.GetString(writer.WrittenSpan); + var expected = string.Format(format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, arg16); + actual.Should().Be(expected); +#endif + } + } + [Fact] public void EmptyFormat() { @@ -86,6 +134,21 @@ public void DoubleFormat3() Test("abc{0}def{1}ghi", 100, 200); } + [Fact] + public void EmptyFormatString() + { + // UtfFormatter Deny space only + Test(false, "{0:}{1: }", 100, 200); + } + + [Fact] + public void MaximumFormat() + { + Test("abc{0}de{1}f{2}g{3}h{4}i{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}z", + 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600); + } + + [Fact] public void Nullable() { @@ -99,6 +162,7 @@ public void Nullable() public void Comment() { Test("abc{{0}}def{1}ghi", 100, 200); + Test("}}{{{0}{{}}{1}{{", 123, 456); } [Fact] From 2d0f9cfa4162a9558e4d9e6842be6a4c8c6b7803 Mon Sep 17 00:00:00 2001 From: udaken Date: Thu, 6 Aug 2020 19:47:28 +0900 Subject: [PATCH 2/6] Update Benchmark code --- sandbox/PerfBenchmark/Benchmarks/FormatBenchmark.cs | 10 +++++++++- .../Benchmarks/SixObjectConcatBenchmark.cs | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/sandbox/PerfBenchmark/Benchmarks/FormatBenchmark.cs b/sandbox/PerfBenchmark/Benchmarks/FormatBenchmark.cs index adf2ff4..cab1dad 100644 --- a/sandbox/PerfBenchmark/Benchmarks/FormatBenchmark.cs +++ b/sandbox/PerfBenchmark/Benchmarks/FormatBenchmark.cs @@ -14,6 +14,7 @@ public class FormatBenchmark int y; string format; StringBuilder stringBuilder; + Utf16PreparedFormat preparedFormat; public FormatBenchmark() { @@ -21,9 +22,10 @@ public FormatBenchmark() y = int.Parse("200"); format = "x:{0}, y:{1}"; stringBuilder = new StringBuilder(); + preparedFormat = new Utf16PreparedFormat(format); } - [Benchmark] + [Benchmark(Baseline = true)] public string StringFormat() { return string.Format(format, x, y); @@ -35,6 +37,12 @@ public string ZStringFormat() return ZString.Format(format, x, y); } + [Benchmark] + public string ZStringPreparedFormat() + { + return preparedFormat.Format(x, y); + } + [Benchmark] public string StringFormatterFormat() { diff --git a/sandbox/PerfBenchmark/Benchmarks/SixObjectConcatBenchmark.cs b/sandbox/PerfBenchmark/Benchmarks/SixObjectConcatBenchmark.cs index 267ada6..b397bdd 100644 --- a/sandbox/PerfBenchmark/Benchmarks/SixObjectConcatBenchmark.cs +++ b/sandbox/PerfBenchmark/Benchmarks/SixObjectConcatBenchmark.cs @@ -19,7 +19,7 @@ public SixObjectConcatBenchmark() z = int.Parse("555"); } - [Benchmark] + [Benchmark(Baseline = true)] public string StringPlus() { return "x:" + x + " y:" + y + " z:" + z; From 12f3e69bb85d25345a259a550c0c8d7403509c94 Mon Sep 17 00:00:00 2001 From: udaken Date: Thu, 6 Aug 2020 17:35:27 +0900 Subject: [PATCH 3/6] update BenchmarkVsReleasedVersion --- .../BenchmarkVsReleasedVersion.csproj | 2 +- .../BuiltinTypesBenchmark.cs | 20 +++++++++++++++---- .../FormatBenchmark.cs | 20 +++++++++++++++---- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/sandbox/BenchmarkVsReleasedVersion/BenchmarkVsReleasedVersion.csproj b/sandbox/BenchmarkVsReleasedVersion/BenchmarkVsReleasedVersion.csproj index 7e34c82..4e03240 100644 --- a/sandbox/BenchmarkVsReleasedVersion/BenchmarkVsReleasedVersion.csproj +++ b/sandbox/BenchmarkVsReleasedVersion/BenchmarkVsReleasedVersion.csproj @@ -9,7 +9,7 @@ - + diff --git a/sandbox/BenchmarkVsReleasedVersion/BuiltinTypesBenchmark.cs b/sandbox/BenchmarkVsReleasedVersion/BuiltinTypesBenchmark.cs index 20b8ab4..8c223f9 100644 --- a/sandbox/BenchmarkVsReleasedVersion/BuiltinTypesBenchmark.cs +++ b/sandbox/BenchmarkVsReleasedVersion/BuiltinTypesBenchmark.cs @@ -96,18 +96,30 @@ public string FormatN() _byte, _dt, _dto, _decimal, _double, _guid, _short, _float, _ts, _uint, _ulong, _null, _string, _bool, _enum, _char); } - [BenchmarkCategory("CreatePreparedFormat"), Benchmark(Baseline = true)] - public object CreatePreparedFormat_() + [BenchmarkCategory("CreateUtf16PreparedFormat"), Benchmark(Baseline = true)] + public object CreateUtf16PreparedFormat_() { return new PF16(_format); } - [BenchmarkCategory("CreatePreparedFormat"), Benchmark] - public object CreatePreparedFormatN() + [BenchmarkCategory("CreateUtf16PreparedFormat"), Benchmark] + public object CreateUtf16PreparedFormatN() { return new NPF16(_format); } + [BenchmarkCategory("CreateUtf8PreparedFormat"), Benchmark(Baseline = true)] + public object CreateUtf8PreparedFormat_() + { + return new PF8(_format); + } + + [BenchmarkCategory("CreateUtf8PreparedFormat"), Benchmark] + public object CreateUtf8PreparedFormatN() + { + return new NPF8(_format); + } + [BenchmarkCategory("Utf16PreparedFormat"), Benchmark(Baseline = true)] public string Utf16PreparedFormat_() { diff --git a/sandbox/BenchmarkVsReleasedVersion/FormatBenchmark.cs b/sandbox/BenchmarkVsReleasedVersion/FormatBenchmark.cs index 14a49c2..3f6169f 100644 --- a/sandbox/BenchmarkVsReleasedVersion/FormatBenchmark.cs +++ b/sandbox/BenchmarkVsReleasedVersion/FormatBenchmark.cs @@ -61,18 +61,30 @@ public string FormatN() return NZString.Format(FormatString, x, y); } - [BenchmarkCategory("CreatePreparedFormat"), Benchmark(Baseline = true)] - public object CreatePreparedFormat_() + [BenchmarkCategory("CreateUtf16PreparedFormat"), Benchmark(Baseline = true)] + public object CreateUtf16PreparedFormat_() { return new Utf16PreparedFormat(FormatString); } - [BenchmarkCategory("CreatePreparedFormat"), Benchmark] - public object CreatePreparedFormatN() + [BenchmarkCategory("CreateUtf16PreparedFormat"), Benchmark] + public object CreateUtf16PreparedFormatN() { return new NewZString::Cysharp.Text.Utf16PreparedFormat(FormatString); } + [BenchmarkCategory("CreateUtf8PreparedFormat"), Benchmark(Baseline = true)] + public object CreateUtf8PreparedFormat_() + { + return new Utf8PreparedFormat(FormatString); + } + + [BenchmarkCategory("CreateUtf8PreparedFormat"), Benchmark] + public object CreateUtf8PreparedFormatN() + { + return new NewZString::Cysharp.Text.Utf8PreparedFormat(FormatString); + } + [BenchmarkCategory("Utf16PreparedFormat"), Benchmark(Baseline = true)] public string Utf16PreparedFormat_() { From b2975a8a70c77745edd588763a9292cd994a1dd8 Mon Sep 17 00:00:00 2001 From: udaken Date: Mon, 20 Jul 2020 00:43:44 +0900 Subject: [PATCH 4/6] Improved compatibility of AppendFormat formatting string --- .../Assets/Scripts/ZString/ExceptionUtil.cs | 5 + .../Assets/Scripts/ZString/FormatHelper.cs | 162 + .../Assets/Scripts/ZString/FormatParser.cs | 197 +- .../Assets/Scripts/ZString/PreparedFormat.cs | 6212 +++++------------ .../Scripts/ZString/PreparedFormatHelper.cs | 92 +- .../Utf16ValueStringBuilder.AppendFormat.cs | 640 +- .../ZString/Utf16ValueStringBuilder.cs | 69 +- .../Utf8ValueStringBuilder.AppendFormat.cs | 640 +- .../Scripts/ZString/Utf8ValueStringBuilder.cs | 65 +- .../Scripts/ZString/ZString.Utf8Format.cs | 2672 ++----- .../Assets/Scripts/ZString/ZString.cs | 8 + src/ZString/ExceptionUtil.cs | 5 + src/ZString/FormatHelper.cs | 162 + src/ZString/FormatHelper.tt | 99 + src/ZString/FormatParser.cs | 197 +- src/ZString/PreparedFormat.cs | 6212 +++++------------ src/ZString/PreparedFormat.tt | 53 +- src/ZString/PreparedFormatHelper.cs | 92 +- .../Utf16ValueStringBuilder.AppendFormat.cs | 640 +- .../Utf16ValueStringBuilder.AppendFormat.tt | 25 +- src/ZString/Utf16ValueStringBuilder.cs | 69 +- .../Utf8ValueStringBuilder.AppendFormat.cs | 640 +- .../Utf8ValueStringBuilder.AppendFormat.tt | 25 +- src/ZString/Utf8ValueStringBuilder.cs | 65 +- src/ZString/ZString.Utf8Format.cs | 2672 ++----- src/ZString/ZString.Utf8Format.tt | 122 +- src/ZString/ZString.cs | 8 + src/ZString/ZString.csproj | 9 + 28 files changed, 7474 insertions(+), 14383 deletions(-) create mode 100644 src/ZString.Unity/Assets/Scripts/ZString/FormatHelper.cs create mode 100644 src/ZString/FormatHelper.cs create mode 100644 src/ZString/FormatHelper.tt diff --git a/src/ZString.Unity/Assets/Scripts/ZString/ExceptionUtil.cs b/src/ZString.Unity/Assets/Scripts/ZString/ExceptionUtil.cs index 6441369..401e1c6 100644 --- a/src/ZString.Unity/Assets/Scripts/ZString/ExceptionUtil.cs +++ b/src/ZString.Unity/Assets/Scripts/ZString/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/src/ZString.Unity/Assets/Scripts/ZString/FormatHelper.cs b/src/ZString.Unity/Assets/Scripts/ZString/FormatHelper.cs new file mode 100644 index 0000000..5e23b58 --- /dev/null +++ b/src/ZString.Unity/Assets/Scripts/ZString/FormatHelper.cs @@ -0,0 +1,162 @@ +using System; +using System.Text; +using System.Buffers; +using System.Runtime.CompilerServices; + +namespace Cysharp.Text +{ + internal static partial class Utf16PreparedFormat + { + public static void FormatTo(ref TBufferWriter sb, in FormatSegment item, T arg, string argName) + where TBufferWriter : IBufferWriter + { + const char sp = (char)' '; + var width = item.Alignment; + var format = item.FormatString.AsSpan(item.Offset, item.Count); + + if (width <= 0) // leftJustify + { + width *= -1; + + 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); + + int padding = width - argWritten; + if (width > 0 && padding > 0) + { + var paddingSpan = sb.GetSpan(padding); + paddingSpan.Fill(sp); + sb.Advance(padding); + } + } + else // rightJustify + { + 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 Utf8PreparedFormat + { + public static void FormatTo(ref TBufferWriter sb, in FormatSegment item, T arg, string argName) + where TBufferWriter : IBufferWriter + { + const byte sp = (byte)' '; + var width = item.Alignment; + var format = item.StandardFormat; + + if (width <= 0) // leftJustify + { + width *= -1; + + 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); + + int padding = width - argWritten; + if (width > 0 && padding > 0) + { + var paddingSpan = sb.GetSpan(padding); + paddingSpan.Fill(sp); + sb.Advance(padding); + } + } + else // rightJustify + { + 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); + } + } + } + } +} diff --git a/src/ZString.Unity/Assets/Scripts/ZString/FormatParser.cs b/src/ZString.Unity/Assets/Scripts/ZString/FormatParser.cs index eed6286..a955281 100644 --- a/src/ZString.Unity/Assets/Scripts/ZString/FormatParser.cs +++ b/src/ZString.Unity/Assets/Scripts/ZString/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/src/ZString.Unity/Assets/Scripts/ZString/PreparedFormat.cs b/src/ZString.Unity/Assets/Scripts/ZString/PreparedFormat.cs index c343a57..8d5210f 100644 --- a/src/ZString.Unity/Assets/Scripts/ZString/PreparedFormat.cs +++ b/src/ZString.Unity/Assets/Scripts/ZString/PreparedFormat.cs @@ -46,40 +46,25 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1) { 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) - { - 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; - } + switch (item.FormatIndex) + { + case FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + } + break; + case 0: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } @@ -122,55 +107,30 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2) { 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; - } - default: - break; - } + case FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + } + break; + case 0: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } @@ -213,70 +173,35 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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; - } - default: - break; - } + case FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + } + break; + case 0: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } @@ -319,85 +244,40 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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 FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + } + break; + case 0: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } @@ -440,100 +320,45 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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; - } - default: - break; - } + case FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + } + break; + case 0: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } @@ -576,115 +401,50 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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; - } - default: - break; - } + case FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + } + break; + case 0: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } @@ -727,130 +487,55 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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 FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + } + break; + case 0: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } @@ -893,145 +578,60 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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 FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + } + break; + case 0: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } + case 7: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + break; + } } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } @@ -1074,160 +674,65 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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; - } - default: - break; - } + case FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + } + break; + case 0: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } + case 7: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + break; + } + case 8: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + break; + } } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } @@ -1270,175 +775,70 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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 FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + } + break; + case 0: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } + case 7: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + break; + } + case 8: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + break; + } + case 9: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + break; + } } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } @@ -1481,190 +881,75 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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 FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + } + break; + case 0: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } + case 7: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + break; + } + case 8: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + break; + } + case 9: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + break; + } + case 10: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + break; + } } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } @@ -1707,205 +992,80 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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; - } - default: - break; - } + case FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + } + break; + case 0: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } + case 7: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + break; + } + case 8: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + break; + } + case 9: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + break; + } + case 10: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + break; + } + case 11: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + break; + } } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } @@ -1948,220 +1108,85 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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; - } - default: - break; - } + case FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + } + break; + case 0: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } + case 7: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + break; + } + case 8: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + break; + } + case 9: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + break; + } + case 10: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + break; + } + case 11: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + break; + } + case 12: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg13, nameof(arg13)); + break; + } } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } @@ -2204,235 +1229,90 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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 FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + } + break; + case 0: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } + case 7: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + break; + } + case 8: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + break; + } + case 9: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + break; + } + case 10: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + break; + } + case 11: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + break; + } + case 12: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg13, nameof(arg13)); + break; + } + case 13: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg14, nameof(arg14)); + break; + } } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } @@ -2475,250 +1355,95 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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; - } - 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 FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + } + break; + case 0: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } + case 7: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + break; + } + case 8: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + break; + } + case 9: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + break; + } + case 10: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + break; + } + case 11: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + break; + } + case 12: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg13, nameof(arg13)); + break; + } + case 13: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg14, nameof(arg14)); + break; + } + case 14: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg15, nameof(arg15)); + break; + } } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } @@ -2761,265 +1486,100 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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; - } - 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 FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + } + break; + case 0: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } + case 7: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + break; + } + case 8: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + break; + } + case 9: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + break; + } + case 10: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + break; + } + case 11: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + break; + } + case 12: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg13, nameof(arg13)); + break; + } + case 13: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg14, nameof(arg14)); + break; + } + case 14: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg15, nameof(arg15)); + break; + } + case 15: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg16, nameof(arg16)); + break; + } } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } @@ -3062,41 +1622,26 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1) { 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 FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var span = sb.GetSpan(size); + var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + sb.Advance(count); + } + break; + case 0: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } @@ -3139,56 +1684,31 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2) { 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; - } - default: - break; - } + case FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var span = sb.GetSpan(size); + var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + sb.Advance(count); + } + break; + case 0: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } @@ -3231,71 +1751,36 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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; - } - default: - break; - } + case FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var span = sb.GetSpan(size); + var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + sb.Advance(count); + } + break; + case 0: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } @@ -3338,86 +1823,41 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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 FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var span = sb.GetSpan(size); + var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + sb.Advance(count); + } + break; + case 0: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } @@ -3460,101 +1900,46 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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; - } - default: - break; - } + case FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var span = sb.GetSpan(size); + var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + sb.Advance(count); + } + break; + case 0: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } @@ -3597,116 +1982,51 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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; - } - default: - break; - } + case FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var span = sb.GetSpan(size); + var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + sb.Advance(count); + } + break; + case 0: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } @@ -3749,131 +2069,56 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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 FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var span = sb.GetSpan(size); + var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + sb.Advance(count); + } + break; + case 0: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } @@ -3916,146 +2161,61 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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 FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var span = sb.GetSpan(size); + var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + sb.Advance(count); + } + break; + case 0: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } + case 7: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + break; + } } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } @@ -4098,161 +2258,66 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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; - } - default: - break; - } + case FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var span = sb.GetSpan(size); + var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + sb.Advance(count); + } + break; + case 0: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } + case 7: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + break; + } + case 8: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + break; + } } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } @@ -4295,176 +2360,71 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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 FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var span = sb.GetSpan(size); + var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + sb.Advance(count); + } + break; + case 0: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } + case 7: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + break; + } + case 8: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + break; + } + case 9: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + break; + } } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } @@ -4507,191 +2467,76 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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 FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var span = sb.GetSpan(size); + var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + sb.Advance(count); + } + break; + case 0: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } + case 7: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + break; + } + case 8: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + break; + } + case 9: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + break; + } + case 10: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + break; + } } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } @@ -4734,206 +2579,81 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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; - } - default: - break; - } + case FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var span = sb.GetSpan(size); + var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + sb.Advance(count); + } + break; + case 0: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } + case 7: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + break; + } + case 8: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + break; + } + case 9: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + break; + } + case 10: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + break; + } + case 11: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + break; + } } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } @@ -4976,221 +2696,86 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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; - } - default: - break; - } + case FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var span = sb.GetSpan(size); + var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + sb.Advance(count); + } + break; + case 0: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } + case 7: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + break; + } + case 8: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + break; + } + case 9: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + break; + } + case 10: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + break; + } + case 11: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + break; + } + case 12: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg13, nameof(arg13)); + break; + } } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } @@ -5233,236 +2818,91 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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 FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var span = sb.GetSpan(size); + var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + sb.Advance(count); + } + break; + case 0: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } + case 7: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + break; + } + case 8: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + break; + } + case 9: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + break; + } + case 10: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + break; + } + case 11: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + break; + } + case 12: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg13, nameof(arg13)); + break; + } + case 13: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg14, nameof(arg14)); + break; + } } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } @@ -5505,251 +2945,96 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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; - } - 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 FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var span = sb.GetSpan(size); + var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + sb.Advance(count); + } + break; + case 0: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } + case 7: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + break; + } + case 8: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + break; + } + case 9: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + break; + } + case 10: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + break; + } + case 11: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + break; + } + case 12: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg13, nameof(arg13)); + break; + } + case 13: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg14, nameof(arg14)); + break; + } + case 14: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg15, nameof(arg15)); + break; + } } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } @@ -5792,264 +3077,99 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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; - } - 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 FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var span = sb.GetSpan(size); + var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + sb.Advance(count); + } + break; + case 0: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } + case 7: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + break; + } + case 8: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + break; + } + case 9: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + break; + } + case 10: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + break; + } + case 11: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + break; + } + case 12: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg13, nameof(arg13)); + break; + } + case 13: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg14, nameof(arg14)); + break; + } + case 14: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg15, nameof(arg15)); + break; + } + case 15: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg16, nameof(arg16)); + break; + } } } } } - -} \ No newline at end of file +} diff --git a/src/ZString.Unity/Assets/Scripts/ZString/PreparedFormatHelper.cs b/src/ZString.Unity/Assets/Scripts/ZString/PreparedFormatHelper.cs index 60bdcfd..048c55d 100644 --- a/src/ZString.Unity/Assets/Scripts/ZString/PreparedFormatHelper.cs +++ b/src/ZString.Unity/Assets/Scripts/ZString/PreparedFormatHelper.cs @@ -1,6 +1,7 @@ using System; using System.Buffers; using System.Collections.Generic; +using System.Diagnostics; namespace Cysharp.Text { @@ -8,97 +9,92 @@ internal static class PreparedFormatHelper { internal static FormatSegment[] Parse(string format, bool withStandardFormat) { + 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 FormatSegment(copyFrom, size, FormatSegment.NotFormatIndex, format, default, 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; - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; - } - else if (format[i] == '}') - { - if (i != format.Length && format[i + 1] == '}') + if (ParserScanResult.BraceOpen == 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; + 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 FormatSegment(indexParse.LastIndex - indexParse.FormatString.Length - 1, indexParse.FormatString.Length, indexParse.Index, format, withStandardFormat, indexParse.Alignment)); + } return list.ToArray(); } } - internal readonly struct FormatSegment { + public const int NotFormatIndex = -1; + public readonly int Offset; public readonly int Count; - public readonly bool IsFormatArgument; + public bool IsFormatArgument => FormatIndex != NotFormatIndex; public readonly int FormatIndex; public readonly string FormatString; + public readonly int Alignment; // Utf8 public readonly StandardFormat StandardFormat; - public FormatSegment(int offset, int count, bool isFormatArgument, int formatIndex, string formatString, bool utf8) + public FormatSegment(int offset, int count, int formatIndex, string formatString, bool utf8, int alignment) { Offset = offset; Count = count; - IsFormatArgument = isFormatArgument; FormatIndex = formatIndex; FormatString = formatString; + Alignment = alignment; if (utf8) { - StandardFormat = (formatString != null) ? StandardFormat.Parse(formatString) : default; + StandardFormat = (formatString != null) ? StandardFormat.Parse(formatString.AsSpan(Offset, Count)) : default; } else { diff --git a/src/ZString.Unity/Assets/Scripts/ZString/Utf16/Utf16ValueStringBuilder.AppendFormat.cs b/src/ZString.Unity/Assets/Scripts/ZString/Utf16/Utf16ValueStringBuilder.AppendFormat.cs index 515186d..717464b 100644 --- a/src/ZString.Unity/Assets/Scripts/ZString/Utf16/Utf16ValueStringBuilder.AppendFormat.cs +++ b/src/ZString.Unity/Assets/Scripts/ZString/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/src/ZString.Unity/Assets/Scripts/ZString/Utf8/Utf8ValueStringBuilder.AppendFormat.cs b/src/ZString.Unity/Assets/Scripts/ZString/Utf8/Utf8ValueStringBuilder.AppendFormat.cs index 8fd66bc..4f0b1ad 100644 --- a/src/ZString.Unity/Assets/Scripts/ZString/Utf8/Utf8ValueStringBuilder.AppendFormat.cs +++ b/src/ZString.Unity/Assets/Scripts/ZString/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, const int ThreadStaticBufferSize = 64444; const int DefaultBufferSize = 65536; // use 64K default buffer. - static Encoding UTF8NoBom = new UTF8Encoding(false); + internal static readonly Encoding UTF8NoBom = new UTF8Encoding(false); static byte newLine1; static byte newLine2; @@ -366,19 +366,66 @@ static void ThrowNestedException() throw new NestedStringBuilderCreationException(nameof(Utf16ValueStringBuilder)); } - private void AppendFormatInternal(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/src/ZString.Unity/Assets/Scripts/ZString/ZString.Utf8Format.cs b/src/ZString.Unity/Assets/Scripts/ZString/ZString.Utf8Format.cs index dcf7f39..0756b1f 100644 --- a/src/ZString.Unity/Assets/Scripts/ZString/ZString.Utf8Format.cs +++ b/src/ZString.Unity/Assets/Scripts/ZString/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; - } + Utf8FormatInternal(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; } { @@ -99,10 +97,16 @@ 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 +133,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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -172,9 +152,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 +162,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; } { @@ -204,10 +186,16 @@ 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 +222,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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -292,9 +244,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 +254,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; } { @@ -324,10 +278,16 @@ 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 +314,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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -427,9 +339,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 +349,13 @@ public static void Utf8Format(IBufferWriter bufferWriter, bufferWriter.Advance(written); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ExceptionUtil.ThrowFormatException(); } } - - NEXT_LOOP: - continue; } { @@ -459,10 +373,16 @@ 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 +409,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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -577,9 +437,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 +447,13 @@ public static void Utf8Format(IBufferWriter bufferWrit bufferWriter.Advance(written); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ExceptionUtil.ThrowFormatException(); } } - - NEXT_LOOP: - continue; } { @@ -609,10 +471,16 @@ 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 +507,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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(bufferWriter, arg6, indexParse.Alignment, writeFormat, nameof(arg6)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -742,9 +538,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 +548,13 @@ public static void Utf8Format(IBufferWriter buffer bufferWriter.Advance(written); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ExceptionUtil.ThrowFormatException(); } } - - NEXT_LOOP: - continue; } { @@ -774,10 +572,16 @@ 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 +608,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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(bufferWriter, arg7, indexParse.Alignment, writeFormat, nameof(arg7)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -922,9 +642,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 +652,13 @@ public static void Utf8Format(IBufferWriter bu bufferWriter.Advance(written); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ExceptionUtil.ThrowFormatException(); } } - - NEXT_LOOP: - continue; } { @@ -954,10 +676,16 @@ 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 +712,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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(bufferWriter, arg8, indexParse.Alignment, writeFormat, nameof(arg8)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -1117,9 +749,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 +819,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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(bufferWriter, arg9, indexParse.Alignment, writeFormat, nameof(arg9)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -1327,9 +859,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 +869,13 @@ public static void Utf8Format(IBufferWriter< bufferWriter.Advance(written); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ExceptionUtil.ThrowFormatException(); } } - - NEXT_LOOP: - continue; } { @@ -1359,10 +893,16 @@ 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 +929,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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(bufferWriter, arg10, indexParse.Alignment, writeFormat, nameof(arg10)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -1552,9 +972,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 +982,13 @@ public static void Utf8Format(IBufferWr bufferWriter.Advance(written); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ExceptionUtil.ThrowFormatException(); } } - - NEXT_LOOP: - continue; } { @@ -1584,10 +1006,16 @@ 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 +1042,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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(bufferWriter, arg11, indexParse.Alignment, writeFormat, nameof(arg11)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -1792,9 +1088,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 +1098,13 @@ public static void Utf8Format(IBuf bufferWriter.Advance(written); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ExceptionUtil.ThrowFormatException(); } } - - NEXT_LOOP: - continue; } { @@ -1824,10 +1122,16 @@ 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 +1158,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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(bufferWriter, arg12, indexParse.Alignment, writeFormat, nameof(arg12)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -2047,9 +1207,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 +1217,13 @@ public static void Utf8Format bufferWriter.Advance(written); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ExceptionUtil.ThrowFormatException(); } } - - NEXT_LOOP: - continue; } { @@ -2079,10 +1241,16 @@ 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 +1277,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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(bufferWriter, arg13, indexParse.Alignment, writeFormat, nameof(arg13)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -2317,9 +1329,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 +1399,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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(bufferWriter, arg14, indexParse.Alignment, writeFormat, nameof(arg14)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -2602,9 +1454,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 +1524,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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(bufferWriter, arg15, indexParse.Alignment, writeFormat, nameof(arg15)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -2902,9 +1582,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 +1652,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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(bufferWriter, arg16, indexParse.Alignment, writeFormat, nameof(arg16)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -3217,9 +1713,9 @@ public static void Utf8Format(IBufferWriter bufferWriter, T arg, int width, StandardFormat format, string argName) + { + if (width <= 0) // leftJustify + { + width *= -1; + + var buffer = bufferWriter.GetSpan(); + + if (!FormatterCache.TryFormatDelegate(arg, buffer, out var written, format)) + { + bufferWriter.Advance(0); + buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); + + if (!FormatterCache.TryFormatDelegate(arg, buffer, out written, format)) + { + ExceptionUtil.ThrowArgumentException(argName); + } + } + + bufferWriter.Advance(written); + + int padding = width - written; + if (width > 0 && padding > 0) + { + // TODO Append(' ', padding); + bufferWriter.GetSpan(padding).Fill((byte)' '); // TODO Fill Method is too slow. + bufferWriter.Advance(padding); + } + } + else // rightJustify + { + if (typeof(T) == typeof(string)) + { + var s = Unsafe.As(arg); + int padding = width - s.Length; + if (padding > 0) + { + // TODO Append(' ', padding); + bufferWriter.GetSpan(padding).Fill((byte)' '); // TODO Fill Method is too slow. + bufferWriter.Advance(padding); + } + + ZString.AppendChars(ref bufferWriter, s.AsSpan()); + } + 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)) + { + ExceptionUtil.ThrowArgumentException(argName); + } + } + + int padding = width - charsWritten; + if (padding > 0) + { + // TODO Append(' ', padding); + bufferWriter.GetSpan(padding).Fill((byte)' '); // TODO Fill Method is too slow. + bufferWriter.Advance(padding); + } + + s.CopyTo(bufferWriter.GetSpan(charsWritten)); + bufferWriter.Advance(charsWritten); + } + } + } + } -} \ No newline at end of file +} diff --git a/src/ZString.Unity/Assets/Scripts/ZString/ZString.cs b/src/ZString.Unity/Assets/Scripts/ZString/ZString.cs index 6a48bb2..aa4cb11 100644 --- a/src/ZString.Unity/Assets/Scripts/ZString/ZString.cs +++ b/src/ZString.Unity/Assets/Scripts/ZString/ZString.cs @@ -11,6 +11,14 @@ public static partial class ZString { static Encoding UTF8NoBom = new UTF8Encoding(false); + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static void AppendChars(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() { diff --git a/src/ZString/ExceptionUtil.cs b/src/ZString/ExceptionUtil.cs index 6441369..401e1c6 100644 --- a/src/ZString/ExceptionUtil.cs +++ b/src/ZString/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/src/ZString/FormatHelper.cs b/src/ZString/FormatHelper.cs new file mode 100644 index 0000000..5e23b58 --- /dev/null +++ b/src/ZString/FormatHelper.cs @@ -0,0 +1,162 @@ +using System; +using System.Text; +using System.Buffers; +using System.Runtime.CompilerServices; + +namespace Cysharp.Text +{ + internal static partial class Utf16PreparedFormat + { + public static void FormatTo(ref TBufferWriter sb, in FormatSegment item, T arg, string argName) + where TBufferWriter : IBufferWriter + { + const char sp = (char)' '; + var width = item.Alignment; + var format = item.FormatString.AsSpan(item.Offset, item.Count); + + if (width <= 0) // leftJustify + { + width *= -1; + + 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); + + int padding = width - argWritten; + if (width > 0 && padding > 0) + { + var paddingSpan = sb.GetSpan(padding); + paddingSpan.Fill(sp); + sb.Advance(padding); + } + } + else // rightJustify + { + 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 Utf8PreparedFormat + { + public static void FormatTo(ref TBufferWriter sb, in FormatSegment item, T arg, string argName) + where TBufferWriter : IBufferWriter + { + const byte sp = (byte)' '; + var width = item.Alignment; + var format = item.StandardFormat; + + if (width <= 0) // leftJustify + { + width *= -1; + + 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); + + int padding = width - argWritten; + if (width > 0 && padding > 0) + { + var paddingSpan = sb.GetSpan(padding); + paddingSpan.Fill(sp); + sb.Advance(padding); + } + } + else // rightJustify + { + 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); + } + } + } + } +} diff --git a/src/ZString/FormatHelper.tt b/src/ZString/FormatHelper.tt new file mode 100644 index 0000000..db3f1c8 --- /dev/null +++ b/src/ZString/FormatHelper.tt @@ -0,0 +1,99 @@ +<#@ template debug="false" hostspecific="false" language="C#" #> +<#@ assembly name="System.Core" #> +<#@ import namespace="System.Linq" #> +<#@ import namespace="System.Text" #> +<#@ import namespace="System.Collections.Generic" #> +<#@ output extension=".cs" #> +<#@ include file="T4Common.t4" once="true" #> +using System; +using System.Text; +using System.Buffers; +using System.Runtime.CompilerServices; + +namespace Cysharp.Text +{ +<# foreach(var utf in utfTypes ) { var isUtf16 = (utf == "Utf16"); var elemType = isUtf16 ? "char" : "byte"; #> + internal static partial class <#= utf #>PreparedFormat + { + public static void FormatTo(ref TBufferWriter sb, in FormatSegment item, T arg, string argName) + where TBufferWriter : IBufferWriter<<#= elemType #>> + { + const <#= elemType #> sp = (<#= elemType #>)' '; + var width = item.Alignment; + var format = <#= isUtf16 ? "item.FormatString.AsSpan(item.Offset, item.Count)" : "item.StandardFormat" #>; + + if (width <= 0) // leftJustify + { + width *= -1; + + var span = sb.GetSpan(0); + if (!<#= utf #>ValueStringBuilder.FormatterCache.TryFormatDelegate(arg, span, out var argWritten, format)) + { + sb.Advance(0); + span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); + if (!<#= utf #>ValueStringBuilder.FormatterCache.TryFormatDelegate(arg, span, out argWritten, format)) + { + ExceptionUtil.ThrowArgumentException(argName); + } + } + sb.Advance(argWritten); + + int padding = width - argWritten; + if (width > 0 && padding > 0) + { + var paddingSpan = sb.GetSpan(padding); + paddingSpan.Fill(sp); + sb.Advance(padding); + } + } + else // rightJustify + { + 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); + } + +<# if(isUtf16) { #> + var span = sb.GetSpan(s.Length); + s.AsSpan().CopyTo(span); + sb.Advance(s.Length); +<# }else { #> + ZString.AppendChars(ref sb, s.AsSpan()); +<# } #> + } + else + { + Span<<#= elemType #>> s = stackalloc <#= elemType #>[typeof(T).IsValueType ? Unsafe.SizeOf() * 8 : 1024]; + + if (!<#= utf #>ValueStringBuilder.FormatterCache.TryFormatDelegate(arg, s, out var charsWritten, format)) + { + s = stackalloc <#= elemType #>[s.Length * 2]; + if (!<#= utf #>ValueStringBuilder.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); + } + } + } + } +<# } // foreach(utf) #> +} diff --git a/src/ZString/FormatParser.cs b/src/ZString/FormatParser.cs index eed6286..a955281 100644 --- a/src/ZString/FormatParser.cs +++ b/src/ZString/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/src/ZString/PreparedFormat.cs b/src/ZString/PreparedFormat.cs index c343a57..8d5210f 100644 --- a/src/ZString/PreparedFormat.cs +++ b/src/ZString/PreparedFormat.cs @@ -46,40 +46,25 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1) { 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) - { - 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; - } + switch (item.FormatIndex) + { + case FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + } + break; + case 0: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } @@ -122,55 +107,30 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2) { 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; - } - default: - break; - } + case FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + } + break; + case 0: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } @@ -213,70 +173,35 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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; - } - default: - break; - } + case FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + } + break; + case 0: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } @@ -319,85 +244,40 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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 FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + } + break; + case 0: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } @@ -440,100 +320,45 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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; - } - default: - break; - } + case FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + } + break; + case 0: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } @@ -576,115 +401,50 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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; - } - default: - break; - } + case FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + } + break; + case 0: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } @@ -727,130 +487,55 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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 FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + } + break; + case 0: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } @@ -893,145 +578,60 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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 FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + } + break; + case 0: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } + case 7: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + break; + } } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } @@ -1074,160 +674,65 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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; - } - default: - break; - } + case FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + } + break; + case 0: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } + case 7: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + break; + } + case 8: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + break; + } } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } @@ -1270,175 +775,70 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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 FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + } + break; + case 0: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } + case 7: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + break; + } + case 8: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + break; + } + case 9: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + break; + } } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } @@ -1481,190 +881,75 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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 FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + } + break; + case 0: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } + case 7: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + break; + } + case 8: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + break; + } + case 9: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + break; + } + case 10: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + break; + } } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } @@ -1707,205 +992,80 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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; - } - default: - break; - } + case FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + } + break; + case 0: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } + case 7: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + break; + } + case 8: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + break; + } + case 9: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + break; + } + case 10: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + break; + } + case 11: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + break; + } } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } @@ -1948,220 +1108,85 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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; - } - default: - break; - } + case FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + } + break; + case 0: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } + case 7: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + break; + } + case 8: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + break; + } + case 9: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + break; + } + case 10: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + break; + } + case 11: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + break; + } + case 12: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg13, nameof(arg13)); + break; + } } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } @@ -2204,235 +1229,90 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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 FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + } + break; + case 0: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } + case 7: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + break; + } + case 8: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + break; + } + case 9: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + break; + } + case 10: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + break; + } + case 11: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + break; + } + case 12: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg13, nameof(arg13)); + break; + } + case 13: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg14, nameof(arg14)); + break; + } } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } @@ -2475,250 +1355,95 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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; - } - 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 FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + } + break; + case 0: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } + case 7: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + break; + } + case 8: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + break; + } + case 9: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + break; + } + case 10: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + break; + } + case 11: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + break; + } + case 12: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg13, nameof(arg13)); + break; + } + case 13: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg14, nameof(arg14)); + break; + } + case 14: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg15, nameof(arg15)); + break; + } } } } } - public sealed partial class Utf16PreparedFormat { public string FormatString { get; } @@ -2761,265 +1486,100 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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; - } - 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 FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); + } + break; + case 0: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } + case 7: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + break; + } + case 8: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + break; + } + case 9: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + break; + } + case 10: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + break; + } + case 11: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + break; + } + case 12: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg13, nameof(arg13)); + break; + } + case 13: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg14, nameof(arg14)); + break; + } + case 14: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg15, nameof(arg15)); + break; + } + case 15: + { + Utf16PreparedFormat.FormatTo(ref sb, item, arg16, nameof(arg16)); + break; + } } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } @@ -3062,41 +1622,26 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1) { 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 FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var span = sb.GetSpan(size); + var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + sb.Advance(count); + } + break; + case 0: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } @@ -3139,56 +1684,31 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2) { 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; - } - default: - break; - } + case FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var span = sb.GetSpan(size); + var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + sb.Advance(count); + } + break; + case 0: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } @@ -3231,71 +1751,36 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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; - } - default: - break; - } + case FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var span = sb.GetSpan(size); + var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + sb.Advance(count); + } + break; + case 0: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } @@ -3338,86 +1823,41 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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 FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var span = sb.GetSpan(size); + var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + sb.Advance(count); + } + break; + case 0: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } @@ -3460,101 +1900,46 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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; - } - default: - break; - } + case FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var span = sb.GetSpan(size); + var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + sb.Advance(count); + } + break; + case 0: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } @@ -3597,116 +1982,51 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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; - } - default: - break; - } + case FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var span = sb.GetSpan(size); + var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + sb.Advance(count); + } + break; + case 0: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } @@ -3749,131 +2069,56 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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 FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var span = sb.GetSpan(size); + var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + sb.Advance(count); + } + break; + case 0: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } @@ -3916,146 +2161,61 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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 FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var span = sb.GetSpan(size); + var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + sb.Advance(count); + } + break; + case 0: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } + case 7: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + break; + } } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } @@ -4098,161 +2258,66 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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; - } - default: - break; - } + case FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var span = sb.GetSpan(size); + var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + sb.Advance(count); + } + break; + case 0: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } + case 7: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + break; + } + case 8: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + break; + } } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } @@ -4295,176 +2360,71 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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 FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var span = sb.GetSpan(size); + var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + sb.Advance(count); + } + break; + case 0: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } + case 7: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + break; + } + case 8: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + break; + } + case 9: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + break; + } } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } @@ -4507,191 +2467,76 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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 FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var span = sb.GetSpan(size); + var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + sb.Advance(count); + } + break; + case 0: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } + case 7: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + break; + } + case 8: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + break; + } + case 9: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + break; + } + case 10: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + break; + } } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } @@ -4734,206 +2579,81 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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; - } - default: - break; - } + case FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var span = sb.GetSpan(size); + var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + sb.Advance(count); + } + break; + case 0: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } + case 7: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + break; + } + case 8: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + break; + } + case 9: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + break; + } + case 10: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + break; + } + case 11: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + break; + } } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } @@ -4976,221 +2696,86 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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; - } - default: - break; - } + case FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var span = sb.GetSpan(size); + var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + sb.Advance(count); + } + break; + case 0: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } + case 7: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + break; + } + case 8: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + break; + } + case 9: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + break; + } + case 10: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + break; + } + case 11: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + break; + } + case 12: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg13, nameof(arg13)); + break; + } } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } @@ -5233,236 +2818,91 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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 FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var span = sb.GetSpan(size); + var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + sb.Advance(count); + } + break; + case 0: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } + case 7: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + break; + } + case 8: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + break; + } + case 9: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + break; + } + case 10: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + break; + } + case 11: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + break; + } + case 12: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg13, nameof(arg13)); + break; + } + case 13: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg14, nameof(arg14)); + break; + } } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } @@ -5505,251 +2945,96 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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; - } - 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 FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var span = sb.GetSpan(size); + var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + sb.Advance(count); + } + break; + case 0: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } + case 7: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + break; + } + case 8: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + break; + } + case 9: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + break; + } + case 10: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + break; + } + case 11: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + break; + } + case 12: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg13, nameof(arg13)); + break; + } + case 13: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg14, nameof(arg14)); + break; + } + case 14: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg15, nameof(arg15)); + break; + } } } } } - public sealed partial class Utf8PreparedFormat { public string FormatString { get; } @@ -5792,264 +3077,99 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { 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; - } - 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 FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var span = sb.GetSpan(size); + var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + sb.Advance(count); + } + break; + case 0: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + break; + } + case 1: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + break; + } + case 2: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + break; + } + case 3: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + break; + } + case 4: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + break; + } + case 5: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + break; + } + case 6: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + break; + } + case 7: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + break; + } + case 8: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + break; + } + case 9: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + break; + } + case 10: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + break; + } + case 11: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + break; + } + case 12: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg13, nameof(arg13)); + break; + } + case 13: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg14, nameof(arg14)); + break; + } + case 14: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg15, nameof(arg15)); + break; + } + case 15: + { + Utf8PreparedFormat.FormatTo(ref sb, item, arg16, nameof(arg16)); + break; + } } } } } - -} \ No newline at end of file +} diff --git a/src/ZString/PreparedFormat.tt b/src/ZString/PreparedFormat.tt index 4087bea..b34f0cc 100644 --- a/src/ZString/PreparedFormat.tt +++ b/src/ZString/PreparedFormat.tt @@ -55,49 +55,34 @@ namespace Cysharp.Text { foreach (var item in segments) { - if (!item.IsFormatArgument) + switch (item.FormatIndex) { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); + case FormatSegment.NotFormatIndex: + { + var strSpan = FormatString.AsSpan(item.Offset, item.Count); <# if(isUtf16) { #> - var span = sb.GetSpan(item.Count); - strSpan.TryCopyTo(span); - sb.Advance(item.Count); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); <# } else { #> - var size = Encoding.UTF8.GetMaxByteCount(item.Count); - var span = sb.GetSpan(size); - var count = Encoding.UTF8.GetBytes(strSpan, span); - sb.Advance(count); + var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var span = sb.GetSpan(size); + var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + sb.Advance(count); <# } #> - } - else - { - switch (item.FormatIndex) - { + } + break; <# for(var j = 0; j < i; j++) { #> - case <#= j #>: - { - var span = sb.GetSpan(0); - if (!<#= utf #>ValueStringBuilder.FormatterCache>.TryFormatDelegate(arg<#= j + 1 #>, span, out var argWritten, <#= isUtf16 ? "item.FormatString.AsSpan()" : "item.StandardFormat" #>)) - { - sb.Advance(0); - span = sb.GetSpan(Math.Max(span.Length + 1, argWritten)); - if (!<#= utf #>ValueStringBuilder.FormatterCache>.TryFormatDelegate(arg<#= j + 1 #>, span, out argWritten, <#= isUtf16 ? "item.FormatString.AsSpan()" : "item.StandardFormat" #>)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg<#= j + 1 #>)); - } - } - sb.Advance(argWritten); - break; - } -<# } // for(j) #> - default: + case <#= j #>: + { + <#= utf #>PreparedFormat.FormatTo(ref sb, item, arg<#= j + 1 #>, nameof(arg<#= j + 1 #>)); break; - } + } +<# } // for(j) #> } } } } - <# } // for(i) #> <# } // foreach(utf) #> -} \ No newline at end of file +} diff --git a/src/ZString/PreparedFormatHelper.cs b/src/ZString/PreparedFormatHelper.cs index 60bdcfd..048c55d 100644 --- a/src/ZString/PreparedFormatHelper.cs +++ b/src/ZString/PreparedFormatHelper.cs @@ -1,6 +1,7 @@ using System; using System.Buffers; using System.Collections.Generic; +using System.Diagnostics; namespace Cysharp.Text { @@ -8,97 +9,92 @@ internal static class PreparedFormatHelper { internal static FormatSegment[] Parse(string format, bool withStandardFormat) { + 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 FormatSegment(copyFrom, size, FormatSegment.NotFormatIndex, format, default, 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; - copyFrom = i + indexParse.LastIndex + 1; - i = i + indexParse.LastIndex; - } - else if (format[i] == '}') - { - if (i != format.Length && format[i + 1] == '}') + if (ParserScanResult.BraceOpen == 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; + 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 FormatSegment(indexParse.LastIndex - indexParse.FormatString.Length - 1, indexParse.FormatString.Length, indexParse.Index, format, withStandardFormat, indexParse.Alignment)); + } return list.ToArray(); } } - internal readonly struct FormatSegment { + public const int NotFormatIndex = -1; + public readonly int Offset; public readonly int Count; - public readonly bool IsFormatArgument; + public bool IsFormatArgument => FormatIndex != NotFormatIndex; public readonly int FormatIndex; public readonly string FormatString; + public readonly int Alignment; // Utf8 public readonly StandardFormat StandardFormat; - public FormatSegment(int offset, int count, bool isFormatArgument, int formatIndex, string formatString, bool utf8) + public FormatSegment(int offset, int count, int formatIndex, string formatString, bool utf8, int alignment) { Offset = offset; Count = count; - IsFormatArgument = isFormatArgument; FormatIndex = formatIndex; FormatString = formatString; + Alignment = alignment; if (utf8) { - StandardFormat = (formatString != null) ? StandardFormat.Parse(formatString) : default; + StandardFormat = (formatString != null) ? StandardFormat.Parse(formatString.AsSpan(Offset, Count)) : default; } else { diff --git a/src/ZString/Utf16/Utf16ValueStringBuilder.AppendFormat.cs b/src/ZString/Utf16/Utf16ValueStringBuilder.AppendFormat.cs index 515186d..717464b 100644 --- a/src/ZString/Utf16/Utf16ValueStringBuilder.AppendFormat.cs +++ b/src/ZString/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 AppendFormatAppends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. public void AppendFormat<<#= CreateTypeArgument(i) #>>(string format, <#= CreateParameters(i) #>) { + 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) @@ -41,14 +47,14 @@ namespace Cysharp.Text } // 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) { <# for(var j = 0; j < i; j++) { #> case <#= j #>: - AppendFormatInternal(arg<#= j + 1 #>, indexParse.FormatString, nameof(arg<#= j + 1 #>)); + AppendFormatInternal(arg<#= j + 1 #>, indexParse.Alignment, indexParse.FormatString, nameof(arg<#= j + 1 #>)); continue; <# } #> default: @@ -56,9 +62,9 @@ namespace Cysharp.Text 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)); @@ -66,6 +72,10 @@ namespace Cysharp.Text copyFrom = i; continue; } + else + { + ThrowFormatException(); + } } } @@ -78,7 +88,6 @@ namespace Cysharp.Text } } } - <# } #> } } diff --git a/src/ZString/Utf16ValueStringBuilder.cs b/src/ZString/Utf16ValueStringBuilder.cs index 7a4368a..ed401d7 100644 --- a/src/ZString/Utf16ValueStringBuilder.cs +++ b/src/ZString/Utf16ValueStringBuilder.cs @@ -554,11 +554,74 @@ void ThrowArgumentException(string paramName) { throw new ArgumentException("Can't format argument.", paramName); } - - void ThrowFormatException() + 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."); } + private static void FormatError() + { + throw new FormatException("Input string was not in a correct format."); + } + + void AppendFormatInternal(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/src/ZString/Utf8/Utf8ValueStringBuilder.AppendFormat.cs b/src/ZString/Utf8/Utf8ValueStringBuilder.AppendFormat.cs index 8fd66bc..4f0b1ad 100644 --- a/src/ZString/Utf8/Utf8ValueStringBuilder.AppendFormat.cs +++ b/src/ZString/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 AppendFormatAppends the string returned by processing a composite format string, each format item is replaced by the string representation of arguments. public void AppendFormat<<#= CreateTypeArgument(i) #>>(string format, <#= CreateParameters(i) #>) { + 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) @@ -42,15 +48,15 @@ namespace Cysharp.Text } // 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) { <# for(var j = 0; j < i; j++) { #> case <#= j #>: - AppendFormatInternal(arg<#= j + 1 #>, writeFormat, nameof(arg<#= j + 1 #>)); + AppendFormatInternal(arg<#= j + 1 #>, indexParse.Alignment, writeFormat, nameof(arg<#= j + 1 #>)); continue; <# } #> default: @@ -58,14 +64,19 @@ namespace Cysharp.Text 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(); } } diff --git a/src/ZString/Utf8ValueStringBuilder.cs b/src/ZString/Utf8ValueStringBuilder.cs index 5acbf83..2c4c822 100644 --- a/src/ZString/Utf8ValueStringBuilder.cs +++ b/src/ZString/Utf8ValueStringBuilder.cs @@ -13,7 +13,7 @@ public partial struct Utf8ValueStringBuilder : IDisposable, IBufferWriter, const int ThreadStaticBufferSize = 64444; const int DefaultBufferSize = 65536; // use 64K default buffer. - static Encoding UTF8NoBom = new UTF8Encoding(false); + internal static readonly Encoding UTF8NoBom = new UTF8Encoding(false); static byte newLine1; static byte newLine2; @@ -366,19 +366,66 @@ static void ThrowNestedException() throw new NestedStringBuilderCreationException(nameof(Utf16ValueStringBuilder)); } - private void AppendFormatInternal(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/src/ZString/ZString.Utf8Format.cs b/src/ZString/ZString.Utf8Format.cs index dcf7f39..0756b1f 100644 --- a/src/ZString/ZString.Utf8Format.cs +++ b/src/ZString/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; - } + Utf8FormatInternal(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; } { @@ -99,10 +97,16 @@ 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 +133,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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -172,9 +152,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 +162,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; } { @@ -204,10 +186,16 @@ 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 +222,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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -292,9 +244,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 +254,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; } { @@ -324,10 +278,16 @@ 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 +314,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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -427,9 +339,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 +349,13 @@ public static void Utf8Format(IBufferWriter bufferWriter, bufferWriter.Advance(written); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ExceptionUtil.ThrowFormatException(); } } - - NEXT_LOOP: - continue; } { @@ -459,10 +373,16 @@ 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 +409,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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -577,9 +437,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 +447,13 @@ public static void Utf8Format(IBufferWriter bufferWrit bufferWriter.Advance(written); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ExceptionUtil.ThrowFormatException(); } } - - NEXT_LOOP: - continue; } { @@ -609,10 +471,16 @@ 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 +507,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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(bufferWriter, arg6, indexParse.Alignment, writeFormat, nameof(arg6)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -742,9 +538,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 +548,13 @@ public static void Utf8Format(IBufferWriter buffer bufferWriter.Advance(written); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ExceptionUtil.ThrowFormatException(); } } - - NEXT_LOOP: - continue; } { @@ -774,10 +572,16 @@ 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 +608,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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(bufferWriter, arg7, indexParse.Alignment, writeFormat, nameof(arg7)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -922,9 +642,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 +652,13 @@ public static void Utf8Format(IBufferWriter bu bufferWriter.Advance(written); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ExceptionUtil.ThrowFormatException(); } } - - NEXT_LOOP: - continue; } { @@ -954,10 +676,16 @@ 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 +712,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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(bufferWriter, arg8, indexParse.Alignment, writeFormat, nameof(arg8)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -1117,9 +749,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 +819,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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(bufferWriter, arg9, indexParse.Alignment, writeFormat, nameof(arg9)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -1327,9 +859,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 +869,13 @@ public static void Utf8Format(IBufferWriter< bufferWriter.Advance(written); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ExceptionUtil.ThrowFormatException(); } } - - NEXT_LOOP: - continue; } { @@ -1359,10 +893,16 @@ 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 +929,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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(bufferWriter, arg10, indexParse.Alignment, writeFormat, nameof(arg10)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -1552,9 +972,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 +982,13 @@ public static void Utf8Format(IBufferWr bufferWriter.Advance(written); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ExceptionUtil.ThrowFormatException(); } } - - NEXT_LOOP: - continue; } { @@ -1584,10 +1006,16 @@ 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 +1042,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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(bufferWriter, arg11, indexParse.Alignment, writeFormat, nameof(arg11)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -1792,9 +1088,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 +1098,13 @@ public static void Utf8Format(IBuf bufferWriter.Advance(written); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ExceptionUtil.ThrowFormatException(); } } - - NEXT_LOOP: - continue; } { @@ -1824,10 +1122,16 @@ 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 +1158,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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(bufferWriter, arg12, indexParse.Alignment, writeFormat, nameof(arg12)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -2047,9 +1207,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 +1217,13 @@ public static void Utf8Format bufferWriter.Advance(written); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ExceptionUtil.ThrowFormatException(); } } - - NEXT_LOOP: - continue; } { @@ -2079,10 +1241,16 @@ 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 +1277,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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(bufferWriter, arg13, indexParse.Alignment, writeFormat, nameof(arg13)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -2317,9 +1329,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 +1399,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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(bufferWriter, arg14, indexParse.Alignment, writeFormat, nameof(arg14)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -2602,9 +1454,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 +1524,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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(bufferWriter, arg15, indexParse.Alignment, writeFormat, nameof(arg15)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -2902,9 +1582,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 +1652,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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(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; - } + Utf8FormatInternal(bufferWriter, arg16, indexParse.Alignment, writeFormat, nameof(arg16)); + continue; default: ExceptionUtil.ThrowFormatException(); break; @@ -3217,9 +1713,9 @@ public static void Utf8Format(IBufferWriter bufferWriter, T arg, int width, StandardFormat format, string argName) + { + if (width <= 0) // leftJustify + { + width *= -1; + + var buffer = bufferWriter.GetSpan(); + + if (!FormatterCache.TryFormatDelegate(arg, buffer, out var written, format)) + { + bufferWriter.Advance(0); + buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); + + if (!FormatterCache.TryFormatDelegate(arg, buffer, out written, format)) + { + ExceptionUtil.ThrowArgumentException(argName); + } + } + + bufferWriter.Advance(written); + + int padding = width - written; + if (width > 0 && padding > 0) + { + // TODO Append(' ', padding); + bufferWriter.GetSpan(padding).Fill((byte)' '); // TODO Fill Method is too slow. + bufferWriter.Advance(padding); + } + } + else // rightJustify + { + if (typeof(T) == typeof(string)) + { + var s = Unsafe.As(arg); + int padding = width - s.Length; + if (padding > 0) + { + // TODO Append(' ', padding); + bufferWriter.GetSpan(padding).Fill((byte)' '); // TODO Fill Method is too slow. + bufferWriter.Advance(padding); + } + + ZString.AppendChars(ref bufferWriter, s.AsSpan()); + } + 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)) + { + ExceptionUtil.ThrowArgumentException(argName); + } + } + + int padding = width - charsWritten; + if (padding > 0) + { + // TODO Append(' ', padding); + bufferWriter.GetSpan(padding).Fill((byte)' '); // TODO Fill Method is too slow. + bufferWriter.Advance(padding); + } + + s.CopyTo(bufferWriter.GetSpan(charsWritten)); + bufferWriter.Advance(charsWritten); + } + } + } + } -} \ No newline at end of file +} diff --git a/src/ZString/ZString.Utf8Format.tt b/src/ZString/ZString.Utf8Format.tt index 5ca32f7..28c4129 100644 --- a/src/ZString/ZString.Utf8Format.tt +++ b/src/ZString/ZString.Utf8Format.tt @@ -5,9 +5,11 @@ <#@ import namespace="System.Collections.Generic" #> <#@ output extension=".cs" #> <#@ include file="T4Common.t4" once="true" #> -using System.Runtime.CompilerServices; -using System.Buffers; using System; +using System.Buffers; +using System.Runtime.CompilerServices; + +using static Cysharp.Text.Utf8ValueStringBuilder; namespace Cysharp.Text { @@ -17,10 +19,16 @@ namespace Cysharp.Text /// Replaces one or more format items in a string with the string representation of some specified values. public static void Utf8Format<<#= CreateTypeArgument(i) #>>(IBufferWriter bufferWriter, string format, <#= CreateParameters(i) #>) { + 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) @@ -47,28 +55,16 @@ namespace Cysharp.Text } // 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) { <# for(var j = 0; j < i; j++) { #> case <#= j #>: - { - var buffer = bufferWriter.GetSpan(); - if (!Utf8ValueStringBuilder.FormatterCache>.TryFormatDelegate(arg<#= j +1 #>, buffer, out var written, writeFormat)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - if (!Utf8ValueStringBuilder.FormatterCache>.TryFormatDelegate(arg<#= j + 1 #>, buffer, out written, writeFormat)) - { - ExceptionUtil.ThrowArgumentException(nameof(arg<#= j + 1 #>)); - } - } - bufferWriter.Advance(written); - goto NEXT_LOOP; - } + Utf8FormatInternal(bufferWriter, arg<#= j + 1 #>, indexParse.Alignment, writeFormat, nameof(arg<#= j + 1 #>)); + continue; <# } #> default: ExceptionUtil.ThrowFormatException(); @@ -77,9 +73,9 @@ namespace Cysharp.Text 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)); @@ -87,11 +83,13 @@ namespace Cysharp.Text bufferWriter.Advance(written); i = i + 1; // skip escaped '}' copyFrom = i; + continue; + } + else + { + ExceptionUtil.ThrowFormatException(); } } - - NEXT_LOOP: - continue; } { @@ -107,5 +105,77 @@ namespace Cysharp.Text } <# } #> + + static void Utf8FormatInternal(IBufferWriter bufferWriter, T arg, int width, StandardFormat format, string argName) + { + if (width <= 0) // leftJustify + { + width *= -1; + + var buffer = bufferWriter.GetSpan(); + + if (!FormatterCache.TryFormatDelegate(arg, buffer, out var written, format)) + { + bufferWriter.Advance(0); + buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); + + if (!FormatterCache.TryFormatDelegate(arg, buffer, out written, format)) + { + ExceptionUtil.ThrowArgumentException(argName); + } + } + + bufferWriter.Advance(written); + + int padding = width - written; + if (width > 0 && padding > 0) + { + // TODO Append(' ', padding); + bufferWriter.GetSpan(padding).Fill((byte)' '); // TODO Fill Method is too slow. + bufferWriter.Advance(padding); + } + } + else // rightJustify + { + if (typeof(T) == typeof(string)) + { + var s = Unsafe.As(arg); + int padding = width - s.Length; + if (padding > 0) + { + // TODO Append(' ', padding); + bufferWriter.GetSpan(padding).Fill((byte)' '); // TODO Fill Method is too slow. + bufferWriter.Advance(padding); + } + + ZString.AppendChars(ref bufferWriter, s.AsSpan()); + } + 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)) + { + ExceptionUtil.ThrowArgumentException(argName); + } + } + + int padding = width - charsWritten; + if (padding > 0) + { + // TODO Append(' ', padding); + bufferWriter.GetSpan(padding).Fill((byte)' '); // TODO Fill Method is too slow. + bufferWriter.Advance(padding); + } + + s.CopyTo(bufferWriter.GetSpan(charsWritten)); + bufferWriter.Advance(charsWritten); + } + } + } + } -} \ No newline at end of file +} diff --git a/src/ZString/ZString.cs b/src/ZString/ZString.cs index 6a48bb2..aa4cb11 100644 --- a/src/ZString/ZString.cs +++ b/src/ZString/ZString.cs @@ -11,6 +11,14 @@ public static partial class ZString { static Encoding UTF8NoBom = new UTF8Encoding(false); + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static void AppendChars(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() { diff --git a/src/ZString/ZString.csproj b/src/ZString/ZString.csproj index eab6ce5..e2bfb15 100644 --- a/src/ZString/ZString.csproj +++ b/src/ZString/ZString.csproj @@ -31,6 +31,10 @@ + + TextTemplatingFileGenerator + FormatHelper.cs + TextTemplatingFileGenerator PreparedFormat.cs @@ -94,6 +98,11 @@ + + True + True + FormatHelper.tt + True True From 353fd617681885abd8a101ad6263d311746a825a Mon Sep 17 00:00:00 2001 From: udaken Date: Thu, 30 Jul 2020 01:29:00 +0900 Subject: [PATCH 5/6] Improved compatibility of PreparedFormat formatting string --- .../Assets/Scripts/ZString/FormatHelper.cs | 172 ++-- .../Assets/Scripts/ZString/PreparedFormat.cs | 864 ++++++++++-------- .../Scripts/ZString/PreparedFormatHelper.cs | 7 +- .../Scripts/ZString/Utf8ValueStringBuilder.cs | 2 +- .../Scripts/ZString/ZString.Utf8Format.cs | 360 +++----- src/ZString/FormatHelper.cs | 172 ++-- src/ZString/FormatHelper.tt | 93 +- src/ZString/PreparedFormat.cs | 864 ++++++++++-------- src/ZString/PreparedFormat.tt | 14 +- src/ZString/PreparedFormatHelper.cs | 7 +- src/ZString/Utf8ValueStringBuilder.cs | 2 +- src/ZString/ZString.Utf8Format.cs | 360 +++----- src/ZString/ZString.Utf8Format.tt | 75 +- 13 files changed, 1517 insertions(+), 1475 deletions(-) diff --git a/src/ZString.Unity/Assets/Scripts/ZString/FormatHelper.cs b/src/ZString.Unity/Assets/Scripts/ZString/FormatHelper.cs index 5e23b58..de9846c 100644 --- a/src/ZString.Unity/Assets/Scripts/ZString/FormatHelper.cs +++ b/src/ZString.Unity/Assets/Scripts/ZString/FormatHelper.cs @@ -5,19 +5,16 @@ namespace Cysharp.Text { - internal static partial class Utf16PreparedFormat + internal static partial class Utf16FormatHelper { - public static void FormatTo(ref TBufferWriter sb, in FormatSegment item, T arg, string argName) + 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 { - const char sp = (char)' '; - var width = item.Alignment; - var format = item.FormatString.AsSpan(item.Offset, item.Count); - if (width <= 0) // leftJustify { - width *= -1; - var span = sb.GetSpan(0); if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg, span, out var argWritten, format)) { @@ -30,6 +27,7 @@ public static void FormatTo(ref TBufferWriter sb, in FormatSeg } sb.Advance(argWritten); + width *= -1; int padding = width - argWritten; if (width > 0 && padding > 0) { @@ -38,64 +36,68 @@ public static void FormatTo(ref TBufferWriter sb, in FormatSeg sb.Advance(padding); } } - else // rightJustify + else { - 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); - } + FormatToRightJustify(ref sb, arg, width, format, argName); + } + } - var span = sb.GetSpan(s.Length); - s.AsSpan().CopyTo(span); - sb.Advance(s.Length); - } - else + 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) { - Span s = stackalloc char[typeof(T).IsValueType ? Unsafe.SizeOf() * 8 : 1024]; + var paddingSpan = sb.GetSpan(padding); + paddingSpan.Fill(sp); + sb.Advance(padding); + } - 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); - } - } + 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]; - int padding = width - charsWritten; - if (padding > 0) + 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)) { - var paddingSpan = sb.GetSpan(padding); - paddingSpan.Fill(sp); - sb.Advance(padding); + ExceptionUtil.ThrowArgumentException(argName); } + } - var span = sb.GetSpan(charsWritten); - s.CopyTo(span); - sb.Advance(charsWritten); + 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 Utf8PreparedFormat + + internal static partial class Utf8FormatHelper { - public static void FormatTo(ref TBufferWriter sb, in FormatSegment item, T arg, string argName) + 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 { - const byte sp = (byte)' '; - var width = item.Alignment; - var format = item.StandardFormat; - if (width <= 0) // leftJustify { - width *= -1; - var span = sb.GetSpan(0); if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg, span, out var argWritten, format)) { @@ -108,6 +110,7 @@ public static void FormatTo(ref TBufferWriter sb, in FormatSeg } sb.Advance(argWritten); + width *= -1; int padding = width - argWritten; if (width > 0 && padding > 0) { @@ -116,47 +119,54 @@ public static void FormatTo(ref TBufferWriter sb, in FormatSeg sb.Advance(padding); } } - else // rightJustify + else { - 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); - } + FormatToRightJustify(ref sb, arg, width, format, argName); + } + } - ZString.AppendChars(ref sb, s.AsSpan()); - } - else + 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) { - Span s = stackalloc byte[typeof(T).IsValueType ? Unsafe.SizeOf() * 8 : 1024]; + var paddingSpan = sb.GetSpan(padding); + paddingSpan.Fill(sp); + sb.Advance(padding); + } - 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); - } - } + ZString.AppendChars(ref sb, s.AsSpan()); + } + else + { + Span s = stackalloc byte[typeof(T).IsValueType ? Unsafe.SizeOf() * 8 : 1024]; - int padding = width - charsWritten; - if (padding > 0) + 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)) { - var paddingSpan = sb.GetSpan(padding); - paddingSpan.Fill(sp); - sb.Advance(padding); + ExceptionUtil.ThrowArgumentException(argName); } + } - var span = sb.GetSpan(charsWritten); - s.CopyTo(span); - sb.Advance(charsWritten); + 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/src/ZString.Unity/Assets/Scripts/ZString/PreparedFormat.cs b/src/ZString.Unity/Assets/Scripts/ZString/PreparedFormat.cs index 8d5210f..ae9a297 100644 --- a/src/ZString.Unity/Assets/Scripts/ZString/PreparedFormat.cs +++ b/src/ZString.Unity/Assets/Scripts/ZString/PreparedFormat.cs @@ -44,23 +44,27 @@ 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) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); strSpan.TryCopyTo(span); sb.Advance(item.Count); + break; } - break; case 0: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); break; } + default: + break; } } } @@ -105,28 +109,32 @@ 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) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); strSpan.TryCopyTo(span); sb.Advance(item.Count); + break; } - break; case 0: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); break; } case 1: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); break; } + default: + break; } } } @@ -171,33 +179,37 @@ 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) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); strSpan.TryCopyTo(span); sb.Advance(item.Count); + break; } - break; case 0: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); break; } case 1: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); break; } case 2: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); break; } + default: + break; } } } @@ -242,38 +254,42 @@ 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) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); strSpan.TryCopyTo(span); sb.Advance(item.Count); + break; } - break; case 0: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); break; } case 1: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); break; } case 2: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); break; } case 3: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); break; } + default: + break; } } } @@ -318,43 +334,47 @@ 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) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); strSpan.TryCopyTo(span); sb.Advance(item.Count); + break; } - break; case 0: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); break; } case 1: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); break; } case 2: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); break; } case 3: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); break; } case 4: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf16FormatHelper.FormatTo(ref sb, arg5, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg5)); break; } + default: + break; } } } @@ -399,48 +419,52 @@ 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) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); strSpan.TryCopyTo(span); sb.Advance(item.Count); + break; } - break; case 0: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); break; } case 1: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); break; } case 2: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); break; } case 3: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); break; } case 4: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf16FormatHelper.FormatTo(ref sb, arg5, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg5)); break; } case 5: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf16FormatHelper.FormatTo(ref sb, arg6, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg6)); break; } + default: + break; } } } @@ -485,53 +509,57 @@ 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) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); strSpan.TryCopyTo(span); sb.Advance(item.Count); + break; } - break; case 0: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); break; } case 1: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); break; } case 2: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); break; } case 3: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); break; } case 4: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf16FormatHelper.FormatTo(ref sb, arg5, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg5)); break; } case 5: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf16FormatHelper.FormatTo(ref sb, arg6, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg6)); break; } case 6: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf16FormatHelper.FormatTo(ref sb, arg7, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg7)); break; } + default: + break; } } } @@ -576,58 +604,62 @@ 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) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); strSpan.TryCopyTo(span); sb.Advance(item.Count); + break; } - break; case 0: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); break; } case 1: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); break; } case 2: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); break; } case 3: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); break; } case 4: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf16FormatHelper.FormatTo(ref sb, arg5, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg5)); break; } case 5: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf16FormatHelper.FormatTo(ref sb, arg6, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg6)); break; } case 6: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf16FormatHelper.FormatTo(ref sb, arg7, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg7)); break; } case 7: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + Utf16FormatHelper.FormatTo(ref sb, arg8, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg8)); break; } + default: + break; } } } @@ -672,63 +704,67 @@ 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) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); strSpan.TryCopyTo(span); sb.Advance(item.Count); + break; } - break; case 0: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); break; } case 1: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); break; } case 2: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); break; } case 3: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); break; } case 4: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf16FormatHelper.FormatTo(ref sb, arg5, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg5)); break; } case 5: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf16FormatHelper.FormatTo(ref sb, arg6, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg6)); break; } case 6: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf16FormatHelper.FormatTo(ref sb, arg7, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg7)); break; } case 7: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + Utf16FormatHelper.FormatTo(ref sb, arg8, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg8)); break; } case 8: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + Utf16FormatHelper.FormatTo(ref sb, arg9, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg9)); break; } + default: + break; } } } @@ -773,68 +809,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, T8 arg8, T9 arg9, T10 arg10) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); strSpan.TryCopyTo(span); sb.Advance(item.Count); + break; } - break; case 0: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); break; } case 1: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); break; } case 2: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); break; } case 3: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); break; } case 4: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf16FormatHelper.FormatTo(ref sb, arg5, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg5)); break; } case 5: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf16FormatHelper.FormatTo(ref sb, arg6, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg6)); break; } case 6: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf16FormatHelper.FormatTo(ref sb, arg7, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg7)); break; } case 7: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + Utf16FormatHelper.FormatTo(ref sb, arg8, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg8)); break; } case 8: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + Utf16FormatHelper.FormatTo(ref sb, arg9, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg9)); break; } case 9: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + Utf16FormatHelper.FormatTo(ref sb, arg10, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg10)); break; } + default: + break; } } } @@ -879,73 +919,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, T9 arg9, T10 arg10, T11 arg11) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); strSpan.TryCopyTo(span); sb.Advance(item.Count); + break; } - break; case 0: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); break; } case 1: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); break; } case 2: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); break; } case 3: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); break; } case 4: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf16FormatHelper.FormatTo(ref sb, arg5, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg5)); break; } case 5: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf16FormatHelper.FormatTo(ref sb, arg6, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg6)); break; } case 6: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf16FormatHelper.FormatTo(ref sb, arg7, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg7)); break; } case 7: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + Utf16FormatHelper.FormatTo(ref sb, arg8, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg8)); break; } case 8: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + Utf16FormatHelper.FormatTo(ref sb, arg9, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg9)); break; } case 9: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + Utf16FormatHelper.FormatTo(ref sb, arg10, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg10)); break; } case 10: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + Utf16FormatHelper.FormatTo(ref sb, arg11, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg11)); break; } + default: + break; } } } @@ -990,78 +1034,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, T10 arg10, T11 arg11, T12 arg12) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); strSpan.TryCopyTo(span); sb.Advance(item.Count); + break; } - break; case 0: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); break; } case 1: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); break; } case 2: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); break; } case 3: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); break; } case 4: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf16FormatHelper.FormatTo(ref sb, arg5, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg5)); break; } case 5: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf16FormatHelper.FormatTo(ref sb, arg6, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg6)); break; } case 6: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf16FormatHelper.FormatTo(ref sb, arg7, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg7)); break; } case 7: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + Utf16FormatHelper.FormatTo(ref sb, arg8, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg8)); break; } case 8: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + Utf16FormatHelper.FormatTo(ref sb, arg9, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg9)); break; } case 9: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + Utf16FormatHelper.FormatTo(ref sb, arg10, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg10)); break; } case 10: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + Utf16FormatHelper.FormatTo(ref sb, arg11, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg11)); break; } case 11: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + Utf16FormatHelper.FormatTo(ref sb, arg12, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg12)); break; } + default: + break; } } } @@ -1106,83 +1154,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, T11 arg11, T12 arg12, T13 arg13) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); strSpan.TryCopyTo(span); sb.Advance(item.Count); + break; } - break; case 0: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); break; } case 1: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); break; } case 2: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); break; } case 3: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); break; } case 4: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf16FormatHelper.FormatTo(ref sb, arg5, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg5)); break; } case 5: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf16FormatHelper.FormatTo(ref sb, arg6, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg6)); break; } case 6: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf16FormatHelper.FormatTo(ref sb, arg7, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg7)); break; } case 7: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + Utf16FormatHelper.FormatTo(ref sb, arg8, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg8)); break; } case 8: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + Utf16FormatHelper.FormatTo(ref sb, arg9, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg9)); break; } case 9: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + Utf16FormatHelper.FormatTo(ref sb, arg10, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg10)); break; } case 10: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + Utf16FormatHelper.FormatTo(ref sb, arg11, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg11)); break; } case 11: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + Utf16FormatHelper.FormatTo(ref sb, arg12, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg12)); break; } case 12: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg13, nameof(arg13)); + Utf16FormatHelper.FormatTo(ref sb, arg13, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg13)); break; } + default: + break; } } } @@ -1227,88 +1279,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, T12 arg12, T13 arg13, T14 arg14) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); strSpan.TryCopyTo(span); sb.Advance(item.Count); + break; } - break; case 0: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); break; } case 1: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); break; } case 2: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); break; } case 3: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); break; } case 4: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf16FormatHelper.FormatTo(ref sb, arg5, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg5)); break; } case 5: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf16FormatHelper.FormatTo(ref sb, arg6, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg6)); break; } case 6: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf16FormatHelper.FormatTo(ref sb, arg7, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg7)); break; } case 7: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + Utf16FormatHelper.FormatTo(ref sb, arg8, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg8)); break; } case 8: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + Utf16FormatHelper.FormatTo(ref sb, arg9, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg9)); break; } case 9: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + Utf16FormatHelper.FormatTo(ref sb, arg10, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg10)); break; } case 10: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + Utf16FormatHelper.FormatTo(ref sb, arg11, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg11)); break; } case 11: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + Utf16FormatHelper.FormatTo(ref sb, arg12, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg12)); break; } case 12: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg13, nameof(arg13)); + Utf16FormatHelper.FormatTo(ref sb, arg13, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg13)); break; } case 13: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg14, nameof(arg14)); + Utf16FormatHelper.FormatTo(ref sb, arg14, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg14)); break; } + default: + break; } } } @@ -1353,93 +1409,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, T13 arg13, T14 arg14, T15 arg15) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); strSpan.TryCopyTo(span); sb.Advance(item.Count); + break; } - break; case 0: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); break; } case 1: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); break; } case 2: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); break; } case 3: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); break; } case 4: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf16FormatHelper.FormatTo(ref sb, arg5, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg5)); break; } case 5: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf16FormatHelper.FormatTo(ref sb, arg6, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg6)); break; } case 6: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf16FormatHelper.FormatTo(ref sb, arg7, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg7)); break; } case 7: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + Utf16FormatHelper.FormatTo(ref sb, arg8, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg8)); break; } case 8: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + Utf16FormatHelper.FormatTo(ref sb, arg9, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg9)); break; } case 9: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + Utf16FormatHelper.FormatTo(ref sb, arg10, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg10)); break; } case 10: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + Utf16FormatHelper.FormatTo(ref sb, arg11, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg11)); break; } case 11: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + Utf16FormatHelper.FormatTo(ref sb, arg12, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg12)); break; } case 12: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg13, nameof(arg13)); + Utf16FormatHelper.FormatTo(ref sb, arg13, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg13)); break; } case 13: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg14, nameof(arg14)); + Utf16FormatHelper.FormatTo(ref sb, arg14, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg14)); break; } case 14: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg15, nameof(arg15)); + Utf16FormatHelper.FormatTo(ref sb, arg15, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg15)); break; } + default: + break; } } } @@ -1484,98 +1544,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, T14 arg14, T15 arg15, T16 arg16) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); strSpan.TryCopyTo(span); sb.Advance(item.Count); + break; } - break; case 0: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); break; } case 1: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); break; } case 2: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); break; } case 3: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); break; } case 4: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf16FormatHelper.FormatTo(ref sb, arg5, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg5)); break; } case 5: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf16FormatHelper.FormatTo(ref sb, arg6, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg6)); break; } case 6: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf16FormatHelper.FormatTo(ref sb, arg7, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg7)); break; } case 7: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + Utf16FormatHelper.FormatTo(ref sb, arg8, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg8)); break; } case 8: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + Utf16FormatHelper.FormatTo(ref sb, arg9, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg9)); break; } case 9: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + Utf16FormatHelper.FormatTo(ref sb, arg10, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg10)); break; } case 10: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + Utf16FormatHelper.FormatTo(ref sb, arg11, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg11)); break; } case 11: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + Utf16FormatHelper.FormatTo(ref sb, arg12, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg12)); break; } case 12: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg13, nameof(arg13)); + Utf16FormatHelper.FormatTo(ref sb, arg13, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg13)); break; } case 13: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg14, nameof(arg14)); + Utf16FormatHelper.FormatTo(ref sb, arg14, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg14)); break; } case 14: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg15, nameof(arg15)); + Utf16FormatHelper.FormatTo(ref sb, arg15, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg15)); break; } case 15: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg16, nameof(arg16)); + Utf16FormatHelper.FormatTo(ref sb, arg16, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg16)); break; } + default: + break; } } } @@ -1620,24 +1684,28 @@ 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) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var size = Encoding.UTF8.GetMaxByteCount(item.Count); var span = sb.GetSpan(size); - var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + var count = Encoding.UTF8.GetBytes(strSpan, span); sb.Advance(count); + break; } - break; case 0: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); break; } + default: + break; } } } @@ -1682,29 +1750,33 @@ 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) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var size = Encoding.UTF8.GetMaxByteCount(item.Count); var span = sb.GetSpan(size); - var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + var count = Encoding.UTF8.GetBytes(strSpan, span); sb.Advance(count); + break; } - break; case 0: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); break; } case 1: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); break; } + default: + break; } } } @@ -1749,34 +1821,38 @@ 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) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var size = Encoding.UTF8.GetMaxByteCount(item.Count); var span = sb.GetSpan(size); - var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + var count = Encoding.UTF8.GetBytes(strSpan, span); sb.Advance(count); + break; } - break; case 0: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); break; } case 1: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); break; } case 2: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); break; } + default: + break; } } } @@ -1821,39 +1897,43 @@ 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) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var size = Encoding.UTF8.GetMaxByteCount(item.Count); var span = sb.GetSpan(size); - var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + var count = Encoding.UTF8.GetBytes(strSpan, span); sb.Advance(count); + break; } - break; case 0: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); break; } case 1: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); break; } case 2: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); break; } case 3: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); break; } + default: + break; } } } @@ -1898,44 +1978,48 @@ 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) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var size = Encoding.UTF8.GetMaxByteCount(item.Count); var span = sb.GetSpan(size); - var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + var count = Encoding.UTF8.GetBytes(strSpan, span); sb.Advance(count); + break; } - break; case 0: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); break; } case 1: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); break; } case 2: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); break; } case 3: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); break; } case 4: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref sb, arg5, item.Alignment, item.StandardFormat, nameof(arg5)); break; } + default: + break; } } } @@ -1980,49 +2064,53 @@ 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) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var size = Encoding.UTF8.GetMaxByteCount(item.Count); var span = sb.GetSpan(size); - var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + var count = Encoding.UTF8.GetBytes(strSpan, span); sb.Advance(count); + break; } - break; case 0: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); break; } case 1: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); break; } case 2: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); break; } case 3: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); break; } case 4: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref sb, arg5, item.Alignment, item.StandardFormat, nameof(arg5)); break; } case 5: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf8FormatHelper.FormatTo(ref sb, arg6, item.Alignment, item.StandardFormat, nameof(arg6)); break; } + default: + break; } } } @@ -2067,54 +2155,58 @@ 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) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var size = Encoding.UTF8.GetMaxByteCount(item.Count); var span = sb.GetSpan(size); - var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + var count = Encoding.UTF8.GetBytes(strSpan, span); sb.Advance(count); + break; } - break; case 0: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); break; } case 1: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); break; } case 2: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); break; } case 3: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); break; } case 4: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref sb, arg5, item.Alignment, item.StandardFormat, nameof(arg5)); break; } case 5: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf8FormatHelper.FormatTo(ref sb, arg6, item.Alignment, item.StandardFormat, nameof(arg6)); break; } case 6: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf8FormatHelper.FormatTo(ref sb, arg7, item.Alignment, item.StandardFormat, nameof(arg7)); break; } + default: + break; } } } @@ -2159,59 +2251,63 @@ 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) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var size = Encoding.UTF8.GetMaxByteCount(item.Count); var span = sb.GetSpan(size); - var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + var count = Encoding.UTF8.GetBytes(strSpan, span); sb.Advance(count); + break; } - break; case 0: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); break; } case 1: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); break; } case 2: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); break; } case 3: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); break; } case 4: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref sb, arg5, item.Alignment, item.StandardFormat, nameof(arg5)); break; } case 5: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf8FormatHelper.FormatTo(ref sb, arg6, item.Alignment, item.StandardFormat, nameof(arg6)); break; } case 6: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf8FormatHelper.FormatTo(ref sb, arg7, item.Alignment, item.StandardFormat, nameof(arg7)); break; } case 7: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + Utf8FormatHelper.FormatTo(ref sb, arg8, item.Alignment, item.StandardFormat, nameof(arg8)); break; } + default: + break; } } } @@ -2256,64 +2352,68 @@ 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) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var size = Encoding.UTF8.GetMaxByteCount(item.Count); var span = sb.GetSpan(size); - var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + var count = Encoding.UTF8.GetBytes(strSpan, span); sb.Advance(count); + break; } - break; case 0: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); break; } case 1: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); break; } case 2: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); break; } case 3: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); break; } case 4: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref sb, arg5, item.Alignment, item.StandardFormat, nameof(arg5)); break; } case 5: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf8FormatHelper.FormatTo(ref sb, arg6, item.Alignment, item.StandardFormat, nameof(arg6)); break; } case 6: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf8FormatHelper.FormatTo(ref sb, arg7, item.Alignment, item.StandardFormat, nameof(arg7)); break; } case 7: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + Utf8FormatHelper.FormatTo(ref sb, arg8, item.Alignment, item.StandardFormat, nameof(arg8)); break; } case 8: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + Utf8FormatHelper.FormatTo(ref sb, arg9, item.Alignment, item.StandardFormat, nameof(arg9)); break; } + default: + break; } } } @@ -2358,69 +2458,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, T8 arg8, T9 arg9, T10 arg10) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var size = Encoding.UTF8.GetMaxByteCount(item.Count); var span = sb.GetSpan(size); - var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + var count = Encoding.UTF8.GetBytes(strSpan, span); sb.Advance(count); + break; } - break; case 0: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); break; } case 1: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); break; } case 2: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); break; } case 3: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); break; } case 4: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref sb, arg5, item.Alignment, item.StandardFormat, nameof(arg5)); break; } case 5: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf8FormatHelper.FormatTo(ref sb, arg6, item.Alignment, item.StandardFormat, nameof(arg6)); break; } case 6: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf8FormatHelper.FormatTo(ref sb, arg7, item.Alignment, item.StandardFormat, nameof(arg7)); break; } case 7: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + Utf8FormatHelper.FormatTo(ref sb, arg8, item.Alignment, item.StandardFormat, nameof(arg8)); break; } case 8: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + Utf8FormatHelper.FormatTo(ref sb, arg9, item.Alignment, item.StandardFormat, nameof(arg9)); break; } case 9: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + Utf8FormatHelper.FormatTo(ref sb, arg10, item.Alignment, item.StandardFormat, nameof(arg10)); break; } + default: + break; } } } @@ -2465,74 +2569,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, T9 arg9, T10 arg10, T11 arg11) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var size = Encoding.UTF8.GetMaxByteCount(item.Count); var span = sb.GetSpan(size); - var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + var count = Encoding.UTF8.GetBytes(strSpan, span); sb.Advance(count); + break; } - break; case 0: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); break; } case 1: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); break; } case 2: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); break; } case 3: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); break; } case 4: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref sb, arg5, item.Alignment, item.StandardFormat, nameof(arg5)); break; } case 5: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf8FormatHelper.FormatTo(ref sb, arg6, item.Alignment, item.StandardFormat, nameof(arg6)); break; } case 6: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf8FormatHelper.FormatTo(ref sb, arg7, item.Alignment, item.StandardFormat, nameof(arg7)); break; } case 7: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + Utf8FormatHelper.FormatTo(ref sb, arg8, item.Alignment, item.StandardFormat, nameof(arg8)); break; } case 8: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + Utf8FormatHelper.FormatTo(ref sb, arg9, item.Alignment, item.StandardFormat, nameof(arg9)); break; } case 9: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + Utf8FormatHelper.FormatTo(ref sb, arg10, item.Alignment, item.StandardFormat, nameof(arg10)); break; } case 10: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + Utf8FormatHelper.FormatTo(ref sb, arg11, item.Alignment, item.StandardFormat, nameof(arg11)); break; } + default: + break; } } } @@ -2577,79 +2685,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, T10 arg10, T11 arg11, T12 arg12) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var size = Encoding.UTF8.GetMaxByteCount(item.Count); var span = sb.GetSpan(size); - var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + var count = Encoding.UTF8.GetBytes(strSpan, span); sb.Advance(count); + break; } - break; case 0: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); break; } case 1: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); break; } case 2: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); break; } case 3: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); break; } case 4: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref sb, arg5, item.Alignment, item.StandardFormat, nameof(arg5)); break; } case 5: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf8FormatHelper.FormatTo(ref sb, arg6, item.Alignment, item.StandardFormat, nameof(arg6)); break; } case 6: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf8FormatHelper.FormatTo(ref sb, arg7, item.Alignment, item.StandardFormat, nameof(arg7)); break; } case 7: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + Utf8FormatHelper.FormatTo(ref sb, arg8, item.Alignment, item.StandardFormat, nameof(arg8)); break; } case 8: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + Utf8FormatHelper.FormatTo(ref sb, arg9, item.Alignment, item.StandardFormat, nameof(arg9)); break; } case 9: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + Utf8FormatHelper.FormatTo(ref sb, arg10, item.Alignment, item.StandardFormat, nameof(arg10)); break; } case 10: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + Utf8FormatHelper.FormatTo(ref sb, arg11, item.Alignment, item.StandardFormat, nameof(arg11)); break; } case 11: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + Utf8FormatHelper.FormatTo(ref sb, arg12, item.Alignment, item.StandardFormat, nameof(arg12)); break; } + default: + break; } } } @@ -2694,84 +2806,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, T11 arg11, T12 arg12, T13 arg13) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var size = Encoding.UTF8.GetMaxByteCount(item.Count); var span = sb.GetSpan(size); - var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + var count = Encoding.UTF8.GetBytes(strSpan, span); sb.Advance(count); + break; } - break; case 0: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); break; } case 1: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); break; } case 2: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); break; } case 3: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); break; } case 4: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref sb, arg5, item.Alignment, item.StandardFormat, nameof(arg5)); break; } case 5: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf8FormatHelper.FormatTo(ref sb, arg6, item.Alignment, item.StandardFormat, nameof(arg6)); break; } case 6: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf8FormatHelper.FormatTo(ref sb, arg7, item.Alignment, item.StandardFormat, nameof(arg7)); break; } case 7: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + Utf8FormatHelper.FormatTo(ref sb, arg8, item.Alignment, item.StandardFormat, nameof(arg8)); break; } case 8: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + Utf8FormatHelper.FormatTo(ref sb, arg9, item.Alignment, item.StandardFormat, nameof(arg9)); break; } case 9: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + Utf8FormatHelper.FormatTo(ref sb, arg10, item.Alignment, item.StandardFormat, nameof(arg10)); break; } case 10: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + Utf8FormatHelper.FormatTo(ref sb, arg11, item.Alignment, item.StandardFormat, nameof(arg11)); break; } case 11: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + Utf8FormatHelper.FormatTo(ref sb, arg12, item.Alignment, item.StandardFormat, nameof(arg12)); break; } case 12: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg13, nameof(arg13)); + Utf8FormatHelper.FormatTo(ref sb, arg13, item.Alignment, item.StandardFormat, nameof(arg13)); break; } + default: + break; } } } @@ -2816,89 +2932,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, T12 arg12, T13 arg13, T14 arg14) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var size = Encoding.UTF8.GetMaxByteCount(item.Count); var span = sb.GetSpan(size); - var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + var count = Encoding.UTF8.GetBytes(strSpan, span); sb.Advance(count); + break; } - break; case 0: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); break; } case 1: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); break; } case 2: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); break; } case 3: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); break; } case 4: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref sb, arg5, item.Alignment, item.StandardFormat, nameof(arg5)); break; } case 5: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf8FormatHelper.FormatTo(ref sb, arg6, item.Alignment, item.StandardFormat, nameof(arg6)); break; } case 6: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf8FormatHelper.FormatTo(ref sb, arg7, item.Alignment, item.StandardFormat, nameof(arg7)); break; } case 7: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + Utf8FormatHelper.FormatTo(ref sb, arg8, item.Alignment, item.StandardFormat, nameof(arg8)); break; } case 8: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + Utf8FormatHelper.FormatTo(ref sb, arg9, item.Alignment, item.StandardFormat, nameof(arg9)); break; } case 9: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + Utf8FormatHelper.FormatTo(ref sb, arg10, item.Alignment, item.StandardFormat, nameof(arg10)); break; } case 10: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + Utf8FormatHelper.FormatTo(ref sb, arg11, item.Alignment, item.StandardFormat, nameof(arg11)); break; } case 11: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + Utf8FormatHelper.FormatTo(ref sb, arg12, item.Alignment, item.StandardFormat, nameof(arg12)); break; } case 12: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg13, nameof(arg13)); + Utf8FormatHelper.FormatTo(ref sb, arg13, item.Alignment, item.StandardFormat, nameof(arg13)); break; } case 13: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg14, nameof(arg14)); + Utf8FormatHelper.FormatTo(ref sb, arg14, item.Alignment, item.StandardFormat, nameof(arg14)); break; } + default: + break; } } } @@ -2943,94 +3063,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, T13 arg13, T14 arg14, T15 arg15) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var size = Encoding.UTF8.GetMaxByteCount(item.Count); var span = sb.GetSpan(size); - var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + var count = Encoding.UTF8.GetBytes(strSpan, span); sb.Advance(count); + break; } - break; case 0: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); break; } case 1: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); break; } case 2: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); break; } case 3: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); break; } case 4: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref sb, arg5, item.Alignment, item.StandardFormat, nameof(arg5)); break; } case 5: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf8FormatHelper.FormatTo(ref sb, arg6, item.Alignment, item.StandardFormat, nameof(arg6)); break; } case 6: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf8FormatHelper.FormatTo(ref sb, arg7, item.Alignment, item.StandardFormat, nameof(arg7)); break; } case 7: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + Utf8FormatHelper.FormatTo(ref sb, arg8, item.Alignment, item.StandardFormat, nameof(arg8)); break; } case 8: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + Utf8FormatHelper.FormatTo(ref sb, arg9, item.Alignment, item.StandardFormat, nameof(arg9)); break; } case 9: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + Utf8FormatHelper.FormatTo(ref sb, arg10, item.Alignment, item.StandardFormat, nameof(arg10)); break; } case 10: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + Utf8FormatHelper.FormatTo(ref sb, arg11, item.Alignment, item.StandardFormat, nameof(arg11)); break; } case 11: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + Utf8FormatHelper.FormatTo(ref sb, arg12, item.Alignment, item.StandardFormat, nameof(arg12)); break; } case 12: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg13, nameof(arg13)); + Utf8FormatHelper.FormatTo(ref sb, arg13, item.Alignment, item.StandardFormat, nameof(arg13)); break; } case 13: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg14, nameof(arg14)); + Utf8FormatHelper.FormatTo(ref sb, arg14, item.Alignment, item.StandardFormat, nameof(arg14)); break; } case 14: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg15, nameof(arg15)); + Utf8FormatHelper.FormatTo(ref sb, arg15, item.Alignment, item.StandardFormat, nameof(arg15)); break; } + default: + break; } } } @@ -3075,99 +3199,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, T14 arg14, T15 arg15, T16 arg16) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var size = Encoding.UTF8.GetMaxByteCount(item.Count); var span = sb.GetSpan(size); - var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + var count = Encoding.UTF8.GetBytes(strSpan, span); sb.Advance(count); + break; } - break; case 0: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); break; } case 1: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); break; } case 2: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); break; } case 3: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); break; } case 4: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref sb, arg5, item.Alignment, item.StandardFormat, nameof(arg5)); break; } case 5: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf8FormatHelper.FormatTo(ref sb, arg6, item.Alignment, item.StandardFormat, nameof(arg6)); break; } case 6: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf8FormatHelper.FormatTo(ref sb, arg7, item.Alignment, item.StandardFormat, nameof(arg7)); break; } case 7: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + Utf8FormatHelper.FormatTo(ref sb, arg8, item.Alignment, item.StandardFormat, nameof(arg8)); break; } case 8: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + Utf8FormatHelper.FormatTo(ref sb, arg9, item.Alignment, item.StandardFormat, nameof(arg9)); break; } case 9: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + Utf8FormatHelper.FormatTo(ref sb, arg10, item.Alignment, item.StandardFormat, nameof(arg10)); break; } case 10: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + Utf8FormatHelper.FormatTo(ref sb, arg11, item.Alignment, item.StandardFormat, nameof(arg11)); break; } case 11: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + Utf8FormatHelper.FormatTo(ref sb, arg12, item.Alignment, item.StandardFormat, nameof(arg12)); break; } case 12: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg13, nameof(arg13)); + Utf8FormatHelper.FormatTo(ref sb, arg13, item.Alignment, item.StandardFormat, nameof(arg13)); break; } case 13: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg14, nameof(arg14)); + Utf8FormatHelper.FormatTo(ref sb, arg14, item.Alignment, item.StandardFormat, nameof(arg14)); break; } case 14: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg15, nameof(arg15)); + Utf8FormatHelper.FormatTo(ref sb, arg15, item.Alignment, item.StandardFormat, nameof(arg15)); break; } case 15: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg16, nameof(arg16)); + Utf8FormatHelper.FormatTo(ref sb, arg16, item.Alignment, item.StandardFormat, nameof(arg16)); break; } + default: + break; } } } diff --git a/src/ZString.Unity/Assets/Scripts/ZString/PreparedFormatHelper.cs b/src/ZString.Unity/Assets/Scripts/ZString/PreparedFormatHelper.cs index 048c55d..8178f74 100644 --- a/src/ZString.Unity/Assets/Scripts/ZString/PreparedFormatHelper.cs +++ b/src/ZString.Unity/Assets/Scripts/ZString/PreparedFormatHelper.cs @@ -75,12 +75,14 @@ internal readonly struct FormatSegment { public const int NotFormatIndex = -1; + //public readonly bool IsFormatArgument; + public bool IsFormatArgument => FormatIndex != NotFormatIndex; + public readonly int Offset; public readonly int Count; - public bool IsFormatArgument => FormatIndex != NotFormatIndex; + public readonly int Alignment; public readonly int FormatIndex; public readonly string FormatString; - public readonly int Alignment; // Utf8 public readonly StandardFormat StandardFormat; @@ -89,6 +91,7 @@ public FormatSegment(int offset, int count, int formatIndex, string formatString { Offset = offset; Count = count; + //IsFormatArgument = formatIndex != NotFormatIndex; FormatIndex = formatIndex; FormatString = formatString; Alignment = alignment; diff --git a/src/ZString.Unity/Assets/Scripts/ZString/Utf8ValueStringBuilder.cs b/src/ZString.Unity/Assets/Scripts/ZString/Utf8ValueStringBuilder.cs index 2c4c822..56cfce2 100644 --- a/src/ZString.Unity/Assets/Scripts/ZString/Utf8ValueStringBuilder.cs +++ b/src/ZString.Unity/Assets/Scripts/ZString/Utf8ValueStringBuilder.cs @@ -13,7 +13,7 @@ public partial struct Utf8ValueStringBuilder : IDisposable, IBufferWriter, const int ThreadStaticBufferSize = 64444; const int DefaultBufferSize = 65536; // use 64K default buffer. - internal static readonly Encoding UTF8NoBom = new UTF8Encoding(false); + static Encoding UTF8NoBom = new UTF8Encoding(false); static byte newLine1; static byte newLine2; diff --git a/src/ZString.Unity/Assets/Scripts/ZString/ZString.Utf8Format.cs b/src/ZString.Unity/Assets/Scripts/ZString/ZString.Utf8Format.cs index 0756b1f..ce267b9 100644 --- a/src/ZString.Unity/Assets/Scripts/ZString/ZString.Utf8Format.cs +++ b/src/ZString.Unity/Assets/Scripts/ZString/ZString.Utf8Format.cs @@ -54,7 +54,7 @@ public static void Utf8Format(IBufferWriter bufferWriter, string forma switch (indexParse.Index) { case 0: - Utf8FormatInternal(bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); continue; default: ExceptionUtil.ThrowFormatException(); @@ -93,7 +93,6 @@ 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) { @@ -140,10 +139,10 @@ public static void Utf8Format(IBufferWriter bufferWriter, string f switch (indexParse.Index) { case 0: - Utf8FormatInternal(bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); continue; case 1: - Utf8FormatInternal(bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); continue; default: ExceptionUtil.ThrowFormatException(); @@ -182,7 +181,6 @@ 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) { @@ -229,13 +227,13 @@ public static void Utf8Format(IBufferWriter bufferWriter, stri switch (indexParse.Index) { case 0: - Utf8FormatInternal(bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); continue; case 1: - Utf8FormatInternal(bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); continue; case 2: - Utf8FormatInternal(bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); continue; default: ExceptionUtil.ThrowFormatException(); @@ -274,7 +272,6 @@ 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) { @@ -321,16 +318,16 @@ public static void Utf8Format(IBufferWriter bufferWriter, switch (indexParse.Index) { case 0: - Utf8FormatInternal(bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); continue; case 1: - Utf8FormatInternal(bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); continue; case 2: - Utf8FormatInternal(bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); continue; case 3: - Utf8FormatInternal(bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); continue; default: ExceptionUtil.ThrowFormatException(); @@ -369,7 +366,6 @@ 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) { @@ -416,19 +412,19 @@ public static void Utf8Format(IBufferWriter bufferWrit switch (indexParse.Index) { case 0: - Utf8FormatInternal(bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); continue; case 1: - Utf8FormatInternal(bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); continue; case 2: - Utf8FormatInternal(bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); continue; case 3: - Utf8FormatInternal(bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); continue; case 4: - Utf8FormatInternal(bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); continue; default: ExceptionUtil.ThrowFormatException(); @@ -467,7 +463,6 @@ 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) { @@ -514,22 +509,22 @@ public static void Utf8Format(IBufferWriter buffer switch (indexParse.Index) { case 0: - Utf8FormatInternal(bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); continue; case 1: - Utf8FormatInternal(bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); continue; case 2: - Utf8FormatInternal(bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); continue; case 3: - Utf8FormatInternal(bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); continue; case 4: - Utf8FormatInternal(bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); continue; case 5: - Utf8FormatInternal(bufferWriter, arg6, indexParse.Alignment, writeFormat, nameof(arg6)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg6, indexParse.Alignment, writeFormat, nameof(arg6)); continue; default: ExceptionUtil.ThrowFormatException(); @@ -568,7 +563,6 @@ 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) { @@ -615,25 +609,25 @@ public static void Utf8Format(IBufferWriter bu switch (indexParse.Index) { case 0: - Utf8FormatInternal(bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); continue; case 1: - Utf8FormatInternal(bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); continue; case 2: - Utf8FormatInternal(bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); continue; case 3: - Utf8FormatInternal(bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); continue; case 4: - Utf8FormatInternal(bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); continue; case 5: - Utf8FormatInternal(bufferWriter, arg6, indexParse.Alignment, writeFormat, nameof(arg6)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg6, indexParse.Alignment, writeFormat, nameof(arg6)); continue; case 6: - Utf8FormatInternal(bufferWriter, arg7, indexParse.Alignment, writeFormat, nameof(arg7)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg7, indexParse.Alignment, writeFormat, nameof(arg7)); continue; default: ExceptionUtil.ThrowFormatException(); @@ -672,7 +666,6 @@ 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) { @@ -719,28 +712,28 @@ public static void Utf8Format(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) { @@ -826,31 +818,31 @@ public static void Utf8Format(IBufferWriter< switch (indexParse.Index) { case 0: - Utf8FormatInternal(bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); continue; case 1: - Utf8FormatInternal(bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); continue; case 2: - Utf8FormatInternal(bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); continue; case 3: - Utf8FormatInternal(bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); continue; case 4: - Utf8FormatInternal(bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); continue; case 5: - Utf8FormatInternal(bufferWriter, arg6, indexParse.Alignment, writeFormat, nameof(arg6)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg6, indexParse.Alignment, writeFormat, nameof(arg6)); continue; case 6: - Utf8FormatInternal(bufferWriter, arg7, indexParse.Alignment, writeFormat, nameof(arg7)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg7, indexParse.Alignment, writeFormat, nameof(arg7)); continue; case 7: - Utf8FormatInternal(bufferWriter, arg8, indexParse.Alignment, writeFormat, nameof(arg8)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg8, indexParse.Alignment, writeFormat, nameof(arg8)); continue; case 8: - Utf8FormatInternal(bufferWriter, arg9, indexParse.Alignment, writeFormat, nameof(arg9)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg9, indexParse.Alignment, writeFormat, nameof(arg9)); continue; default: ExceptionUtil.ThrowFormatException(); @@ -889,7 +881,6 @@ 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) { @@ -936,34 +927,34 @@ public static void Utf8Format(IBufferWr switch (indexParse.Index) { case 0: - Utf8FormatInternal(bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); continue; case 1: - Utf8FormatInternal(bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); continue; case 2: - Utf8FormatInternal(bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); continue; case 3: - Utf8FormatInternal(bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); continue; case 4: - Utf8FormatInternal(bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); continue; case 5: - Utf8FormatInternal(bufferWriter, arg6, indexParse.Alignment, writeFormat, nameof(arg6)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg6, indexParse.Alignment, writeFormat, nameof(arg6)); continue; case 6: - Utf8FormatInternal(bufferWriter, arg7, indexParse.Alignment, writeFormat, nameof(arg7)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg7, indexParse.Alignment, writeFormat, nameof(arg7)); continue; case 7: - Utf8FormatInternal(bufferWriter, arg8, indexParse.Alignment, writeFormat, nameof(arg8)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg8, indexParse.Alignment, writeFormat, nameof(arg8)); continue; case 8: - Utf8FormatInternal(bufferWriter, arg9, indexParse.Alignment, writeFormat, nameof(arg9)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg9, indexParse.Alignment, writeFormat, nameof(arg9)); continue; case 9: - Utf8FormatInternal(bufferWriter, arg10, indexParse.Alignment, writeFormat, nameof(arg10)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg10, indexParse.Alignment, writeFormat, nameof(arg10)); continue; default: ExceptionUtil.ThrowFormatException(); @@ -1002,7 +993,6 @@ 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) { @@ -1049,37 +1039,37 @@ public static void Utf8Format(IBuf switch (indexParse.Index) { case 0: - Utf8FormatInternal(bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); continue; case 1: - Utf8FormatInternal(bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); continue; case 2: - Utf8FormatInternal(bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); continue; case 3: - Utf8FormatInternal(bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); continue; case 4: - Utf8FormatInternal(bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); continue; case 5: - Utf8FormatInternal(bufferWriter, arg6, indexParse.Alignment, writeFormat, nameof(arg6)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg6, indexParse.Alignment, writeFormat, nameof(arg6)); continue; case 6: - Utf8FormatInternal(bufferWriter, arg7, indexParse.Alignment, writeFormat, nameof(arg7)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg7, indexParse.Alignment, writeFormat, nameof(arg7)); continue; case 7: - Utf8FormatInternal(bufferWriter, arg8, indexParse.Alignment, writeFormat, nameof(arg8)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg8, indexParse.Alignment, writeFormat, nameof(arg8)); continue; case 8: - Utf8FormatInternal(bufferWriter, arg9, indexParse.Alignment, writeFormat, nameof(arg9)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg9, indexParse.Alignment, writeFormat, nameof(arg9)); continue; case 9: - Utf8FormatInternal(bufferWriter, arg10, indexParse.Alignment, writeFormat, nameof(arg10)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg10, indexParse.Alignment, writeFormat, nameof(arg10)); continue; case 10: - Utf8FormatInternal(bufferWriter, arg11, indexParse.Alignment, writeFormat, nameof(arg11)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg11, indexParse.Alignment, writeFormat, nameof(arg11)); continue; default: ExceptionUtil.ThrowFormatException(); @@ -1118,7 +1108,6 @@ 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) { @@ -1165,40 +1154,40 @@ public static void Utf8Format switch (indexParse.Index) { case 0: - Utf8FormatInternal(bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); continue; case 1: - Utf8FormatInternal(bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); continue; case 2: - Utf8FormatInternal(bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); continue; case 3: - Utf8FormatInternal(bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); continue; case 4: - Utf8FormatInternal(bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); continue; case 5: - Utf8FormatInternal(bufferWriter, arg6, indexParse.Alignment, writeFormat, nameof(arg6)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg6, indexParse.Alignment, writeFormat, nameof(arg6)); continue; case 6: - Utf8FormatInternal(bufferWriter, arg7, indexParse.Alignment, writeFormat, nameof(arg7)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg7, indexParse.Alignment, writeFormat, nameof(arg7)); continue; case 7: - Utf8FormatInternal(bufferWriter, arg8, indexParse.Alignment, writeFormat, nameof(arg8)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg8, indexParse.Alignment, writeFormat, nameof(arg8)); continue; case 8: - Utf8FormatInternal(bufferWriter, arg9, indexParse.Alignment, writeFormat, nameof(arg9)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg9, indexParse.Alignment, writeFormat, nameof(arg9)); continue; case 9: - Utf8FormatInternal(bufferWriter, arg10, indexParse.Alignment, writeFormat, nameof(arg10)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg10, indexParse.Alignment, writeFormat, nameof(arg10)); continue; case 10: - Utf8FormatInternal(bufferWriter, arg11, indexParse.Alignment, writeFormat, nameof(arg11)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg11, indexParse.Alignment, writeFormat, nameof(arg11)); continue; case 11: - Utf8FormatInternal(bufferWriter, arg12, indexParse.Alignment, writeFormat, nameof(arg12)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg12, indexParse.Alignment, writeFormat, nameof(arg12)); continue; default: ExceptionUtil.ThrowFormatException(); @@ -1237,7 +1226,6 @@ 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) { @@ -1284,43 +1272,43 @@ 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) { @@ -1406,46 +1393,46 @@ 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) { @@ -1531,49 +1517,49 @@ 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) { @@ -1659,52 +1644,52 @@ public static void Utf8Format(IBufferWriter bufferWriter, T arg, int width, StandardFormat format, string argName) - { - if (width <= 0) // leftJustify - { - width *= -1; - - var buffer = bufferWriter.GetSpan(); - - if (!FormatterCache.TryFormatDelegate(arg, buffer, out var written, format)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - - if (!FormatterCache.TryFormatDelegate(arg, buffer, out written, format)) - { - ExceptionUtil.ThrowArgumentException(argName); - } - } - - bufferWriter.Advance(written); - - int padding = width - written; - if (width > 0 && padding > 0) - { - // TODO Append(' ', padding); - bufferWriter.GetSpan(padding).Fill((byte)' '); // TODO Fill Method is too slow. - bufferWriter.Advance(padding); - } - } - else // rightJustify - { - if (typeof(T) == typeof(string)) - { - var s = Unsafe.As(arg); - int padding = width - s.Length; - if (padding > 0) - { - // TODO Append(' ', padding); - bufferWriter.GetSpan(padding).Fill((byte)' '); // TODO Fill Method is too slow. - bufferWriter.Advance(padding); - } - - ZString.AppendChars(ref bufferWriter, s.AsSpan()); - } - 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)) - { - ExceptionUtil.ThrowArgumentException(argName); - } - } - - int padding = width - charsWritten; - if (padding > 0) - { - // TODO Append(' ', padding); - bufferWriter.GetSpan(padding).Fill((byte)' '); // TODO Fill Method is too slow. - bufferWriter.Advance(padding); - } - - s.CopyTo(bufferWriter.GetSpan(charsWritten)); - bufferWriter.Advance(charsWritten); - } - } - } - } } diff --git a/src/ZString/FormatHelper.cs b/src/ZString/FormatHelper.cs index 5e23b58..de9846c 100644 --- a/src/ZString/FormatHelper.cs +++ b/src/ZString/FormatHelper.cs @@ -5,19 +5,16 @@ namespace Cysharp.Text { - internal static partial class Utf16PreparedFormat + internal static partial class Utf16FormatHelper { - public static void FormatTo(ref TBufferWriter sb, in FormatSegment item, T arg, string argName) + 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 { - const char sp = (char)' '; - var width = item.Alignment; - var format = item.FormatString.AsSpan(item.Offset, item.Count); - if (width <= 0) // leftJustify { - width *= -1; - var span = sb.GetSpan(0); if (!Utf16ValueStringBuilder.FormatterCache.TryFormatDelegate(arg, span, out var argWritten, format)) { @@ -30,6 +27,7 @@ public static void FormatTo(ref TBufferWriter sb, in FormatSeg } sb.Advance(argWritten); + width *= -1; int padding = width - argWritten; if (width > 0 && padding > 0) { @@ -38,64 +36,68 @@ public static void FormatTo(ref TBufferWriter sb, in FormatSeg sb.Advance(padding); } } - else // rightJustify + else { - 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); - } + FormatToRightJustify(ref sb, arg, width, format, argName); + } + } - var span = sb.GetSpan(s.Length); - s.AsSpan().CopyTo(span); - sb.Advance(s.Length); - } - else + 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) { - Span s = stackalloc char[typeof(T).IsValueType ? Unsafe.SizeOf() * 8 : 1024]; + var paddingSpan = sb.GetSpan(padding); + paddingSpan.Fill(sp); + sb.Advance(padding); + } - 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); - } - } + 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]; - int padding = width - charsWritten; - if (padding > 0) + 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)) { - var paddingSpan = sb.GetSpan(padding); - paddingSpan.Fill(sp); - sb.Advance(padding); + ExceptionUtil.ThrowArgumentException(argName); } + } - var span = sb.GetSpan(charsWritten); - s.CopyTo(span); - sb.Advance(charsWritten); + 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 Utf8PreparedFormat + + internal static partial class Utf8FormatHelper { - public static void FormatTo(ref TBufferWriter sb, in FormatSegment item, T arg, string argName) + 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 { - const byte sp = (byte)' '; - var width = item.Alignment; - var format = item.StandardFormat; - if (width <= 0) // leftJustify { - width *= -1; - var span = sb.GetSpan(0); if (!Utf8ValueStringBuilder.FormatterCache.TryFormatDelegate(arg, span, out var argWritten, format)) { @@ -108,6 +110,7 @@ public static void FormatTo(ref TBufferWriter sb, in FormatSeg } sb.Advance(argWritten); + width *= -1; int padding = width - argWritten; if (width > 0 && padding > 0) { @@ -116,47 +119,54 @@ public static void FormatTo(ref TBufferWriter sb, in FormatSeg sb.Advance(padding); } } - else // rightJustify + else { - 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); - } + FormatToRightJustify(ref sb, arg, width, format, argName); + } + } - ZString.AppendChars(ref sb, s.AsSpan()); - } - else + 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) { - Span s = stackalloc byte[typeof(T).IsValueType ? Unsafe.SizeOf() * 8 : 1024]; + var paddingSpan = sb.GetSpan(padding); + paddingSpan.Fill(sp); + sb.Advance(padding); + } - 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); - } - } + ZString.AppendChars(ref sb, s.AsSpan()); + } + else + { + Span s = stackalloc byte[typeof(T).IsValueType ? Unsafe.SizeOf() * 8 : 1024]; - int padding = width - charsWritten; - if (padding > 0) + 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)) { - var paddingSpan = sb.GetSpan(padding); - paddingSpan.Fill(sp); - sb.Advance(padding); + ExceptionUtil.ThrowArgumentException(argName); } + } - var span = sb.GetSpan(charsWritten); - s.CopyTo(span); - sb.Advance(charsWritten); + 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/src/ZString/FormatHelper.tt b/src/ZString/FormatHelper.tt index db3f1c8..d6ed5aa 100644 --- a/src/ZString/FormatHelper.tt +++ b/src/ZString/FormatHelper.tt @@ -13,19 +13,16 @@ using System.Runtime.CompilerServices; namespace Cysharp.Text { <# foreach(var utf in utfTypes ) { var isUtf16 = (utf == "Utf16"); var elemType = isUtf16 ? "char" : "byte"; #> - internal static partial class <#= utf #>PreparedFormat + internal static partial class <#= utf #>FormatHelper { - public static void FormatTo(ref TBufferWriter sb, in FormatSegment item, T arg, string argName) + const <#= elemType #> sp = (<#= elemType #>)' '; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void FormatTo(ref TBufferWriter sb, T arg, int width, <#= isUtf16 ? "ReadOnlySpan" : "StandardFormat" #> format, string argName) where TBufferWriter : IBufferWriter<<#= elemType #>> { - const <#= elemType #> sp = (<#= elemType #>)' '; - var width = item.Alignment; - var format = <#= isUtf16 ? "item.FormatString.AsSpan(item.Offset, item.Count)" : "item.StandardFormat" #>; - if (width <= 0) // leftJustify { - width *= -1; - var span = sb.GetSpan(0); if (!<#= utf #>ValueStringBuilder.FormatterCache.TryFormatDelegate(arg, span, out var argWritten, format)) { @@ -38,6 +35,7 @@ namespace Cysharp.Text } sb.Advance(argWritten); + width *= -1; int padding = width - argWritten; if (width > 0 && padding > 0) { @@ -46,54 +44,61 @@ namespace Cysharp.Text sb.Advance(padding); } } - else // rightJustify + else { - if (typeof(T) == typeof(string)) + FormatToRightJustify(ref sb, arg, width, format, argName); + } + } + + private static void FormatToRightJustify(ref TBufferWriter sb, T arg, int width, <#= isUtf16 ? "ReadOnlySpan" : "StandardFormat" #> format, string argName) + where TBufferWriter : IBufferWriter<<#= elemType #>> + { + if (typeof(T) == typeof(string)) + { + var s = Unsafe.As(arg); + int padding = width - s.Length; + if (padding > 0) { - 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 paddingSpan = sb.GetSpan(padding); + paddingSpan.Fill(sp); + sb.Advance(padding); + } <# if(isUtf16) { #> - var span = sb.GetSpan(s.Length); - s.AsSpan().CopyTo(span); - sb.Advance(s.Length); + var span = sb.GetSpan(s.Length); + s.AsSpan().CopyTo(span); + sb.Advance(s.Length); <# }else { #> - ZString.AppendChars(ref sb, s.AsSpan()); + ZString.AppendChars(ref sb, s.AsSpan()); <# } #> - } - else - { - Span<<#= elemType #>> s = stackalloc <#= elemType #>[typeof(T).IsValueType ? Unsafe.SizeOf() * 8 : 1024]; - - if (!<#= utf #>ValueStringBuilder.FormatterCache.TryFormatDelegate(arg, s, out var charsWritten, format)) - { - s = stackalloc <#= elemType #>[s.Length * 2]; - if (!<#= utf #>ValueStringBuilder.FormatterCache.TryFormatDelegate(arg, s, out charsWritten, format)) - { - ExceptionUtil.ThrowArgumentException(argName); - } - } + } + else + { + Span<<#= elemType #>> s = stackalloc <#= elemType #>[typeof(T).IsValueType ? Unsafe.SizeOf() * 8 : 1024]; - int padding = width - charsWritten; - if (padding > 0) + if (!<#= utf #>ValueStringBuilder.FormatterCache.TryFormatDelegate(arg, s, out var charsWritten, format)) + { + s = stackalloc <#= elemType #>[s.Length * 2]; + if (!<#= utf #>ValueStringBuilder.FormatterCache.TryFormatDelegate(arg, s, out charsWritten, format)) { - var paddingSpan = sb.GetSpan(padding); - paddingSpan.Fill(sp); - sb.Advance(padding); + ExceptionUtil.ThrowArgumentException(argName); } + } - var span = sb.GetSpan(charsWritten); - s.CopyTo(span); - sb.Advance(charsWritten); + 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); } } } + <# } // foreach(utf) #> -} +} \ No newline at end of file diff --git a/src/ZString/PreparedFormat.cs b/src/ZString/PreparedFormat.cs index 8d5210f..ae9a297 100644 --- a/src/ZString/PreparedFormat.cs +++ b/src/ZString/PreparedFormat.cs @@ -44,23 +44,27 @@ 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) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); strSpan.TryCopyTo(span); sb.Advance(item.Count); + break; } - break; case 0: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); break; } + default: + break; } } } @@ -105,28 +109,32 @@ 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) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); strSpan.TryCopyTo(span); sb.Advance(item.Count); + break; } - break; case 0: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); break; } case 1: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); break; } + default: + break; } } } @@ -171,33 +179,37 @@ 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) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); strSpan.TryCopyTo(span); sb.Advance(item.Count); + break; } - break; case 0: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); break; } case 1: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); break; } case 2: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); break; } + default: + break; } } } @@ -242,38 +254,42 @@ 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) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); strSpan.TryCopyTo(span); sb.Advance(item.Count); + break; } - break; case 0: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); break; } case 1: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); break; } case 2: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); break; } case 3: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); break; } + default: + break; } } } @@ -318,43 +334,47 @@ 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) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); strSpan.TryCopyTo(span); sb.Advance(item.Count); + break; } - break; case 0: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); break; } case 1: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); break; } case 2: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); break; } case 3: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); break; } case 4: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf16FormatHelper.FormatTo(ref sb, arg5, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg5)); break; } + default: + break; } } } @@ -399,48 +419,52 @@ 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) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); strSpan.TryCopyTo(span); sb.Advance(item.Count); + break; } - break; case 0: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); break; } case 1: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); break; } case 2: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); break; } case 3: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); break; } case 4: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf16FormatHelper.FormatTo(ref sb, arg5, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg5)); break; } case 5: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf16FormatHelper.FormatTo(ref sb, arg6, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg6)); break; } + default: + break; } } } @@ -485,53 +509,57 @@ 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) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); strSpan.TryCopyTo(span); sb.Advance(item.Count); + break; } - break; case 0: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); break; } case 1: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); break; } case 2: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); break; } case 3: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); break; } case 4: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf16FormatHelper.FormatTo(ref sb, arg5, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg5)); break; } case 5: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf16FormatHelper.FormatTo(ref sb, arg6, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg6)); break; } case 6: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf16FormatHelper.FormatTo(ref sb, arg7, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg7)); break; } + default: + break; } } } @@ -576,58 +604,62 @@ 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) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); strSpan.TryCopyTo(span); sb.Advance(item.Count); + break; } - break; case 0: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); break; } case 1: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); break; } case 2: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); break; } case 3: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); break; } case 4: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf16FormatHelper.FormatTo(ref sb, arg5, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg5)); break; } case 5: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf16FormatHelper.FormatTo(ref sb, arg6, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg6)); break; } case 6: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf16FormatHelper.FormatTo(ref sb, arg7, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg7)); break; } case 7: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + Utf16FormatHelper.FormatTo(ref sb, arg8, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg8)); break; } + default: + break; } } } @@ -672,63 +704,67 @@ 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) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); strSpan.TryCopyTo(span); sb.Advance(item.Count); + break; } - break; case 0: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); break; } case 1: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); break; } case 2: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); break; } case 3: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); break; } case 4: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf16FormatHelper.FormatTo(ref sb, arg5, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg5)); break; } case 5: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf16FormatHelper.FormatTo(ref sb, arg6, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg6)); break; } case 6: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf16FormatHelper.FormatTo(ref sb, arg7, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg7)); break; } case 7: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + Utf16FormatHelper.FormatTo(ref sb, arg8, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg8)); break; } case 8: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + Utf16FormatHelper.FormatTo(ref sb, arg9, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg9)); break; } + default: + break; } } } @@ -773,68 +809,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, T8 arg8, T9 arg9, T10 arg10) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); strSpan.TryCopyTo(span); sb.Advance(item.Count); + break; } - break; case 0: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); break; } case 1: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); break; } case 2: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); break; } case 3: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); break; } case 4: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf16FormatHelper.FormatTo(ref sb, arg5, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg5)); break; } case 5: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf16FormatHelper.FormatTo(ref sb, arg6, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg6)); break; } case 6: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf16FormatHelper.FormatTo(ref sb, arg7, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg7)); break; } case 7: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + Utf16FormatHelper.FormatTo(ref sb, arg8, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg8)); break; } case 8: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + Utf16FormatHelper.FormatTo(ref sb, arg9, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg9)); break; } case 9: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + Utf16FormatHelper.FormatTo(ref sb, arg10, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg10)); break; } + default: + break; } } } @@ -879,73 +919,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, T9 arg9, T10 arg10, T11 arg11) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); strSpan.TryCopyTo(span); sb.Advance(item.Count); + break; } - break; case 0: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); break; } case 1: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); break; } case 2: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); break; } case 3: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); break; } case 4: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf16FormatHelper.FormatTo(ref sb, arg5, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg5)); break; } case 5: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf16FormatHelper.FormatTo(ref sb, arg6, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg6)); break; } case 6: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf16FormatHelper.FormatTo(ref sb, arg7, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg7)); break; } case 7: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + Utf16FormatHelper.FormatTo(ref sb, arg8, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg8)); break; } case 8: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + Utf16FormatHelper.FormatTo(ref sb, arg9, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg9)); break; } case 9: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + Utf16FormatHelper.FormatTo(ref sb, arg10, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg10)); break; } case 10: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + Utf16FormatHelper.FormatTo(ref sb, arg11, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg11)); break; } + default: + break; } } } @@ -990,78 +1034,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, T10 arg10, T11 arg11, T12 arg12) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); strSpan.TryCopyTo(span); sb.Advance(item.Count); + break; } - break; case 0: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); break; } case 1: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); break; } case 2: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); break; } case 3: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); break; } case 4: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf16FormatHelper.FormatTo(ref sb, arg5, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg5)); break; } case 5: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf16FormatHelper.FormatTo(ref sb, arg6, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg6)); break; } case 6: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf16FormatHelper.FormatTo(ref sb, arg7, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg7)); break; } case 7: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + Utf16FormatHelper.FormatTo(ref sb, arg8, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg8)); break; } case 8: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + Utf16FormatHelper.FormatTo(ref sb, arg9, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg9)); break; } case 9: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + Utf16FormatHelper.FormatTo(ref sb, arg10, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg10)); break; } case 10: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + Utf16FormatHelper.FormatTo(ref sb, arg11, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg11)); break; } case 11: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + Utf16FormatHelper.FormatTo(ref sb, arg12, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg12)); break; } + default: + break; } } } @@ -1106,83 +1154,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, T11 arg11, T12 arg12, T13 arg13) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); strSpan.TryCopyTo(span); sb.Advance(item.Count); + break; } - break; case 0: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); break; } case 1: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); break; } case 2: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); break; } case 3: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); break; } case 4: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf16FormatHelper.FormatTo(ref sb, arg5, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg5)); break; } case 5: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf16FormatHelper.FormatTo(ref sb, arg6, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg6)); break; } case 6: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf16FormatHelper.FormatTo(ref sb, arg7, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg7)); break; } case 7: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + Utf16FormatHelper.FormatTo(ref sb, arg8, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg8)); break; } case 8: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + Utf16FormatHelper.FormatTo(ref sb, arg9, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg9)); break; } case 9: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + Utf16FormatHelper.FormatTo(ref sb, arg10, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg10)); break; } case 10: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + Utf16FormatHelper.FormatTo(ref sb, arg11, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg11)); break; } case 11: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + Utf16FormatHelper.FormatTo(ref sb, arg12, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg12)); break; } case 12: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg13, nameof(arg13)); + Utf16FormatHelper.FormatTo(ref sb, arg13, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg13)); break; } + default: + break; } } } @@ -1227,88 +1279,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, T12 arg12, T13 arg13, T14 arg14) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); strSpan.TryCopyTo(span); sb.Advance(item.Count); + break; } - break; case 0: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); break; } case 1: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); break; } case 2: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); break; } case 3: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); break; } case 4: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf16FormatHelper.FormatTo(ref sb, arg5, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg5)); break; } case 5: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf16FormatHelper.FormatTo(ref sb, arg6, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg6)); break; } case 6: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf16FormatHelper.FormatTo(ref sb, arg7, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg7)); break; } case 7: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + Utf16FormatHelper.FormatTo(ref sb, arg8, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg8)); break; } case 8: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + Utf16FormatHelper.FormatTo(ref sb, arg9, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg9)); break; } case 9: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + Utf16FormatHelper.FormatTo(ref sb, arg10, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg10)); break; } case 10: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + Utf16FormatHelper.FormatTo(ref sb, arg11, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg11)); break; } case 11: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + Utf16FormatHelper.FormatTo(ref sb, arg12, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg12)); break; } case 12: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg13, nameof(arg13)); + Utf16FormatHelper.FormatTo(ref sb, arg13, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg13)); break; } case 13: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg14, nameof(arg14)); + Utf16FormatHelper.FormatTo(ref sb, arg14, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg14)); break; } + default: + break; } } } @@ -1353,93 +1409,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, T13 arg13, T14 arg14, T15 arg15) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); strSpan.TryCopyTo(span); sb.Advance(item.Count); + break; } - break; case 0: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); break; } case 1: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); break; } case 2: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); break; } case 3: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); break; } case 4: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf16FormatHelper.FormatTo(ref sb, arg5, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg5)); break; } case 5: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf16FormatHelper.FormatTo(ref sb, arg6, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg6)); break; } case 6: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf16FormatHelper.FormatTo(ref sb, arg7, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg7)); break; } case 7: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + Utf16FormatHelper.FormatTo(ref sb, arg8, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg8)); break; } case 8: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + Utf16FormatHelper.FormatTo(ref sb, arg9, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg9)); break; } case 9: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + Utf16FormatHelper.FormatTo(ref sb, arg10, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg10)); break; } case 10: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + Utf16FormatHelper.FormatTo(ref sb, arg11, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg11)); break; } case 11: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + Utf16FormatHelper.FormatTo(ref sb, arg12, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg12)); break; } case 12: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg13, nameof(arg13)); + Utf16FormatHelper.FormatTo(ref sb, arg13, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg13)); break; } case 13: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg14, nameof(arg14)); + Utf16FormatHelper.FormatTo(ref sb, arg14, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg14)); break; } case 14: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg15, nameof(arg15)); + Utf16FormatHelper.FormatTo(ref sb, arg15, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg15)); break; } + default: + break; } } } @@ -1484,98 +1544,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, T14 arg14, T15 arg15, T16 arg16) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); strSpan.TryCopyTo(span); sb.Advance(item.Count); + break; } - break; case 0: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf16FormatHelper.FormatTo(ref sb, arg1, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg1)); break; } case 1: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf16FormatHelper.FormatTo(ref sb, arg2, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg2)); break; } case 2: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf16FormatHelper.FormatTo(ref sb, arg3, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg3)); break; } case 3: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf16FormatHelper.FormatTo(ref sb, arg4, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg4)); break; } case 4: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf16FormatHelper.FormatTo(ref sb, arg5, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg5)); break; } case 5: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf16FormatHelper.FormatTo(ref sb, arg6, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg6)); break; } case 6: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf16FormatHelper.FormatTo(ref sb, arg7, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg7)); break; } case 7: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + Utf16FormatHelper.FormatTo(ref sb, arg8, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg8)); break; } case 8: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + Utf16FormatHelper.FormatTo(ref sb, arg9, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg9)); break; } case 9: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + Utf16FormatHelper.FormatTo(ref sb, arg10, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg10)); break; } case 10: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + Utf16FormatHelper.FormatTo(ref sb, arg11, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg11)); break; } case 11: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + Utf16FormatHelper.FormatTo(ref sb, arg12, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg12)); break; } case 12: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg13, nameof(arg13)); + Utf16FormatHelper.FormatTo(ref sb, arg13, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg13)); break; } case 13: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg14, nameof(arg14)); + Utf16FormatHelper.FormatTo(ref sb, arg14, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg14)); break; } case 14: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg15, nameof(arg15)); + Utf16FormatHelper.FormatTo(ref sb, arg15, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg15)); break; } case 15: { - Utf16PreparedFormat.FormatTo(ref sb, item, arg16, nameof(arg16)); + Utf16FormatHelper.FormatTo(ref sb, arg16, item.Alignment, formatSpan.Slice(item.Offset, item.Count), nameof(arg16)); break; } + default: + break; } } } @@ -1620,24 +1684,28 @@ 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) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var size = Encoding.UTF8.GetMaxByteCount(item.Count); var span = sb.GetSpan(size); - var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + var count = Encoding.UTF8.GetBytes(strSpan, span); sb.Advance(count); + break; } - break; case 0: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); break; } + default: + break; } } } @@ -1682,29 +1750,33 @@ 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) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var size = Encoding.UTF8.GetMaxByteCount(item.Count); var span = sb.GetSpan(size); - var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + var count = Encoding.UTF8.GetBytes(strSpan, span); sb.Advance(count); + break; } - break; case 0: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); break; } case 1: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); break; } + default: + break; } } } @@ -1749,34 +1821,38 @@ 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) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var size = Encoding.UTF8.GetMaxByteCount(item.Count); var span = sb.GetSpan(size); - var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + var count = Encoding.UTF8.GetBytes(strSpan, span); sb.Advance(count); + break; } - break; case 0: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); break; } case 1: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); break; } case 2: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); break; } + default: + break; } } } @@ -1821,39 +1897,43 @@ 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) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var size = Encoding.UTF8.GetMaxByteCount(item.Count); var span = sb.GetSpan(size); - var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + var count = Encoding.UTF8.GetBytes(strSpan, span); sb.Advance(count); + break; } - break; case 0: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); break; } case 1: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); break; } case 2: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); break; } case 3: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); break; } + default: + break; } } } @@ -1898,44 +1978,48 @@ 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) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var size = Encoding.UTF8.GetMaxByteCount(item.Count); var span = sb.GetSpan(size); - var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + var count = Encoding.UTF8.GetBytes(strSpan, span); sb.Advance(count); + break; } - break; case 0: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); break; } case 1: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); break; } case 2: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); break; } case 3: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); break; } case 4: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref sb, arg5, item.Alignment, item.StandardFormat, nameof(arg5)); break; } + default: + break; } } } @@ -1980,49 +2064,53 @@ 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) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var size = Encoding.UTF8.GetMaxByteCount(item.Count); var span = sb.GetSpan(size); - var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + var count = Encoding.UTF8.GetBytes(strSpan, span); sb.Advance(count); + break; } - break; case 0: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); break; } case 1: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); break; } case 2: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); break; } case 3: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); break; } case 4: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref sb, arg5, item.Alignment, item.StandardFormat, nameof(arg5)); break; } case 5: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf8FormatHelper.FormatTo(ref sb, arg6, item.Alignment, item.StandardFormat, nameof(arg6)); break; } + default: + break; } } } @@ -2067,54 +2155,58 @@ 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) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var size = Encoding.UTF8.GetMaxByteCount(item.Count); var span = sb.GetSpan(size); - var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + var count = Encoding.UTF8.GetBytes(strSpan, span); sb.Advance(count); + break; } - break; case 0: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); break; } case 1: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); break; } case 2: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); break; } case 3: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); break; } case 4: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref sb, arg5, item.Alignment, item.StandardFormat, nameof(arg5)); break; } case 5: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf8FormatHelper.FormatTo(ref sb, arg6, item.Alignment, item.StandardFormat, nameof(arg6)); break; } case 6: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf8FormatHelper.FormatTo(ref sb, arg7, item.Alignment, item.StandardFormat, nameof(arg7)); break; } + default: + break; } } } @@ -2159,59 +2251,63 @@ 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) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var size = Encoding.UTF8.GetMaxByteCount(item.Count); var span = sb.GetSpan(size); - var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + var count = Encoding.UTF8.GetBytes(strSpan, span); sb.Advance(count); + break; } - break; case 0: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); break; } case 1: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); break; } case 2: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); break; } case 3: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); break; } case 4: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref sb, arg5, item.Alignment, item.StandardFormat, nameof(arg5)); break; } case 5: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf8FormatHelper.FormatTo(ref sb, arg6, item.Alignment, item.StandardFormat, nameof(arg6)); break; } case 6: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf8FormatHelper.FormatTo(ref sb, arg7, item.Alignment, item.StandardFormat, nameof(arg7)); break; } case 7: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + Utf8FormatHelper.FormatTo(ref sb, arg8, item.Alignment, item.StandardFormat, nameof(arg8)); break; } + default: + break; } } } @@ -2256,64 +2352,68 @@ 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) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var size = Encoding.UTF8.GetMaxByteCount(item.Count); var span = sb.GetSpan(size); - var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + var count = Encoding.UTF8.GetBytes(strSpan, span); sb.Advance(count); + break; } - break; case 0: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); break; } case 1: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); break; } case 2: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); break; } case 3: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); break; } case 4: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref sb, arg5, item.Alignment, item.StandardFormat, nameof(arg5)); break; } case 5: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf8FormatHelper.FormatTo(ref sb, arg6, item.Alignment, item.StandardFormat, nameof(arg6)); break; } case 6: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf8FormatHelper.FormatTo(ref sb, arg7, item.Alignment, item.StandardFormat, nameof(arg7)); break; } case 7: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + Utf8FormatHelper.FormatTo(ref sb, arg8, item.Alignment, item.StandardFormat, nameof(arg8)); break; } case 8: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + Utf8FormatHelper.FormatTo(ref sb, arg9, item.Alignment, item.StandardFormat, nameof(arg9)); break; } + default: + break; } } } @@ -2358,69 +2458,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, T8 arg8, T9 arg9, T10 arg10) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var size = Encoding.UTF8.GetMaxByteCount(item.Count); var span = sb.GetSpan(size); - var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + var count = Encoding.UTF8.GetBytes(strSpan, span); sb.Advance(count); + break; } - break; case 0: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); break; } case 1: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); break; } case 2: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); break; } case 3: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); break; } case 4: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref sb, arg5, item.Alignment, item.StandardFormat, nameof(arg5)); break; } case 5: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf8FormatHelper.FormatTo(ref sb, arg6, item.Alignment, item.StandardFormat, nameof(arg6)); break; } case 6: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf8FormatHelper.FormatTo(ref sb, arg7, item.Alignment, item.StandardFormat, nameof(arg7)); break; } case 7: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + Utf8FormatHelper.FormatTo(ref sb, arg8, item.Alignment, item.StandardFormat, nameof(arg8)); break; } case 8: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + Utf8FormatHelper.FormatTo(ref sb, arg9, item.Alignment, item.StandardFormat, nameof(arg9)); break; } case 9: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + Utf8FormatHelper.FormatTo(ref sb, arg10, item.Alignment, item.StandardFormat, nameof(arg10)); break; } + default: + break; } } } @@ -2465,74 +2569,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, T9 arg9, T10 arg10, T11 arg11) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var size = Encoding.UTF8.GetMaxByteCount(item.Count); var span = sb.GetSpan(size); - var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + var count = Encoding.UTF8.GetBytes(strSpan, span); sb.Advance(count); + break; } - break; case 0: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); break; } case 1: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); break; } case 2: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); break; } case 3: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); break; } case 4: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref sb, arg5, item.Alignment, item.StandardFormat, nameof(arg5)); break; } case 5: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf8FormatHelper.FormatTo(ref sb, arg6, item.Alignment, item.StandardFormat, nameof(arg6)); break; } case 6: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf8FormatHelper.FormatTo(ref sb, arg7, item.Alignment, item.StandardFormat, nameof(arg7)); break; } case 7: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + Utf8FormatHelper.FormatTo(ref sb, arg8, item.Alignment, item.StandardFormat, nameof(arg8)); break; } case 8: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + Utf8FormatHelper.FormatTo(ref sb, arg9, item.Alignment, item.StandardFormat, nameof(arg9)); break; } case 9: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + Utf8FormatHelper.FormatTo(ref sb, arg10, item.Alignment, item.StandardFormat, nameof(arg10)); break; } case 10: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + Utf8FormatHelper.FormatTo(ref sb, arg11, item.Alignment, item.StandardFormat, nameof(arg11)); break; } + default: + break; } } } @@ -2577,79 +2685,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, T10 arg10, T11 arg11, T12 arg12) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var size = Encoding.UTF8.GetMaxByteCount(item.Count); var span = sb.GetSpan(size); - var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + var count = Encoding.UTF8.GetBytes(strSpan, span); sb.Advance(count); + break; } - break; case 0: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); break; } case 1: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); break; } case 2: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); break; } case 3: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); break; } case 4: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref sb, arg5, item.Alignment, item.StandardFormat, nameof(arg5)); break; } case 5: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf8FormatHelper.FormatTo(ref sb, arg6, item.Alignment, item.StandardFormat, nameof(arg6)); break; } case 6: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf8FormatHelper.FormatTo(ref sb, arg7, item.Alignment, item.StandardFormat, nameof(arg7)); break; } case 7: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + Utf8FormatHelper.FormatTo(ref sb, arg8, item.Alignment, item.StandardFormat, nameof(arg8)); break; } case 8: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + Utf8FormatHelper.FormatTo(ref sb, arg9, item.Alignment, item.StandardFormat, nameof(arg9)); break; } case 9: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + Utf8FormatHelper.FormatTo(ref sb, arg10, item.Alignment, item.StandardFormat, nameof(arg10)); break; } case 10: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + Utf8FormatHelper.FormatTo(ref sb, arg11, item.Alignment, item.StandardFormat, nameof(arg11)); break; } case 11: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + Utf8FormatHelper.FormatTo(ref sb, arg12, item.Alignment, item.StandardFormat, nameof(arg12)); break; } + default: + break; } } } @@ -2694,84 +2806,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, T11 arg11, T12 arg12, T13 arg13) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var size = Encoding.UTF8.GetMaxByteCount(item.Count); var span = sb.GetSpan(size); - var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + var count = Encoding.UTF8.GetBytes(strSpan, span); sb.Advance(count); + break; } - break; case 0: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); break; } case 1: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); break; } case 2: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); break; } case 3: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); break; } case 4: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref sb, arg5, item.Alignment, item.StandardFormat, nameof(arg5)); break; } case 5: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf8FormatHelper.FormatTo(ref sb, arg6, item.Alignment, item.StandardFormat, nameof(arg6)); break; } case 6: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf8FormatHelper.FormatTo(ref sb, arg7, item.Alignment, item.StandardFormat, nameof(arg7)); break; } case 7: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + Utf8FormatHelper.FormatTo(ref sb, arg8, item.Alignment, item.StandardFormat, nameof(arg8)); break; } case 8: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + Utf8FormatHelper.FormatTo(ref sb, arg9, item.Alignment, item.StandardFormat, nameof(arg9)); break; } case 9: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + Utf8FormatHelper.FormatTo(ref sb, arg10, item.Alignment, item.StandardFormat, nameof(arg10)); break; } case 10: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + Utf8FormatHelper.FormatTo(ref sb, arg11, item.Alignment, item.StandardFormat, nameof(arg11)); break; } case 11: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + Utf8FormatHelper.FormatTo(ref sb, arg12, item.Alignment, item.StandardFormat, nameof(arg12)); break; } case 12: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg13, nameof(arg13)); + Utf8FormatHelper.FormatTo(ref sb, arg13, item.Alignment, item.StandardFormat, nameof(arg13)); break; } + default: + break; } } } @@ -2816,89 +2932,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, T12 arg12, T13 arg13, T14 arg14) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var size = Encoding.UTF8.GetMaxByteCount(item.Count); var span = sb.GetSpan(size); - var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + var count = Encoding.UTF8.GetBytes(strSpan, span); sb.Advance(count); + break; } - break; case 0: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); break; } case 1: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); break; } case 2: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); break; } case 3: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); break; } case 4: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref sb, arg5, item.Alignment, item.StandardFormat, nameof(arg5)); break; } case 5: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf8FormatHelper.FormatTo(ref sb, arg6, item.Alignment, item.StandardFormat, nameof(arg6)); break; } case 6: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf8FormatHelper.FormatTo(ref sb, arg7, item.Alignment, item.StandardFormat, nameof(arg7)); break; } case 7: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + Utf8FormatHelper.FormatTo(ref sb, arg8, item.Alignment, item.StandardFormat, nameof(arg8)); break; } case 8: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + Utf8FormatHelper.FormatTo(ref sb, arg9, item.Alignment, item.StandardFormat, nameof(arg9)); break; } case 9: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + Utf8FormatHelper.FormatTo(ref sb, arg10, item.Alignment, item.StandardFormat, nameof(arg10)); break; } case 10: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + Utf8FormatHelper.FormatTo(ref sb, arg11, item.Alignment, item.StandardFormat, nameof(arg11)); break; } case 11: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + Utf8FormatHelper.FormatTo(ref sb, arg12, item.Alignment, item.StandardFormat, nameof(arg12)); break; } case 12: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg13, nameof(arg13)); + Utf8FormatHelper.FormatTo(ref sb, arg13, item.Alignment, item.StandardFormat, nameof(arg13)); break; } case 13: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg14, nameof(arg14)); + Utf8FormatHelper.FormatTo(ref sb, arg14, item.Alignment, item.StandardFormat, nameof(arg14)); break; } + default: + break; } } } @@ -2943,94 +3063,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, T13 arg13, T14 arg14, T15 arg15) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var size = Encoding.UTF8.GetMaxByteCount(item.Count); var span = sb.GetSpan(size); - var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + var count = Encoding.UTF8.GetBytes(strSpan, span); sb.Advance(count); + break; } - break; case 0: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); break; } case 1: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); break; } case 2: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); break; } case 3: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); break; } case 4: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref sb, arg5, item.Alignment, item.StandardFormat, nameof(arg5)); break; } case 5: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf8FormatHelper.FormatTo(ref sb, arg6, item.Alignment, item.StandardFormat, nameof(arg6)); break; } case 6: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf8FormatHelper.FormatTo(ref sb, arg7, item.Alignment, item.StandardFormat, nameof(arg7)); break; } case 7: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + Utf8FormatHelper.FormatTo(ref sb, arg8, item.Alignment, item.StandardFormat, nameof(arg8)); break; } case 8: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + Utf8FormatHelper.FormatTo(ref sb, arg9, item.Alignment, item.StandardFormat, nameof(arg9)); break; } case 9: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + Utf8FormatHelper.FormatTo(ref sb, arg10, item.Alignment, item.StandardFormat, nameof(arg10)); break; } case 10: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + Utf8FormatHelper.FormatTo(ref sb, arg11, item.Alignment, item.StandardFormat, nameof(arg11)); break; } case 11: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + Utf8FormatHelper.FormatTo(ref sb, arg12, item.Alignment, item.StandardFormat, nameof(arg12)); break; } case 12: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg13, nameof(arg13)); + Utf8FormatHelper.FormatTo(ref sb, arg13, item.Alignment, item.StandardFormat, nameof(arg13)); break; } case 13: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg14, nameof(arg14)); + Utf8FormatHelper.FormatTo(ref sb, arg14, item.Alignment, item.StandardFormat, nameof(arg14)); break; } case 14: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg15, nameof(arg15)); + Utf8FormatHelper.FormatTo(ref sb, arg15, item.Alignment, item.StandardFormat, nameof(arg15)); break; } + default: + break; } } } @@ -3075,99 +3199,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, T14 arg14, T15 arg15, T16 arg16) where TBufferWriter : IBufferWriter { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); - var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); + var size = Encoding.UTF8.GetMaxByteCount(item.Count); var span = sb.GetSpan(size); - var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + var count = Encoding.UTF8.GetBytes(strSpan, span); sb.Advance(count); + break; } - break; case 0: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg1, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref sb, arg1, item.Alignment, item.StandardFormat, nameof(arg1)); break; } case 1: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg2, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref sb, arg2, item.Alignment, item.StandardFormat, nameof(arg2)); break; } case 2: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg3, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref sb, arg3, item.Alignment, item.StandardFormat, nameof(arg3)); break; } case 3: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg4, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref sb, arg4, item.Alignment, item.StandardFormat, nameof(arg4)); break; } case 4: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg5, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref sb, arg5, item.Alignment, item.StandardFormat, nameof(arg5)); break; } case 5: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg6, nameof(arg6)); + Utf8FormatHelper.FormatTo(ref sb, arg6, item.Alignment, item.StandardFormat, nameof(arg6)); break; } case 6: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg7, nameof(arg7)); + Utf8FormatHelper.FormatTo(ref sb, arg7, item.Alignment, item.StandardFormat, nameof(arg7)); break; } case 7: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg8, nameof(arg8)); + Utf8FormatHelper.FormatTo(ref sb, arg8, item.Alignment, item.StandardFormat, nameof(arg8)); break; } case 8: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg9, nameof(arg9)); + Utf8FormatHelper.FormatTo(ref sb, arg9, item.Alignment, item.StandardFormat, nameof(arg9)); break; } case 9: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg10, nameof(arg10)); + Utf8FormatHelper.FormatTo(ref sb, arg10, item.Alignment, item.StandardFormat, nameof(arg10)); break; } case 10: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg11, nameof(arg11)); + Utf8FormatHelper.FormatTo(ref sb, arg11, item.Alignment, item.StandardFormat, nameof(arg11)); break; } case 11: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg12, nameof(arg12)); + Utf8FormatHelper.FormatTo(ref sb, arg12, item.Alignment, item.StandardFormat, nameof(arg12)); break; } case 12: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg13, nameof(arg13)); + Utf8FormatHelper.FormatTo(ref sb, arg13, item.Alignment, item.StandardFormat, nameof(arg13)); break; } case 13: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg14, nameof(arg14)); + Utf8FormatHelper.FormatTo(ref sb, arg14, item.Alignment, item.StandardFormat, nameof(arg14)); break; } case 14: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg15, nameof(arg15)); + Utf8FormatHelper.FormatTo(ref sb, arg15, item.Alignment, item.StandardFormat, nameof(arg15)); break; } case 15: { - Utf8PreparedFormat.FormatTo(ref sb, item, arg16, nameof(arg16)); + Utf8FormatHelper.FormatTo(ref sb, arg16, item.Alignment, item.StandardFormat, nameof(arg16)); break; } + default: + break; } } } diff --git a/src/ZString/PreparedFormat.tt b/src/ZString/PreparedFormat.tt index b34f0cc..81af478 100644 --- a/src/ZString/PreparedFormat.tt +++ b/src/ZString/PreparedFormat.tt @@ -53,32 +53,36 @@ namespace Cysharp.Text public void FormatTo(ref TBufferWriter sb, <#= CreateParameters(i) #>) where TBufferWriter : IBufferWriter<<#= isUtf16 ? "char" : "byte" #>> { + var formatSpan = FormatString.AsSpan(); + foreach (var item in segments) { switch (item.FormatIndex) { case FormatSegment.NotFormatIndex: { - var strSpan = FormatString.AsSpan(item.Offset, item.Count); + var strSpan = formatSpan.Slice(item.Offset, item.Count); <# if(isUtf16) { #> var span = sb.GetSpan(item.Count); strSpan.TryCopyTo(span); sb.Advance(item.Count); <# } else { #> - var size = Utf8ValueStringBuilder.UTF8NoBom.GetMaxByteCount(item.Count); + var size = Encoding.UTF8.GetMaxByteCount(item.Count); var span = sb.GetSpan(size); - var count = Utf8ValueStringBuilder.UTF8NoBom.GetBytes(strSpan, span); + var count = Encoding.UTF8.GetBytes(strSpan, span); sb.Advance(count); <# } #> + break; } - break; <# for(var j = 0; j < i; j++) { #> case <#= j #>: { - <#= utf #>PreparedFormat.FormatTo(ref sb, item, arg<#= j + 1 #>, nameof(arg<#= j + 1 #>)); + <#= utf #>FormatHelper.FormatTo(ref sb, arg<#= j + 1 #>, item.Alignment, <#= isUtf16 ? "formatSpan.Slice(item.Offset, item.Count)" : "item.StandardFormat" #>, nameof(arg<#= j + 1 #>)); break; } <# } // for(j) #> + default: + break; } } } diff --git a/src/ZString/PreparedFormatHelper.cs b/src/ZString/PreparedFormatHelper.cs index 048c55d..8178f74 100644 --- a/src/ZString/PreparedFormatHelper.cs +++ b/src/ZString/PreparedFormatHelper.cs @@ -75,12 +75,14 @@ internal readonly struct FormatSegment { public const int NotFormatIndex = -1; + //public readonly bool IsFormatArgument; + public bool IsFormatArgument => FormatIndex != NotFormatIndex; + public readonly int Offset; public readonly int Count; - public bool IsFormatArgument => FormatIndex != NotFormatIndex; + public readonly int Alignment; public readonly int FormatIndex; public readonly string FormatString; - public readonly int Alignment; // Utf8 public readonly StandardFormat StandardFormat; @@ -89,6 +91,7 @@ public FormatSegment(int offset, int count, int formatIndex, string formatString { Offset = offset; Count = count; + //IsFormatArgument = formatIndex != NotFormatIndex; FormatIndex = formatIndex; FormatString = formatString; Alignment = alignment; diff --git a/src/ZString/Utf8ValueStringBuilder.cs b/src/ZString/Utf8ValueStringBuilder.cs index 2c4c822..56cfce2 100644 --- a/src/ZString/Utf8ValueStringBuilder.cs +++ b/src/ZString/Utf8ValueStringBuilder.cs @@ -13,7 +13,7 @@ public partial struct Utf8ValueStringBuilder : IDisposable, IBufferWriter, const int ThreadStaticBufferSize = 64444; const int DefaultBufferSize = 65536; // use 64K default buffer. - internal static readonly Encoding UTF8NoBom = new UTF8Encoding(false); + static Encoding UTF8NoBom = new UTF8Encoding(false); static byte newLine1; static byte newLine2; diff --git a/src/ZString/ZString.Utf8Format.cs b/src/ZString/ZString.Utf8Format.cs index 0756b1f..ce267b9 100644 --- a/src/ZString/ZString.Utf8Format.cs +++ b/src/ZString/ZString.Utf8Format.cs @@ -54,7 +54,7 @@ public static void Utf8Format(IBufferWriter bufferWriter, string forma switch (indexParse.Index) { case 0: - Utf8FormatInternal(bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); continue; default: ExceptionUtil.ThrowFormatException(); @@ -93,7 +93,6 @@ 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) { @@ -140,10 +139,10 @@ public static void Utf8Format(IBufferWriter bufferWriter, string f switch (indexParse.Index) { case 0: - Utf8FormatInternal(bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); continue; case 1: - Utf8FormatInternal(bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); continue; default: ExceptionUtil.ThrowFormatException(); @@ -182,7 +181,6 @@ 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) { @@ -229,13 +227,13 @@ public static void Utf8Format(IBufferWriter bufferWriter, stri switch (indexParse.Index) { case 0: - Utf8FormatInternal(bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); continue; case 1: - Utf8FormatInternal(bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); continue; case 2: - Utf8FormatInternal(bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); continue; default: ExceptionUtil.ThrowFormatException(); @@ -274,7 +272,6 @@ 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) { @@ -321,16 +318,16 @@ public static void Utf8Format(IBufferWriter bufferWriter, switch (indexParse.Index) { case 0: - Utf8FormatInternal(bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); continue; case 1: - Utf8FormatInternal(bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); continue; case 2: - Utf8FormatInternal(bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); continue; case 3: - Utf8FormatInternal(bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); continue; default: ExceptionUtil.ThrowFormatException(); @@ -369,7 +366,6 @@ 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) { @@ -416,19 +412,19 @@ public static void Utf8Format(IBufferWriter bufferWrit switch (indexParse.Index) { case 0: - Utf8FormatInternal(bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); continue; case 1: - Utf8FormatInternal(bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); continue; case 2: - Utf8FormatInternal(bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); continue; case 3: - Utf8FormatInternal(bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); continue; case 4: - Utf8FormatInternal(bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); continue; default: ExceptionUtil.ThrowFormatException(); @@ -467,7 +463,6 @@ 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) { @@ -514,22 +509,22 @@ public static void Utf8Format(IBufferWriter buffer switch (indexParse.Index) { case 0: - Utf8FormatInternal(bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); continue; case 1: - Utf8FormatInternal(bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); continue; case 2: - Utf8FormatInternal(bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); continue; case 3: - Utf8FormatInternal(bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); continue; case 4: - Utf8FormatInternal(bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); continue; case 5: - Utf8FormatInternal(bufferWriter, arg6, indexParse.Alignment, writeFormat, nameof(arg6)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg6, indexParse.Alignment, writeFormat, nameof(arg6)); continue; default: ExceptionUtil.ThrowFormatException(); @@ -568,7 +563,6 @@ 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) { @@ -615,25 +609,25 @@ public static void Utf8Format(IBufferWriter bu switch (indexParse.Index) { case 0: - Utf8FormatInternal(bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); continue; case 1: - Utf8FormatInternal(bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); continue; case 2: - Utf8FormatInternal(bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); continue; case 3: - Utf8FormatInternal(bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); continue; case 4: - Utf8FormatInternal(bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); continue; case 5: - Utf8FormatInternal(bufferWriter, arg6, indexParse.Alignment, writeFormat, nameof(arg6)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg6, indexParse.Alignment, writeFormat, nameof(arg6)); continue; case 6: - Utf8FormatInternal(bufferWriter, arg7, indexParse.Alignment, writeFormat, nameof(arg7)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg7, indexParse.Alignment, writeFormat, nameof(arg7)); continue; default: ExceptionUtil.ThrowFormatException(); @@ -672,7 +666,6 @@ 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) { @@ -719,28 +712,28 @@ public static void Utf8Format(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) { @@ -826,31 +818,31 @@ public static void Utf8Format(IBufferWriter< switch (indexParse.Index) { case 0: - Utf8FormatInternal(bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); continue; case 1: - Utf8FormatInternal(bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); continue; case 2: - Utf8FormatInternal(bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); continue; case 3: - Utf8FormatInternal(bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); continue; case 4: - Utf8FormatInternal(bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); continue; case 5: - Utf8FormatInternal(bufferWriter, arg6, indexParse.Alignment, writeFormat, nameof(arg6)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg6, indexParse.Alignment, writeFormat, nameof(arg6)); continue; case 6: - Utf8FormatInternal(bufferWriter, arg7, indexParse.Alignment, writeFormat, nameof(arg7)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg7, indexParse.Alignment, writeFormat, nameof(arg7)); continue; case 7: - Utf8FormatInternal(bufferWriter, arg8, indexParse.Alignment, writeFormat, nameof(arg8)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg8, indexParse.Alignment, writeFormat, nameof(arg8)); continue; case 8: - Utf8FormatInternal(bufferWriter, arg9, indexParse.Alignment, writeFormat, nameof(arg9)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg9, indexParse.Alignment, writeFormat, nameof(arg9)); continue; default: ExceptionUtil.ThrowFormatException(); @@ -889,7 +881,6 @@ 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) { @@ -936,34 +927,34 @@ public static void Utf8Format(IBufferWr switch (indexParse.Index) { case 0: - Utf8FormatInternal(bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); continue; case 1: - Utf8FormatInternal(bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); continue; case 2: - Utf8FormatInternal(bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); continue; case 3: - Utf8FormatInternal(bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); continue; case 4: - Utf8FormatInternal(bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); continue; case 5: - Utf8FormatInternal(bufferWriter, arg6, indexParse.Alignment, writeFormat, nameof(arg6)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg6, indexParse.Alignment, writeFormat, nameof(arg6)); continue; case 6: - Utf8FormatInternal(bufferWriter, arg7, indexParse.Alignment, writeFormat, nameof(arg7)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg7, indexParse.Alignment, writeFormat, nameof(arg7)); continue; case 7: - Utf8FormatInternal(bufferWriter, arg8, indexParse.Alignment, writeFormat, nameof(arg8)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg8, indexParse.Alignment, writeFormat, nameof(arg8)); continue; case 8: - Utf8FormatInternal(bufferWriter, arg9, indexParse.Alignment, writeFormat, nameof(arg9)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg9, indexParse.Alignment, writeFormat, nameof(arg9)); continue; case 9: - Utf8FormatInternal(bufferWriter, arg10, indexParse.Alignment, writeFormat, nameof(arg10)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg10, indexParse.Alignment, writeFormat, nameof(arg10)); continue; default: ExceptionUtil.ThrowFormatException(); @@ -1002,7 +993,6 @@ 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) { @@ -1049,37 +1039,37 @@ public static void Utf8Format(IBuf switch (indexParse.Index) { case 0: - Utf8FormatInternal(bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); continue; case 1: - Utf8FormatInternal(bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); continue; case 2: - Utf8FormatInternal(bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); continue; case 3: - Utf8FormatInternal(bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); continue; case 4: - Utf8FormatInternal(bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); continue; case 5: - Utf8FormatInternal(bufferWriter, arg6, indexParse.Alignment, writeFormat, nameof(arg6)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg6, indexParse.Alignment, writeFormat, nameof(arg6)); continue; case 6: - Utf8FormatInternal(bufferWriter, arg7, indexParse.Alignment, writeFormat, nameof(arg7)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg7, indexParse.Alignment, writeFormat, nameof(arg7)); continue; case 7: - Utf8FormatInternal(bufferWriter, arg8, indexParse.Alignment, writeFormat, nameof(arg8)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg8, indexParse.Alignment, writeFormat, nameof(arg8)); continue; case 8: - Utf8FormatInternal(bufferWriter, arg9, indexParse.Alignment, writeFormat, nameof(arg9)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg9, indexParse.Alignment, writeFormat, nameof(arg9)); continue; case 9: - Utf8FormatInternal(bufferWriter, arg10, indexParse.Alignment, writeFormat, nameof(arg10)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg10, indexParse.Alignment, writeFormat, nameof(arg10)); continue; case 10: - Utf8FormatInternal(bufferWriter, arg11, indexParse.Alignment, writeFormat, nameof(arg11)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg11, indexParse.Alignment, writeFormat, nameof(arg11)); continue; default: ExceptionUtil.ThrowFormatException(); @@ -1118,7 +1108,6 @@ 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) { @@ -1165,40 +1154,40 @@ public static void Utf8Format switch (indexParse.Index) { case 0: - Utf8FormatInternal(bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg1, indexParse.Alignment, writeFormat, nameof(arg1)); continue; case 1: - Utf8FormatInternal(bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg2, indexParse.Alignment, writeFormat, nameof(arg2)); continue; case 2: - Utf8FormatInternal(bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg3, indexParse.Alignment, writeFormat, nameof(arg3)); continue; case 3: - Utf8FormatInternal(bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg4, indexParse.Alignment, writeFormat, nameof(arg4)); continue; case 4: - Utf8FormatInternal(bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg5, indexParse.Alignment, writeFormat, nameof(arg5)); continue; case 5: - Utf8FormatInternal(bufferWriter, arg6, indexParse.Alignment, writeFormat, nameof(arg6)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg6, indexParse.Alignment, writeFormat, nameof(arg6)); continue; case 6: - Utf8FormatInternal(bufferWriter, arg7, indexParse.Alignment, writeFormat, nameof(arg7)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg7, indexParse.Alignment, writeFormat, nameof(arg7)); continue; case 7: - Utf8FormatInternal(bufferWriter, arg8, indexParse.Alignment, writeFormat, nameof(arg8)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg8, indexParse.Alignment, writeFormat, nameof(arg8)); continue; case 8: - Utf8FormatInternal(bufferWriter, arg9, indexParse.Alignment, writeFormat, nameof(arg9)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg9, indexParse.Alignment, writeFormat, nameof(arg9)); continue; case 9: - Utf8FormatInternal(bufferWriter, arg10, indexParse.Alignment, writeFormat, nameof(arg10)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg10, indexParse.Alignment, writeFormat, nameof(arg10)); continue; case 10: - Utf8FormatInternal(bufferWriter, arg11, indexParse.Alignment, writeFormat, nameof(arg11)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg11, indexParse.Alignment, writeFormat, nameof(arg11)); continue; case 11: - Utf8FormatInternal(bufferWriter, arg12, indexParse.Alignment, writeFormat, nameof(arg12)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg12, indexParse.Alignment, writeFormat, nameof(arg12)); continue; default: ExceptionUtil.ThrowFormatException(); @@ -1237,7 +1226,6 @@ 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) { @@ -1284,43 +1272,43 @@ 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) { @@ -1406,46 +1393,46 @@ 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) { @@ -1531,49 +1517,49 @@ 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) { @@ -1659,52 +1644,52 @@ public static void Utf8Format(IBufferWriter bufferWriter, T arg, int width, StandardFormat format, string argName) - { - if (width <= 0) // leftJustify - { - width *= -1; - - var buffer = bufferWriter.GetSpan(); - - if (!FormatterCache.TryFormatDelegate(arg, buffer, out var written, format)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - - if (!FormatterCache.TryFormatDelegate(arg, buffer, out written, format)) - { - ExceptionUtil.ThrowArgumentException(argName); - } - } - - bufferWriter.Advance(written); - - int padding = width - written; - if (width > 0 && padding > 0) - { - // TODO Append(' ', padding); - bufferWriter.GetSpan(padding).Fill((byte)' '); // TODO Fill Method is too slow. - bufferWriter.Advance(padding); - } - } - else // rightJustify - { - if (typeof(T) == typeof(string)) - { - var s = Unsafe.As(arg); - int padding = width - s.Length; - if (padding > 0) - { - // TODO Append(' ', padding); - bufferWriter.GetSpan(padding).Fill((byte)' '); // TODO Fill Method is too slow. - bufferWriter.Advance(padding); - } - - ZString.AppendChars(ref bufferWriter, s.AsSpan()); - } - 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)) - { - ExceptionUtil.ThrowArgumentException(argName); - } - } - - int padding = width - charsWritten; - if (padding > 0) - { - // TODO Append(' ', padding); - bufferWriter.GetSpan(padding).Fill((byte)' '); // TODO Fill Method is too slow. - bufferWriter.Advance(padding); - } - - s.CopyTo(bufferWriter.GetSpan(charsWritten)); - bufferWriter.Advance(charsWritten); - } - } - } - } } diff --git a/src/ZString/ZString.Utf8Format.tt b/src/ZString/ZString.Utf8Format.tt index 28c4129..4382e5c 100644 --- a/src/ZString/ZString.Utf8Format.tt +++ b/src/ZString/ZString.Utf8Format.tt @@ -63,7 +63,7 @@ namespace Cysharp.Text { <# for(var j = 0; j < i; j++) { #> case <#= j #>: - Utf8FormatInternal(bufferWriter, arg<#= j + 1 #>, indexParse.Alignment, writeFormat, nameof(arg<#= j + 1 #>)); + Utf8FormatHelper.FormatTo(ref bufferWriter, arg<#= j + 1 #>, indexParse.Alignment, writeFormat, nameof(arg<#= j + 1 #>)); continue; <# } #> default: @@ -103,79 +103,6 @@ namespace Cysharp.Text } } } - <# } #> - - static void Utf8FormatInternal(IBufferWriter bufferWriter, T arg, int width, StandardFormat format, string argName) - { - if (width <= 0) // leftJustify - { - width *= -1; - - var buffer = bufferWriter.GetSpan(); - - if (!FormatterCache.TryFormatDelegate(arg, buffer, out var written, format)) - { - bufferWriter.Advance(0); - buffer = bufferWriter.GetSpan(Math.Max(buffer.Length + 1, written)); - - if (!FormatterCache.TryFormatDelegate(arg, buffer, out written, format)) - { - ExceptionUtil.ThrowArgumentException(argName); - } - } - - bufferWriter.Advance(written); - - int padding = width - written; - if (width > 0 && padding > 0) - { - // TODO Append(' ', padding); - bufferWriter.GetSpan(padding).Fill((byte)' '); // TODO Fill Method is too slow. - bufferWriter.Advance(padding); - } - } - else // rightJustify - { - if (typeof(T) == typeof(string)) - { - var s = Unsafe.As(arg); - int padding = width - s.Length; - if (padding > 0) - { - // TODO Append(' ', padding); - bufferWriter.GetSpan(padding).Fill((byte)' '); // TODO Fill Method is too slow. - bufferWriter.Advance(padding); - } - - ZString.AppendChars(ref bufferWriter, s.AsSpan()); - } - 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)) - { - ExceptionUtil.ThrowArgumentException(argName); - } - } - - int padding = width - charsWritten; - if (padding > 0) - { - // TODO Append(' ', padding); - bufferWriter.GetSpan(padding).Fill((byte)' '); // TODO Fill Method is too slow. - bufferWriter.Advance(padding); - } - - s.CopyTo(bufferWriter.GetSpan(charsWritten)); - bufferWriter.Advance(charsWritten); - } - } - } - } } From f790e99555efa4c6572945011e8d4b8b3dfc8fed Mon Sep 17 00:00:00 2001 From: udaken Date: Thu, 6 Aug 2020 21:41:31 +0900 Subject: [PATCH 6/6] Optimize PreparedFormat --- .../Assets/Scripts/ZString/PreparedFormat.cs | 352 +++++++++--------- .../Scripts/ZString/PreparedFormatHelper.cs | 115 ++++-- src/ZString/PreparedFormat.cs | 352 +++++++++--------- src/ZString/PreparedFormat.tt | 20 +- src/ZString/PreparedFormatHelper.cs | 115 ++++-- 5 files changed, 548 insertions(+), 406 deletions(-) diff --git a/src/ZString.Unity/Assets/Scripts/ZString/PreparedFormat.cs b/src/ZString.Unity/Assets/Scripts/ZString/PreparedFormat.cs index ae9a297..955c5ca 100644 --- a/src/ZString.Unity/Assets/Scripts/ZString/PreparedFormat.cs +++ b/src/ZString.Unity/Assets/Scripts/ZString/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) @@ -50,7 +50,7 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1) { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf16FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); @@ -74,12 +74,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) @@ -115,7 +115,7 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2) { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf16FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); @@ -144,12 +144,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) @@ -185,7 +185,7 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf16FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); @@ -219,12 +219,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) @@ -260,7 +260,7 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf16FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); @@ -299,12 +299,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) @@ -340,7 +340,7 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf16FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); @@ -384,12 +384,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) @@ -425,7 +425,7 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf16FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); @@ -474,12 +474,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) @@ -515,7 +515,7 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf16FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); @@ -569,12 +569,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) @@ -610,7 +610,7 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf16FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); @@ -669,12 +669,12 @@ public sealed partial class Utf16PreparedFormat(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf16FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); @@ -774,12 +774,12 @@ public sealed partial class Utf16PreparedFormat(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf16FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); @@ -884,12 +884,12 @@ public sealed partial class Utf16PreparedFormat(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf16FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); @@ -999,12 +999,12 @@ public sealed partial class Utf16PreparedFormat(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf16FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); @@ -1119,12 +1119,12 @@ public sealed partial class Utf16PreparedFormat(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf16FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); @@ -1244,12 +1244,12 @@ public sealed partial class Utf16PreparedFormat(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf16FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); @@ -1374,12 +1374,12 @@ public sealed partial class Utf16PreparedFormat(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf16FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); @@ -1509,12 +1509,12 @@ public sealed partial class Utf16PreparedFormat(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf16FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); @@ -1649,12 +1649,13 @@ 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) @@ -1684,19 +1685,18 @@ public string Format(T1 arg1) public void FormatTo(ref TBufferWriter sb, T1 arg1) where TBufferWriter : IBufferWriter { - var formatSpan = FormatString.AsSpan(); + var formatSpan = utf8PreEncodedbuffer.AsSpan(); foreach (var item in segments) { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf8FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(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); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); break; } case 0: @@ -1715,12 +1715,13 @@ 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) @@ -1750,19 +1751,18 @@ public string Format(T1 arg1, T2 arg2) public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2) where TBufferWriter : IBufferWriter { - var formatSpan = FormatString.AsSpan(); + var formatSpan = utf8PreEncodedbuffer.AsSpan(); foreach (var item in segments) { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf8FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(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); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); break; } case 0: @@ -1786,12 +1786,13 @@ 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) @@ -1821,19 +1822,18 @@ 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(); + var formatSpan = utf8PreEncodedbuffer.AsSpan(); foreach (var item in segments) { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf8FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(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); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); break; } case 0: @@ -1862,12 +1862,13 @@ 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) @@ -1897,19 +1898,18 @@ 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(); + var formatSpan = utf8PreEncodedbuffer.AsSpan(); foreach (var item in segments) { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf8FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(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); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); break; } case 0: @@ -1943,12 +1943,13 @@ 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) @@ -1978,19 +1979,18 @@ 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(); + var formatSpan = utf8PreEncodedbuffer.AsSpan(); foreach (var item in segments) { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf8FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(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); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); break; } case 0: @@ -2029,12 +2029,13 @@ 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) @@ -2064,19 +2065,18 @@ 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(); + var formatSpan = utf8PreEncodedbuffer.AsSpan(); foreach (var item in segments) { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf8FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(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); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); break; } case 0: @@ -2120,12 +2120,13 @@ 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) @@ -2155,19 +2156,18 @@ 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(); + var formatSpan = utf8PreEncodedbuffer.AsSpan(); foreach (var item in segments) { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf8FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(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); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); break; } case 0: @@ -2216,12 +2216,13 @@ 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) @@ -2251,19 +2252,18 @@ 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(); + var formatSpan = utf8PreEncodedbuffer.AsSpan(); foreach (var item in segments) { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf8FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(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); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); break; } case 0: @@ -2317,12 +2317,13 @@ public sealed partial class Utf8PreparedFormat(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(); + var formatSpan = utf8PreEncodedbuffer.AsSpan(); foreach (var item in segments) { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf8FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(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); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); break; } case 0: @@ -2423,12 +2423,13 @@ public sealed partial class Utf8PreparedFormat(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(); + var formatSpan = utf8PreEncodedbuffer.AsSpan(); foreach (var item in segments) { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf8FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(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); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); break; } case 0: @@ -2534,12 +2534,13 @@ public sealed partial class Utf8PreparedFormat(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(); + var formatSpan = utf8PreEncodedbuffer.AsSpan(); foreach (var item in segments) { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf8FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(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); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); break; } case 0: @@ -2650,12 +2650,13 @@ public sealed partial class Utf8PreparedFormat(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(); + var formatSpan = utf8PreEncodedbuffer.AsSpan(); foreach (var item in segments) { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf8FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(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); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); break; } case 0: @@ -2771,12 +2771,13 @@ public sealed partial class Utf8PreparedFormat(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(); + var formatSpan = utf8PreEncodedbuffer.AsSpan(); foreach (var item in segments) { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf8FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(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); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); break; } case 0: @@ -2897,12 +2897,13 @@ public sealed partial class Utf8PreparedFormat(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(); + var formatSpan = utf8PreEncodedbuffer.AsSpan(); foreach (var item in segments) { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf8FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(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); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); break; } case 0: @@ -3028,12 +3028,13 @@ public sealed partial class Utf8PreparedFormat(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(); + var formatSpan = utf8PreEncodedbuffer.AsSpan(); foreach (var item in segments) { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf8FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(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); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); break; } case 0: @@ -3164,12 +3164,13 @@ public sealed partial class Utf8PreparedFormat(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(); + var formatSpan = utf8PreEncodedbuffer.AsSpan(); foreach (var item in segments) { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf8FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(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); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); break; } case 0: diff --git a/src/ZString.Unity/Assets/Scripts/ZString/PreparedFormatHelper.cs b/src/ZString.Unity/Assets/Scripts/ZString/PreparedFormatHelper.cs index 8178f74..18d808d 100644 --- a/src/ZString.Unity/Assets/Scripts/ZString/PreparedFormatHelper.cs +++ b/src/ZString.Unity/Assets/Scripts/ZString/PreparedFormatHelper.cs @@ -1,20 +1,20 @@ using System; using System.Buffers; using System.Collections.Generic; -using System.Diagnostics; +using System.Text; namespace Cysharp.Text { internal static class PreparedFormatHelper { - internal static FormatSegment[] Parse(string format, bool withStandardFormat) + internal static Utf16FormatSegment[] Utf16Parse(string format) { if (format == null) { throw new ArgumentNullException(nameof(format)); } - var list = new List(); + var list = new List(); int i = 0; int len = format.Length; @@ -42,7 +42,7 @@ internal static FormatSegment[] Parse(string format, bool withStandardFormat) if (size != 0) { - list.Add(new FormatSegment(copyFrom, size, FormatSegment.NotFormatIndex, format, default, 0)); + list.Add(new Utf16FormatSegment(copyFrom, size, Utf16FormatSegment.NotFormatIndex, 0)); } copyFrom = i; @@ -63,46 +63,117 @@ internal static FormatSegment[] Parse(string format, bool withStandardFormat) copyFrom = indexParse.LastIndex; // continue after '}' i = indexParse.LastIndex; - list.Add(new FormatSegment(indexParse.LastIndex - indexParse.FormatString.Length - 1, indexParse.FormatString.Length, indexParse.Index, format, withStandardFormat, indexParse.Alignment)); + 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) + { + var parserScanResult = FormatParser.ScanFormatString(formatSpan, ref i); + + if (ParserScanResult.NormalChar == parserScanResult && i < len) + { + // 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; + } + } + + if (i >= len) + { + 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 FormatSegment + internal readonly struct Utf8FormatSegment { public const int NotFormatIndex = -1; - //public readonly bool IsFormatArgument; + 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 Utf16FormatSegment + { + public const int NotFormatIndex = -1; + public readonly int Offset; public readonly int Count; - public readonly int Alignment; 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, int formatIndex, string formatString, bool utf8, int alignment) + public Utf16FormatSegment(int offset, int count, int formatIndex, int alignment) { Offset = offset; Count = count; - //IsFormatArgument = formatIndex != NotFormatIndex; FormatIndex = formatIndex; - FormatString = formatString; Alignment = alignment; - if (utf8) - { - StandardFormat = (formatString != null) ? StandardFormat.Parse(formatString.AsSpan(Offset, Count)) : default; - } - else - { - StandardFormat = default; - } } } } diff --git a/src/ZString/PreparedFormat.cs b/src/ZString/PreparedFormat.cs index ae9a297..955c5ca 100644 --- a/src/ZString/PreparedFormat.cs +++ b/src/ZString/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) @@ -50,7 +50,7 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1) { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf16FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); @@ -74,12 +74,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) @@ -115,7 +115,7 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2) { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf16FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); @@ -144,12 +144,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) @@ -185,7 +185,7 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf16FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); @@ -219,12 +219,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) @@ -260,7 +260,7 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf16FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); @@ -299,12 +299,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) @@ -340,7 +340,7 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf16FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); @@ -384,12 +384,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) @@ -425,7 +425,7 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf16FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); @@ -474,12 +474,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) @@ -515,7 +515,7 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf16FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); @@ -569,12 +569,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) @@ -610,7 +610,7 @@ public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf16FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); @@ -669,12 +669,12 @@ public sealed partial class Utf16PreparedFormat(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf16FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); @@ -774,12 +774,12 @@ public sealed partial class Utf16PreparedFormat(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf16FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); @@ -884,12 +884,12 @@ public sealed partial class Utf16PreparedFormat(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf16FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); @@ -999,12 +999,12 @@ public sealed partial class Utf16PreparedFormat(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf16FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); @@ -1119,12 +1119,12 @@ public sealed partial class Utf16PreparedFormat(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf16FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); @@ -1244,12 +1244,12 @@ public sealed partial class Utf16PreparedFormat(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf16FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); @@ -1374,12 +1374,12 @@ public sealed partial class Utf16PreparedFormat(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf16FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); @@ -1509,12 +1509,12 @@ public sealed partial class Utf16PreparedFormat(ref TBufferWriter sb, T1 arg1, T2 arg2, T3 a { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf16FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(item.Offset, item.Count); var span = sb.GetSpan(item.Count); @@ -1649,12 +1649,13 @@ 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) @@ -1684,19 +1685,18 @@ public string Format(T1 arg1) public void FormatTo(ref TBufferWriter sb, T1 arg1) where TBufferWriter : IBufferWriter { - var formatSpan = FormatString.AsSpan(); + var formatSpan = utf8PreEncodedbuffer.AsSpan(); foreach (var item in segments) { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf8FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(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); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); break; } case 0: @@ -1715,12 +1715,13 @@ 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) @@ -1750,19 +1751,18 @@ public string Format(T1 arg1, T2 arg2) public void FormatTo(ref TBufferWriter sb, T1 arg1, T2 arg2) where TBufferWriter : IBufferWriter { - var formatSpan = FormatString.AsSpan(); + var formatSpan = utf8PreEncodedbuffer.AsSpan(); foreach (var item in segments) { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf8FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(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); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); break; } case 0: @@ -1786,12 +1786,13 @@ 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) @@ -1821,19 +1822,18 @@ 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(); + var formatSpan = utf8PreEncodedbuffer.AsSpan(); foreach (var item in segments) { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf8FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(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); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); break; } case 0: @@ -1862,12 +1862,13 @@ 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) @@ -1897,19 +1898,18 @@ 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(); + var formatSpan = utf8PreEncodedbuffer.AsSpan(); foreach (var item in segments) { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf8FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(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); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); break; } case 0: @@ -1943,12 +1943,13 @@ 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) @@ -1978,19 +1979,18 @@ 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(); + var formatSpan = utf8PreEncodedbuffer.AsSpan(); foreach (var item in segments) { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf8FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(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); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); break; } case 0: @@ -2029,12 +2029,13 @@ 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) @@ -2064,19 +2065,18 @@ 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(); + var formatSpan = utf8PreEncodedbuffer.AsSpan(); foreach (var item in segments) { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf8FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(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); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); break; } case 0: @@ -2120,12 +2120,13 @@ 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) @@ -2155,19 +2156,18 @@ 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(); + var formatSpan = utf8PreEncodedbuffer.AsSpan(); foreach (var item in segments) { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf8FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(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); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); break; } case 0: @@ -2216,12 +2216,13 @@ 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) @@ -2251,19 +2252,18 @@ 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(); + var formatSpan = utf8PreEncodedbuffer.AsSpan(); foreach (var item in segments) { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf8FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(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); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); break; } case 0: @@ -2317,12 +2317,13 @@ public sealed partial class Utf8PreparedFormat(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(); + var formatSpan = utf8PreEncodedbuffer.AsSpan(); foreach (var item in segments) { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf8FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(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); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); break; } case 0: @@ -2423,12 +2423,13 @@ public sealed partial class Utf8PreparedFormat(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(); + var formatSpan = utf8PreEncodedbuffer.AsSpan(); foreach (var item in segments) { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf8FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(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); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); break; } case 0: @@ -2534,12 +2534,13 @@ public sealed partial class Utf8PreparedFormat(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(); + var formatSpan = utf8PreEncodedbuffer.AsSpan(); foreach (var item in segments) { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf8FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(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); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); break; } case 0: @@ -2650,12 +2650,13 @@ public sealed partial class Utf8PreparedFormat(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(); + var formatSpan = utf8PreEncodedbuffer.AsSpan(); foreach (var item in segments) { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf8FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(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); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); break; } case 0: @@ -2771,12 +2771,13 @@ public sealed partial class Utf8PreparedFormat(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(); + var formatSpan = utf8PreEncodedbuffer.AsSpan(); foreach (var item in segments) { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf8FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(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); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); break; } case 0: @@ -2897,12 +2897,13 @@ public sealed partial class Utf8PreparedFormat(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(); + var formatSpan = utf8PreEncodedbuffer.AsSpan(); foreach (var item in segments) { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf8FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(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); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); break; } case 0: @@ -3028,12 +3028,13 @@ public sealed partial class Utf8PreparedFormat(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(); + var formatSpan = utf8PreEncodedbuffer.AsSpan(); foreach (var item in segments) { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf8FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(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); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); break; } case 0: @@ -3164,12 +3164,13 @@ public sealed partial class Utf8PreparedFormat(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(); + var formatSpan = utf8PreEncodedbuffer.AsSpan(); foreach (var item in segments) { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case Utf8FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(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); + var span = sb.GetSpan(item.Count); + strSpan.TryCopyTo(span); + sb.Advance(item.Count); break; } case 0: diff --git a/src/ZString/PreparedFormat.tt b/src/ZString/PreparedFormat.tt index 81af478..19f724a 100644 --- a/src/ZString/PreparedFormat.tt +++ b/src/ZString/PreparedFormat.tt @@ -18,12 +18,15 @@ namespace Cysharp.Text public string FormatString { get; } public int MinSize { get; } - readonly FormatSegment[] segments; + readonly <#= utf #>FormatSegment[] segments; +<# if(!isUtf16) { #> + readonly byte[] utf8PreEncodedbuffer; +<# } #> public <#= utf #>PreparedFormat(string format) { this.FormatString = format; - this.segments = PreparedFormatHelper.Parse(format, <#= (utf == "Utf16" ? false : true).ToString().ToLower() #>); + this.segments = PreparedFormatHelper.<#= utf #>Parse(format<#= (!isUtf16 ? ", out utf8PreEncodedbuffer" : "") #>); var size = 0; foreach (var item in segments) @@ -53,25 +56,22 @@ namespace Cysharp.Text public void FormatTo(ref TBufferWriter sb, <#= CreateParameters(i) #>) where TBufferWriter : IBufferWriter<<#= isUtf16 ? "char" : "byte" #>> { +<# if(isUtf16) { #> var formatSpan = FormatString.AsSpan(); +<# } else { #> + var formatSpan = utf8PreEncodedbuffer.AsSpan(); +<# } #> foreach (var item in segments) { switch (item.FormatIndex) { - case FormatSegment.NotFormatIndex: + case <#= utf #>FormatSegment.NotFormatIndex: { var strSpan = formatSpan.Slice(item.Offset, item.Count); -<# if(isUtf16) { #> var span = sb.GetSpan(item.Count); strSpan.TryCopyTo(span); sb.Advance(item.Count); -<# } else { #> - var size = Encoding.UTF8.GetMaxByteCount(item.Count); - var span = sb.GetSpan(size); - var count = Encoding.UTF8.GetBytes(strSpan, span); - sb.Advance(count); -<# } #> break; } <# for(var j = 0; j < i; j++) { #> diff --git a/src/ZString/PreparedFormatHelper.cs b/src/ZString/PreparedFormatHelper.cs index 8178f74..18d808d 100644 --- a/src/ZString/PreparedFormatHelper.cs +++ b/src/ZString/PreparedFormatHelper.cs @@ -1,20 +1,20 @@ using System; using System.Buffers; using System.Collections.Generic; -using System.Diagnostics; +using System.Text; namespace Cysharp.Text { internal static class PreparedFormatHelper { - internal static FormatSegment[] Parse(string format, bool withStandardFormat) + internal static Utf16FormatSegment[] Utf16Parse(string format) { if (format == null) { throw new ArgumentNullException(nameof(format)); } - var list = new List(); + var list = new List(); int i = 0; int len = format.Length; @@ -42,7 +42,7 @@ internal static FormatSegment[] Parse(string format, bool withStandardFormat) if (size != 0) { - list.Add(new FormatSegment(copyFrom, size, FormatSegment.NotFormatIndex, format, default, 0)); + list.Add(new Utf16FormatSegment(copyFrom, size, Utf16FormatSegment.NotFormatIndex, 0)); } copyFrom = i; @@ -63,46 +63,117 @@ internal static FormatSegment[] Parse(string format, bool withStandardFormat) copyFrom = indexParse.LastIndex; // continue after '}' i = indexParse.LastIndex; - list.Add(new FormatSegment(indexParse.LastIndex - indexParse.FormatString.Length - 1, indexParse.FormatString.Length, indexParse.Index, format, withStandardFormat, indexParse.Alignment)); + 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) + { + var parserScanResult = FormatParser.ScanFormatString(formatSpan, ref i); + + if (ParserScanResult.NormalChar == parserScanResult && i < len) + { + // 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; + } + } + + if (i >= len) + { + 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 FormatSegment + internal readonly struct Utf8FormatSegment { public const int NotFormatIndex = -1; - //public readonly bool IsFormatArgument; + 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 Utf16FormatSegment + { + public const int NotFormatIndex = -1; + public readonly int Offset; public readonly int Count; - public readonly int Alignment; 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, int formatIndex, string formatString, bool utf8, int alignment) + public Utf16FormatSegment(int offset, int count, int formatIndex, int alignment) { Offset = offset; Count = count; - //IsFormatArgument = formatIndex != NotFormatIndex; FormatIndex = formatIndex; - FormatString = formatString; Alignment = alignment; - if (utf8) - { - StandardFormat = (formatString != null) ? StandardFormat.Parse(formatString.AsSpan(Offset, Count)) : default; - } - else - { - StandardFormat = default; - } } } }