SimpleTextTemplate 2.1.0

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

// Install SimpleTextTemplate as a Cake Tool
#tool nuget:?package=SimpleTextTemplate&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 net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
2.1.0 1,048 2/10/2024
2.0.0 2,083 11/15/2023
1.1.0 252 3/18/2023
1.0.1 635 2/22/2022
1.0.0 453 2/22/2022
0.6.0 500 1/14/2022
0.5.0 463 10/17/2021
0.4.0 378 9/4/2021
0.3.0 436 8/29/2021
0.2.0 1,934 6/4/2021
0.1.0 393 5/16/2021