SimpleTextTemplate.Abstractions 2.1.0

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

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

SimpleTextTemplate

Build(.NET) NuGet Azure Artifacts

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

説明

  • 文字列をUTF-8バイト列としてIBufferWriter<byte>に出力します。
  • 文字列を{{}}で囲むことで変数を埋め込みます。
  • {{}}内の先頭と末尾の空白(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.Buffers;
using System.Text;
using CommunityToolkit.HighPerformance.Buffers;
using SimpleTextTemplate;

var context = new SampleContext("Hello, World", new(2000, 1, 1, 0, 0, 0, TimeSpan.Zero));

var bufferWriter = new ArrayPoolBufferWriter<byte>();
var template = new TemplateWriter<ArrayPoolBufferWriter<byte>>(ref bufferWriter);

template.Write("{{ DateTime }}_{{ Identifier }}!!!", context);
template.Dispose();

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

bufferWriter.Dispose();

readonly record struct SampleContext(
    string Identifier,
    [property: Identifier("o")] DateTimeOffset DateTime);

サンプルプロジェクト

生成コード
file static class Intercept
{
    [global::System.Runtime.CompilerServices.InterceptsLocation(@"<path>\Program.cs", 11, 10)]
    public static void Write0(this ref global::SimpleTextTemplate.TemplateWriter<global::CommunityToolkit.HighPerformance.Buffers.ArrayPoolBufferWriter<byte>> writer, string _, in global::SampleContext context)
    {
        writer.WriteValue(global::System.Runtime.CompilerServices.Unsafe.AsRef(in context).@DateTime, "o");
        writer.WriteConstantLiteral("_"u8);
        writer.WriteString(global::System.Runtime.CompilerServices.Unsafe.AsRef(in context).@Identifier);
        writer.WriteConstantLiteral("!!!"u8);
    }
}

SimpleTextTemplate(非推奨)

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(), (Utf8Array)"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));

ベンチマーク

Method Mean Error Ratio Gen0 Gen1 Allocated
SimpleTextTemplate 38.51 ns 0.149 ns 1.95 0.0067 - 56 B
SimpleTextTemplate_SG 18.83 ns 0.425 ns 1.00 0.0067 - 56 B
Scriban 8,532.18 ns 128.391 ns 434.20 3.6621 0.3357 30778 B
ScribanLiquid 6,945.50 ns 34.946 ns 352.62 3.9673 0.3891 33194 B
(Utf8.TryWrite) 22.42 ns 0.470 ns 1.18 0.0067 - 56 B
(InterpolatedStringHandler) 40.12 ns 0.299 ns 2.04 0.0105 - 88 B
(Regex.Replace) 130.70 ns 0.513 ns 6.65 0.0105 - 88 B
(string.Format) 52.18 ns 1.083 ns 2.74 0.0105 - 88 B
(CompositeFormat) 37.80 ns 0.696 ns 1.94 0.0105 - 88 B

[!Note] UTF-8またはUTF-16で出力
()で囲まれているメソッドは正確には処理が異なるため、参考情報

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

サポートフレームワーク

.NET 8

作者

finphie

ライセンス

MIT

クレジット

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

ライブラリ

テスト

アナライザー

ベンチマーク

その他

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

NuGet packages (2)

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

Package Downloads
SimpleTextTemplate

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

SimpleTextTemplate.Contexts

テンプレートのレンダリングで使用するコンテキストの作成を行います。

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.1.0 1,077 2/10/2024
2.0.0 2,185 11/15/2023
1.1.0 313 3/18/2023
1.0.1 842 2/22/2022
1.0.0 651 2/22/2022
0.6.0 1,115 1/14/2022
0.5.0 791 10/17/2021
0.4.0 692 9/4/2021
0.3.0 734 8/29/2021
0.2.0 2,253 6/4/2021
0.1.0 780 5/16/2021