$npx -y skills add managedcode/dotnet-skills --skill sepUse Sep for high-performance separated-value parsing and writing in .NET, including delimiter inference, explicit parser/writer options, and low-allocation row/column workflows. USE FOR: delimited data needs are performance-sensitive and allocation-aware; project needs explicit c
| 1 | # Sep for .NET separated values |
| 2 | |
| 3 | ## Trigger On |
| 4 | |
| 5 | - delimited data needs are performance-sensitive and allocation-aware |
| 6 | - project needs explicit control over separator inference, escaping, trimming, and header behavior |
| 7 | - reading/writing large or long-lived file pipelines in ML, ETL, or analytics workloads |
| 8 | - startup/perf tests require AOT/trimming-friendly CSV/TSV processing |
| 9 | |
| 10 | ## Install |
| 11 | |
| 12 | - NuGet: |
| 13 | - `dotnet add package Sep` |
| 14 | - `dotnet add package Sep --version <version>` |
| 15 | - XML package reference: |
| 16 | - `<PackageReference Include="Sep" Version="x.y.z" />` |
| 17 | - Verify baseline support by checking the package page: |
| 18 | - [NuGet: Sep](https://www.nuget.org/packages/Sep/) |
| 19 | - Source: |
| 20 | - [GitHub: nietras/Sep](https://github.com/nietras/Sep) |
| 21 | |
| 22 | ## Workflow |
| 23 | |
| 24 | ```mermaid |
| 25 | flowchart LR |
| 26 | A[Input source: file/text/stream] --> B[Sep.Reader or Sep.New(...).Reader] |
| 27 | B --> C[SepReaderOptions] |
| 28 | C --> D[Rows -> Cols -> Span/Parse] |
| 29 | D --> E[Transform and validate] |
| 30 | E --> F[SepWriter via SepWriterOptions] |
| 31 | F --> G[To file/text output] |
| 32 | ``` |
| 33 | |
| 34 | 1. Decide schema shape |
| 35 | - header present or no header |
| 36 | - separator known (`;`, `,`, tab, custom) or infer from first row |
| 37 | - row/column quoting rules |
| 38 | 2. Build reader with `Sep.Reader(...)` and explicit options only where needed: |
| 39 | - `Sep.Reader()` for inferred separator from header-like first row |
| 40 | - `Sep.New(',').Reader(...)` for explicit separator mode |
| 41 | - `Sep.Reader(o => o with { HasHeader = false })` if header is absent |
| 42 | 3. Read rows and map columns as `ReadOnlySpan<char>` first, convert only when needed. |
| 43 | 4. For output, use `reader.Spec.Writer()` when you need the same separator/culture as input. |
| 44 | 5. Control writer behavior with `Sep.Writer(...)` and `SepWriterOptions` (`WriteHeader`, `Escape`, `DisableColCountCheck`). |
| 45 | 6. Add async only where it brings value and your runtime is C# 13 / .NET 9+ for `await foreach` over async reader rows. |
| 46 | 7. Use `ParallelEnumerate` for CPU-heavy transformations only after benchmarking single-threaded baseline. |
| 47 | |
| 48 | ### Install and read patterns |
| 49 | |
| 50 | ```csharp |
| 51 | using var reader = Sep.Reader(o => o with |
| 52 | { |
| 53 | HasHeader = true, |
| 54 | Unescape = true, |
| 55 | Trim = SepTrim.Both |
| 56 | }).FromText(data); |
| 57 | |
| 58 | foreach (var row in reader) |
| 59 | { |
| 60 | var id = row["Id"].Parse<int>(); |
| 61 | var name = row[1].ToString(); |
| 62 | // process row |
| 63 | } |
| 64 | ``` |
| 65 | |
| 66 | ### Write patterns |
| 67 | |
| 68 | ```csharp |
| 69 | using var reader = Sep.Reader().FromFile("input.csv"); |
| 70 | using var writer = reader.Spec.Writer().ToFile("output.csv"); |
| 71 | |
| 72 | foreach (var row in reader) |
| 73 | { |
| 74 | using var writeRow = writer.NewRow(row); |
| 75 | writeRow["Amount"].Format(row["Amount"].Parse<double>() * 1.2); |
| 76 | } |
| 77 | ``` |
| 78 | |
| 79 | ### Async reading and writing |
| 80 | |
| 81 | ```csharp |
| 82 | var text = "A;B\n1;hello\n"; |
| 83 | |
| 84 | using var reader = await Sep.Reader().FromTextAsync(text); |
| 85 | await using var writer = reader.Spec.Writer().ToText(); |
| 86 | |
| 87 | await foreach (var row in reader) |
| 88 | { |
| 89 | await using var writeRow = writer.NewRow(row); |
| 90 | var normalized = row["B"].ToString().ToUpperInvariant(); |
| 91 | writeRow["B"].Set(normalized); |
| 92 | } |
| 93 | ``` |
| 94 | |
| 95 | ### Common configuration patterns |
| 96 | |
| 97 | - Header-driven read |
| 98 | - default `HasHeader = true` |
| 99 | - query by name: `row["ColName"]` |
| 100 | - Headerless pipelines |
| 101 | - `HasHeader = false` |
| 102 | - use index-based access: `row[0]`, `row[1]` |
| 103 | - Round-trip output |
| 104 | - start writer with `reader.Spec.Writer()` to preserve inference and formatting contract |
| 105 | - Speed-first processing |
| 106 | - keep default buffer + culture unless profiling proves a need to tune |
| 107 | |
| 108 | ## Best practices |
| 109 | |
| 110 | - Parse to primitive types with `Parse<T>` in hot paths to avoid extra allocations. |
| 111 | - Keep `ToString`/format conversions at the edge (presentational layers), not in inner loops. |
| 112 | - Prefer `Unescape`, `Trim`, and `DisableQuotesParsing` settings deliberately and test with realistic samples. |
| 113 | - For large transforms, isolate heavy CPU work after enumeration and then apply `ParallelEnumerate` where appropriate. |
| 114 | |
| 115 | ## Limitations to check before production |
| 116 | |
| 117 | - `SepReader.Row` and `SepWriter.Row` are `ref struct`s: |
| 118 | - avoid patterns that store rows beyond immediate scope |
| 119 | - materialize if you truly need random async/LINQ-style buffering |
| 120 | - `SepReader` row iteration is row-by-row by design; it is intentionally not the same as a classic collection model. |
| 121 | |
| 122 | ## Deliver |
| 123 | |
| 124 | - installation and usage guide that is ready to c |