A C# Source Generator for optimizing UTF-8 binaries.
Original source (manually written):
namespaceSample{partialclassLiterals{[StringLiteral.Utf8Attribute("aαあ😊")]publicstaticpartialSystem.ReadOnlySpan<byte>S();}}Generated source:
namespaceSample{partialclassLiterals{publicstaticpartialSystem.ReadOnlySpan<byte>S()=>newbyte[]{97,206,177,227,129,130,240,159,152,138,};}}- Generates UTF-8 binary data from string literals (UTF-16).
- The UTF-8 data is optimized to avoid allocation. see: C# ReadOnlySpan and static data
<ProjectSdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReferenceInclude="StringLiteralGenerator"Version="1.0.0" />
</ItemGroup>
</Project>For versions earlier than .NET 5 SDK RC2 you may also need to add a reference to Microsoft.Net.Compilers.Toolset.
So the csproj may look like this:
<ProjectSdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReferenceInclude="StringLiteralGenerator"Version="1.0.0-preiew" />
<PackageReferenceInclude="Microsoft.Net.Compilers.Toolset"Version="3.8.0-4.final"PrivateAssets="all" />
</ItemGroup>
</Project>