diff --git a/NCsvPerf/CsvReadable/Benchmarks/PackageAssetsSuite.cs b/NCsvPerf/CsvReadable/Benchmarks/PackageAssetsSuite.cs index 166c5fb..7011fd2 100644 --- a/NCsvPerf/CsvReadable/Benchmarks/PackageAssetsSuite.cs +++ b/NCsvPerf/CsvReadable/Benchmarks/PackageAssetsSuite.cs @@ -56,6 +56,12 @@ private void Execute(ICsvReader reader) } } + [Benchmark] + public void Addax_Formats_Tabular() + { + Execute(new Addax_Formats_Tabular()); + } + [Benchmark] public void Angara_Table() { diff --git a/NCsvPerf/CsvReadable/Implementations/Addax_Formats_Tabular.cs b/NCsvPerf/CsvReadable/Implementations/Addax_Formats_Tabular.cs new file mode 100644 index 0000000..5845215 --- /dev/null +++ b/NCsvPerf/CsvReadable/Implementations/Addax_Formats_Tabular.cs @@ -0,0 +1,66 @@ +using Addax.Formats.Tabular; +using System.Collections.Generic; +using System.IO; + +namespace Knapcode.NCsvPerf.CsvReadable +{ + /// + /// Package: https://www.nuget.org/packages/Addax.Formats.Tabular + /// Source: https://github.com/alexanderkozlenko/addax + /// + public class Addax_Formats_Tabular : ICsvReader + { + private readonly TabularDialect _dialect; + + public Addax_Formats_Tabular() + { + _dialect = new TabularDialect("\r\n", ',', '\"'); + } + + public List GetRecords(MemoryStream stream) where T : ICsvReadable, new() + { + var allRecords = new List(); + + using (var reader = new TabularReader(stream, _dialect)) + { + while (reader.TryPickRecord()) + { + // workaround for https://github.com/alexanderkozlenko/addax/issues/21 + string firstValue; + if (!reader.TryReadField() || !reader.TryGetString(out firstValue)) + { + break; + } + + string secondValue; + if (!reader.TryReadField() || !reader.TryGetString(out secondValue)) + { + break; + } + + var record = new T(); + record.Read(i => + { + if (i == 0) + { + return firstValue; + } + else if (i == 1) + { + return secondValue; + } + else if (reader.TryReadField() && reader.TryGetString(out var value)) + { + return value; + } + + return null; + }); + allRecords.Add(record); + } + } + + return allRecords; + } + } +} diff --git a/NCsvPerf/NCsvPerf.csproj b/NCsvPerf/NCsvPerf.csproj index 84fe292..07e2bbd 100644 --- a/NCsvPerf/NCsvPerf.csproj +++ b/NCsvPerf/NCsvPerf.csproj @@ -9,6 +9,7 @@ +