Skip to content

Commit

Permalink
fixed a boxing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
j0nimost committed Oct 25, 2023
1 parent 47d985a commit 6aba232
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 41 deletions.
37 changes: 11 additions & 26 deletions src/Kafa/Reader/KafaReader.Row.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,34 @@ public partial struct KafaReader

public readonly struct Row
{
public readonly ReadOnlyMemory<char> Value;
public readonly KafaReader _reader;
private readonly ReadOnlyMemory<char> _value;
private readonly KafaReader _reader;
private readonly int _startColMarkerIndex;
private readonly int _lastColMarkerIndex;
public readonly int ColMarkerLength;
private readonly int _colMarkerLength;

public Row(in KafaReader reader, int startIndex)
{
_reader = reader;
_startColMarkerIndex = startIndex;
Value = reader.ReadRowSpan(_startColMarkerIndex, out _lastColMarkerIndex);
ColMarkerLength = _lastColMarkerIndex - _startColMarkerIndex + 1;
_value = reader.ReadRowSpan(_startColMarkerIndex, out int lastColMarkerIndex);
_colMarkerLength = lastColMarkerIndex - _startColMarkerIndex + 1;
}

public readonly ColEnumerable Cols => new(_reader, _startColMarkerIndex, ColMarkerLength);
public readonly ColEnumerable Cols => new(_reader, _startColMarkerIndex, _colMarkerLength);
public readonly ColEnumerable GetColsRange(Range range)
{
var(offset, length) = range.GetOffsetAndLength(ColMarkerLength);
var(offset, length) = range.GetOffsetAndLength(_colMarkerLength);
return new(_reader, offset, length);
}

public override string ToString()
{
return $"{Value}";
return $"{_value}";
}

}

public struct RowEnumerable : IEnumerable<Row>, IDisposable
public readonly struct RowEnumerable : IDisposable
{
private readonly KafaReader _reader;

Expand All @@ -62,26 +61,20 @@ private int GetIndexOffset(int index)
{
return index * _reader.ColumnCount;
}

IEnumerator<Row> IEnumerable<Row>.GetEnumerator() => new Enumerator(_reader);

IEnumerator IEnumerable.GetEnumerator() => new Enumerator(_reader);
public Enumerator GetEnumerator() => new Enumerator(_reader);

public void Dispose()
{
_reader.Dispose();
}
public struct Enumerator : IEnumerator<Row>
public struct Enumerator
{
private int _index = -1;
private readonly int _columnCount;
private readonly int _colMarkerLastIndex;
private readonly KafaReader _reader;

public Row Current => new(_reader, _index);

object IEnumerator.Current => Current;

public Enumerator(in KafaReader reader)
{
_reader = reader;
Expand All @@ -91,8 +84,6 @@ public Enumerator(in KafaReader reader)
_index = _reader.OffSet == 0 ? _index : 0;

}


public bool MoveNext()
{
if(_index > -1)
Expand All @@ -106,16 +97,10 @@ public bool MoveNext()

return _index < _colMarkerLastIndex;
}


public void Reset()
{
_index = -1;
}

public void Dispose()
{
}
}
}

Expand Down
30 changes: 15 additions & 15 deletions src/KafaTests/KafaReadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void ReadRowStream(string sampleCSV)
var stream = new MemoryStream(Encoding.UTF8.GetBytes(sampleCSV));

using var rows = Kafa.Read(stream, ReadEverythingOption);
Assert.NotEmpty(rows);
Assert.NotEqual(0, rows.Length);
int count = 0;
foreach (var row in rows)
{
Expand All @@ -62,7 +62,7 @@ public void ReadColStream(string sampleCSV)
var stream = new MemoryStream(Encoding.UTF8.GetBytes(sampleCSV));

using var rows = Kafa.Read(stream, ReadEverythingOption);
Assert.NotEmpty(rows);
Assert.NotEqual(0, rows.Length);
int rowCount =0, colCount = 0;
foreach (var row in rows)
{
Expand All @@ -82,7 +82,7 @@ public void ReadColStream(string sampleCSV)
public void ReadColString(string sampleCSV)
{
using var rows = Kafa.Read(sampleCSV, ReadEverythingOption);
Assert.NotEmpty(rows);
Assert.NotEqual(0, rows.Length);
int rowCount = 0, colCount = 0;
foreach (var row in rows)
{
Expand All @@ -105,7 +105,7 @@ public void ReadStream(string sampleCSV)
{
var stream = new MemoryStream(Encoding.UTF8.GetBytes(sampleCSV));
using var rows = Kafa.Read(stream);
Assert.NotEmpty(rows);
Assert.NotEqual(0, rows.Length);
}

[Theory]
Expand All @@ -114,7 +114,7 @@ public async Task ReadStreamAsync(string sampleCSV)
{
var stream = new MemoryStream(Encoding.UTF8.GetBytes(sampleCSV));
using var rows = await Kafa.ReadAsync(stream);
Assert.NotEmpty(rows);
Assert.NotEqual(0, rows.Length);
}

[Theory]
Expand All @@ -124,7 +124,7 @@ public void ReadRowByIntIndex(string sampleCSV)
var stream = new MemoryStream(Encoding.UTF8.GetBytes(sampleCSV));

var rows = Kafa.Read(stream);
Assert.NotEmpty(rows);
Assert.NotEqual(0, rows.Length);
Assert.NotEmpty(rows[0].ToString());
}

Expand All @@ -135,7 +135,7 @@ public void ReadRowByIndexStruct(string sampleCSV)
var stream = new MemoryStream(Encoding.UTF8.GetBytes(sampleCSV));

using var rows = Kafa.Read(stream);
Assert.NotEmpty(rows);
Assert.NotEqual(0, rows.Length);
Assert.NotEmpty(rows[new Index(0)].ToString());
}

Expand All @@ -146,7 +146,7 @@ public void ReadColByIntIndex(string sampleCSV)
var stream = new MemoryStream(Encoding.UTF8.GetBytes(sampleCSV));

using var rows = Kafa.Read(stream);
Assert.NotEmpty(rows);
Assert.NotEqual(0, rows.Length);
Assert.NotEmpty(rows[0].Cols[0].ToString());
Assert.Equal("date", rows[0].Cols[0].ToString());
Assert.Equal("2013-02-08", rows[1].Cols[0].ToString());
Expand All @@ -163,7 +163,7 @@ public void ReadColByIndexStruct(string sampleCSV)
var stream = new MemoryStream(Encoding.UTF8.GetBytes(sampleCSV));

using var rows = Kafa.Read(stream);
Assert.NotEmpty(rows);
Assert.NotEqual(0, rows.Length);
Assert.Equal("date", rows[new Index(0)].Cols[new Index(0)].ToString());
Assert.Equal("2013-02-08", rows[new Index(1)].Cols[new Index(0)].ToString());
Assert.Equal("15.12", rows[new Index(1)].Cols[new Index(2)].ToString());
Expand All @@ -179,7 +179,7 @@ public void ReadColByColumnName(string sampleCSV)
var stream = new MemoryStream(Encoding.UTF8.GetBytes(sampleCSV));

using var rows = Kafa.Read(stream);
Assert.NotEmpty(rows);
Assert.NotEqual(0, rows.Length);
Assert.Equal("2013-02-08", rows[1].Cols["date"].ToString());
Assert.Equal("15.12", rows[1].Cols["high"].ToString());
Assert.Equal("AAL", rows[3].Cols["Name"].ToString());
Expand All @@ -192,7 +192,7 @@ public void ReadRowOutOfRange(string sampleCSV)
{
var stream = new MemoryStream(Encoding.UTF8.GetBytes(sampleCSV));
using var rows = Kafa.Read(stream);
Assert.NotEmpty(rows);
Assert.NotEqual(0, rows.Length);
Assert.Throws<KafaException>(() => rows[-1].ToString());
}

Expand All @@ -202,7 +202,7 @@ public void ReadColOutOfRange(string sampleCSV)
{
var stream = new MemoryStream(Encoding.UTF8.GetBytes(sampleCSV));
using var rows = Kafa.Read(stream);
Assert.NotEmpty(rows);
Assert.NotEqual(0, rows.Length);
Assert.Throws<KafaException>(() => rows[0].Cols[-1].ToString());
Assert.Throws<KafaException>(() => rows[0].Cols[-1].ToString());
}
Expand All @@ -214,7 +214,7 @@ public void ReadColNameNotFound(string sampleCSV)
{
var stream = new MemoryStream(Encoding.UTF8.GetBytes(sampleCSV));
using var rows = Kafa.Read(stream);
Assert.NotEmpty(rows);
Assert.NotEqual(0, rows.Length);
Assert.Throws<KafaException>(() => rows[0].Cols["city"].ToString());
}

Expand Down Expand Up @@ -288,7 +288,7 @@ public void ReadSpecialSeparator(string specialStyle)
{
using var ioStream = new MemoryStream(Encoding.UTF8.GetBytes(specialStyle));
using var rows = Kafa.Read(ioStream);
Assert.NotEmpty(rows);
Assert.NotEqual(0, rows.Length);
Assert.Equal("\"AAL\"", rows[1].Cols[6].ToString());

}
Expand All @@ -299,7 +299,7 @@ public void ReadUnicode()
{
string unicodeCsv = "2013-02-08,15.07 €,15.12 ¥,14.63 ¥,14.75 ¥,8407500,\"AAL✅\"";
using var rows = Kafa.Read(unicodeCsv, ReadEverythingOption);
Assert.NotEmpty(rows);
Assert.NotEqual(0, rows.Length);
Assert.Equal("15.07 €", rows[0].Cols[1].ToString());
Assert.Equal("14.75 ¥", rows[0].Cols[4].ToString());
Assert.Equal("\"AAL✅\"", rows[0].Cols[6].ToString());
Expand Down

0 comments on commit 6aba232

Please sign in to comment.