Skip to content

Commit

Permalink
changed the file separator from enum
Browse files Browse the repository at this point in the history
  • Loading branch information
j0nimost committed Oct 16, 2023
1 parent 405b074 commit c6dc686
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 15 deletions.
10 changes: 5 additions & 5 deletions src/Kafa/KafaOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@

namespace nyingi.Kafa
{
public enum FileType
public class SeparatorFileType
{
CSV = (byte)',',
TSV = (byte)';'
public const char CSV = ',';
public const char TSV = ';';
}
public sealed partial class KafaOptions
{
public CultureInfo? CultureInfo { get; set; }
public Encoding? Encoding { get; set; }
public FileType FileType { get; set; }
public char Separator { get; set; } = '\0';

public bool HasHeader { get; set; } = true;

internal static KafaOptions Default
{
get
{
return new KafaOptions { FileType = FileType.CSV, CultureInfo = CultureInfo.InvariantCulture, Encoding = Encoding.UTF8 };
return new KafaOptions { Separator = SeparatorFileType.CSV, CultureInfo = CultureInfo.InvariantCulture, Encoding = Encoding.UTF8 };
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Kafa/Reader/KafaReadState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private void ReadColMarkers()
i = j + 1;
continue;
}
else if (Buffer[i] == (int)Options.FileType || Buffer[i] == '\n' || Buffer[i + 1] == '\0')
else if (Buffer[i] == (int)Options.Separator || Buffer[i] == '\n' || Buffer[i + 1] == '\0')
{
ColMarker[colIndexer] = i;
colIndexer++;
Expand Down Expand Up @@ -175,7 +175,7 @@ private void ReadColCount()
i = j + 1;
continue;
}
else if (Buffer[i] == (int)Options.FileType)
else if (Buffer[i] == (int)Options.Separator)
{
ColCount++;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Kafa/Reader/KafaReader.Col.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public ReadOnlySpan<char> ReadColSpan(int index)
int startIndex = _colMarkerIndexes[index];
startIndex = startIndex == 0 ? 0 : startIndex + 1; // SKIP Separator
int lastIndex = _colMarkerIndexes[lastColMarker];
lastIndex = _reader.HasCRLF && lastColMarker == _colMarkerIndexes.Length - 1 && lastIndex != _reader.LastBufferIndex // figure out use case
// TODO: Simplify this checks
lastIndex = _reader.HasCRLF && lastColMarker == _colMarkerIndexes.Length - 1 && lastIndex != _reader.LastBufferIndex
? lastIndex - 1 : lastIndex;
return _reader.ReadColSpan(startIndex, lastIndex);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Kafa/Reflection/KafaReflection.Reader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public async Task<TextWriter> GetProperties<T>(List<T> entities, TextWriter text

if (countHeader < propertyInfos.Length - 1)
{
await textWriter.WriteAsync((char)TypeInfo.KafaOptions.FileType);
await textWriter.WriteAsync((char)TypeInfo.KafaOptions.Separator);

}
countHeader++;
Expand All @@ -56,7 +56,7 @@ public async Task<TextWriter> GetProperties<T>(List<T> entities, TextWriter text

if (count < propertyCount - 1)
{
await textWriter.WriteAsync((char)TypeInfo.KafaOptions.FileType);
await textWriter.WriteAsync((char)TypeInfo.KafaOptions.Separator);
}
count++;
}
Expand Down
6 changes: 3 additions & 3 deletions src/KafaTests/KafaReadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class KafaReadTests
new object[]{ "date,open,high,low,close,volume,Name\n2013-02-08,15.07,15.12,14.63,14.75,8407500,\"AAL\"\n2013-02-12,14.45,14.51,14.1,14.27,8126000,AAL" },
};

private KafaOptions ReadEverythingOption => new KafaOptions() { HasHeader = false, FileType = FileType.CSV };
private KafaOptions ReadEverythingOption => new KafaOptions() { HasHeader = false, Separator = SeparatorFileType.CSV};

[Theory]
[MemberData(nameof(GetDifferentRows))]
Expand Down Expand Up @@ -213,7 +213,7 @@ public void ReadRowWithHeader()
{
CultureInfo = System.Globalization.CultureInfo.CurrentCulture,
HasHeader = true,
FileType = FileType.CSV
Separator = SeparatorFileType.CSV
};

using var kafaReaderState = new KafaReadState((int)ioStream.Length, kafaOptions);
Expand All @@ -239,7 +239,7 @@ public void ReadRowWithoutHeader()
{
CultureInfo = System.Globalization.CultureInfo.CurrentCulture,
HasHeader = false,
FileType = FileType.CSV
Separator = SeparatorFileType.CSV

};

Expand Down
2 changes: 1 addition & 1 deletion src/KafaTests/KafaWriteTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public async Task WriteCSVNoHeaderAsync()
new CsvData{ Date = DateTime.Parse("10/10/2023 4:09:45 PM"), Open=12.45, Close=12.99, High=13.00, Low=12.1, Name="AMZN", Volume=1233435512}
};

var rowmem = await Kafa.WriteAsync<CsvData>(csvs, new KafaOptions() { HasHeader = false, FileType = FileType.CSV });
var rowmem = await Kafa.WriteAsync<CsvData>(csvs, new KafaOptions() { HasHeader = false, Separator=SeparatorFileType.CSV});
string expected = "";

if (Environment.OSVersion.Platform == PlatformID.Unix)
Expand Down
2 changes: 1 addition & 1 deletion src/KafaTests/RFC4180Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/// </summary>
public class RFC4180Tests
{
private KafaOptions ReadEverythingOption => new KafaOptions() { HasHeader = false, FileType = FileType.CSV };
private KafaOptions ReadEverythingOption => new KafaOptions() { HasHeader = false, Separator= SeparatorFileType.CSV};

[Fact]
public void ReadRowsWithCRLF()
Expand Down

0 comments on commit c6dc686

Please sign in to comment.