MemoryTemplate
Represents various extensions for MemoryTemplate<T> type.
using System;
using System.Buffers;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
namespace DotNext.Buffers
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public static class MemoryTemplate
{
private static void Rewrite(this string[] replacement, int index, StringBuilder output)
{
output.Append(replacement[index]);
}
public static void Render([In] [IsReadOnly] [System.Runtime.CompilerServices.Nullable(0)] ref MemoryTemplate<char> template, StringBuilder output, params string[] replacement)
{
template.Render(output, (Action<int, StringBuilder>)replacement.Rewrite, (ReadOnlySpanAction<char, StringBuilder>)Span.CopyTo);
}
private static void Rewrite(this string[] replacement, int index, IBufferWriter<char> output)
{
output.Write(replacement[index]);
}
public static string Render([In] [IsReadOnly] [System.Runtime.CompilerServices.Nullable(0)] ref MemoryTemplate<char> template, params string[] replacement)
{
using (PooledArrayBufferWriter<char> pooledArrayBufferWriter = new PooledArrayBufferWriter<char>(template.Value.Length)) {
template.Render(pooledArrayBufferWriter, (Action<int, PooledArrayBufferWriter<char>>)replacement.Rewrite);
return new string(pooledArrayBufferWriter.WrittenArray);
}
}
private static void Rewrite(this string[] replacement, int index, TextWriter output)
{
output.Write(replacement[index]);
}
public static void Render([In] [IsReadOnly] [System.Runtime.CompilerServices.Nullable(0)] ref MemoryTemplate<char> template, TextWriter output, params string[] replacement)
{
template.Render(output, (Action<int, TextWriter>)replacement.Rewrite, (ReadOnlySpanAction<char, TextWriter>)Span.CopyTo);
}
}
}