Skip to content

Commit

Permalink
refactor/split-text-find-helper (#82)
Browse files Browse the repository at this point in the history
* done

* fix
  • Loading branch information
leandromoh authored Oct 24, 2023
1 parent 767501f commit 21840e2
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions RecordParser/Engines/Reader/TextFindHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,45 @@ namespace RecordParser.Engines.Reader
internal ref struct TextFindHelper
{
private readonly ReadOnlySpan<char> line;
private ReadOnlySpan<char> currentValue;
private TextFindHelperCore core;

public TextFindHelper(ReadOnlySpan<char> source, string delimiter, (char ch, string str) quote)
{
line = source;
currentValue = default;
core = new TextFindHelperCore(delimiter, quote);
}

public ReadOnlySpan<char> GetValue(int index)
{
currentValue = core.GetValue(index, currentValue, line);

return currentValue;
}

public void Dispose() => core.Dispose();
}

internal struct TextFindHelperCore
{
private readonly string delimiter;
private readonly (char ch, string str) quote;

private int scanned;
private int position;
private int currentIndex;
private ReadOnlySpan<char> currentValue;

private char[] buffer;

public TextFindHelper(ReadOnlySpan<char> source, string delimiter, (char ch, string str) quote)
public TextFindHelperCore(string delimiter, (char ch, string str) quote)
{
this.line = source;
this.delimiter = delimiter;
this.quote = quote;

scanned = -delimiter.Length;
position = 0;
currentIndex = -1;
currentValue = default;
buffer = null;
}

Expand All @@ -38,7 +57,7 @@ public void Dispose()
}
}

public ReadOnlySpan<char> GetValue(int index)
public ReadOnlySpan<char> GetValue(int index, ReadOnlySpan<char> currentValue, ReadOnlySpan<char> line)
{
if (index <= currentIndex)
{
Expand All @@ -51,7 +70,7 @@ public ReadOnlySpan<char> GetValue(int index)
while (currentIndex <= index)
{
var match = index == ++currentIndex;
currentValue = ParseChunk(match);
currentValue = ParseChunk(match, line);

if (match)
{
Expand All @@ -62,7 +81,7 @@ public ReadOnlySpan<char> GetValue(int index)
throw new Exception("invalid index for line");
}

private ReadOnlySpan<char> ParseChunk(bool match)
private ReadOnlySpan<char> ParseChunk(bool match, ReadOnlySpan<char> line)
{
scanned += position + delimiter.Length;

Expand All @@ -71,7 +90,7 @@ private ReadOnlySpan<char> ParseChunk(bool match)

if (isQuotedField)
{
return ParseQuotedChuck(match);
return ParseQuotedChuck(match, line);
}

position = unlook.IndexOf(delimiter);
Expand All @@ -83,7 +102,7 @@ private ReadOnlySpan<char> ParseChunk(bool match)
return line.Slice(scanned, position);
}

private ReadOnlySpan<char> ParseQuotedChuck(bool match)
private ReadOnlySpan<char> ParseQuotedChuck(bool match, ReadOnlySpan<char> line)
{
const string corruptFieldError = "Double quote is not escaped or there is extra data after a quoted field.";

Expand Down

0 comments on commit 21840e2

Please sign in to comment.