StringLiteral by Nobuyuki Iwanaga
Nuget / site data
Details
Info
Name: StringLiteral
A C# Source Generator for optimizing UTF-8 binaries.
Author: Nobuyuki Iwanaga
NuGet: https://www.nuget.org/packages/StringLiteralGenerator/
You can find more details at https://github.com/ufcpp/StringLiteralGenerator
Original Readme
C# StringLiteralGenerator
A C# Source Generator for optimizing UTF-8 binaries.
Original source (manually written):
namespace Sample
{
partial class Literals
{
[StringLiteral.Utf8Attribute("aαあ😊")]
public static partial System.ReadOnlySpan<byte> S();
}
}
Generated source:
namespace Sample
{
partial class Literals
{
public static partial System.ReadOnlySpan<byte> S() => new byte[] {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
NuGet
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="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:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="StringLiteralGenerator" Version="1.0.0-preiew" />
<PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="3.8.0-4.final" PrivateAssets="all" />
</ItemGroup>
</Project>
About
Optimizing memory for strings
How to use
Example ( source csproj, source files )
- CSharp Project
- Program.cs
- LiteralConstants.cs
This is the CSharp Project that references StringLiteral
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="StringLiteralGenerator" Version="2.0.0" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>
This is the use of StringLiteral in Program.cs
using StringLiteralDemo;
using System.Text;
Console.WriteLine(Encoding.UTF8.GetString(LiteralConstants.MyName()));
This is the use of StringLiteral in LiteralConstants.cs
namespace StringLiteralDemo;
partial class LiteralConstants
{
[StringLiteral.Utf8Attribute("Andrei Ignat")]
public static partial System.ReadOnlySpan<byte> MyName();
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- StringLiteralDemo_LiteralConstants_utf8literal.cs
- Utf8Attribute.cs
// <auto-generated />
namespace StringLiteralDemo
{
partial class LiteralConstants
{
public static partial System.ReadOnlySpan<byte> MyName() => new byte[] {65, 110, 100, 114, 101, 105, 32, 73, 103, 110, 97, 116, };
}
}
// <auto-generated />
using System;
namespace StringLiteral
{
[System.Diagnostics.Conditional("COMPILE_TIME_ONLY")]
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
sealed class Utf8Attribute : Attribute
{
public Utf8Attribute(string s) { }
}
}
Usefull
Download Example (.NET C# )
Share StringLiteral
https://ignatandrei.github.io/RSCG_Examples/v2/docs/StringLiteral