SimpleTextTemplate.Generator 3.2.0

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

SimpleTextTemplate

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

使い方

サンプルプロジェクト

SimpleTextTemplate.Generator

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

var bufferWriter = new ArrayBufferWriter<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;
}
生成コード
file static class Intercept
{
    [global::System.Runtime.CompilerServices.InterceptsLocation(1, "...")]
    public static void Render0(ref global::SimpleTextTemplate.TemplateWriter<global::System.Buffers.ArrayBufferWriter<byte>> writer, string text, in global::SampleContext context, global::System.IFormatProvider provider = null)
    {
        writer.WriteValue(global::System.Runtime.CompilerServices.Unsafe.AsRef(in context).@DateTimeOffsetValue, "o", global::System.Globalization.CultureInfo.InvariantCulture);
        writer.Grow(2
            + global::System.Text.Encoding.UTF8.GetMaxByteCount(
                global::System.Runtime.CompilerServices.Unsafe.AsRef(in context).@StringValue.Length));
        writer.DangerousWriteConstantLiteral("_"u8);
        writer.DangerousWriteString(global::System.Runtime.CompilerServices.Unsafe.AsRef(in context).@StringValue);
        writer.DangerousWriteConstantLiteral("!"u8);
    }

    [global::System.Runtime.CompilerServices.InterceptsLocation(1, "...")]
    public static void Render1(ref global::SimpleTextTemplate.TemplateWriter<global::System.Buffers.ArrayBufferWriter<byte>> writer, string text, in global::SampleContext context, global::System.IFormatProvider provider = null)
    {
        writer.Grow(15);
        writer.DangerousWriteConstantLiteral("_Hello_999.000_"u8);
        writer.WriteValue(global::System.Runtime.CompilerServices.Unsafe.AsRef(in context).@IntValue, default, global::System.Globalization.CultureInfo.InvariantCulture);
    }
}

SimpleTextTemplate

using System;
using System.Buffers;
using System.Text;
using SimpleTextTemplate;

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

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

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

ベンチマーク

定数(string)

Method Mean Error Ratio Gen0 Gen1 Allocated
SimpleTextTemplate.Generator 4.664 ns 0.0067 ns 1.00 - - -
SimpleTextTemplate 100.016 ns 0.1215 ns 21.45 - - -
Utf8.TryWrite 26.145 ns 0.0387 ns 5.61 - - -
InterpolatedStringHandler 17.810 ns 0.3476 ns 3.82 0.0043 - 72 B
CompositeFormat 40.057 ns 0.2720 ns 8.59 0.0043 - 72 B
Regex 152.400 ns 0.2889 ns 32.68 0.0043 - 72 B
Scriban 10,953.132 ns 157.4480 ns 2,348.54 3.6621 0.3662 62090 B
Scriban_Liquid 9,733.122 ns 90.6237 ns 2,086.95 3.8452 0.3662 64374 B

定数(int)

Method Mean Error Ratio Gen0 Gen1 Allocated
SimpleTextTemplate.Generator 4.627 ns 0.0193 ns 1.00 - - -
SimpleTextTemplate 71.926 ns 0.0699 ns 15.54 - - -
Utf8.TryWrite 14.638 ns 0.0376 ns 3.16 - - -
InterpolatedStringHandler 34.755 ns 0.1168 ns 7.51 0.0043 - 72 B
CompositeFormat 49.985 ns 0.7447 ns 10.80 0.0043 - 72 B
Regex 158.656 ns 0.4039 ns 34.29 0.0062 - 104 B
Scriban 11,049.313 ns 131.1204 ns 2,387.86 3.6926 0.3662 62247 B
Scriban_Liquid 10,085.002 ns 102.6479 ns 2,179.46 3.8452 0.3662 64534 B

string

Method Mean Error Ratio Gen0 Gen1 Allocated
SimpleTextTemplate.Generator 29.73 ns 0.069 ns 1.00 - - -
SimpleTextTemplate 88.72 ns 0.461 ns 2.98 - - -
Utf8.TryWrite 27.01 ns 0.021 ns 0.91 - - -
InterpolatedStringHandler 26.65 ns 0.132 ns 0.90 0.0043 - 72 B
CompositeFormat 41.19 ns 0.120 ns 1.39 0.0043 - 72 B
Regex 154.58 ns 0.560 ns 5.20 0.0043 - 72 B
Scriban 10,953.11 ns 176.259 ns 368.47 3.6621 0.3662 62103 B
Scriban_Liquid 9,955.17 ns 135.368 ns 334.90 3.8452 0.3662 64374 B

int

Method Mean Error Ratio Gen0 Gen1 Allocated
SimpleTextTemplate.Generator 24.41 ns 0.053 ns 1.00 - - -
SimpleTextTemplate 76.66 ns 0.090 ns 3.14 - - -
Utf8.TryWrite 20.20 ns 0.052 ns 0.83 - - -
InterpolatedStringHandler 35.54 ns 0.162 ns 1.46 0.0043 - 72 B
CompositeFormat 49.87 ns 0.135 ns 2.04 0.0043 - 72 B
Regex 148.38 ns 1.536 ns 6.08 0.0062 - 104 B
Scriban 11,223.34 ns 111.485 ns 459.73 3.6621 0.2441 62255 B
Scriban_Liquid 9,870.61 ns 127.730 ns 404.32 3.8452 0.3662 64534 B

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

サポートフレームワーク

  • .NET 11
  • .NET 10

作者

finphie

ライセンス

MIT

クレジット

このプロジェクトでは、次のパッケージ等を使用しています。

ライブラリ

テスト

アナライザー

ベンチマーク

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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.2.0 93 5/1/2026
3.1.0 566 5/24/2025
3.0.0 837 11/17/2024
2.1.0 1,242 2/10/2024
2.0.0 2,314 11/15/2023
1.1.0 393 3/18/2023
1.0.1 799 2/22/2022
1.0.0 602 2/22/2022
0.6.0 656 1/14/2022
0.5.0 550 10/17/2021
0.4.0 541 9/4/2021
0.3.0 614 8/29/2021
0.2.0 2,080 6/4/2021
0.1.0 594 5/16/2021