From 6aba2326bb18ef422f086dec508cbe90caf74afb Mon Sep 17 00:00:00 2001 From: j0nimost Date: Wed, 25 Oct 2023 06:17:04 +0300 Subject: [PATCH] fixed a boxing issue --- src/Kafa/Reader/KafaReader.Row.cs | 37 +++++++++---------------------- src/KafaTests/KafaReadTests.cs | 30 ++++++++++++------------- 2 files changed, 26 insertions(+), 41 deletions(-) diff --git a/src/Kafa/Reader/KafaReader.Row.cs b/src/Kafa/Reader/KafaReader.Row.cs index 5e5a414..747cff6 100644 --- a/src/Kafa/Reader/KafaReader.Row.cs +++ b/src/Kafa/Reader/KafaReader.Row.cs @@ -9,35 +9,34 @@ public partial struct KafaReader public readonly struct Row { - public readonly ReadOnlyMemory Value; - public readonly KafaReader _reader; + private readonly ReadOnlyMemory _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, IDisposable + public readonly struct RowEnumerable : IDisposable { private readonly KafaReader _reader; @@ -62,16 +61,13 @@ private int GetIndexOffset(int index) { return index * _reader.ColumnCount; } - - IEnumerator IEnumerable.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 + public struct Enumerator { private int _index = -1; private readonly int _columnCount; @@ -79,9 +75,6 @@ public struct Enumerator : IEnumerator private readonly KafaReader _reader; public Row Current => new(_reader, _index); - - object IEnumerator.Current => Current; - public Enumerator(in KafaReader reader) { _reader = reader; @@ -91,8 +84,6 @@ public Enumerator(in KafaReader reader) _index = _reader.OffSet == 0 ? _index : 0; } - - public bool MoveNext() { if(_index > -1) @@ -106,16 +97,10 @@ public bool MoveNext() return _index < _colMarkerLastIndex; } - - public void Reset() { _index = -1; } - - public void Dispose() - { - } } } diff --git a/src/KafaTests/KafaReadTests.cs b/src/KafaTests/KafaReadTests.cs index f341180..b298277 100644 --- a/src/KafaTests/KafaReadTests.cs +++ b/src/KafaTests/KafaReadTests.cs @@ -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) { @@ -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) { @@ -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) { @@ -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] @@ -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] @@ -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()); } @@ -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()); } @@ -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()); @@ -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()); @@ -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()); @@ -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(() => rows[-1].ToString()); } @@ -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(() => rows[0].Cols[-1].ToString()); Assert.Throws(() => rows[0].Cols[-1].ToString()); } @@ -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(() => rows[0].Cols["city"].ToString()); } @@ -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()); } @@ -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());