Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ufcpp/StringLiteralGenerator

C# Source Generator for UTF-8 binary literal
https://github.com/ufcpp/StringLiteralGenerator

csharp-sourcegenerator

Last synced: 3 months ago
JSON representation

C# Source Generator for UTF-8 binary literal

Awesome Lists containing this project

README

        

# C# StringLiteralGenerator

A C# [Source Generator](https://github.com/dotnet/roslyn/blob/master/docs/features/source-generators.md) for optimizing UTF-8 binaries.

Original source (manually written):

```cs
namespace Sample
{
partial class Literals
{
[StringLiteral.Utf8Attribute("aαあ😊")]
public static partial System.ReadOnlySpan S();
}
}
```

Generated source:

```cs
namespace Sample
{
partial class Literals
{
public static partial System.ReadOnlySpan 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](https://vcsjones.dev/2019/02/01/csharp-readonly-span-bytes-static/)

## NuGet

[![NuGet](https://img.shields.io/nuget/v/StringLiteralGenerator?style=flat-square)](https://www.nuget.org/packages/StringLiteralGenerator)

```xml


Exe
net5.0



```

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:
```xml


Exe
net5.0




```