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.
Make sure any variable you parse into is a variable living on the stack. The reason why is explained below.
InterpolatedParser supports anything that implements IParseable, which includes many common types in .NET. This also means you can use your own types by having them implement IParseable.
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");
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.