Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Permit Delimeters Other Than Commas #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ obj/
project.lock.json
.DS_Store
*.pyc
/.vs
31 changes: 23 additions & 8 deletions CsvParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,40 @@ public class CsvParser
{
private CsvTable _table;
private StreamReader _reader;
private static string _delimeter;
private const string DefaultDelimeter = ",";

private CsvParser()
{
}

public static CsvTable ParseTable(byte[] data, bool normalizeHeaderNames = true)
{
return new CsvParser().InternalParse(data, normalizeHeaderNames);
_delimeter = DefaultDelimeter;
return new CsvParser().InternalParse(data, normalizeHeaderNames, _delimeter);
}

private CsvTable InternalParse(byte[] data, bool normalizeHeaderNames)
public static CsvTable ParseTable(byte[] data, bool normalizeHeaderNames = true, string delimeter = DefaultDelimeter)
{
_delimeter = delimeter;
return new CsvParser().InternalParse(data, normalizeHeaderNames, _delimeter);
}

private CsvTable InternalParse(byte[] data, bool normalizeHeaderNames, string delimeter)
{
_delimeter = delimeter;
_table = new CsvTable();
_reader = new StreamReader(new MemoryStream(data));
string[] row = null;

ReadHeaders(normalizeHeaderNames);

for (;;) {
for (; ; )
{
row = GetLineValues();

if (row == null) {
if (row == null)
{
break;
}

Expand All @@ -45,9 +57,11 @@ private void ReadHeaders(bool normalizeHeaderNames)
string[] line = GetLineValues();

int hIndex = 0;
foreach (var name in line) {
foreach (var name in line)
{
string columnName = name;
if (normalizeHeaderNames) {
if (normalizeHeaderNames)
{
columnName = new string(name.ToLower()
.Where(c => char.IsLetterOrDigit(c)).ToArray());
}
Expand All @@ -61,11 +75,12 @@ private string[] GetLineValues()
string[] result;
string line = _reader.ReadLine();

if (line == null) {
if (line == null)
{
return null;
}

result = Regex.Split(line, @",(?=(?:[^""]*""[^""]*"")*(?![^""]*""))");
result = Regex.Split(line, $@"{_delimeter}(?=(?:[^""]*""[^""]*"")*(?![^""]*""))");

return result;
}
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# csvtools
Csv parsing tools for dotnet core

Permits delimeters other than just ",".

NuGet package: https://www.nuget.org/packages/CsvToolsCore/1.0.0