Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AsyncContinueOnCapturedContext to SepReaderOptions/SepWriterOptions (forward to ConfigureAwait) #226

Merged
merged 2 commits into from
Jan 22, 2025
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
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ dotnet_diagnostic.CA1851.severity = warning
dotnet_diagnostic.CA1834.severity = warning

# CA2007: Do not directly await a Task
dotnet_diagnostic.CA2007.severity = none
dotnet_diagnostic.CA2007.severity = warning

# CA2008: Do not create tasks without passing a TaskScheduler
dotnet_diagnostic.CA2008.severity = warning
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,12 @@ public bool Unescape { get; init; } = false;
/// 32) character is trimmed, not any whitespace character.
/// </remarks>
public SepTrim Trim { get; init; } = SepTrim.None;
/// <summary>
/// Forwarded to <see
/// cref="System.Threading.Tasks.ValueTask.ConfigureAwait(bool)"/> or
/// similar when async methods are called.
/// </summary>
public bool AsyncContinueOnCapturedContext { get; init; } = false;
```

#### Unescaping
Expand Down Expand Up @@ -867,6 +873,12 @@ public SepColNotSetOption ColNotSetOption { get; init; } = SepColNotSetOption.Th
/// too, but only the written name.
/// </remarks>
public bool Escape { get; init; } = false;
/// <summary>
/// Forwarded to <see
/// cref="System.Threading.Tasks.ValueTask.ConfigureAwait(bool)"/> or
/// similar when async methods are called.
/// </summary>
public bool AsyncContinueOnCapturedContext { get; init; } = false;
```

#### Escaping
Expand Down Expand Up @@ -2056,6 +2068,7 @@ namespace nietras.SeparatedValues
{
public SepReaderOptions() { }
public SepReaderOptions(nietras.SeparatedValues.Sep? sep) { }
public bool AsyncContinueOnCapturedContext { get; init; }
public System.Collections.Generic.IEqualityComparer<string> ColNameComparer { get; init; }
public nietras.SeparatedValues.SepCreateToString CreateToString { get; init; }
public System.Globalization.CultureInfo? CultureInfo { get; init; }
Expand Down Expand Up @@ -2219,6 +2232,7 @@ namespace nietras.SeparatedValues
{
public SepWriterOptions() { }
public SepWriterOptions(nietras.SeparatedValues.Sep sep) { }
public bool AsyncContinueOnCapturedContext { get; init; }
public nietras.SeparatedValues.SepColNotSetOption ColNotSetOption { get; init; }
public System.Globalization.CultureInfo? CultureInfo { get; init; }
public bool DisableColCountCheck { get; init; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<RootNamespace>nietras.SeparatedValues.Benchmarks</RootNamespace>
<OutputType>Exe</OutputType>
<NoWarn>$(NoWarn);CA2007</NoWarn>
</PropertyGroup>

<PropertyGroup>
Expand Down
1 change: 1 addition & 0 deletions src/Sep.Test/Sep.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<RootNamespace>nietras.SeparatedValues.Test</RootNamespace>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<IsPackable>false</IsPackable>
<NoWarn>$(NoWarn);CA2007</NoWarn>
</PropertyGroup>

<ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions src/Sep.Test/SepReaderOptionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public void SepReaderOptionsTest_Defaults()
Assert.AreSame(SepToString.Direct, sut.CreateToString);
Assert.IsFalse(sut.DisableFastFloat);
Assert.IsFalse(sut.DisableColCountCheck);
Assert.IsFalse(sut.AsyncContinueOnCapturedContext);
}

[TestMethod]
Expand All @@ -34,6 +35,7 @@ public void SepReaderOptionsTest_Override()
CreateToString = SepToString.OnePool(),
DisableFastFloat = true,
DisableColCountCheck = true,
AsyncContinueOnCapturedContext = true,
};

Assert.AreEqual(new Sep(','), sut.Sep);
Expand All @@ -44,5 +46,6 @@ public void SepReaderOptionsTest_Override()
Assert.AreNotSame(SepToString.Direct, sut.CreateToString);
Assert.IsTrue(sut.DisableFastFloat);
Assert.IsTrue(sut.DisableColCountCheck);
Assert.IsTrue(sut.AsyncContinueOnCapturedContext);
}
}
1 change: 1 addition & 0 deletions src/Sep.Test/SepWriterOptionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ public void SepWriterOptionsTest_Defaults()
Assert.IsFalse(sut.DisableColCountCheck);
Assert.AreEqual(SepColNotSetOption.Throw, sut.ColNotSetOption);
Assert.IsFalse(sut.Escape);
Assert.IsFalse(sut.AsyncContinueOnCapturedContext);
}
}
1 change: 1 addition & 0 deletions src/Sep.XyzTest/Sep.XyzTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<RootNamespace>nietras.SeparatedValues.XyzTest</RootNamespace>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<IsPackable>false</IsPackable>
<NoWarn>$(NoWarn);CA2007</NoWarn>
</PropertyGroup>

<ItemGroup>
Expand Down
17 changes: 11 additions & 6 deletions src/Sep/SepReader.IO.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal async ValueTask InitializeAsync(SepReaderOptions options, CancellationT
#if SYNC
if (MoveNext())
#else
if (await MoveNextAsync(cancellationToken))
if (await MoveNextAsync(cancellationToken).ConfigureAwait(_continueOnCapturedContext))
#endif
{
A.Assert(_parsedRowsCount > 0);
Expand Down Expand Up @@ -56,7 +56,8 @@ internal async ValueTask InitializeAsync(SepReaderOptions options, CancellationT
#if SYNC
HasRows = !FillAndMaybeDoubleCharsBuffer(_charsPaddingLength);
#else
HasRows = !await FillAndMaybeDoubleCharsBufferAsync(_charsPaddingLength, cancellationToken);
HasRows = !await FillAndMaybeDoubleCharsBufferAsync(_charsPaddingLength, cancellationToken)
.ConfigureAwait(_continueOnCapturedContext);
#endif
}
}
Expand Down Expand Up @@ -111,7 +112,8 @@ public async ValueTask<bool> MoveNextAsync(CancellationToken cancellationToken =
#if SYNC
} while (ParseNewRows());
#else
} while (await ParseNewRowsAsync(cancellationToken));
} while (await ParseNewRowsAsync(cancellationToken)
.ConfigureAwait(_continueOnCapturedContext));
#endif
return false;
}
Expand Down Expand Up @@ -216,7 +218,8 @@ internal async ValueTask<bool> ParseNewRowsAsync(CancellationToken cancellationT
#if SYNC
endOfFile = EnsureInitializeAndReadData(endOfFile);
#else
endOfFile = await EnsureInitializeAndReadDataAsync(endOfFile, cancellationToken);
endOfFile = await EnsureInitializeAndReadDataAsync(endOfFile, cancellationToken)
.ConfigureAwait(_continueOnCapturedContext);
#endif
if (endOfFile && _parsingRowCharsStartIndex < _charsDataEnd && _charsParseStart == _charsDataEnd)
{
Expand Down Expand Up @@ -258,7 +261,8 @@ async ValueTask<bool> EnsureInitializeAndReadDataAsync(bool endOfFile, Cancellat
var nothingLeftToRead = FillAndMaybeDoubleCharsBuffer(_charsPaddingLength);
CheckPoint($"{nameof(FillAndMaybeDoubleCharsBuffer)} AFTER");
#else
var nothingLeftToRead = await FillAndMaybeDoubleCharsBufferAsync(_charsPaddingLength, cancellationToken);
var nothingLeftToRead = await FillAndMaybeDoubleCharsBufferAsync(_charsPaddingLength, cancellationToken)
.ConfigureAwait(_continueOnCapturedContext);
CheckPoint($"{nameof(FillAndMaybeDoubleCharsBufferAsync)} AFTER");
#endif
if (_parser == null)
Expand Down Expand Up @@ -322,7 +326,8 @@ async ValueTask<bool> FillAndMaybeDoubleCharsBufferAsync(int paddingLength, Canc
((readCount = _reader.Read(freeChars.Slice(totalBytesRead))) > 0))
#else
while (totalBytesRead < freeLength &&
((readCount = await _reader.ReadAsync(freeChars.Slice(totalBytesRead), cancellationToken)) > 0))
((readCount = await _reader.ReadAsync(freeChars.Slice(totalBytesRead), cancellationToken)
.ConfigureAwait(_continueOnCapturedContext)) > 0))
#endif
{
_charsDataEnd += readCount;
Expand Down
17 changes: 11 additions & 6 deletions src/Sep/SepReader.IO.Sync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal async ValueTask InitializeAsync(SepReaderOptions options, CancellationT
#if SYNC
if (MoveNext())
#else
if (await MoveNextAsync(cancellationToken))
if (await MoveNextAsync(cancellationToken).ConfigureAwait(_continueOnCapturedContext))
#endif
{
A.Assert(_parsedRowsCount > 0);
Expand Down Expand Up @@ -56,7 +56,8 @@ internal async ValueTask InitializeAsync(SepReaderOptions options, CancellationT
#if SYNC
HasRows = !FillAndMaybeDoubleCharsBuffer(_charsPaddingLength);
#else
HasRows = !await FillAndMaybeDoubleCharsBufferAsync(_charsPaddingLength, cancellationToken);
HasRows = !await FillAndMaybeDoubleCharsBufferAsync(_charsPaddingLength, cancellationToken)
.ConfigureAwait(_continueOnCapturedContext);
#endif
}
}
Expand Down Expand Up @@ -111,7 +112,8 @@ public async ValueTask<bool> MoveNextAsync(CancellationToken cancellationToken =
#if SYNC
} while (ParseNewRows());
#else
} while (await ParseNewRowsAsync(cancellationToken));
} while (await ParseNewRowsAsync(cancellationToken)
.ConfigureAwait(_continueOnCapturedContext));
#endif
return false;
}
Expand Down Expand Up @@ -216,7 +218,8 @@ internal async ValueTask<bool> ParseNewRowsAsync(CancellationToken cancellationT
#if SYNC
endOfFile = EnsureInitializeAndReadData(endOfFile);
#else
endOfFile = await EnsureInitializeAndReadDataAsync(endOfFile, cancellationToken);
endOfFile = await EnsureInitializeAndReadDataAsync(endOfFile, cancellationToken)
.ConfigureAwait(_continueOnCapturedContext);
#endif
if (endOfFile && _parsingRowCharsStartIndex < _charsDataEnd && _charsParseStart == _charsDataEnd)
{
Expand Down Expand Up @@ -258,7 +261,8 @@ async ValueTask<bool> EnsureInitializeAndReadDataAsync(bool endOfFile, Cancellat
var nothingLeftToRead = FillAndMaybeDoubleCharsBuffer(_charsPaddingLength);
CheckPoint($"{nameof(FillAndMaybeDoubleCharsBuffer)} AFTER");
#else
var nothingLeftToRead = await FillAndMaybeDoubleCharsBufferAsync(_charsPaddingLength, cancellationToken);
var nothingLeftToRead = await FillAndMaybeDoubleCharsBufferAsync(_charsPaddingLength, cancellationToken)
.ConfigureAwait(_continueOnCapturedContext);
CheckPoint($"{nameof(FillAndMaybeDoubleCharsBufferAsync)} AFTER");
#endif
if (_parser == null)
Expand Down Expand Up @@ -322,7 +326,8 @@ async ValueTask<bool> FillAndMaybeDoubleCharsBufferAsync(int paddingLength, Canc
((readCount = _reader.Read(freeChars.Slice(totalBytesRead))) > 0))
#else
while (totalBytesRead < freeLength &&
((readCount = await _reader.ReadAsync(freeChars.Slice(totalBytesRead), cancellationToken)) > 0))
((readCount = await _reader.ReadAsync(freeChars.Slice(totalBytesRead), cancellationToken)
.ConfigureAwait(_continueOnCapturedContext)) > 0))
#endif
{
_charsDataEnd += readCount;
Expand Down
2 changes: 2 additions & 0 deletions src/Sep/SepReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public sealed partial class SepReader : SepReaderState
readonly Info _info;
char _separator;
readonly bool _disableQuotesParsing;
readonly bool _continueOnCapturedContext;
readonly TextReader _reader;
ISepParser? _parser;

Expand All @@ -46,6 +47,7 @@ internal SepReader(Info info, in SepReaderOptions options, TextReader reader)
_cultureInfo = options.CultureInfo;
_createToString = options.CreateToString;
_disableQuotesParsing = options.DisableQuotesParsing;
_continueOnCapturedContext = options.AsyncContinueOnCapturedContext;
_arrayPool = new();

var decimalSeparator = _cultureInfo?.NumberFormat.CurrencyDecimalSeparator ??
Expand Down
1 change: 1 addition & 0 deletions src/Sep/SepReaderExtensions.IO.Async.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//#define SYNC
#pragma warning disable CA2007
using System;
using System.IO;
#if !SYNC
Expand Down
1 change: 1 addition & 0 deletions src/Sep/SepReaderExtensions.IO.Sync.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#define SYNC
#pragma warning disable CA2007
using System;
using System.IO;
#if !SYNC
Expand Down
6 changes: 6 additions & 0 deletions src/Sep/SepReaderOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,10 @@ public SepReaderOptions(Sep? sep)
/// 32) character is trimmed, not any whitespace character.
/// </remarks>
public SepTrim Trim { get; init; } = SepTrim.None;
/// <summary>
/// Forwarded to <see
/// cref="System.Threading.Tasks.ValueTask.ConfigureAwait(bool)"/> or
/// similar when async methods are called.
/// </summary>
public bool AsyncContinueOnCapturedContext { get; init; } = false;
}
Loading
Loading