Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 3 additions & 24 deletions Csv/CsvReader.Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,11 @@
internal struct MemorySliceLineSource : ILineSource
{
private readonly ReadOnlyMemory<char> csv;
private readonly CsvMemoryOptions memoryOptions;
private int position;

public MemorySliceLineSource(ReadOnlyMemory<char> csv, CsvMemoryOptions memoryOptions)
public MemorySliceLineSource(ReadOnlyMemory<char> csv)
{
this.csv = csv;
this.memoryOptions = memoryOptions;
this.position = 0;
}

Expand Down Expand Up @@ -164,27 +162,8 @@

public MemoryText Concat(MemoryText head, string newLine, MemoryText tail, out string? combined)
{
combined = null;

var separator = newLine.AsMemory();
var totalLength = head.Length + separator.Length + tail.Length;
var buffer = memoryOptions.CharArrayPool.Rent(totalLength);

try
{
var span = buffer.AsSpan();
head.Span.CopyTo(span);
separator.Span.CopyTo(span.Slice(head.Length));
tail.Span.CopyTo(span.Slice(head.Length + separator.Length));

var result = new char[totalLength];
span.Slice(0, totalLength).CopyTo(result);
return result.AsMemory();
}
finally
{
memoryOptions.CharArrayPool.Return(buffer);
}
combined = string.Concat(head.Span, newLine.AsSpan(), tail.Span);
return combined.AsMemory();
}
}

Expand Down Expand Up @@ -294,7 +273,7 @@
// case via index == RowsToSkip + 1 and skips its own multiline pass to avoid double-reading.
if (!skipInitialLine && options.AllowNewLineInEnclosedFieldValues)
{
var splitLine = options.Splitter.Split(line, options);

Check warning on line 276 in Csv/CsvReader.Engine.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Dereference of a possibly null reference.

Check warning on line 276 in Csv/CsvReader.Engine.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Dereference of a possibly null reference.

Check warning on line 276 in Csv/CsvReader.Engine.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Dereference of a possibly null reference.

while (splitLine.Count > 0 && CsvLineSplitter.IsUnterminatedQuotedValue(splitLine[splitLine.Count - 1].AsSpan(), options))
{
Expand Down Expand Up @@ -349,7 +328,7 @@
var isFirstDataLineInHeaderAbsentMode = options.HeaderMode == HeaderMode.HeaderAbsent && index == (options.RowsToSkip + 1);
if (options.AllowNewLineInEnclosedFieldValues && !isFirstDataLineInHeaderAbsentMode)
{
var rawSplit = options.Splitter.Split(line, options);

Check warning on line 331 in Csv/CsvReader.Engine.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Dereference of a possibly null reference.

Check warning on line 331 in Csv/CsvReader.Engine.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Dereference of a possibly null reference.

Check warning on line 331 in Csv/CsvReader.Engine.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Dereference of a possibly null reference.
while (rawSplit.Count > 0 && CsvLineSplitter.IsUnterminatedQuotedValue(rawSplit[rawSplit.Count - 1].AsSpan(), options))
{
if (!source.TryReadLine(out var nextLine, out _))
Expand Down Expand Up @@ -397,7 +376,7 @@
// case via index == RowsToSkip + 1 and skips its own multiline pass to avoid double-reading.
if (!skipInitialLine && options.AllowNewLineInEnclosedFieldValues)
{
var splitLine = options.Splitter.Split(line, options);

Check warning on line 379 in Csv/CsvReader.Engine.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Dereference of a possibly null reference.

Check warning on line 379 in Csv/CsvReader.Engine.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Dereference of a possibly null reference.

while (splitLine.Count > 0 && CsvLineSplitter.IsUnterminatedQuotedValue(splitLine[splitLine.Count - 1].AsSpan(), options))
{
Expand Down Expand Up @@ -453,7 +432,7 @@
var isFirstDataLineInHeaderAbsentMode = options.HeaderMode == HeaderMode.HeaderAbsent && index == (options.RowsToSkip + 1);
if (options.AllowNewLineInEnclosedFieldValues && !isFirstDataLineInHeaderAbsentMode)
{
var rawSplit = options.Splitter.Split(line, options);

Check warning on line 435 in Csv/CsvReader.Engine.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Dereference of a possibly null reference.

Check warning on line 435 in Csv/CsvReader.Engine.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Dereference of a possibly null reference.
while (rawSplit.Count > 0 && CsvLineSplitter.IsUnterminatedQuotedValue(rawSplit[rawSplit.Count - 1].AsSpan(), options))
{
var (nextOk, nextLine, _) = await source.TryReadLineAsync(ct).ConfigureAwait(false);
Expand Down
2 changes: 1 addition & 1 deletion Csv/CsvReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public static CsvBufferWriter CreateBufferWriter(ReadOnlySpan<string> headers, c
}

private static IEnumerable<ICsvLineSpan> ReadFromMemoryOptimizedImpl(ReadOnlyMemory<char> csv, CsvOptions options, CsvMemoryOptions memoryOptions)
=> Enumerate<MemorySliceLineSource, OptimizedRowFactory, ReadLineSpanOptimized>(new MemorySliceLineSource(csv, memoryOptions), new OptimizedRowFactory(memoryOptions), options);
=> Enumerate<MemorySliceLineSource, OptimizedRowFactory, ReadLineSpanOptimized>(new MemorySliceLineSource(csv), new OptimizedRowFactory(memoryOptions), options);

#endif

Expand Down
Loading