### Utf8StringWriter Append Methods Examples Source: https://context7.com/cysharp/utf8stringinterpolation/llms.txt Demonstrates various append methods of Utf8StringWriter for different data types, including literals, nullable strings, characters, lines, formatted strings, whitespace, pre-encoded UTF8 data, and generic formatted values. These methods avoid intermediate string allocations. ```csharp using Utf8StringInterpolation; using System.Buffers; using var buffer = Utf8String.CreateWriter(out var writer); // AppendLiteral - for string literals writer.AppendLiteral("Static text "); // Append - for nullable strings string? maybeNull = null; writer.Append(maybeNull); // Writes nothing if null writer.Append("not null"); // Writes the string // Append char with repeat count writer.Append('-', 20); // Writes "--------------------" // AppendLine - add platform-specific newline writer.AppendLine(); writer.AppendLine("Line with newline at end"); // AppendFormat - for interpolated strings int count = 5; double price = 19.99; writer.AppendFormat($"Items: {count}, Total: ${"price":F2}"); // AppendWhitespace - add specific number of spaces writer.AppendWhitespace(4); // AppendUtf8 - for pre-encoded UTF8 data ReadOnlySpan utf8Data = "Already UTF8"u8; writer.AppendUtf8(utf8Data); // AppendFormatted - generic value formatting with alignment/format writer.AppendFormatted(42, alignment: 10, format: "X"); // Hex with padding writer.AppendFormatted("text", alignment: -15); // Left-aligned writer.Flush(); byte[] result = buffer.ToArray(); ``` -------------------------------- ### Create Utf8StringBuffer with Pooled Internal Buffer Source: https://context7.com/cysharp/utf8stringinterpolation/llms.txt Utilize the Utf8String.CreateWriter overload with an 'out' parameter to get a Utf8StringBuffer managing an internally pooled buffer. This simplifies usage when a byte[] result is needed without manual buffer management. Remember to dispose of the buffer. ```csharp using Utf8StringInterpolation; using System.Buffers; // Create writer with pooled internal buffer (must dispose!) using var buffer = Utf8String.CreateWriter(out var writer); // Build content writer.Append("Header: "); writer.AppendFormat($"Request ID: {Guid.NewGuid()}"); writer.AppendLine(); writer.Append("Body: "); writer.AppendFormat($"Payload size: {1024} bytes"); // Flush to commit all writes writer.Flush(); // Access results in multiple ways byte[] bytes = buffer.ToArray(); // Copy to new array ReadOnlySpan span = buffer.WrittenSpan; // Direct span access ReadOnlyMemory memory = buffer.WrittenMemory; // Memory access int count = buffer.WrittenCount; // Total bytes written // Copy to another buffer writer var otherBuffer = new ArrayBufferWriter(); buffer.CopyTo(otherBuffer); // Convert to string for debugging string debugString = buffer.ToString(); // buffer.Dispose() called automatically by 'using' ``` -------------------------------- ### Create Utf8StringWriter with External Buffer Source: https://context7.com/cysharp/utf8stringinterpolation/llms.txt Use Utf8String.CreateWriter with an IBufferWriter to build UTF8 content incrementally. Ensure Flush() or Dispose() is called to commit changes to the buffer. ```csharp using Utf8StringInterpolation; using System.Buffers; // Create writer with external buffer var bufferWriter = new ArrayBufferWriter(); var writer = Utf8String.CreateWriter(bufferWriter); // Build content using various append methods writer.Append("User Profile"); writer.AppendLine(); writer.AppendFormat($"Name: {"John Doe"}"); writer.AppendLine(); writer.AppendFormat($"Email: {"john@example.com"}"); writer.AppendLine(); writer.AppendFormat($"Created: {DateTime.Now:yyyy-MM-dd HH:mm:ss}"); // Must call Flush() or Dispose() to commit to buffer writer.Flush(); // Access written data ReadOnlySpan result = bufferWriter.WrittenSpan; Console.WriteLine($"Total bytes: {bufferWriter.WrittenCount}"); ``` -------------------------------- ### Utf8String.CreateWriter with IBufferWriter Source: https://github.com/cysharp/utf8stringinterpolation/blob/main/README.md Obtain a Utf8StringWriter to write continuously to an IBufferWriter. Remember to call Flush or Dispose to ensure all data is written. ```csharp var writer = Utf8String.CreateWriter(bufferWriter); // call each append methods. writer.Append("foo"); writer.AppendFormat($"bar {Guid.NewGuid}"); // finally call Flush(or Dispose) writer.Flush(); ``` -------------------------------- ### Utf8String.Format Overloads Source: https://github.com/cysharp/utf8stringinterpolation/blob/main/README.md Use these overloads for one-shot formatting into a byte array or an IBufferWriter. TryFormat writes to a provided Span and returns false if the destination is insufficient. ```csharp byte[] Format(ref Utf8StringWriter format) ``` ```csharp void Format(TBufferWriter bufferWriter, ref Utf8StringWriter format) where TBufferWriter : IBufferWriter ``` ```csharp bool TryFormat(Span destination, out int bytesWritten, ref Utf8StringWriter format) ``` -------------------------------- ### UTF-8 Formatting in .NET 8 vs. .NET Standard 2.1/6/7 Source: https://github.com/cysharp/utf8stringinterpolation/blob/main/README.md Demonstrates UTF-8 string formatting. .NET 8 supports all numeric custom format strings, while older versions use Utf8Formatter.TryFormat with StandardFormat, except for DateTime, DateTimeOffset, and TimeSpan which support custom formats on all platforms. ```csharp Utf8String.Format($"Double value is {123.456789:.###}"); ``` ```csharp Utf8String.Format($"Today is {DateTime.Now:yyyy-MM-dd}"); ``` -------------------------------- ### Interpolated String Handler Usage Source: https://github.com/cysharp/utf8stringinterpolation/blob/main/README.md Demonstrates how interpolated strings are handled by Utf8StringWriter, which compiles into appendLiteral and appendFormatted calls. ```csharp // `Format(ref Utf8StringWriter)` accepts string interpolation Utf8String.Format($"Hello, {name}, Your id is {id}!"); // Interpolated string compiles like following. var writer = new Utf8StringWriter(literalLength: 20, formattedCount: 2); writer.AppendLiteral("Hello, "); writer.AppendFormatted(name); writer.AppendLiteral(", You id is "); writer.AppendFormatted(id); writer.AppendLiteral("!"); ``` -------------------------------- ### StringBuilder-like UTF8 Writing Source: https://github.com/cysharp/utf8stringinterpolation/blob/main/README.md Provides a StringBuilder-like interface for appending and formatting UTF8 strings to an IBufferWriter. ```csharp // like a StringBuilder var writer = Utf8String.CreateWriter(bufferWriter); writer.Append("My Name..."); writer.AppendFormat($"is...? {name}"); writer.AppendLine(); writer.Flush(); ``` -------------------------------- ### Format Strings and Alignment with Utf8String Source: https://context7.com/cysharp/utf8stringinterpolation/llms.txt Utilizes standard .NET format strings and alignment specifiers for UTF8 output. Supports custom formatting for dates/times and standard formatting for numerics, with .NET 8+ offering full IUtf8SpanFormattable support. ```csharp using Utf8StringInterpolation; // Alignment (padding) // Positive = right-align (pad left), Negative = left-align (pad right) string name = "Test"; int value = 42; byte[] aligned = Utf8String.Format($"|{name,10}|{value,-10}|"); // Result: "| Test|42 |" // DateTime formatting (works on all platforms) DateTime now = DateTime.Now; byte[] dateFormatted = Utf8String.Format($"Date: {now:yyyy-MM-dd}"); byte[] timeFormatted = Utf8String.Format($"Time: {now:HH:mm:ss}"); byte[] isoFormatted = Utf8String.Format($"ISO: {now:O}"); // TimeSpan formatting TimeSpan span = new TimeSpan(1, 23, 45, 30); byte[] spanFormatted = Utf8String.Format($"Duration: {span:d\\.hh\\:mm\\:ss}"); // Result: "Duration: 1.23:45:30" // Numeric formatting (.NET 8+ for full support) double pi = Math.PI; byte[] decimalPlaces = Utf8String.Format($"Pi: {pi:F4}"); // "Pi: 3.1416" byte[] scientific = Utf8String.Format($"Pi: {pi:E2}"); // "Pi: 3.14E+000" byte[] percentage = Utf8String.Format($"Rate: {0.156:P1}"); // "Rate: 15.6%" // Hexadecimal int hex = 255; byte[] hexFormatted = Utf8String.Format($"Hex: {hex:X4}"); // "Hex: 00FF" // Nullable types with alignment int? nullableValue = 100; int? nullValue = null; byte[] nullable = Utf8String.Format($"|{nullableValue,5}|{nullValue,5}|"); // Result: "| 100| |" // Escaping braces byte[] escaped = Utf8String.Format($"Use {{braces}} like this: {42}"); // Result: "Use {braces} like this: 42" ``` -------------------------------- ### Utf8String.Concat and Join API Source: https://github.com/cysharp/utf8stringinterpolation/blob/main/README.md Enables concatenation and joining of strings into UTF8 byte arrays or IBufferWriter, similar to String.Concat and String.Join. ```APIDOC ## Utf8String.Concat, Join ### Description `Utf8String.Concat` and `Utf8String.Join` are similar to `String.Concat` and `String.Join`, but they write everything in UTF8. Like Format, there are two overloads: one that writes to `IBufferWriter` and another that writes to the internally pooled `ArrayBufferWriter` and then returns a `byte[]`. ### Concat Overloads - `byte[] Concat(params string?[] values)` - `void Concat(TBufferWriter bufferWriter, params string?[] values) where TBufferWriter : IBufferWriter` - `byte[] Concat(IEnumerable values)` - `void Concat(TBufferWriter bufferWriter, IEnumerable values) where TBufferWriter : IBufferWriter` ### Join Overloads - `byte[] Join(string separator, params string?[] values)` - `void Join(TBufferWriter bufferWriter, string separator, params string?[] values) where TBufferWriter : IBufferWriter` - `byte[] Join(string separator, IEnumerable values)` - `void Join(TBufferWriter bufferWriter, string separator, IEnumerable values) where TBufferWriter : IBufferWriter` ``` -------------------------------- ### Utf8StringWriter Methods Source: https://github.com/cysharp/utf8stringinterpolation/blob/main/README.md Details the methods available on Utf8StringWriter for appending various types of data and managing the buffer. ```APIDOC ## Utf8StringWriter ### Description `AppendLiteral`, `AppendFormatted` is called from compiler generated code. ### Methods - `void AppendLiteral(string s)` - `void AppendWhitespace(int count)` - `void Append(string? s)` - `void Append(char c)` - `void Append(char c, int repeatCount)` - `void AppendUtf8(scoped ReadOnlySpan utf8String)` - `void AppendFormatted(scoped ReadOnlySpan utf8String)` - `void AppendFormatted(scoped ReadOnlySpan s)` - `void AppendFormatted(string value, int alignment = 0, string? format = null)` - `void AppendFormatted(T value, int alignment = 0, string? format = null)` - `void AppendFormat(ref Utf8StringWriter format) // extension method` - `void AppendLine(ref Utf8StringWriter format) // extension method` - `void AppendLine()` - `void AppendLine(string s)` - `void Flush()` - `void Dispose() // call Flush and dereference buffer and bufferwriter` ``` -------------------------------- ### Utf8StringWriter Methods Source: https://github.com/cysharp/utf8stringinterpolation/blob/main/README.md Provides a comprehensive list of methods for appending various data types and formats to a Utf8StringWriter, including literal strings, formatted strings, and raw UTF8 bytes. Flush and Dispose ensure data is written and resources are released. ```csharp void AppendLiteral(string s) ``` ```csharp void AppendWhitespace(int count) ``` ```csharp void Append(string? s) ``` ```csharp void Append(char c) ``` ```csharp void Append(char c, int repeatCount) ``` ```csharp void AppendUtf8(scoped ReadOnlySpan utf8String) ``` ```csharp void AppendFormatted(scoped ReadOnlySpan utf8String) ``` ```csharp void AppendFormatted(scoped ReadOnlySpan s) ``` ```csharp void AppendFormatted(string value, int alignment = 0, string? format = null) ``` ```csharp void AppendFormatted(T value, int alignment = 0, string? format = null) ``` ```csharp void AppendFormat(ref Utf8StringWriter format) // extension method ``` ```csharp void AppendLine(ref Utf8StringWriter format) // extension method ``` ```csharp void AppendLine() ``` ```csharp void AppendLine(string s) ``` ```csharp void Flush() ``` ```csharp void Dispose() // call Flush and dereference buffer and bufferwriter ``` -------------------------------- ### Utf8String.TryFormat - Safe Formatting to Span Source: https://context7.com/cysharp/utf8stringinterpolation/llms.txt Utf8String.TryFormat attempts to write formatted UTF8 content to a pre-allocated Span, returning true on success or false if the buffer is too small. This is suitable for fixed-size buffer scenarios. ```csharp using Utf8StringInterpolation; int value = 10; // Allocate a fixed-size buffer on the stack Span buffer = stackalloc byte[20]; // Try to format - returns true if successful bool success = Utf8String.TryFormat(buffer, out int bytesWritten, $("Value: {value}")); if (success) { ReadOnlySpan result = buffer.Slice(0, bytesWritten); // result contains "Value: 10" as UTF8 Console.WriteLine($"Wrote {bytesWritten} bytes"); } // Returns false when buffer is too small Span smallBuffer = stackalloc byte[5]; bool failed = Utf8String.TryFormat(smallBuffer, out _, $("This is too long")); // failed == false, buffer unchanged ``` -------------------------------- ### Join Values with Separator using Utf8String Source: https://context7.com/cysharp/utf8stringinterpolation/llms.txt Combines multiple values into a UTF8 byte array using a specified separator. Supports various data types and collections. Can also write directly to an IBufferWriter. ```csharp using Utf8StringInterpolation; using System.Buffers; // Join strings with separator byte[] csv = Utf8String.Join(",", "apple", "banana", "cherry"); // Result: UTF8 bytes for "apple,banana,cherry" // Join with multi-character separator byte[] piped = Utf8String.Join(" | ", "A", "B", "C"); // Result: UTF8 bytes for "A | B | C" // Join integers byte[] numbers = Utf8String.Join(", ", 1, 2, 3, 4, 5); // Result: UTF8 bytes for "1, 2, 3, 4, 5" // Join IEnumerable var range = Enumerable.Range(1, 5); byte[] rangeJoined = Utf8String.Join("-", range); // Result: UTF8 bytes for "1-2-3-4-5" // Join List var list = new List { 10, 20, 30 }; byte[] listJoined = Utf8String.Join(", ", list); // Result: UTF8 bytes for "10, 20, 30" // Handle null values in string arrays var withNulls = new[] { "abc", null, "def" }; byte[] withNullsJoined = Utf8String.Join(",", withNulls); // Result: UTF8 bytes for "abc,,def" // Write directly to IBufferWriter var bufferWriter = new ArrayBufferWriter(); Utf8String.Join(bufferWriter, "; ", new[] { "X", "Y", "Z" }); ``` -------------------------------- ### Utf8StringWriter CreateWriter Overloads Source: https://github.com/cysharp/utf8stringinterpolation/blob/main/README.md These overloads allow creating a Utf8StringWriter, either by providing your own IBufferWriter or by using an internally pooled buffer managed by Utf8StringBuffer. ```csharp Utf8StringWriter CreateWriter(TBufferWriter bufferWriter, IFormatProvider? formatProvider = null) where TBufferWriter : IBufferWriter ``` ```csharp Utf8StringBuffer CreateWriter(out Utf8StringWriter> stringWriter, IFormatProvider? formatProvider = null) ``` -------------------------------- ### Utf8StringWriter Internal Structure Source: https://github.com/cysharp/utf8stringinterpolation/blob/main/README.md Illustrates the internal structure of Utf8StringWriter, including its buffer management and methods for appending literals and formatted values. ```csharp // internal struct writer write value to utf8 directly without boxing. [InterpolatedStringHandler] public ref struct Utf8StringWriter where TBufferWriter : IBufferWriter { TBufferWriter bufferWriter; // when buffer is full, advance and get more buffer Span buffer; // current write buffer public void AppendLiteral(string value) { // encode string literal to Utf8 buffer directly var bytesWritten = Encoding.UTF8.GetBytes(value, buffer); buffer = buffer.Slice(bytesWritten); } public void AppendFormatted(T value, int alignment = 0, string? format = null) where T : IUtf8SpanFormattable { // write value to Utf8 buffer directly while (!value.TryFormat(buffer, out bytesWritten, format)) { Grow(); } buffer = buffer.Slice(bytesWritten); } } ``` -------------------------------- ### Utf8String.Join Overloads Source: https://github.com/cysharp/utf8stringinterpolation/blob/main/README.md Join strings with a separator into a UTF8 byte array or an IBufferWriter. Supports params string[] or IEnumerable for values. ```csharp // Join overloads, return byte[] or receive IBufferWriter. byte[] Join(string separator, params string?[] values) ``` ```csharp void Join(TBufferWriter bufferWriter, string separator, params string?[] values) where TBufferWriter : IBufferWriter ``` ```csharp byte[] Join(string separator, IEnumerable values) ``` ```csharp void Join(TBufferWriter bufferWriter, string separator, IEnumerable values) where TBufferWriter : IBufferWriter ``` -------------------------------- ### Utf8String.CreateWriter with Utf8StringBuffer Source: https://github.com/cysharp/utf8stringinterpolation/blob/main/README.md Create a Utf8StringWriter using an internally pooled buffer for convenience when a byte[] is the final output. Ensure the buffer is disposed after use. ```csharp // buffer must Dispose after used(recommend to use using) using var buffer = Utf8String.CreateWriter(out var writer); // call each append methods. writer.Append("foo"); writer.AppendFormat($"bar {Guid.NewGuid}"); // finally call Flush(no need to call Dispose for writer) writer.Flush(); // copy to written byte[] var bytes = buffer.ToArray(); // or copy to other IBufferWriter, get ReadOnlySpan buffer.CopyTo(otherBufferWriter); var writtenData = buffer.WrittenSpan; ``` -------------------------------- ### Utf8String.Format - Write Directly to IBufferWriter Source: https://context7.com/cysharp/utf8stringinterpolation/llms.txt This overload of Utf8String.Format writes UTF8 content directly to a provided IBufferWriter, ideal for scenarios like ASP.NET HttpResponse.BodyWriter or PipeWriter, avoiding intermediate array allocations. ```csharp using Utf8StringInterpolation; using System.Buffers; using System.IO.Pipelines; // Write to ArrayBufferWriter var bufferWriter = new ArrayBufferWriter(); string name = "Bob"; int score = 95; Utf8String.Format(bufferWriter, $("Player: {name}, Score: {score}")); // bufferWriter.WrittenSpan now contains UTF8 bytes // Get the written data ReadOnlySpan writtenData = bufferWriter.WrittenSpan; Console.WriteLine($"Wrote {bufferWriter.WrittenCount} bytes"); // Write to PipeWriter (for file/network streaming) using var fileStream = File.OpenWrite("output.txt"); var pipeWriter = PipeWriter.Create(fileStream); Utf8String.Format(pipeWriter, $("Timestamp: {DateTime.UtcNow:O}")); await pipeWriter.FlushAsync(); // ASP.NET example (conceptual) // Utf8String.Format(Response.BodyWriter, $"{{"id": {userId}, "name": "{userName}"}}"); ``` -------------------------------- ### Join and Concat UTF8 Sequences Source: https://github.com/cysharp/utf8stringinterpolation/blob/main/README.md Provides methods to join sequences of items into a single UTF8 encoded byte array, separated by a specified delimiter. ```csharp // Join, Concat methods var seq = Enumerable.Range(1, 10); byte[] utf8seq = Utf8String.Join(", ", seq); ``` -------------------------------- ### Utf8String.Format API Source: https://github.com/cysharp/utf8stringinterpolation/blob/main/README.md Provides methods for formatting strings directly into UTF8 byte arrays or IBufferWriter, with options for direct span writing. ```APIDOC ## Utf8String.Format ### Description Format is a one-shot method. The Format that takes an `IBufferWriter` performs writing based on the bufferWriter and finally flushes (Advance). The Format that returns a `byte[]` writes using its internally pooled `ArrayBufferWriter` and generates a final `byte[]`. TryFormat writes to the specified `Span`, and if the length is insufficient, it returns false. ### Methods - `byte[] Format(ref Utf8StringWriter format)` - `void Format(TBufferWriter bufferWriter, ref Utf8StringWriter format) where TBufferWriter : IBufferWriter` - `bool TryFormat(Span destination, out int bytesWritten, ref Utf8StringWriter format)` ``` -------------------------------- ### Utf8String.CreateWriter API Source: https://github.com/cysharp/utf8stringinterpolation/blob/main/README.md Provides a Utf8StringWriter for continuous writing to an IBufferWriter, similar to StringBuilder, with manual flush control. ```APIDOC ## Utf8String.CreateWriter ### Description `Utf8String.CreateWriter` allows you to obtain a `Utf8StringWriter` that can write continuously, similar to `StringBuilder`. Each `Append***` method can be used in a manner similar to `StringBuilder`. However, unlike `StringBuilder`, the buffer is managed by the provided BufferWriter, which is why it's named Writer. For performance reasons, the Append methods don't always flush (Advance) to the internal BufferWriter. By manually calling Flush, you ensure that Advance is invoked. Additionally, Dispose also calls Flush. ### Usage Example ```csharp var writer = Utf8String.CreateWriter(bufferWriter); // call each append methods. writer.Append("foo"); writer.AppendFormat($"bar {Guid.NewGuid}"); // finally call Flush(or Dispose) writer.Flush(); ``` When writing larger messages, it's advisable to periodically call Flush. At those times, for instance, if using a `PipeWriter`, you can invoke `pipeWriter.FlushAsync()` to stream the write operations without holding onto a large buffer internally. ### Overloads - `Utf8StringWriter CreateWriter(TBufferWriter bufferWriter, IFormatProvider? formatProvider = null) where TBufferWriter : IBufferWriter` - `Utf8StringBuffer CreateWriter(out Utf8StringWriter> stringWriter, IFormatProvider? formatProvider = null)` `Utf8StringBuffer` is convenient because it uses an internally pooled buffer. Therefore, when you just want to obtain a `byte[]`, for example, there's no need to separately prepare or manage an `IBufferWriter`. ### Utf8StringBuffer Usage Example ```csharp // buffer must Dispose after used(recommend to use using) using var buffer = Utf8String.CreateWriter(out var writer); // call each append methods. writer.Append("foo"); writer.AppendFormat($"bar {Guid.NewGuid}"); // finally call Flush(no need to call Dispose for writer) writer.Flush(); // copy to written byte[] var bytes = buffer.ToArray(); // or copy to other IBufferWriter, get ReadOnlySpan buffer.CopyTo(otherBufferWriter); var writtenData = buffer.WrittenSpan; ``` ``` -------------------------------- ### Write UTF8 to IBufferWriter Source: https://github.com/cysharp/utf8stringinterpolation/blob/main/README.md Writes UTF8 encoded strings directly to an IBufferWriter, commonly used with ASP.NET's HttpResponse.BodyWriter. Supports format specifiers. ```csharp // write to IBufferWriter(for example ASP.NET HttpResponse.BodyWriter) var bufferWriter = new ArrayBufferWriter(); Utf8String.Format(bufferWriter, $"Today is {DateTime.Now:yyyy-MM-dd}"); // support format ``` -------------------------------- ### Utf8String.Concat Overloads Source: https://github.com/cysharp/utf8stringinterpolation/blob/main/README.md Concatenate strings into a UTF8 byte array or an IBufferWriter. Supports params string[] or IEnumerable for values. ```csharp // Concat overloads, return byte[] or receive IBufferWriter. byte[] Concat(params string?[] values) ``` ```csharp void Concat(TBufferWriter bufferWriter, params string?[] values) where TBufferWriter : IBufferWriter ``` ```csharp byte[] Concat(IEnumerable values) ``` ```csharp void Concat(TBufferWriter bufferWriter, IEnumerable values) where TBufferWriter : IBufferWriter ``` -------------------------------- ### Create UTF8 Encoded String Directly Source: https://github.com/cysharp/utf8stringinterpolation/blob/main/README.md Generates a UTF8 encoded byte array directly from an interpolated string without explicit encoding. ```csharp using Utf8StringInterpolation; // Create UTF8 encoded string directly(without encoding). byte[] utf8 = Utf8String.Format($"Hello, {name}, Your id is {id}!"); ``` -------------------------------- ### Write UTF8 to FileStream Directly Source: https://github.com/cysharp/utf8stringinterpolation/blob/main/README.md Writes UTF8 encoded strings directly to a FileStream using a PipeWriter. Supports alignment and formatting. ```csharp // write to FileStream directly using var fs = File.OpenWrite("foo.txt"); var pipeWriter = PipeWriter.Create(fs); Utf8String.Format(pipeWriter, $"Foo: {id,10} {name,-5}"); // support alignment ``` -------------------------------- ### Concatenate Multiple Values with Utf8String.Concat Source: https://context7.com/cysharp/utf8stringinterpolation/llms.txt Use Utf8String.Concat to combine multiple strings or values into a single UTF8 byte array. It handles null values by skipping them and supports string arrays, IEnumerable, and direct writing to an IBufferWriter. ```csharp using Utf8StringInterpolation; using System.Buffers; // Concatenate strings (null values are skipped) byte[] result1 = Utf8String.Concat("Hello", " ", "World"); // Result: UTF8 bytes for "HelloWorld" (note: spaces must be explicit) byte[] result2 = Utf8String.Concat("Start", null, "End"); // Result: UTF8 bytes for "StartEnd" // Concatenate with params array string[] parts = { "Part1", "Part2", "Part3" }; byte[] result3 = Utf8String.Concat(parts); // Result: UTF8 bytes for "Part1Part2Part3" // Concatenate IEnumerable (uses AppendFormatted for each) var numbers = new[] { 1, 2, 3, 4, 5 }; byte[] result4 = Utf8String.Concat(numbers); // Result: UTF8 bytes for "12345" // Write directly to IBufferWriter var bufferWriter = new ArrayBufferWriter(); Utf8String.Concat(bufferWriter, "Direct", "Write"); ReadOnlySpan written = bufferWriter.WrittenSpan; ``` -------------------------------- ### Utf8String.Format - Generate byte[] from Interpolated Strings Source: https://context7.com/cysharp/utf8stringinterpolation/llms.txt Use Utf8String.Format to create UTF8 encoded byte arrays directly from interpolated strings. This method supports standard .NET format strings for alignment and formatting, including numeric and date formats. ```csharp using Utf8StringInterpolation; using System.Buffers; // Basic format returning byte[] string name = "Alice"; int id = 42; byte[] utf8 = Utf8String.Format($"Hello, {name}, Your id is {id}!"); // Result: UTF8 bytes for "Hello, Alice, Your id is 42!" // Format with date formatting byte[] dateUtf8 = Utf8String.Format($"Today is {DateTime.Now:yyyy-MM-dd}"); // Result: UTF8 bytes for "Today is 2024-01-15" // Format with alignment (padding) byte[] alignedUtf8 = Utf8String.Format($"ID: {id,10} Name: {name,-15}"); // Result: UTF8 bytes for "ID: 42 Name: Alice " // Format with numeric formatting (.NET 8+) double value = 123.456789; byte[] numericUtf8 = Utf8String.Format($"Value: {value:F2}"); // Result: UTF8 bytes for "Value: 123.46" // Empty and literal-only formats work too byte[] empty = Utf8String.Format($""); byte[] literal = Utf8String.Format($"Hello World"); ``` -------------------------------- ### Utf8StringBuffer Members Source: https://github.com/cysharp/utf8stringinterpolation/blob/main/README.md Members of Utf8StringBuffer for obtaining and managing UTF-8 string buffers. Remember to dispose of the buffer after use. ```csharp int WrittenCount { get; } ReadOnlySpan WrittenSpan { get; } ReadOnlyMemory WrittenMemory { get; } byte[] ToArray() void CopyTo(TBufferWriter bufferWriter) where TBufferWriter : IBufferWriter Dispose() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.