Skip to content

Commit

Permalink
Add ignore blank lines option.
Browse files Browse the repository at this point in the history
Add trim whitespaces option.
  • Loading branch information
Ivan Ivon committed Aug 14, 2021
1 parent 7325e02 commit 13b7d04
Show file tree
Hide file tree
Showing 24 changed files with 444 additions and 205 deletions.
2 changes: 1 addition & 1 deletion Deploy/buildlpx.cmd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@echo off

set version=6.17.0
set version=6.18.0
set fileName=CsvLINQPadDriver.%version%.lpx

set zip="%ProgramFiles%\7-Zip\7z.exe"
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,12 @@ CSV files connection can be added to LINQPad 6/5 the same way as any other conne

### Format ###

* CSV separator: character used to separate columns in files. Can be `,`, `\t`, etc. Auto-detected if empty.
* Separator: characters used to separate columns in files. Can be `,`, `\t`, etc. Auto-detected if empty.
* Use [CsvHelper](https://joshclose.github.io/CsvHelper) library separator auto-detection: use CsvHelper library separator auto-detection instead of internal one.
* Ignore bad data: ignore malformed CSV data.
* Allow comments: lines starting with `#` will be ignored.
* Ignore bad data: ignore malformed files.
* Ignore blank lines: do not process blank lines.
* Trim whitespaces: data whitespaces trim options.
* Allow comments: allow single-line comments - lines starting with `#` (default) will be ignored.

### Memory ###

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,7 @@ public override string ToString()

private string ToStringFromEnd()
{
#if (!NETSTANDARD2_0 && !NETFRAMEWORK)
Span<char> span = stackalloc char[11]; // 1 for ^ and 10 for longest possible uint value
bool formatted = ((uint)Value).TryFormat(span.Slice(1), out int charsWritten);
Debug.Assert(formatted);
span[0] = '^';
return new string(span.Slice(0, charsWritten + 1));
#else
return '^' + Value.ToString();
#endif
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,34 +55,7 @@ public override int GetHashCode()
/// <summary>Converts the value of the current Range object to its equivalent string representation.</summary>
public override string ToString()
{
#if (!NETSTANDARD2_0 && !NETFRAMEWORK)
Span<char> span = stackalloc char[2 + (2 * 11)]; // 2 for "..", then for each index 1 for '^' and 10 for longest possible uint
int pos = 0;

if (Start.IsFromEnd)
{
span[0] = '^';
pos = 1;
}
bool formatted = ((uint)Start.Value).TryFormat(span.Slice(pos), out int charsWritten);
Debug.Assert(formatted);
pos += charsWritten;

span[pos++] = '.';
span[pos++] = '.';

if (End.IsFromEnd)
{
span[pos++] = '^';
}
formatted = ((uint)End.Value).TryFormat(span.Slice(pos), out charsWritten);
Debug.Assert(formatted);
pos += charsWritten;

return new string(span.Slice(0, pos));
#else
return Start.ToString() + ".." + End.ToString();
#endif
}

/// <summary>Create a Range object starting from start index to the end of the collection.</summary>
Expand Down
12 changes: 9 additions & 3 deletions Src/CsvLINQPadDriver/CodeGen/CsvCSharpCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,16 @@ public class {_contextTypeName} : {typeof(CsvDataContextBase).GetCodeTypeClassNa
// Init tables data {string.Join(string.Empty, csvTables.Select(table => $@"
this.{table.CodeName} = {typeof(CsvTableFactory).GetCodeTypeClassName()}.CreateTable<{GetClassName(table)}>(
{GetBoolConst(isStringInternEnabled)},
{(isStringInternEnabled && _properties.UseStringComparerForStringIntern ? GetStringComparer(_properties.StringComparison) : "null")},
{GetNullableValue(isStringInternEnabled && _properties.UseStringComparerForStringIntern, () => GetStringComparer(_properties.StringComparison))},
{GetBoolConst(_properties.IsCacheEnabled)},
{table.CsvSeparator.AsValidCSharpCode()},
{typeof(NoBomEncoding).GetCodeTypeClassName()}.{_properties.NoBomEncoding},
{GetBoolConst(_properties.AllowComments)},
{GetNullableValue(_properties.AllowComments && _properties.CommentChar.HasValue, () => _properties.CommentChar.AsValidCSharpCode())},
{GetBoolConst(_properties.IgnoreBadData)},
{GetBoolConst(_properties.AutoDetectEncoding)},
{GetBoolConst(_properties.IgnoreBlankLines)},
{typeof(WhitespaceTrimOptions).GetCodeTypeClassName()}.{_properties.WhitespaceTrimOptions},
{table.FilePath.AsValidCSharpCode()},
new {typeof(CsvColumnInfoList<>).GetCodeTypeClassName(GetClassName(table))} {{
{string.Join(string.Empty, table.Columns.Select(c => $@"{{ {c.Index}, x => x.{c.CodeName} }}, "))}
Expand All @@ -93,8 +96,11 @@ public class {_contextTypeName} : {typeof(CsvDataContextBase).GetCodeTypeClassNa
}} // namespace
", groups);

static string GetBoolConst(bool val) =>
val ? "true" : "false";
static string GetBoolConst(bool value) =>
value ? "true" : "false";

static string GetNullableValue(bool hasValue, Func<string> valueProvider) =>
hasValue ? valueProvider() : "null";
}

private static TypeCodeResult GenerateTableRowDataTypeClass(CsvTable table, bool useRecordType, StringComparison stringComparison, bool hideRelationsFromDump)
Expand Down
12 changes: 12 additions & 0 deletions Src/CsvLINQPadDriver/CodeGen/CsvTableBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ public abstract class CsvTableBase<TRow> : CsvTableBase, IEnumerable<TRow>
private readonly NoBomEncoding _noBomEncoding;
private readonly StringComparer? _internStringComparer;
private readonly bool _allowComments;
private readonly char? _commentChar;
private readonly bool _ignoreBadData;
private readonly bool _autoDetectEncoding;
private readonly bool _ignoreBlankLines;
private readonly WhitespaceTrimOptions _whitespaceTrimOptions;

protected readonly string FilePath;

Expand All @@ -36,8 +39,11 @@ protected CsvTableBase(
char? csvSeparator,
NoBomEncoding noBomEncoding,
bool allowComments,
char? commentChar,
bool ignoreBadData,
bool autoDetectEncoding,
bool ignoreBlankLines,
WhitespaceTrimOptions whitespaceTrimOptions,
string filePath,
IEnumerable<CsvColumnInfo> propertiesInfo,
Action<TRow> relationsInit)
Expand All @@ -47,8 +53,11 @@ protected CsvTableBase(
_csvSeparator = csvSeparator;
_noBomEncoding = noBomEncoding;
_allowComments = allowComments;
_commentChar = commentChar;
_ignoreBadData = ignoreBadData;
_autoDetectEncoding = autoDetectEncoding;
_ignoreBlankLines = ignoreBlankLines;
_whitespaceTrimOptions = whitespaceTrimOptions;

FilePath = filePath;

Expand All @@ -62,8 +71,11 @@ protected IEnumerable<TRow> ReadData() =>
_internStringComparer,
_noBomEncoding,
_allowComments,
_commentChar,
_ignoreBadData,
_autoDetectEncoding,
_ignoreBlankLines,
_whitespaceTrimOptions,
_cachedCsvRowMappingBase!);

// ReSharper disable once UnusedMember.Global
Expand Down
6 changes: 6 additions & 0 deletions Src/CsvLINQPadDriver/CodeGen/CsvTableEnumerable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ public CsvTableEnumerable(
char? csvSeparator,
NoBomEncoding noBomEncoding,
bool allowComments,
char? commentChar,
bool ignoreBadData,
bool autoDetectEncoding,
bool ignoreBlankLines,
WhitespaceTrimOptions whitespaceTrimOptions,
string filePath,
IEnumerable<CsvColumnInfo> propertiesInfo,
Action<TRow> relationsInit)
Expand All @@ -24,8 +27,11 @@ public CsvTableEnumerable(
csvSeparator,
noBomEncoding,
allowComments,
commentChar,
ignoreBadData,
autoDetectEncoding,
ignoreBlankLines,
whitespaceTrimOptions,
filePath,
propertiesInfo,
relationsInit)
Expand Down
9 changes: 9 additions & 0 deletions Src/CsvLINQPadDriver/CodeGen/CsvTableFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ public static CsvTableBase<TRow> CreateTable<TRow>(
char? csvSeparator,
NoBomEncoding noBomEncoding,
bool allowComments,
char? commentChar,
bool ignoreBadData,
bool autoDetectEncoding,
bool ignoreBlankLines,
WhitespaceTrimOptions whitespaceTrimOptions,
string filePath,
IEnumerable<CsvColumnInfo> propertiesInfo,
Action<TRow> relationsInit)
Expand All @@ -26,8 +29,11 @@ public static CsvTableBase<TRow> CreateTable<TRow>(
csvSeparator,
noBomEncoding,
allowComments,
commentChar,
ignoreBadData,
autoDetectEncoding,
ignoreBlankLines,
whitespaceTrimOptions,
filePath,
propertiesInfo,
relationsInit)
Expand All @@ -37,8 +43,11 @@ public static CsvTableBase<TRow> CreateTable<TRow>(
csvSeparator,
noBomEncoding,
allowComments,
commentChar,
ignoreBadData,
autoDetectEncoding,
ignoreBlankLines,
whitespaceTrimOptions,
filePath,
propertiesInfo,
relationsInit);
Expand Down
6 changes: 6 additions & 0 deletions Src/CsvLINQPadDriver/CodeGen/CsvTableList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ public CsvTableList(
char? csvSeparator,
NoBomEncoding noBomEncoding,
bool allowComments,
char? commentChar,
bool ignoreBadData,
bool autoDetectEncoding,
bool ignoreBlankLines,
WhitespaceTrimOptions whitespaceTrimOptions,
string filePath,
IEnumerable<CsvColumnInfo> propertiesInfo,
Action<TRow> relationsInit)
Expand All @@ -29,8 +32,11 @@ public CsvTableList(
csvSeparator,
noBomEncoding,
allowComments,
commentChar,
ignoreBadData,
autoDetectEncoding,
ignoreBlankLines,
whitespaceTrimOptions,
filePath,
propertiesInfo,
relationsInit) =>
Expand Down
Loading

0 comments on commit 13b7d04

Please sign in to comment.