public void Utf8DisposeTest() { var sb = ZString.CreateUtf8StringBuilder(); sb.Dispose(); sb.Dispose(); // call more than once }
private async Task InternalAsyncTask(IList <LogEventInfo> logEvents, CancellationToken cancellationToken) { if (logEvents.Count == 0) { InternalLogger.Debug("log event is empty."); return; } InternalLogger.Debug("# of Sending log events: {0}", logEvents.Count); using var sb = ZString.CreateUtf8StringBuilder(); sb.Append("[{\"logs\": ["); var delimStart = string.Empty; foreach (var logEvent in logEvents) { sb.Append(delimStart); if (EscapeJson) { sb.Append("{\"timestamp\": "); sb.Append(new DateTimeOffset(logEvent.TimeStamp).ToUnixTimeMilliseconds()); sb.Append(",\"message\": \""); sb.Append(JsonEncodedText.Encode(Layout.Render(logEvent))); sb.Append("\"}"); } else { sb.Append(Layout.Render(logEvent)); } delimStart = ","; } sb.Append("]}]"); await SendLogStringBuilder(sb, cancellationToken).ConfigureAwait(false); }
public int Utf8StringBuilderAppendFormat_() { using var zsh = ZString.CreateUtf8StringBuilder(); zsh.AppendFormat(_format, _byte, _dt, _dto, _decimal, _double, _guid, _short, _float, _ts, _uint, _ulong, _null, _string, _bool, _enum, _char); return(zsh.Length); }
static void Utf8 <T>(T e) where T : Enum { var s = ZString.CreateUtf8StringBuilder(); s.AppendFormat("{0}", e); s.AsSpan().SequenceEqual(Encoding.UTF8.GetBytes(e.ToString())).Should().BeTrue(); }
public void ZStringUse() { using var Builder = ZString.CreateUtf8StringBuilder(); for (var x = 0; x < Count; ++x) { Builder.Append("Testing this"); Builder.AppendFormat(" out {0}", 12); Builder.AppendLine("Blah"); } _ = Builder.ToString(); }
public int ZStringBuilderUtf8() { using var sb = ZString.CreateUtf8StringBuilder(); sb.AppendFormat("{{ {0}: {1}, {2}, {3} }} - {4}", "double", dValues[0], dValues[1], dValues[2], 1); sb.AppendLine(); sb.AppendFormat("{{ {0}: {1}, {2}, {3} }} - {4}", "float", fValues[0], fValues[1], fValues[2], 2); sb.AppendLine(); sb.AppendFormat("{{ {0}: {1}, {2}, {3} }} - {4}", "decimal", mValues[0], mValues[1], mValues[2], 3); sb.AppendLine(); return(sb.Length); }
static void Main(string[] args) { var x = ZString.CreateStringBuilder(); var sb = new StringBuilder(); x.AppendFormat("hoge{0:.##}, tako{1:000}", 123.456, 9); Console.WriteLine(x.ToString()); var utf7 = ZString.CreateUtf8StringBuilder(); }
static void Run() { using var sb = ZString.CreateUtf8StringBuilder(); IBufferWriter <byte> boxed = sb; var writer = new Utf8JsonWriter(boxed); JsonSerializer.Serialize(writer, new { foo = 999 }); using var unboxed = (Utf8ValueStringBuilder)boxed; Console.WriteLine(sb.ToString()); Console.WriteLine(unboxed.ToString()); }
public static string FormatToString(this IZLoggerEntry entry, ZLoggerOptions options, Utf8JsonWriter?jsonWriter) { var boxedBuilder = (IBufferWriter <byte>)ZString.CreateUtf8StringBuilder(); try { entry.FormatUtf8(boxedBuilder, options, jsonWriter); return(boxedBuilder.ToString() !); } finally { ((Utf8ValueStringBuilder)boxedBuilder).Dispose(); } }
void Test <T0, T1>(string format, T0 t0, T1 t1) { { var actual = ZString.Format(format, t0, t1); var expected = string.Format(format, t0, t1); actual.Should().Be(expected); } { var sb = ZString.CreateUtf8StringBuilder(); sb.AppendFormat(format, t0, t1); var actual = sb.ToString(); var expected = string.Format(format, t0, t1); actual.Should().Be(expected); } }
public void NotNestedUtf8() { using (_ = ZString.CreateUtf8StringBuilder(true)) { } using (_ = ZString.CreateUtf8StringBuilder(false)) { using (_ = ZString.CreateUtf8StringBuilder(true)) { using (_ = ZString.CreateUtf8StringBuilder(false)) { } } } }
public void ZStringUtf8() { for (int i = 0; i < COUNT; i++) { using (var sb = ZString.CreateUtf8StringBuilder()) { for (int j = 0; j < strings.Count; j++) { sb.Append(strings[j]); } _ = sb.AsSpan().ToArray(); } } }
public void NestedUtf8() { using (_ = ZString.CreateUtf8StringBuilder(true)) { using (_ = ZString.CreateUtf8StringBuilder(false)) { Assert.Throws <NestedStringBuilderCreationException>(() => { using (_ = ZString.CreateUtf8StringBuilder(true)) { } } ); } } }
public void NotDisposedUtf8() { { _ = ZString.CreateUtf8StringBuilder(true); } { Assert.Throws <NestedStringBuilderCreationException>(() => { using (_ = ZString.CreateUtf8StringBuilder(true)) { } } ); } Utf8ValueStringBuilder.scratchBufferUsed.IsSameOrEqualTo(true); Utf8ValueStringBuilder.scratchBufferUsed = false; }
internal static void Test <T0, T1>(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); var actual = sb.ToString(); var expected = string.Format(format, t0, t1); actual.Should().Be(expected); } // Prepare { var actual = ZString.PrepareUtf16 <T0, T1>(format).Format(t0, t1); var expected = string.Format(format, t0, t1); actual.Should().Be(expected); } if (testUtf8) { var sb = ZString.PrepareUtf8 <T0, T1>(format); var actual = sb.Format(t0, t1); var expected = string.Format(format, t0, t1); actual.Should().Be(expected); } // Direct if (testUtf8) { #if NETCOREAPP3_1 var writer = new ArrayBufferWriter <byte>(); ZString.Utf8Format(writer, format, t0, t1); var actual = Encoding.UTF8.GetString(writer.WrittenSpan); var expected = string.Format(format, t0, t1); actual.Should().Be(expected); #endif } }
public void Single(float x, float y) { using (var sb1 = ZString.CreateStringBuilder()) using (var sb2 = ZString.CreateUtf8StringBuilder()) using (var sb3 = ZString.CreateStringBuilder()) using (var sb4 = ZString.CreateUtf8StringBuilder()) { var sb5 = new StringBuilder(); sb1.Append(x); sb1.Append(y); sb2.Append(x); sb2.Append(y); sb3.AppendMany(x, y); sb4.AppendMany(x, y); sb5.Append(x); sb5.Append(y); sb1.ToString().Should().Be(sb2.ToString()); sb1.ToString().Should().Be(sb3.ToString()); sb1.ToString().Should().Be(sb4.ToString()); sb1.ToString().Should().Be(sb5.ToString()); } }
public void UInt64(ulong x, ulong y) { using (var sb1 = ZString.CreateStringBuilder()) using (var sb2 = ZString.CreateUtf8StringBuilder()) using (var sb3 = ZString.CreateStringBuilder()) using (var sb4 = ZString.CreateUtf8StringBuilder()) { var sb5 = new StringBuilder(); sb1.Append(x); sb1.Append(y); sb2.Append(x); sb2.Append(y); sb3.Append(x); sb3.Append(y); sb4.Append(x); sb4.Append(y); sb5.Append(x); sb5.Append(y); sb1.ToString().Should().Be(sb2.ToString()); sb1.ToString().Should().Be(sb3.ToString()); sb1.ToString().Should().Be(sb4.ToString()); sb1.ToString().Should().Be(sb5.ToString()); } }
public void BoolTest(bool x) { using (var sb1 = ZString.CreateStringBuilder()) using (var sb2 = ZString.CreateUtf8StringBuilder()) using (var sb3 = ZString.CreateStringBuilder()) using (var sb4 = ZString.CreateUtf8StringBuilder()) { var sb5 = new StringBuilder(); sb1.Append(x); sb2.Append(x); sb3.Append(x); sb4.Append(x); sb5.Append(x); sb1.ToString().Should().Be(sb2.ToString()); sb1.ToString().Should().Be(sb3.ToString()); sb1.ToString().Should().Be(sb4.ToString()); sb1.ToString().Should().Be(sb5.ToString()); } }
public void Integer(int x, int y) { using (var sb1 = ZString.CreateStringBuilder()) using (var sb2 = ZString.CreateUtf8StringBuilder()) using (var sb3 = ZString.CreateStringBuilder()) using (var sb4 = ZString.CreateUtf8StringBuilder()) { var sb5 = new StringBuilder(); sb1.Append(x); sb1.Append(y); sb2.Append(x); sb2.Append(y); sb3.Append(x); sb3.Append(y); sb4.Append(x); sb4.Append(y); sb5.Append(x); sb5.Append(y); sb1.ToString().Should().Be(sb2.ToString()); sb1.ToString().Should().Be(sb3.ToString()); sb1.ToString().Should().Be(sb4.ToString()); sb1.ToString().Should().Be(sb5.ToString()); } }
private async Task InternalAsyncTask(LogEventInfo logEvent, CancellationToken cancellationToken) { InternalLogger.Debug("Sending one log event"); using var sb = ZString.CreateUtf8StringBuilder(); sb.Append("[{\"logs\": ["); if (EscapeJson) { sb.Append("{\"timestamp\": "); sb.Append(new DateTimeOffset(logEvent.TimeStamp).ToUnixTimeMilliseconds()); sb.Append(",\"message\": \""); sb.Append(JsonEncodedText.Encode(Layout.Render(logEvent))); sb.Append("\"}"); } else { sb.Append(Layout.Render(logEvent)); } sb.Append("]}]"); await SendLogStringBuilder(sb, cancellationToken).ConfigureAwait(false); }
void Test <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16>(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 <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16>(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 <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16>(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 <byte>(); 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 } }
public void EnumTest() { var x = MoreMyEnum.Apple; var y = MoreMyEnum.Orange; using (var sb1 = ZString.CreateStringBuilder()) using (var sb2 = ZString.CreateUtf8StringBuilder()) using (var sb3 = ZString.CreateStringBuilder()) using (var sb4 = ZString.CreateUtf8StringBuilder()) { var sb5 = new StringBuilder(); sb1.Append(x); sb1.Append(y); sb2.Append(x); sb2.Append(y); sb3.Append(x); sb3.Append(y); sb4.Append(x); sb4.Append(y); sb5.Append(x); sb5.Append(y); sb1.ToString().Should().Be(sb2.ToString()); sb1.ToString().Should().Be(sb3.ToString()); sb1.ToString().Should().Be(sb4.ToString()); sb1.ToString().Should().Be(sb5.ToString()); } }
public void AppendCharRepeat() { using (var zsb = ZString.CreateUtf8StringBuilder(notNested: true)) { var text = "foo"; zsb.Append(text); var bcl = new StringBuilder(text); // ASCII zsb.Append('\x7F', 10); bcl.Append('\x7F', 10); zsb.ToString().Should().Be(bcl.ToString()); // Non-ASCII zsb.Append('\x80', 10); bcl.Append('\x80', 10); zsb.ToString().Should().Be(bcl.ToString()); zsb.Append('\u9bd6', 10); bcl.Append('\u9bd6', 10); zsb.ToString().Should().Be(bcl.ToString()); } }
public int Utf8StringBuilderAppendFormat_() { using var zsh = ZString.CreateUtf8StringBuilder(); zsh.AppendFormat(_format, _args[0], _args[1], _args[2], _args[3], _args[4], _args[5], _args[6], _args[7], _args[8], _args[9], _args[10], _args[11], _args[12], _args[13], _args[14], _args[15]); return(zsh.Length); }
public int Utf8StringBuilderAppendFormat_() { using var zsh = ZString.CreateUtf8StringBuilder(); zsh.AppendFormat(FormatString, x, y); return(zsh.Length); }