SimpleTextTemplate.Writer 3.1.0

dotnet add package SimpleTextTemplate.Writer --version 3.1.0
                    
NuGet\Install-Package SimpleTextTemplate.Writer -Version 3.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SimpleTextTemplate.Writer" Version="3.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SimpleTextTemplate.Writer" Version="3.1.0" />
                    
Directory.Packages.props
<PackageReference Include="SimpleTextTemplate.Writer" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SimpleTextTemplate.Writer --version 3.1.0
                    
#r "nuget: SimpleTextTemplate.Writer, 3.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package SimpleTextTemplate.Writer@3.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SimpleTextTemplate.Writer&version=3.1.0
                    
Install as a Cake Addin
#tool nuget:?package=SimpleTextTemplate.Writer&version=3.1.0
                    
Install as a Cake Tool

SimpleTextTemplate

Build(.NET) NuGet Azure Artifacts

SimpleTextTemplateは、変数の埋め込みのみに対応したテキストテンプレートエンジンです。

説明

  • 文字列をUTF-8バイト列としてIBufferWriter<byte>に出力します。
  • {{ <変数>:<format>:<culture> }}で変数を埋め込みます。(formatcultureは省略可能)
  • {{}}内の先頭と末尾の空白(U+0020)は無視されます。
  • {{}}で囲まれた範囲以外の文字は、そのまま出力されます。

一部のカルチャーではOSによって定数展開が変わります。

インストール

NuGet(正式リリース版)

dotnet add package SimpleTextTemplate.Generator

Azure Artifacts(開発用ビルド)

dotnet add package SimpleTextTemplate.Generator -s https://pkgs.dev.azure.com/finphie/Main/_packaging/DotNet/nuget/v3/index.json

使い方

次の例では、外部のライブラリであるCommunityToolkit.HighPerformanceを参照しています。

SimpleTextTemplate.Generator(推奨)

using System;
using System.Globalization;
using System.Text;
using CommunityToolkit.HighPerformance.Buffers;
using SimpleTextTemplate;

using var bufferWriter = new ArrayPoolBufferWriter<byte>();
var context = new SampleContext("Hello, World", 1000, new(2000, 1, 1, 0, 0, 0, TimeSpan.Zero));

var writer = TemplateWriter.Create(bufferWriter);
TemplateRenderer.Render(ref writer, "{{ DateTimeOffsetValue:o }}_{{ StringValue }}!", in context);
TemplateRenderer.Render(ref writer, "_{{ ConstantString }}_{{ ConstantInt:N3:ja-JP }}_{{ IntValue }}", in context, CultureInfo.InvariantCulture);
writer.Flush();

// 2000-01-01T00:00:00.0000000+00:00_Hello, World!_Hello_999.000_1000
Console.WriteLine(Encoding.UTF8.GetString(bufferWriter.WrittenSpan));

readonly record struct SampleContext(
    string StringValue,
    int IntValue,
    DateTimeOffset DateTimeOffsetValue)
{
    public const string ConstantString = "Hello";
    public const int ConstantInt = 999;
}

サンプルプロジェクト

生成コード
using System.Runtime.CompilerServices;
using System.Text;
using CommunityToolkit.HighPerformance.Buffers;
using SimpleTextTemplate;

file static class Intercept
{
    [InterceptsLocation(1, "...")]
    public static void Render0(ref TemplateWriter<ArrayPoolBufferWriter<byte>> writer, string text, in SampleContext context, IFormatProvider? provider = null)
    {
        writer.WriteValue(Unsafe.AsRef(in context).@DateTimeOffsetValue, "o", CultureInfo.InvariantCulture);
        writer.Grow(2
            + Encoding.UTF8.GetMaxByteCount(
                Unsafe.AsRef(in context).@StringValue.Length));
        writer.DangerousWriteConstantLiteral("_"u8);
        writer.DangerousWriteString(Unsafe.AsRef(in context).@StringValue);
        writer.DangerousWriteConstantLiteral("!"u8);
    }

    [InterceptsLocation(1, "...")]
    public static void Render1(ref TemplateWriter<ArrayPoolBufferWriter<byte>> writer, string text, in SampleContext context, IFormatProvider? provider = null)
    {
        writer.Grow(15);
        writer.WriteConstantLiteral("_Hello_999.000_"u8);
        writer.WriteValue(Unsafe.AsRef(in context).@IntValue, default, CultureInfo.InvariantCulture);
    }
}

<details> <summary>SimpleTextTemplate(非推奨)</summary>

SimpleTextTemplate.Renderer(非推奨)

SimpleTextTemplateSimpleTextTemplate.Contextsへの参照が必要です。

using System;
using System.Text;
using CommunityToolkit.HighPerformance.Buffers;
using SimpleTextTemplate;

var symbols = Context.Create();
symbols.Add("Identifier"u8.ToArray(), "Hello, World!"u8.ToArray());

using var bufferWriter = new ArrayPoolBufferWriter<byte>();
var source = "{{ Identifier }}"u8.ToArray();
var template = Template.Parse(source);
template.Render(bufferWriter, symbols);

// Hello, World!
Console.WriteLine(Encoding.UTF8.GetString(bufferWriter.WrittenSpan));

</details>

ベンチマーク

定数(string)

Method Mean Error Ratio Gen0 Allocated
SimpleTextTemplate.Generator 13.53 ns 0.110 ns 1.00 0.0067 56 B
(Utf8.TryWrite) 25.12 ns 0.164 ns 1.86 0.0067 56 B
(InterpolatedStringHandler) 30.96 ns 0.357 ns 2.29 0.0105 88 B
(CompositeFormat) 29.98 ns 0.468 ns 2.22 0.0105 88 B

定数(int)

Method Mean Error Ratio Gen0 Allocated
SimpleTextTemplate.Generator 12.18 ns 0.051 ns 1.00 0.0048 40 B
(Utf8.TryWrite) 13.54 ns 0.234 ns 1.11 0.0048 40 B
(InterpolatedStringHandler) 32.67 ns 0.487 ns 2.68 0.0067 56 B
(CompositeFormat) 28.76 ns 0.412 ns 2.36 0.0067 56 B

string

Method Mean Error Ratio Gen0 Gen1 Allocated
SimpleTextTemplate.Generator 25.32 ns 0.062 ns 1.00 0.0057 - 48 B
SimpleTextTemplate 61.39 ns 0.530 ns 2.42 0.0057 - 48 B
Scriban 8,339.53 ns 46.900 ns 329.31 3.7842 0.3662 32071 B
Scriban_Liquid 6,670.99 ns 66.825 ns 263.43 4.0131 0.3815 33602 B
(Utf8.TryWrite) 22.73 ns 0.346 ns 0.90 0.0057 - 48 B
(InterpolatedStringHandler) 30.22 ns 0.393 ns 1.19 0.0086 - 72 B
(CompositeFormat) 27.75 ns 0.348 ns 1.10 0.0086 - 72 B

int

Method Mean Error Ratio Gen0 Gen1 Allocated
SimpleTextTemplate.Generator 19.00 ns 0.067 ns 1.00 0.0057 - 48 B
SimpleTextTemplate 60.97 ns 0.690 ns 3.21 0.0057 - 48 B
Scriban 8,600.22 ns 78.630 ns 452.65 3.7842 0.3662 32095 B
Scriban_Liquid 6,794.65 ns 98.124 ns 357.62 4.0131 0.3967 33626 B
(Utf8.TryWrite) 17.98 ns 0.313 ns 0.95 0.0057 - 48 B
(InterpolatedStringHandler) 34.09 ns 0.439 ns 1.79 0.0076 - 64 B
(CompositeFormat) 29.13 ns 0.352 ns 1.53 0.0076 - 64 B

()で囲まれているメソッドは参考として記載。

ベンチマークプロジェクト

サポートフレームワーク

.NET 9

作者

finphie

ライセンス

MIT

クレジット

このプロジェクトでは、次のライブラリ等を使用しています。

ライブラリ

テスト

アナライザー

ベンチマーク

その他

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net9.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on SimpleTextTemplate.Writer:

Package Downloads
SimpleTextTemplate.Generator

コンパイル時にテンプレートの解析を行うソースジェネレーターです。

SimpleTextTemplate

シンプルなテキストテンプレートエンジンです。

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.1.0 270 5/24/2025
3.0.0 526 11/17/2024