RSCG – StringLiteral

RSCG – StringLiteral
 
 

name StringLiteral
nuget https://www.nuget.org/packages/StringLiteralGenerator/
link https://github.com/ufcpp/StringLiteralGenerator
author Nobuyuki Iwanaga

Optimizing memory for strings

 

This is how you can use StringLiteral .

The code that you start with is


<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>


The code that you will use is


using StringLiteralDemo;
using System.Text;

Console.WriteLine(Encoding.UTF8.GetString(LiteralConstants.MyName()));



namespace StringLiteralDemo;

partial class LiteralConstants
{
    [StringLiteral.Utf8Attribute("Andrei Ignat")]
    public static partial System.ReadOnlySpan<byte> MyName();
}


 

The code that is generated is

// <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) { }
    }
}

Code and pdf at

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