Skip to main content

OutParser by Anton Bergaker

NuGet / site data​

Nuget GitHub last commit GitHub Repo stars

Details​

Info​

info

Name: OutParser

A C# string parser based on simple templates using source generators.

Author: Anton Bergaker

NuGet: https://www.nuget.org/packages/OutParser/

You can find more details at https://github.com/AntonBergaker/OutParser

Source: https://github.com/AntonBergaker/OutParser

Author​

note

Anton Bergaker Alt text

Original Readme​

note

OutParser​

OutParser is a nuget library enabling simple string parsing following a simple template pattern.

Example usage:

using OutParsing;

string input = "I selected 69";
OutParser.Parse(input, "I selected {value}", out int value);

Console.WriteLine(value); // Prints 69
Usage​

######### Installing OutParser requires .NET 9.0 as it makes use of Interceptors. The recommended way to add OutParser to your project is to install it from nuget.

######### Supported types OutParser supports anything that implements IParseable<T> and ISpanParseable<T>, which includes many common types in .NET. This also means you can use your own types by having them implement either of the two interfaces.

######### Collections The parser supports Lists and Arrays. A separator is provided after the colon in the template string, like this: OutParser.Parse("1,2,3", "{numbers:,}", out List<int> values);.

About​

note

Parsing typed values and collections from strings using templates

OutParser generates interceptors for literal templates. It requires .NET 9 or later and the OutParsing interceptor namespace.

How to use​

Example (source csproj, source files)​

This is the CSharp Project that references OutParser

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<InterceptorsNamespaces>$(InterceptorsNamespaces);OutParsing</InterceptorsNamespaces>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="OutParser" Version="1.1.1" />
</ItemGroup>
</Project>

Generated Files​

Those are taken from $(BaseIntermediateOutputPath)\GX

#nullable enable
namespace OutParsing {
internal static partial class OutParser {
private struct OutParserInstance {
private readonly string _inputString;
private readonly string[] _templateComponents;
private int _currentIndex;
private int _currentStringPosition;

public OutParserInstance(string input, string[] templateComponents) {
_inputString = input;
_currentIndex = 1;
_currentStringPosition = templateComponents[0].Length;
_templateComponents = templateComponents;
}

private delegate T ParsableConvertMethod<T>(global::System.ReadOnlySpan<char> span);

private static ParsableConvertMethod<T> GetParsableConvertMethod<T>()
where T : global::System.IParsable<T> =>
static x => T.Parse(new string(x), System.Globalization.CultureInfo.InvariantCulture);

private static ParsableConvertMethod<T> GetSpanParsableConvertMethod<T>()
where T : global::System.ISpanParsable<T> =>
static x => T.Parse(x, System.Globalization.CultureInfo.InvariantCulture);


#region Single Parsable
public T GetParsable<T>() where T : global::System.IParsable<T> {
return GetSpanParsableFunction(GetParsableConvertMethod<T>());
}

public T GetSpanParsable<T>() where T : global::System.ISpanParsable<T> {
return GetSpanParsableFunction(GetSpanParsableConvertMethod<T>());
}

private T GetSpanParsableFunction<T>(ParsableConvertMethod<T> parseFunction) {
var nextString = GetNextPart();
return parseFunction(nextString);
}
#endregion

#region List Parsable
public global::System.Collections.Generic.List<T> GetParsableList<T>(string separator) where T : global::System.IParsable<T> {
return GetSpanParsableListFunction(GetParsableConvertMethod<T>(), separator);
}

public global::System.Collections.Generic.List<T> GetSpanParsableList<T>(string separator) where T : global::System.ISpanParsable<T> {
return GetSpanParsableListFunction(GetSpanParsableConvertMethod<T>(), separator);
}

private global::System.Collections.Generic.List<T> GetSpanParsableListFunction<T>(ParsableConvertMethod<T> parseFunction, string separator) {
var chars = GetNextPart();
var list = new global::System.Collections.Generic.List<T>();

while (true) {
int index = System.MemoryExtensions.IndexOf(chars, separator);
if (index == -1) {
list.Add(parseFunction(chars));
break;
}

list.Add(parseFunction(chars[..index]));
chars = chars[(index + separator.Length)..];

}

return list;
}
#endregion

private delegate (bool Success, T Result) TryParsableConvertMethod<T>(global::System.ReadOnlySpan<char> span);
private static TryParsableConvertMethod<T> TryGetParsableConvertMethod<T>()
where T : global::System.IParsable<T> =>
static x => {
var result = T.TryParse(new string(x), System.Globalization.CultureInfo.InvariantCulture, out var list);
return (result, list!);
};
private static TryParsableConvertMethod<T> TryGetSpanParsableConvertMethod<T>()
where T : global::System.ISpanParsable<T> =>
static x => {
var result = T.TryParse(x, System.Globalization.CultureInfo.InvariantCulture, out var list);
return (result, list!);
};

#region Single TryParsable
public bool TryGetParsable<T>([System.Diagnostics.CodeAnalysis.MaybeNullWhen(false)] out T result) where T : global::System.IParsable<T> {
return TryGetSpanParsableFunction(TryGetParsableConvertMethod<T>(), out result);
}

public bool TryGetSpanParsable<T>([System.Diagnostics.CodeAnalysis.MaybeNullWhen(false)] out T result) where T : global::System.ISpanParsable<T> {
return TryGetSpanParsableFunction(TryGetSpanParsableConvertMethod<T>(), out result);
}

private bool TryGetSpanParsableFunction<T>(TryParsableConvertMethod<T> parseFunction, [System.Diagnostics.CodeAnalysis.MaybeNullWhen(false)] out T result) {
if (TryGetNextPart(out var nextString) == false) {
result = default;
return false;
}
var (success, parseResult) = parseFunction(nextString);
result = parseResult;
return success;
}
#endregion

#region List TryParsable
public bool TryGetParsableList<T>(string separator, [System.Diagnostics.CodeAnalysis.MaybeNullWhen(false)] out global::System.Collections.Generic.List<T> list) where T : global::System.IParsable<T> {
return TryGetSpanParsableListFunction(TryGetParsableConvertMethod<T>(), separator, out list);
}
public bool TryGetSpanParsableList<T>(string separator, [System.Diagnostics.CodeAnalysis.MaybeNullWhen(false)] out global::System.Collections.Generic.List<T> list) where T : global::System.ISpanParsable<T> {
return TryGetSpanParsableListFunction(TryGetSpanParsableConvertMethod<T>(), separator, out list);
}

private bool TryGetSpanParsableListFunction<T>(TryParsableConvertMethod<T> parseFunction, string separator, [System.Diagnostics.CodeAnalysis.MaybeNullWhen(false)] out global::System.Collections.Generic.List<T> list) {
list = default;
if (TryGetNextPart(out var chars) == false) {
return false;
}
var workingList = new global::System.Collections.Generic.List<T>();

while (true) {
int index = System.MemoryExtensions.IndexOf(chars, separator);
if (index == -1) {
var (innerSuccess, innerResult) = parseFunction(chars);
if (innerSuccess == false) {
return false;
}
workingList.Add(innerResult);
break;
}

var (success, result) = parseFunction(chars[..index]);
if (success == false) {
return false;
}
workingList.Add(result);
chars = chars[(index + separator.Length)..];
}

list = workingList;
return true;
}

#endregion

private bool TryGetNextPart(out global::System.ReadOnlySpan<char> chars) {
chars = default;

if (_currentStringPosition > _inputString.Length) {
return false;
}

// Final bit
if (_currentIndex >= _templateComponents.Length) {
chars = global::System.MemoryExtensions.AsSpan(_inputString, _currentStringPosition, _inputString.Length - _currentStringPosition);
return true;
}

string nextPart = _templateComponents[_currentIndex++];
int index = _inputString.IndexOf(nextPart, _currentStringPosition);

if (index <= -1) {
return false;
}

var startPos = _currentStringPosition;
_currentStringPosition = index + nextPart.Length;
chars = global::System.MemoryExtensions.AsSpan(_inputString, startPos, index - startPos);
return true;
}

private global::System.ReadOnlySpan<char> GetNextPart() {
if (_currentStringPosition > _inputString.Length) {
throw new global::System.Exception("Tried to read beyond the size of the input string. This usually means the provided string is missing parts of the template.");
}

// Final bit
if (_currentIndex >= _templateComponents.Length) {
return global::System.MemoryExtensions.AsSpan(_inputString, _currentStringPosition, _inputString.Length - _currentStringPosition);
}

string nextPart = _templateComponents[_currentIndex++];
int index = _inputString.IndexOf(nextPart, _currentStringPosition);

if (index <= -1) {
throw new global::System.Exception(
$"Failed to find the next part of the template: \"{nextPart}\" in the remainder of the input string. " +
$"Make sure the template matches the input string, and that any previous parsed part does not contain the substring: \"{nextPart}\""
);
}

var startPos = _currentStringPosition;
_currentStringPosition = index + nextPart.Length;
return global::System.MemoryExtensions.AsSpan(_inputString, startPos, index - startPos);
}
}
}
}

Useful​

Download Example (.NET C#)​

Share OutParser​

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

Category "Serializer" has the following generators:​

1 BinaryBundle Nuget GitHub Repo stars 2026-11-15

2 Csvcsharp Nuget GitHub Repo stars 2025-10-05

3 GenPack Nuget GitHub Repo stars 2024-11-06

4 jsonConverterSourceGenerator Nuget GitHub Repo stars 2023-10-30

5 JsonPolymorphicGenerator Nuget GitHub Repo stars 2023-10-06

6 ModernLibs.FileHelpers Nuget GitHub Repo stars 2026-10-01

7 Nino Nuget GitHub Repo stars 2025-08-06

8 OutParser Nuget GitHub Repo stars 2026-11-12

9 ProtobufSourceGenerator Nuget GitHub Repo stars 2023-09-24

10 Schema Nuget GitHub Repo stars 2025-08-13

11 serde Nuget GitHub Repo stars 2026-08-20

12 StackXML Nuget GitHub Repo stars 2025-08-01

13 System.Text.Json Nuget GitHub Repo stars 2023-04-16

14 VYaml Nuget GitHub Repo stars 2025-10-03

See category​

Serializer