SimpleTextTemplate.Generator 3.0.0

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

// Install SimpleTextTemplate.Generator as a Cake Tool
#tool nuget:?package=SimpleTextTemplate.Generator&version=3.0.0                

SimpleTextTemplate

Build(.NET) NuGet Azure Artifacts

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

説明

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

インストール

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 CommunityToolkit.HighPerformance.Buffers;
using SimpleTextTemplate;

file static class Intercept
{
    [InterceptsLocation(1, "...")]
    public static void Write0(ref TemplateWriter<ArrayPoolBufferWriter<byte>> writer, string text, in SampleContext context, IFormatProvider? provider = null)
    {
        writer.WriteValue(Unsafe.AsRef(in context).@DateTimeOffsetValue, "o", CultureInfo.InvariantCulture);
        writer.WriteConstantLiteral("_"u8);
        writer.WriteString(Unsafe.AsRef(in context).@StringValue);
        writer.WriteConstantLiteral("!"u8);
    }

    [InterceptsLocation(1, "...")]
    public static void Write1(ref TemplateWriter<ArrayPoolBufferWriter<byte>> writer, string text, in SampleContext context, IFormatProvider? provider = null)
    {
        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;
using SimpleTextTemplate.Contexts;
using Utf8Utility;

var symbols = new Utf8ArrayDictionary<Utf8Array>();
symbols.TryAdd((Utf8Array)"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, Context.Create(symbols));

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

</details>

ベンチマーク

Method Categories Mean Error Ratio Gen0 Gen1 Allocated
SimpleTextTemplate.Generator Constant String 13.31 ns 0.179 ns 1.00 0.0067 - 56 B
(Utf8.TryWrite) Constant String 23.42 ns 0.205 ns 1.76 0.0067 - 56 B
(InterpolatedStringHandler) Constant String 39.87 ns 0.268 ns 3.00 0.0105 - 88 B
(CompositeFormat) Constant String 34.57 ns 0.322 ns 2.60 0.0105 - 88 B
SimpleTextTemplate.Generator Constant Int 12.52 ns 0.189 ns 1.00 0.0048 - 40 B
(Utf8.TryWrite) Constant Int 23.29 ns 0.101 ns 1.86 0.0048 - 40 B
(InterpolatedStringHandler) Constant Int 40.04 ns 0.253 ns 3.20 0.0067 - 56 B
(CompositeFormat) Constant Int 30.04 ns 0.171 ns 2.40 0.0067 - 56 B
SimpleTextTemplate.Generator String 24.95 ns 0.242 ns 1.00 0.0057 - 48 B
SimpleTextTemplate String 79.17 ns 0.150 ns 3.17 0.0057 - 48 B
Scriban String 8,751.57 ns 39.993 ns 350.74 3.6926 0.3357 31003 B
Liquid String 7,581.99 ns 80.655 ns 303.84 3.9902 0.4044 33418 B
(Utf8.TryWrite) String 22.21 ns 0.120 ns 0.89 0.0057 - 48 B
(InterpolatedStringHandler) String 38.73 ns 0.219 ns 1.55 0.0086 - 72 B
(CompositeFormat) String 39.36 ns 0.432 ns 1.58 0.0086 - 72 B
SimpleTextTemplate.Generator Int 20.77 ns 0.146 ns 1.00 0.0057 - 48 B
SimpleTextTemplate Int 81.29 ns 0.097 ns 3.91 0.0057 - 48 B
Scriban Int 9,066.34 ns 81.421 ns 436.55 3.6926 0.3357 31027 B
Liquid Int 6,999.40 ns 18.696 ns 337.04 3.9978 0.4425 33442 B
(Utf8.TryWrite) Int 16.55 ns 0.197 ns 0.80 0.0057 - 48 B
(InterpolatedStringHandler) Int 39.82 ns 0.246 ns 1.92 0.0076 - 64 B
(CompositeFormat) Int 30.22 ns 0.240 ns 1.46 0.0076 - 64 B

[!Note] ()で囲まれているメソッドは正確には処理が異なるが、参考として記載。

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

サポートフレームワーク

.NET 9

作者

finphie

ライセンス

MIT

クレジット

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

ライブラリ

テスト

アナライザー

ベンチマーク

その他

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible. 
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.0.0 84 11/17/2024
2.1.0 1,153 2/10/2024
2.0.0 2,234 11/15/2023
1.1.0 269 3/18/2023
1.0.1 630 2/22/2022
1.0.0 432 2/22/2022
0.6.0 475 1/14/2022
0.5.0 348 10/17/2021
0.4.0 360 9/4/2021
0.3.0 431 8/29/2021
0.2.0 1,904 6/4/2021
0.1.0 406 5/16/2021