Skip to content

Repository files navigation

SimpleTextTemplate

NuGetAzure Artifacts

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

説明

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

Important

一部のカルチャーでは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

usingSystem;usingSystem.Buffers;usingSystem.Globalization;usingSystem.Text;usingSimpleTextTemplate;varbufferWriter=newArrayBufferWriter<byte>();varcontext=newSampleContext("Hello, World",1000,new(2000,1,1,0,0,0,TimeSpan.Zero));varwriter=TemplateWriter.Create(bufferWriter);TemplateRenderer.Render(refwriter,"{{ DateTimeOffsetValue:o }}_{{ StringValue }}!",incontext);TemplateRenderer.Render(refwriter,"_{{ ConstantString }}_{{ ConstantInt:N3:ja-JP }}_{{ IntValue }}",incontext,CultureInfo.InvariantCulture);writer.Flush();// 2000-01-01T00:00:00.0000000+00:00_Hello, World!_Hello_999.000_1000Console.WriteLine(Encoding.UTF8.GetString(bufferWriter.WrittenSpan));readonlyrecordstructSampleContext(stringStringValue,intIntValue,DateTimeOffsetDateTimeOffsetValue){publicconststringConstantString="Hello";publicconstintConstantInt=999;}

生成コード

filestaticclassIntercept{[global::System.Runtime.CompilerServices.InterceptsLocation(1,"...")]publicstaticvoidRender0(refglobal::SimpleTextTemplate.TemplateWriter<global::System.Buffers.ArrayBufferWriter<byte>>writer,stringtext,inglobal::SampleContextcontext,global::System.IFormatProviderprovider=null){writer.WriteValue(global::System.Runtime.CompilerServices.Unsafe.AsRef(incontext).@DateTimeOffsetValue,"o",global::System.Globalization.CultureInfo.InvariantCulture);writer.Grow(2+global::System.Text.Encoding.UTF8.GetMaxByteCount(global::System.Runtime.CompilerServices.Unsafe.AsRef(incontext).@StringValue.Length));writer.DangerousWriteConstantLiteral("_"u8);writer.DangerousWriteString(global::System.Runtime.CompilerServices.Unsafe.AsRef(incontext).@StringValue);writer.DangerousWriteConstantLiteral("!"u8);}[global::System.Runtime.CompilerServices.InterceptsLocation(1,"...")]publicstaticvoidRender1(refglobal::SimpleTextTemplate.TemplateWriter<global::System.Buffers.ArrayBufferWriter<byte>>writer,stringtext,inglobal::SampleContextcontext,global::System.IFormatProviderprovider=null){writer.Grow(15);writer.DangerousWriteConstantLiteral("_Hello_999.000_"u8);writer.WriteValue(global::System.Runtime.CompilerServices.Unsafe.AsRef(incontext).@IntValue,default,global::System.Globalization.CultureInfo.InvariantCulture);}}

SimpleTextTemplate

usingSystem;usingSystem.Buffers;usingSystem.Text;usingSimpleTextTemplate;varsymbols=Context.Create();symbols.Add("Identifier"u8.ToArray(),"Hello, World!"u8.ToArray());varbufferWriter=newArrayBufferWriter<byte>();varsource="{{ Identifier }}"u8.ToArray();vartemplate=Template.Parse(source);template.Render(bufferWriter,symbols);// Hello, World!Console.WriteLine(Encoding.UTF8.GetString(bufferWriter.WrittenSpan));

ベンチマーク

定数(string)

MethodMeanErrorRatioGen0Gen1Allocated
SimpleTextTemplate.Generator4.664 ns0.0067 ns1.00---
SimpleTextTemplate100.016 ns0.1215 ns21.45---
Utf8.TryWrite26.145 ns0.0387 ns5.61---
InterpolatedStringHandler17.810 ns0.3476 ns3.820.0043-72 B
CompositeFormat40.057 ns0.2720 ns8.590.0043-72 B
Regex152.400 ns0.2889 ns32.680.0043-72 B
Scriban10,953.132 ns157.4480 ns2,348.543.66210.366262090 B
Scriban_Liquid9,733.122 ns90.6237 ns2,086.953.84520.366264374 B

定数(int)

MethodMeanErrorRatioGen0Gen1Allocated
SimpleTextTemplate.Generator4.627 ns0.0193 ns1.00---
SimpleTextTemplate71.926 ns0.0699 ns15.54---
Utf8.TryWrite14.638 ns0.0376 ns3.16---
InterpolatedStringHandler34.755 ns0.1168 ns7.510.0043-72 B
CompositeFormat49.985 ns0.7447 ns10.800.0043-72 B
Regex158.656 ns0.4039 ns34.290.0062-104 B
Scriban11,049.313 ns131.1204 ns2,387.863.69260.366262247 B
Scriban_Liquid10,085.002 ns102.6479 ns2,179.463.84520.366264534 B

string

MethodMeanErrorRatioGen0Gen1Allocated
SimpleTextTemplate.Generator29.73 ns0.069 ns1.00---
SimpleTextTemplate88.72 ns0.461 ns2.98---
Utf8.TryWrite27.01 ns0.021 ns0.91---
InterpolatedStringHandler26.65 ns0.132 ns0.900.0043-72 B
CompositeFormat41.19 ns0.120 ns1.390.0043-72 B
Regex154.58 ns0.560 ns5.200.0043-72 B
Scriban10,953.11 ns176.259 ns368.473.66210.366262103 B
Scriban_Liquid9,955.17 ns135.368 ns334.903.84520.366264374 B

int

MethodMeanErrorRatioGen0Gen1Allocated
SimpleTextTemplate.Generator24.41 ns0.053 ns1.00---
SimpleTextTemplate76.66 ns0.090 ns3.14---
Utf8.TryWrite20.20 ns0.052 ns0.83---
InterpolatedStringHandler35.54 ns0.162 ns1.460.0043-72 B
CompositeFormat49.87 ns0.135 ns2.040.0043-72 B
Regex148.38 ns1.536 ns6.080.0062-104 B
Scriban11,223.34 ns111.485 ns459.733.66210.244162255 B
Scriban_Liquid9,870.61 ns127.730 ns404.323.84520.366264534 B

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

サポートフレームワーク

  • .NET 11
  • .NET 10

作者

finphie

ライセンス

MIT

クレジット

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

ライブラリ

テスト

アナライザー

ベンチマーク

About

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

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages

Generated from finphie/dotfiles