Skip to content

Commit

Permalink
Add Addax with workaround
Browse files Browse the repository at this point in the history
Workaround filed here: alexanderkozlenko/addax#21
  • Loading branch information
joelverhagen committed Jan 12, 2024
1 parent b8989a9 commit 45fecf7
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
6 changes: 6 additions & 0 deletions NCsvPerf/CsvReadable/Benchmarks/PackageAssetsSuite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
66 changes: 66 additions & 0 deletions NCsvPerf/CsvReadable/Implementations/Addax_Formats_Tabular.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using Addax.Formats.Tabular;
using System.Collections.Generic;
using System.IO;

namespace Knapcode.NCsvPerf.CsvReadable
{
/// <summary>
/// Package: https://www.nuget.org/packages/Addax.Formats.Tabular
/// Source: https://github.com/alexanderkozlenko/addax
/// </summary>
public class Addax_Formats_Tabular : ICsvReader
{
private readonly TabularDialect _dialect;

public Addax_Formats_Tabular()
{
_dialect = new TabularDialect("\r\n", ',', '\"');
}

public List<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
{
var allRecords = new List<T>();

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;
}
}
}
1 change: 1 addition & 0 deletions NCsvPerf/NCsvPerf.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Addax.Formats.Tabular" Version="1.2.0" />
<PackageReference Include="Angara.Table" Version="0.3.3" NoWarn="NU1701" />
<PackageReference Include="Angara.Serialization" Version="0.3.0" NoWarn="NU1701" />
<PackageReference Include="Angara.Statistics" Version="0.1.4" NoWarn="NU1701" />
Expand Down

0 comments on commit 45fecf7

Please sign in to comment.