Skip to content

Commit 44974eb

Browse files
committed
- fix more duplication
1 parent 04e4590 commit 44974eb

1 file changed

Lines changed: 47 additions & 35 deletions

File tree

src/Parsley/Parser.cs

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -247,19 +247,39 @@ private string[] GetDelimiterSeparatedValues(string line)
247247
return list;
248248
}
249249

250-
public T[] Parse<T>(Stream stream, Encoding encoding = null) where T : IFileLine, new()
250+
private async Task<List<string>> ReadLinesFromStreamAsync(Stream stream, ParseOptions options, Encoding encoding, Func<StreamReader, Task<string>> readLineFunc)
251251
{
252-
return Parse<T>(stream, encoding, Options);
252+
var lines = new List<string>();
253+
using (var reader = new StreamReader(stream, encoding ?? Encoding.UTF8))
254+
{
255+
string line;
256+
int lineNumber = 0;
257+
while ((line = await readLineFunc(reader)) != null)
258+
{
259+
// If it's the first line and we should skip header, skip it
260+
if (options.SkipHeaderLine && lineNumber == 0)
261+
{
262+
lineNumber++;
263+
continue;
264+
}
265+
266+
var processedLine = options.TrimFieldValues ? line.Trim() : line;
267+
if (!options.IncludeEmptyLines && string.IsNullOrWhiteSpace(processedLine))
268+
{
269+
// Skip empty lines if not including them
270+
}
271+
else
272+
{
273+
lines.Add(processedLine);
274+
}
275+
lineNumber++;
276+
}
277+
}
278+
return lines;
253279
}
254280

255-
public T[] Parse<T>(Stream stream, Encoding encoding, ParseOptions options) where T : IFileLine, new()
281+
private List<string> ReadLinesFromStream(Stream stream, ParseOptions options, Encoding encoding)
256282
{
257-
if (options == null)
258-
options = new ParseOptions();
259-
260-
if (stream == null || stream.Length == 0)
261-
return Array.Empty<T>();
262-
263283
var lines = new List<string>();
264284
using (var reader = new StreamReader(stream, encoding ?? Encoding.UTF8))
265285
{
@@ -286,6 +306,23 @@ private string[] GetDelimiterSeparatedValues(string line)
286306
lineNumber++;
287307
}
288308
}
309+
return lines;
310+
}
311+
312+
public T[] Parse<T>(Stream stream, Encoding encoding = null) where T : IFileLine, new()
313+
{
314+
return Parse<T>(stream, encoding, Options);
315+
}
316+
317+
public T[] Parse<T>(Stream stream, Encoding encoding, ParseOptions options) where T : IFileLine, new()
318+
{
319+
if (options == null)
320+
options = new ParseOptions();
321+
322+
if (stream == null || stream.Length == 0)
323+
return Array.Empty<T>();
324+
325+
var lines = ReadLinesFromStream(stream, options, encoding);
289326

290327
return lines.Any() ? Parse<T>(lines.ToArray(), options) : Array.Empty<T>();
291328
}
@@ -348,32 +385,7 @@ private string[] GetDelimiterSeparatedValues(string line)
348385
if (stream.Length == 0)
349386
return Array.Empty<T>();
350387

351-
var lines = new List<string>();
352-
using (var reader = new StreamReader(stream, encoding ?? Encoding.UTF8))
353-
{
354-
string line;
355-
int lineNumber = 0;
356-
while ((line = await reader.ReadLineAsync()) != null)
357-
{
358-
// If it's the first line and we should skip header, skip it
359-
if (options.SkipHeaderLine && lineNumber == 0)
360-
{
361-
lineNumber++;
362-
continue;
363-
}
364-
365-
var processedLine = options.TrimFieldValues ? line.Trim() : line;
366-
if (!options.IncludeEmptyLines && string.IsNullOrWhiteSpace(processedLine))
367-
{
368-
// Skip empty lines if not including them
369-
}
370-
else
371-
{
372-
lines.Add(processedLine);
373-
}
374-
lineNumber++;
375-
}
376-
}
388+
var lines = await ReadLinesFromStreamAsync(stream, options, encoding, (reader) => reader.ReadLineAsync());
377389

378390
return lines.Any() ? await ParseAsync<T>(lines.ToArray(), options) : Array.Empty<T>();
379391
}

0 commit comments

Comments
 (0)