SimpleTextTemplate.Generator 3.1.0

dotnet add package SimpleTextTemplate.Generator --version 3.1.0
                    
NuGet\Install-Package SimpleTextTemplate.Generator -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.Generator" Version="3.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SimpleTextTemplate.Generator" Version="3.1.0" />
                    
Directory.Packages.props
<PackageReference Include="SimpleTextTemplate.Generator" />
                    
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.Generator --version 3.1.0
                    
#r "nuget: SimpleTextTemplate.Generator, 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.
#addin nuget:?package=SimpleTextTemplate.Generator&version=3.1.0
                    
Install as a Cake Addin
#tool nuget:?package=SimpleTextTemplate.Generator&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.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.1.0 124 5/24/2025
3.0.0 364 11/17/2024
2.1.0 1,174 2/10/2024
2.0.0 2,247 11/15/2023
1.1.0 298 3/18/2023
1.0.1 677 2/22/2022
1.0.0 481 2/22/2022
0.6.0 524 1/14/2022
0.5.0 402 10/17/2021
0.4.0 412 9/4/2021
0.3.0 485 8/29/2021
0.2.0 1,957 6/4/2021
0.1.0 460 5/16/2021