@@ -48,26 +48,10 @@ public Parser(ParseOptions options)
4848
4949 public T [ ] Parse < T > ( string [ ] lines , ParseOptions options ) where T : IFileLine , new ( )
5050 {
51- if ( options == null )
52- options = new ParseOptions ( ) ;
53-
54- if ( lines == null || lines . Length == 0 )
51+ var linesToProcess = ProcessLinesArray ( lines , options ) ;
52+ if ( linesToProcess . Length == 0 )
5553 return Array . Empty < T > ( ) ;
5654
57- // Store original lines to work with
58- var originalLines = lines ;
59-
60- // Handle SkipHeaderLine option
61- if ( options . SkipHeaderLine && originalLines . Length > 0 )
62- {
63- originalLines = originalLines . Skip ( 1 ) . ToArray ( ) ;
64- }
65-
66- // Determine which lines to process
67- var linesToProcess = options . IncludeEmptyLines
68- ? originalLines
69- : originalLines . Where ( line => ! string . IsNullOrWhiteSpace ( line ) ) . ToArray ( ) ;
70-
7155 return ProcessLines < T > ( linesToProcess , options ) ;
7256 }
7357
@@ -254,8 +238,12 @@ private async Task<List<string>> ReadLinesFromStreamAsync(Stream stream, ParseOp
254238 {
255239 string line ;
256240 int lineNumber = 0 ;
257- while ( ( line = await readLineFunc ( reader ) ) != null )
241+
242+ do
258243 {
244+ line = await readLineFunc ( reader ) ;
245+ if ( line == null ) break ;
246+
259247 // If it's the first line and we should skip header, skip it
260248 if ( options . SkipHeaderLine && lineNumber == 0 )
261249 {
@@ -273,28 +261,57 @@ private async Task<List<string>> ReadLinesFromStreamAsync(Stream stream, ParseOp
273261 lines . Add ( processedLine ) ;
274262 }
275263 lineNumber ++ ;
276- }
264+ } while ( line != null ) ;
277265 }
278266 return lines ;
279267 }
280268
269+ private string [ ] ProcessLinesArray ( string [ ] lines , ParseOptions options )
270+ {
271+ if ( options == null )
272+ options = new ParseOptions ( ) ;
273+
274+ if ( lines == null || lines . Length == 0 )
275+ return Array . Empty < string > ( ) ;
276+
277+ // Store original lines to work with
278+ var originalLines = lines ;
279+
280+ // Handle SkipHeaderLine option
281+ if ( options . SkipHeaderLine && originalLines . Length > 0 )
282+ {
283+ originalLines = originalLines . Skip ( 1 ) . ToArray ( ) ;
284+ }
285+
286+ // Determine which lines to process
287+ var linesToProcess = options . IncludeEmptyLines
288+ ? originalLines
289+ : originalLines . Where ( line => ! string . IsNullOrWhiteSpace ( line ) ) . ToArray ( ) ;
290+
291+ return linesToProcess ;
292+ }
293+
281294 private List < string > ReadLinesFromStream ( Stream stream , ParseOptions options , Encoding encoding )
282295 {
283296 var lines = new List < string > ( ) ;
284297 using ( var reader = new StreamReader ( stream , encoding ?? Encoding . UTF8 ) )
285298 {
299+ var allLines = new List < string > ( ) ;
286300 string line ;
287- int lineNumber = 0 ;
288301 while ( ( line = reader . ReadLine ( ) ) != null )
302+ {
303+ allLines . Add ( line ) ;
304+ }
305+
306+ for ( int i = 0 ; i < allLines . Count ; i ++ )
289307 {
290308 // If it's the first line and we should skip header, skip it
291- if ( options . SkipHeaderLine && lineNumber == 0 )
309+ if ( options . SkipHeaderLine && i == 0 )
292310 {
293- lineNumber ++ ;
294311 continue ;
295312 }
296313
297- var processedLine = options . TrimFieldValues ? line . Trim ( ) : line ;
314+ var processedLine = options . TrimFieldValues ? allLines [ i ] . Trim ( ) : allLines [ i ] ;
298315 if ( ! options . IncludeEmptyLines && string . IsNullOrWhiteSpace ( processedLine ) )
299316 {
300317 // Skip empty lines if not including them
@@ -303,7 +320,6 @@ private List<string> ReadLinesFromStream(Stream stream, ParseOptions options, En
303320 {
304321 lines . Add ( processedLine ) ;
305322 }
306- lineNumber ++ ;
307323 }
308324 }
309325 return lines ;
@@ -397,26 +413,16 @@ private List<string> ReadLinesFromStream(Stream stream, ParseOptions options, En
397413
398414 public async Task < T [ ] > ParseAsync < T > ( string [ ] lines , ParseOptions options ) where T : IFileLine , new ( )
399415 {
400- if ( options == null )
401- options = new ParseOptions ( ) ;
402-
403- if ( lines == null || lines . Length == 0 )
404- return Array . Empty < T > ( ) ;
405-
406- // Store original lines to work with
407- var originalLines = lines ;
408-
409- // Handle SkipHeaderLine option
410- if ( options . SkipHeaderLine && originalLines . Length > 0 )
416+ string [ ] linesToProcess ;
417+ try
411418 {
412- originalLines = originalLines . Skip ( 1 ) . ToArray ( ) ;
419+ linesToProcess = ProcessLinesArray ( lines , options ) ;
413420 }
414-
415- // Determine which lines to process
416- var linesToProcess = options . IncludeEmptyLines
417- ? originalLines
418- : originalLines . Where ( line => ! string . IsNullOrWhiteSpace ( line ) ) . ToArray ( ) ;
419-
421+ catch
422+ {
423+ return Array . Empty < T > ( ) ;
424+ }
425+
420426 if ( linesToProcess . Length == 0 )
421427 return Array . Empty < T > ( ) ;
422428
0 commit comments