Skip to main content

StringLiteral by Nobuyuki Iwanaga

Nuget / site data

Nuget GitHub last commit GitHub Repo stars

Details

Info

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

Source : https://github.com/ufcpp/StringLiteralGenerator

Original Readme

note

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, };
}
}

NuGet

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

note

Optimizing memory for strings

How to use

Example ( source csproj, source files )

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>

Generated Files

Those are taken from $(BaseIntermediateOutputPath)\GX

// <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, };
}
}

Usefull

Download Example (.NET C# )

Share StringLiteral

https://ignatandrei.github.io/RSCG_Examples/v2/docs/StringLiteral

In the same category (Optimizer) - 0 other generators