适用于 .NET Core 的零分配写入器/读取器解析器
RecordParser is a expression tree based parser that helps you to write maintainable parsers with high-performance and zero allocations, thanks to Span type. It makes easier for developers to do parsing by automating non-relevant code, which allow you to focus on the essentials of mapping.
Even the focus of this library being data mapping to objects (classes, structs, etc), it got an excellent result in the blog benchmark which tested how fast libraries can transform a CSV row into an array of strings. We got 1st place by parsing a 1 million lines file in 826ms.
Libraries always say themselves have great perfomance, but how often they show you a benchmark comparing with other libraries? Check the benchmark page to see RecordParser comparisons. If you miss some, a PR is welcome.
Third Party Benchmarks
*ㅤyou can use a "string pool" (function that converts a ReadOnlySpan to string) to avoid creating multiple instances of strings with same content. This optimization is useful when there are a lot of repeated string values present. In this scenario, it may reduce allocated memory and speed-up processing time.
NOTE: MOST EXAMPLES USE TUPLES FOR SIMPLICITY. PARSER ACTUALLY WORKS FOR ANY TYPE (CLASSES, STRUCTS, RECORDS, ARRAYS, TUPLES, ETC)
There are 2 flavors for mapping: indexed or sequential.
Indexed is useful when you want to map columns by its position: start/length.
[Fact]
public void Given_value_using_standard_format_should_parse_without_extra_configuration()
{
var reader = new FixedLengthReaderBuilder()
.Map(x => x.Name, startIndex: 0, length: 11)
.Map(x => x.Birthday, 12, 10)
.Map(x => x.Money, 23, 7)
.Build();
var result = reader.Parse("foo bar baz 2020.05.23 0123.45");
result.Should().BeEquivalentTo((Name: "foo bar baz",
Birthday: new DateTime(2020, 05, 23),
Money: 123.45M));
}Sequential is useful when you want to map columns by its order, so you just need specify the lengths.
[Fact]
public void Given_value_using_standard_format_should_parse_without_extra_configuration()
{
var reader = new FixedLengthReaderSequentialBuilder()
.Map(x => x.Name, length: 11)
.Skip(1)
.Map(x => x.Birthday, 10)
.Skip(1)
.Map(x => x.Money, 7)
.Build();
var result = reader.Parse("foo bar baz 2020.05.23 0123.45");
result.Should().BeEquivalentTo((Name: "foo bar baz",
Birthday: new DateTime(2020, 05, 23),
Money: 123.45M));
}There are 2 flavors for mapping: indexed or sequential.
Indexed is useful when you want to map columns by its indexes.
…Sequential is useful when you want to map columns by its order.
…You can define default converters for some type if you has a custom format.
The following example defines all decimals values will be divided by 100 before assigning,
furthermore all dates being parsed on ddMMyyyy format.
This feature is avaible for both fixed and variable length.
…You can define a custom converter for field/property.
Custom converters have priority case a default type convert is defined.
This feature is avaible for both fixed and variable length.
[Fact]
public void Given_members_with_custom_format_should_use_custom_parser()
{
var reader = new VariableLengthReaderBuilder()
.Map(x => x.Age, 0)
.Map(x => x.MotherAge, 1, value => int.Parse(value) + 3)
.Map(x => x.FatherAge, 2)
.Build(";");
var result = reader.Parse(" 15 ; 40 ; 50 ");
result.Should().BeEquivalentTo((Age: 15,
MotherAge: 43,
FatherAge: 50));
}Just like a regular property, you can also configure nested properties mapping.
The nested objects are created only if it was mapped, which avoids stack overflow problems.
This feature is avaible for both fixed and variable length.
…There are 2 flavors for mapping: indexed or sequential.
Both indexed and sequential builders accept the following optional parameters in Map methods:
Indexed is useful when you want to map columns by its position: start/length.
…Sequential is useful when you want to map columns by its order, so you just need specify the lengths.
…There are 2 flavors for mapping: indexed or sequential.
Both indexed and sequential builders accept the format optional parameter in Map method.
Indexed is useful when you want to map columns by its indexes.
…Sequential is useful when you want to map columns by its order.
…You can define default converters for some type if you has a custom format.
The following example defines all decimals values will be multiplied by 100 before writing (precision 2),
furthermore all dates being written on ddMMyyyy format.
This feature is avaible for both fixed and variable length.
…You can define a custom converter for field/property.
Custom converters have priority case a default type convert is defined.
This feature is avaible for both fixed and variable length.
…While version 1 of the library allows to parse individual records, version 2 introduced features to read/write records directly from/to files.
This functionality bridged the gap between the existing readers/writers and TextReader/TextWriter.
One awesome feature that makes RecordParser innovative and special is the hability to process files using the power of the parallel programmimg!
You can take advantage of multicore to use parallel processing and speed up reading and writing!
You can disable the parallelism just by set the feature flag to false, thus using sequential processing.
Just import the namespace RecordParser.Extensions to see the extension methods to interact in TextReader and TextWriter.
……Note: only recommended when user need to receive each field as string.
Other methods that does not force string uses ReadOnlySpan, which speed up processing and reduces unnecessary allocations.
…This feature is agnostic to builder type since it just receives the delegate of TryFormat method.
So you can use it with instances of both FixedLengthWriter and VariableLengthWriter.
…暂无开放 Issues,或尚未同步最近议题。