diff --git a/src/Kafa/Reader/KafaReadState.cs b/src/Kafa/Reader/KafaReadState.cs index 4518bde..37f0a91 100644 --- a/src/Kafa/Reader/KafaReadState.cs +++ b/src/Kafa/Reader/KafaReadState.cs @@ -6,7 +6,7 @@ namespace nyingi.Kafa.Reader { - public struct KafaReadState : IDisposable + public sealed class KafaReadState : IDisposable { public char[] Buffer { get; private set; } public int[] ColMarker { get; private set; } diff --git a/src/Kafa/Reader/KafaReader.Col.cs b/src/Kafa/Reader/KafaReader.Col.cs index 98f4373..aa35138 100644 --- a/src/Kafa/Reader/KafaReader.Col.cs +++ b/src/Kafa/Reader/KafaReader.Col.cs @@ -2,7 +2,7 @@ namespace nyingi.Kafa.Reader { - public partial struct KafaReader + public partial class KafaReader { public readonly ref struct Col { @@ -20,7 +20,7 @@ public int Length public Col(ColEnumerable colEnumerable, int index) { _colEnumerable = colEnumerable; - Value = _colEnumerable.ReadColSpan(index); + Value = _colEnumerable.ReadColAsSpan(index); } @@ -28,7 +28,7 @@ public Col(ColEnumerable colEnumerable, string columnName) { _colEnumerable = colEnumerable; int index = _colEnumerable.ReadColByHeader(columnName); - Value = _colEnumerable.ReadColSpan(index); + Value = _colEnumerable.ReadColAsSpan(index); } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -45,15 +45,15 @@ public override string ToString() } - public ref struct ColEnumerable + public struct ColEnumerable { private readonly KafaReader _reader; - private ReadOnlySpan _colMarkerIndexes; + private ReadOnlyMemory _colMarkerIndexes; public ColEnumerable(KafaReader reader, int startColIndex, int length) { _reader = reader; - _colMarkerIndexes = reader.ReadColMarkerSpan(startColIndex, length); + _colMarkerIndexes = reader.ReadColMarkerAsMemory(startColIndex, length); } public int Length => _colMarkerIndexes.Length; @@ -76,7 +76,7 @@ public int ReadColByHeader(string columnName) } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public ReadOnlySpan ReadColSpan(int index) + public ReadOnlySpan ReadColAsSpan(int index) { int lastColMarker = index + 1; @@ -84,13 +84,15 @@ public ReadOnlySpan ReadColSpan(int index) { throw new KafaException($"{index}: {nameof(index)} out of range", new ArgumentOutOfRangeException(nameof(index))); } - int startIndex = _colMarkerIndexes[index]; + int startIndex = _colMarkerIndexes.Span[index]; startIndex = startIndex == 0 ? 0 : startIndex + 1; // SKIP Separator - int lastIndex = _colMarkerIndexes[lastColMarker]; + int lastIndex = _colMarkerIndexes.Span[lastColMarker]; // TODO: Simplify this checks lastIndex = _reader.HasCRLF && lastColMarker == _colMarkerIndexes.Length - 1 && lastIndex != _reader.LastBufferIndex ? lastIndex - 1 : lastIndex; - return _reader.ReadColSpan(startIndex, lastIndex); + + var mem = _reader.ReadColAsMemory(startIndex, lastIndex); + return mem.Span; } @@ -107,7 +109,7 @@ public readonly bool TryParse(ReadOnlySpan colValue, out T? result) whe public Enumerator GetEnumerator() => new Enumerator(this); - public ref struct Enumerator + public struct Enumerator { private int _index = -1; diff --git a/src/Kafa/Reader/KafaReader.Row.cs b/src/Kafa/Reader/KafaReader.Row.cs index 747cff6..5a7bb06 100644 --- a/src/Kafa/Reader/KafaReader.Row.cs +++ b/src/Kafa/Reader/KafaReader.Row.cs @@ -4,7 +4,7 @@ namespace nyingi.Kafa.Reader { - public partial struct KafaReader + public partial class KafaReader { public readonly struct Row diff --git a/src/Kafa/Reader/KafaReader.cs b/src/Kafa/Reader/KafaReader.cs index 465e715..1a67817 100644 --- a/src/Kafa/Reader/KafaReader.cs +++ b/src/Kafa/Reader/KafaReader.cs @@ -5,11 +5,11 @@ namespace nyingi.Kafa.Reader { - public partial struct KafaReader : IDisposable + public partial class KafaReader : IDisposable { private readonly KafaReadState _kafaReadState; private readonly CultureInfo? cultureInfo; - public KafaReader(in KafaReadState kafaReadState) + public KafaReader(KafaReadState kafaReadState) { _kafaReadState = kafaReadState; cultureInfo = kafaReadState.Options.CultureInfo; @@ -31,7 +31,7 @@ public KafaReader(in KafaReadState kafaReadState) public OrderedDictionary? Headers => _kafaReadState.Headers; [MethodImpl(MethodImplOptions.AggressiveInlining)] - public readonly ReadOnlyMemory ReadRowSpan(int index, out int lastColMarkerIndex) + public ReadOnlyMemory ReadRowSpan(int index, out int lastColMarkerIndex) { lastColMarkerIndex = index + ColumnCount; @@ -51,9 +51,9 @@ public readonly ReadOnlyMemory ReadRowSpan(int index, out int lastColMarke } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public readonly ReadOnlySpan ReadColSpan(int startIndex, int lastIndex) + public ReadOnlyMemory ReadColAsMemory(int startIndex, int lastIndex) { - return _kafaReadState.Buffer.AsSpan(startIndex, lastIndex - startIndex); + return _kafaReadState.Buffer.AsMemory(startIndex, lastIndex - startIndex); } public RowEnumerable GetRows(Range range) @@ -67,14 +67,14 @@ public RowEnumerable GetRows() return new(this); } - private ReadOnlySpan ReadColMarkerSpan(int index, int length) + private ReadOnlyMemory ReadColMarkerAsMemory(int index, int length) { if(index < 0 || index + length > _kafaReadState.ColMarkerLength) { throw new KafaException($"{index}: {nameof(index)} out of range", new ArgumentOutOfRangeException(nameof(index))); } - return _kafaReadState.ColMarker.AsSpan(index, length); + return _kafaReadState.ColMarker.AsMemory(index, length); } public void Dispose() diff --git a/src/Kafa/Writer/KafaPooledWriter.cs b/src/Kafa/Writer/KafaPooledWriter.cs index 950545b..8bef05d 100644 --- a/src/Kafa/Writer/KafaPooledWriter.cs +++ b/src/Kafa/Writer/KafaPooledWriter.cs @@ -2,7 +2,7 @@ namespace nyingi.Kafa.Writer { - public sealed class KafaPooledWriter : IBufferWriter, IDisposable + public class KafaPooledWriter : IBufferWriter, IDisposable { private const int DefaultBufferLength = 65556; private byte[] _buffer; diff --git a/src/Kafa/Writer/KafaWriter.cs b/src/Kafa/Writer/KafaWriter.cs index 4d3348d..cc731db 100644 --- a/src/Kafa/Writer/KafaWriter.cs +++ b/src/Kafa/Writer/KafaWriter.cs @@ -54,7 +54,7 @@ public void Write(ReadOnlySpan values) } else { - _kafaPooledWriter?.Write(values); + _kafaPooledWriter.Write(values); } } @@ -62,7 +62,7 @@ public void Flush() { if (_stream != null) { - _stream.Write(_kafaPooledWriter!.WrittenAsSpan); + _stream.Write(_kafaPooledWriter.WrittenAsSpan); _stream.Flush(); } }