InterpolatedParser 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package InterpolatedParser --version 1.0.0
                    
NuGet\Install-Package InterpolatedParser -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="InterpolatedParser" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="InterpolatedParser" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="InterpolatedParser" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add InterpolatedParser --version 1.0.0
                    
#r "nuget: InterpolatedParser, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package InterpolatedParser@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=InterpolatedParser&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=InterpolatedParser&version=1.0.0
                    
Install as a Cake Tool

InterpolatedParser

DO NOT USE THIS LIBRARY IN PRODUCTION
The way InterpolatedParser works is by using unsafe code that is very volatile. Do not use this outside small short lived executions to test things or parse simple strings. It's meant more of a weird language showcase than an actual product.

InterpolatedParser is a nuget library enabling string interpolation, but in reverse.

Example code:

using InterpolatedParsing;

int x = 0;

string input = "x is 69";

InterpolatedParser.Parse($"x is {x}", input);

Console.WriteLine(x); // Prints 69.

Usage

Limitations

Make sure any variable you parse into is a variable living on the stack. The reason why is explained below.

Supported types

InterpolatedParser supports anything that implements IParseable<T>, which includes many common types in .NET. This also means you can use your own types by having them implement IParseable<T>.

Collections

The parser supports Lists and Arrays. A separator is provided as a format string. Format strings don't allow trailing whitespace, so if you need that enclose the format string in single quotes.

using InterpolatedParsing;

List<int> numbers = null!;

InterpolatedParser.Parse(
	$"Winning numbers are: {x:,}",
	"Winning numbers are: 5,10,15,25);

List<string> beans = null!;
InterpolatedParser.Parse(
	$"Bean list: {x:', '}", // Add single quotes to support whitespace
	"Bean list: "black", "coffee", "green");

This is cursed, how does it do that?

C# 10 added support for writing custom interpolated string handlers. At compile time they translate interpolated strings into a series of calls to magic methods: AppendLiteral for literal strings, and AppendFormatted to the parameters of the string.

var str = $"Hello {123}!";

// Becomes this code on compile time:

var handler = new DefaultInterpolatedStringHandler(7, 4);
handler.AppendLiteral("Hello ");
handler.AppendFormatted(123);
handler.AppendLiteral("!");
var str = handler.ToStringAndClear();

That's all good and normally doesn't enable the shenanigans we need. However we can abuse the in parameter modifier. The in parameter can be implicit, so the generated calls to AppendFormatted allow it. This means we're now passing down a read only reference to the value when we call AppendFormatted. This is where things become really cursed, using unsafe code it's casted down to a void* so we can keep a reference to it.

    public readonly void AppendFormatted(in int value) {
        void* pointer = Unsafe.AsPointer(ref Unsafe.AsRef(in value));

As you might imagine this is very unsafe and volatile. Because the reference is needed between method calls, the pointer can also not be pinned so it can be moved around by garbage collection. That's why it's very important to only use this for stack variables.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on InterpolatedParser:

Package Downloads
VeeFriends.WikiImporter

VeeFriends Wiki Importer - A .NET library for automating the import of character data from ClickUp, enhanced with AI and media integrations.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.5 38,363 2/21/2025
1.1.4 2,299 2/7/2025
1.1.3 249 12/13/2024
1.1.2 144 12/5/2024
1.1.1 207 8/25/2024
1.1.0 172 8/25/2024
1.0.0 326 12/9/2023