Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
leandromoh committed Nov 12, 2023
1 parent a66028f commit 69cd064
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 20 deletions.
62 changes: 43 additions & 19 deletions RecordParser.Test/QuotedFileReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,47 @@ public class Quoted
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void foo(bool parallel)
[InlineData("\r")]
[InlineData("\n")]
[InlineData("\r\n")]
public void Given_quoted_field_in_any_column_should_parse_successfully(string newline)
{
// Arrange

var fileContent = """
A,B,C,D
"x
y",2,3,4
""";
1,"a,
b",3,4
7,8,"a
z",9
98,99,100,101
12,13,14,"w
s"
"
a,1
",3, b,4
a,b,c,d
""".Replace(Environment.NewLine, newline);

var expected = new[]
{
// column quoted = 1
($"x{newline}y","2","3","4"),
// column quoted = 2
("1",$"a,{newline}b","3","4"),
// column quoted = 3
("7","8",$"a{newline}z","9"),
// no quoted column
("98","99","100","101"),
// column quoted = 4
("12","13","14",$"w{newline}s"),
// column quoted = 1 with whitespace before quote
($"{newline}a,1{newline}", "3", " b", "4"),
// no quoted column
("a","b","c","d"),
};

var reader = new StringReader(fileContent);
var options = new VariableLengthReaderRawOptions
Expand All @@ -41,7 +71,7 @@ public void foo(bool parallel)
Separator = ",",
ParallelismOptions = new()
{
Enabled = parallel,
Enabled = false,
MaxDegreeOfParallelism = 2
},
};
Expand All @@ -50,25 +80,19 @@ public void foo(bool parallel)

var records = reader.ReadRecordsRaw(options, getField =>
{
var record = new
{
A = getField(0),
B = getField(1),
C = getField(2),
D = getField(3)
};
var record =
(
getField(0),
getField(1),
getField(2),
getField(3)
);
return record;
}).ToList();

// Assert

records.Should().HaveCount(1);

var record = records.Single();
record.A.Should().Be("x\r\ny");
record.B.Should().Be("2");
record.C.Should().Be("3");
record.D.Should().Be("4");
records.Should().BeEquivalentTo(expected, cfg => cfg.WithStrictOrdering());
}

[Theory]
Expand Down
25 changes: 24 additions & 1 deletion RecordParser/Extensions/FileReader/RowReaders/RowByQuote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;

namespace RecordParser.Extensions.FileReader.RowReaders
{
Expand Down Expand Up @@ -58,7 +59,7 @@ public override IEnumerable<ReadOnlyMemory<char>> ReadLines()
else if (c == quote)
{
ReadOnlySpan<char> span = buffer.AsSpan().Slice(0, i - 1);
var isQuotedField = i == 1 || span[span.Length - 1] == '\n' || span.TrimEnd().EndsWith(separator);
var isQuotedField = IsFirstColumn(span) || span.TrimEnd().EndsWith(separator);

if (isQuotedField is false)
continue;
Expand Down Expand Up @@ -102,6 +103,28 @@ public override IEnumerable<ReadOnlyMemory<char>> ReadLines()
yield return y;

goto reloop;


[MethodImpl(MethodImplOptions.AggressiveInlining)]
static bool IsFirstColumn(ReadOnlySpan<char> span)
{
var onlyWhiteSpace = true;

for (var i = span.Length - 1; i >= 0; i--)
{
if (char.IsWhiteSpace(span[i]))
{
if (span[i] is '\n' or '\r')
return true;
else
continue;
}
onlyWhiteSpace = false;
break;
}

return onlyWhiteSpace;
}
}
}
}

0 comments on commit 69cd064

Please sign in to comment.