FlexableCsvParser 2.0.0
dotnet add package FlexableCsvParser --version 2.0.0
NuGet\Install-Package FlexableCsvParser -Version 2.0.0
<PackageReference Include="FlexableCsvParser" Version="2.0.0" />
<PackageVersion Include="FlexableCsvParser" Version="2.0.0" />
<PackageReference Include="FlexableCsvParser" />
paket add FlexableCsvParser --version 2.0.0
#r "nuget: FlexableCsvParser, 2.0.0"
#:package FlexableCsvParser@2.0.0
#addin nuget:?package=FlexableCsvParser&version=2.0.0
#tool nuget:?package=FlexableCsvParser&version=2.0.0
FlexableCsvParser
A high-performance, flexible, and zero-allocation oriented CSV parser for modern .NET.
Key Features
- ๐ High Performance: Designed with performance in mind, utilizing
Span<char>,ReadOnlySpan<char>, andStringPoolto minimize allocations and maximize throughput. - ๐ ๏ธ Extremely Flexible: Support for custom delimiters of any length for fields, records, quotes, and escape sequences.
- โ RFC 4180 Compliant: Full support for RFC 4180 by default.
- ๐งน Configurable Trimming: Built-in support for leading and trailing white space trimming.
- ๐ก๏ธ Robust Error Handling: Configurable behavior for handling incomplete records (Throw, Fill, or Truncate).
- ๐งฉ Async Support: Provides asynchronous APIs for non-blocking I/O.
- ๐งถ String Interning: Integrated
StringPoolto reduce memory footprint by interning repeated field values.
Installation
Install via NuGet:
dotnet add package FlexableCsvParser
Quick Start
The simplest way to get started is using the default RFC 4180 configuration.
using System.IO;
using FlexableCsvParser;
const string csv = "123, \"456 ,\"\"789\"\"\" ,ABC";
int expectedFieldCount = 3;
// Initialize the parser with a TextReader and expected field count
var parser = new CsvParser(new StringReader(csv), expectedFieldCount);
string[] record = new string[expectedFieldCount];
// Read records into a span or array
while (parser.ReadRecord(record) > 0)
{
Console.WriteLine($"Field 1: {record[0]}");
Console.WriteLine($"Field 2: {record[1]}");
Console.WriteLine($"Field 3: {record[2]}");
}
Advanced Usage
Custom Delimiters
FlexableCsvParser excels at handling non-standard CSV formats with multi-character delimiters.
const string csv = "123<Field> <Quote>456<Field>789<Quote><Field>ABC<Record>";
var config = new CsvParserConfig(
field: "<Field>",
endOfRecord: "<Record>",
quote: "<Quote>",
escape: "<Quote><Quote>"
);
var parser = new CsvParser(new StringReader(csv), 3, config);
Configuration Options
The CsvParserConfig class provides several options to tune the parser's behavior:
| Option | Description | Default |
|---|---|---|
Delimiters |
Defines the field, record, quote, and escape sequences. | Delimiters.Rfc4180 |
IncompleteRecordHandling |
How to handle records with fewer fields than expected. | ThrowException |
WhiteSpaceTrimming |
Whether to trim leading or trailing white space. | None |
StringCacheMaxLength |
Maximum length of strings to be cached in the StringPool. |
128 |
Incomplete Record Handling
You can control how the parser reacts when it encounters a record that doesn't match the expected field count.
var config = new CsvParserConfig
{
IncompleteRecordHandling = IncompleteRecordHandling.FillInWithNull
};
Available modes:
ThrowException: Throws anInvalidDataException.FillInWithEmpty: Fills missing fields withstring.Empty.FillInWithNull: Fills missing fields withnull.TruncateRecord: Returns only the fields that were successfully read.
White Space Trimming
Trimming can be applied to unquoted and quoted fields.
var config = new CsvParserConfig
{
WhiteSpaceTrimming = WhiteSpaceTrimming.Both
};
Async Support
For high-throughput applications, use the async API:
string[] record = new string[3];
while (await parser.ReadRecordAsync(record) > 0)
{
// Process record
}
Performance Note
FlexableCsvParser is built on top of Tokensharp and utilizes modern .NET features to ensure minimal overhead. By using Span<string> for record output and an internal StringPool, it significantly reduces the GC pressure typically associated with CSV parsing.
License
This project is licensed under the MIT License - see the LICENSE.txt file for details.
| Product | Versions 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 is compatible. 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 is compatible. 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. |
-
net10.0
- CommunityToolkit.HighPerformance (>= 8.4.2)
- Tokensharp (>= 4.7.1)
-
net8.0
- CommunityToolkit.HighPerformance (>= 8.4.2)
- Tokensharp (>= 4.7.1)
-
net9.0
- CommunityToolkit.HighPerformance (>= 8.4.2)
- Tokensharp (>= 4.7.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.