Skip to content

Commit

Permalink
clean up :2
Browse files Browse the repository at this point in the history
  • Loading branch information
j0nimost committed Oct 27, 2023
1 parent 0b4712a commit cbe02c3
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/Kafa/Reader/KafaReadState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
24 changes: 13 additions & 11 deletions src/Kafa/Reader/KafaReader.Col.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace nyingi.Kafa.Reader
{
public partial struct KafaReader
public partial class KafaReader
{
public readonly ref struct Col
{
Expand All @@ -20,15 +20,15 @@ public int Length
public Col(ColEnumerable colEnumerable, int index)
{
_colEnumerable = colEnumerable;
Value = _colEnumerable.ReadColSpan(index);
Value = _colEnumerable.ReadColAsSpan(index);

}

public Col(ColEnumerable colEnumerable, string columnName)
{
_colEnumerable = colEnumerable;
int index = _colEnumerable.ReadColByHeader(columnName);
Value = _colEnumerable.ReadColSpan(index);
Value = _colEnumerable.ReadColAsSpan(index);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand All @@ -45,15 +45,15 @@ public override string ToString()

}

public ref struct ColEnumerable
public struct ColEnumerable
{
private readonly KafaReader _reader;
private ReadOnlySpan<int> _colMarkerIndexes;
private ReadOnlyMemory<int> _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;
Expand All @@ -76,21 +76,23 @@ public int ReadColByHeader(string columnName)
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ReadOnlySpan<char> ReadColSpan(int index)
public ReadOnlySpan<char> ReadColAsSpan(int index)
{
int lastColMarker = index + 1;

if (index < 0 || lastColMarker >= _colMarkerIndexes.Length)
{
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;
}


Expand All @@ -107,7 +109,7 @@ public readonly bool TryParse<T>(ReadOnlySpan<char> colValue, out T? result) whe
public Enumerator GetEnumerator() => new Enumerator(this);


public ref struct Enumerator
public struct Enumerator
{
private int _index = -1;

Expand Down
2 changes: 1 addition & 1 deletion src/Kafa/Reader/KafaReader.Row.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace nyingi.Kafa.Reader
{
public partial struct KafaReader
public partial class KafaReader
{

public readonly struct Row
Expand Down
14 changes: 7 additions & 7 deletions src/Kafa/Reader/KafaReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,7 +31,7 @@ public KafaReader(in KafaReadState kafaReadState)
public OrderedDictionary? Headers => _kafaReadState.Headers;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly ReadOnlyMemory<char> ReadRowSpan(int index, out int lastColMarkerIndex)
public ReadOnlyMemory<char> ReadRowSpan(int index, out int lastColMarkerIndex)
{
lastColMarkerIndex = index + ColumnCount;

Expand All @@ -51,9 +51,9 @@ public readonly ReadOnlyMemory<char> ReadRowSpan(int index, out int lastColMarke
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly ReadOnlySpan<char> ReadColSpan(int startIndex, int lastIndex)
public ReadOnlyMemory<char> ReadColAsMemory(int startIndex, int lastIndex)
{
return _kafaReadState.Buffer.AsSpan(startIndex, lastIndex - startIndex);
return _kafaReadState.Buffer.AsMemory(startIndex, lastIndex - startIndex);
}

public RowEnumerable GetRows(Range range)
Expand All @@ -67,14 +67,14 @@ public RowEnumerable GetRows()
return new(this);
}

private ReadOnlySpan<int> ReadColMarkerSpan(int index, int length)
private ReadOnlyMemory<int> 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()
Expand Down
2 changes: 1 addition & 1 deletion src/Kafa/Writer/KafaPooledWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace nyingi.Kafa.Writer
{
public sealed class KafaPooledWriter : IBufferWriter<byte>, IDisposable
public class KafaPooledWriter : IBufferWriter<byte>, IDisposable
{
private const int DefaultBufferLength = 65556;
private byte[] _buffer;
Expand Down
4 changes: 2 additions & 2 deletions src/Kafa/Writer/KafaWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ public void Write(ReadOnlySpan<byte> values)
}
else
{
_kafaPooledWriter?.Write(values);
_kafaPooledWriter.Write(values);

Check warning on line 57 in src/Kafa/Writer/KafaWriter.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'writer' in 'void BuffersExtensions.Write<byte>(IBufferWriter<byte> writer, ReadOnlySpan<byte> value)'.
}
}

public void Flush()
{
if (_stream != null)
{
_stream.Write(_kafaPooledWriter!.WrittenAsSpan);
_stream.Write(_kafaPooledWriter.WrittenAsSpan);

Check warning on line 65 in src/Kafa/Writer/KafaWriter.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
_stream.Flush();
}
}
Expand Down

0 comments on commit cbe02c3

Please sign in to comment.