SimpleTextTemplate.Contexts
2.1.0
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package SimpleTextTemplate.Contexts --version 2.1.0
NuGet\Install-Package SimpleTextTemplate.Contexts -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.Contexts" Version="2.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SimpleTextTemplate.Contexts --version 2.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: SimpleTextTemplate.Contexts, 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.Contexts as a Cake Addin #addin nuget:?package=SimpleTextTemplate.Contexts&version=2.1.0 // Install SimpleTextTemplate.Contexts as a Cake Tool #tool nuget:?package=SimpleTextTemplate.Contexts&version=2.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
SimpleTextTemplate
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(非推奨)
SimpleTextTemplateとSimpleTextTemplate.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
クレジット
このプロジェクトでは、次のライブラリ等を使用しています。
ライブラリ
- CommunityToolkit.Diagnostics
- CommunityToolkit.HighPerformance
- Microsoft.CodeAnalysis.CSharp
- Utf8Utility
テスト
- FluentAssertions
- Microsoft.NET.Test.Sdk
- Newtonsoft.Json
- NuGet.Frameworks
- xunit
- xunit.runner.visualstudio
アナライザー
- DocumentationAnalyzers
- IDisposableAnalyzers
- Microsoft.CodeAnalysis.Analyzers
- Microsoft.CodeAnalysis.NetAnalyzers
- Microsoft.VisualStudio.Threading.Analyzers
- StyleCop.Analyzers
ベンチマーク
- BenchmarkDotNet
- CommandLineParser
- Iced
- Microsoft.CodeAnalysis.CSharp
- Microsoft.Diagnostics.NETCore.Client
- Microsoft.Diagnostics.Runtime
- Microsoft.Diagnostics.Tracing.TraceEvent
- Perfolizer
- Scriban
その他
Product | Versions 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.
-
net8.0
- SimpleTextTemplate.Abstractions (>= 2.1.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.