88using System . Text . RegularExpressions ;
99using System . Threading ;
1010using System . Threading . Tasks ;
11+ using System . Runtime . CompilerServices ;
1112
1213namespace MiniExcelLibs . Csv
1314{
14- internal class CsvReader : IExcelReader
15+ internal partial class CsvReader : IExcelReader
1516 {
1617 private Stream _stream ;
1718 private CsvConfiguration _config ;
@@ -22,7 +23,8 @@ public CsvReader(Stream stream, IConfiguration configuration)
2223 _config = configuration == null ? CsvConfiguration . DefaultConfiguration : ( CsvConfiguration ) configuration ;
2324 }
2425
25- public IEnumerable < IDictionary < string , object > > Query ( bool useHeaderRow , string sheetName , string startCell )
26+ [ Zomp . SyncMethodGenerator . CreateSyncVersion ]
27+ public async IAsyncEnumerable < IDictionary < string , object > > QueryAsync ( bool useHeaderRow , string sheetName , string startCell , [ EnumeratorCancellation ] CancellationToken ct = default )
2628 {
2729 if ( startCell != "A1" )
2830 throw new NotImplementedException ( "CSV does not implement parameter startCell" ) ;
@@ -35,14 +37,22 @@ public IEnumerable<IDictionary<string, object>> Query(bool useHeaderRow, string
3537 var headRows = new Dictionary < int , string > ( ) ;
3638
3739 string row ;
38- for ( var rowIndex = 1 ; ( row = reader . ReadLine ( ) ) != null ; rowIndex ++ )
40+ for ( var rowIndex = 1 ; ( row = await reader . ReadLineAsync (
41+ #if NET7_0_OR_GREATER
42+ ct
43+ #endif
44+ ) ) != null ; rowIndex ++ )
3945 {
4046 string finalRow = row ;
4147 if ( _config . ReadLineBreaksWithinQuotes )
4248 {
4349 while ( finalRow . Count ( c => c == '"' ) % 2 != 0 )
4450 {
45- var nextPart = reader . ReadLine ( ) ;
51+ var nextPart = await reader . ReadLineAsync (
52+ #if NET7_0_OR_GREATER
53+ ct
54+ #endif
55+ ) ;
4656 if ( nextPart == null )
4757 {
4858 break ;
@@ -107,10 +117,11 @@ public IEnumerable<IDictionary<string, object>> Query(bool useHeaderRow, string
107117 }
108118 }
109119
110- public IEnumerable < T > Query < T > ( string sheetName , string startCell , bool hasHeader ) where T : class , new ( )
120+ [ Zomp . SyncMethodGenerator . CreateSyncVersion ]
121+ public IAsyncEnumerable < T > QueryAsync < T > ( string sheetName , string startCell , bool hasHeader , CancellationToken ct = default ) where T : class , new ( )
111122 {
112- var dynamicRecords = Query ( false , sheetName , startCell ) ;
113- return ExcelOpenXmlSheetReader . QueryImpl < T > ( dynamicRecords , startCell , hasHeader , _config ) ;
123+ var dynamicRecords = QueryAsync ( false , sheetName , startCell , ct ) ;
124+ return ExcelOpenXmlSheetReader . QueryImplAsync < T > ( dynamicRecords , startCell , hasHeader , _config ) ;
114125 }
115126
116127 public IEnumerable < IDictionary < string , object > > QueryRange ( bool useHeaderRow , string sheetName , string startCell , string endCell )
@@ -135,36 +146,6 @@ public IEnumerable<IDictionary<string, object>> QueryRange(bool useHeaderRow, st
135146 return ExcelOpenXmlSheetReader . QueryImpl < T > ( dynamicRecords , ReferenceHelper . ConvertXyToCell ( startRowIndex , startColumnIndex ) , hasHeader , this . _config ) ;
136147 }
137148
138- public Task < IEnumerable < IDictionary < string , object > > > QueryAsync ( bool useHeaderRow , string sheetName , string startCell , CancellationToken cancellationToken = default )
139- {
140- return Task . Run ( ( ) => Query ( useHeaderRow , sheetName , startCell ) , cancellationToken ) ;
141- }
142-
143- public async Task < IEnumerable < T > > QueryAsync < T > ( string sheetName , string startCell , bool hasHeader , CancellationToken cancellationToken = default ) where T : class , new ( )
144- {
145- return await Task . Run ( ( ) => Query < T > ( sheetName , startCell , hasHeader ) , cancellationToken ) . ConfigureAwait ( false ) ;
146- }
147-
148- public Task < IEnumerable < IDictionary < string , object > > > QueryRangeAsync ( bool useHeaderRow , string sheetName , string startCell , string endCel , CancellationToken cancellationToken = default )
149- {
150- return Task . Run ( ( ) => QueryRange ( useHeaderRow , sheetName , startCell , endCel ) , cancellationToken ) ;
151- }
152-
153- public async Task < IEnumerable < T > > QueryRangeAsync < T > ( string sheetName , string startCell , string endCel , bool hasHeader , CancellationToken cancellationToken = default ) where T : class , new ( )
154- {
155- return await Task . Run ( ( ) => QueryRange < T > ( sheetName , startCell , endCel , hasHeader ) , cancellationToken ) . ConfigureAwait ( false ) ;
156- }
157-
158- public Task < IEnumerable < IDictionary < string , object > > > QueryRangeAsync ( bool useHeaderRow , string sheetName , int startRowIndex , int startColumnIndex , int ? endRowIndex , int ? endColumnIndex , CancellationToken cancellationToken = default )
159- {
160- return Task . Run ( ( ) => QueryRange ( useHeaderRow , sheetName , startRowIndex , startColumnIndex , endRowIndex , endColumnIndex ) , cancellationToken ) ;
161- }
162-
163- public async Task < IEnumerable < T > > QueryRangeAsync < T > ( string sheetName , int startRowIndex , int startColumnIndex , int ? endRowIndex , int ? endColumnIndex , bool hasHeader , CancellationToken cancellationToken = default ) where T : class , new ( )
164- {
165- return await Task . Run ( ( ) => QueryRange < T > ( sheetName , startRowIndex , startColumnIndex , endRowIndex , endColumnIndex , hasHeader ) , cancellationToken ) . ConfigureAwait ( false ) ;
166- }
167-
168149 private string [ ] Split ( string row )
169150 {
170151 if ( _config . SplitFn != null )
0 commit comments