Skip to content

Commit 5361048

Browse files
author
Oren (electricessence)
committed
Prepare for snugpk release.
1 parent fdb83c7 commit 5361048

10 files changed

Lines changed: 114 additions & 90 deletions

.editorconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[*.cs]
2+
3+
# CA1707: Identifiers should not contain underscores
4+
dotnet_diagnostic.CA1707.severity = silent
5+
6+
# CA1063: Implement IDisposable Correctly
7+
dotnet_diagnostic.CA1063.severity = silent
8+
9+
# CA1816: Dispose methods should call SuppressFinalize
10+
dotnet_diagnostic.CA1816.severity = silent
11+
12+
# CA1303: Do not pass literals as localized parameters
13+
dotnet_diagnostic.CA1303.severity = silent

CsvReader.cs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,24 @@ public CsvReader(StreamReader source)
1313
_source = source ?? throw new ArgumentNullException(nameof(source));
1414
}
1515

16-
StreamReader _source;
17-
16+
StreamReader? _source;
17+
StreamReader Source => _source ?? throw new ObjectDisposedException(GetType().ToString());
1818

1919
public void Dispose()
2020
{
2121
_source = null; // The intention here is if this object is disposed, then prevent further reading.
2222
}
2323

2424
public IEnumerable<string> ReadNextRow()
25-
=> GetNextRow(_source);
25+
=> GetNextRow(Source);
2626

27-
public ValueTask<IEnumerable<string>> ReadNextRowAsync()
28-
=> GetNextRowAsync(_source);
27+
public ValueTask<IEnumerable<string>?> ReadNextRowAsync()
28+
=> GetNextRowAsync(Source);
2929

3030
public IEnumerable<IEnumerable<string>> ReadRows()
3131
{
3232
var s = _source;
33-
if (s == null)
33+
if (s is null)
3434
throw new ObjectDisposedException(GetType().ToString());
3535
Contract.EndContractBlock();
3636

@@ -40,7 +40,7 @@ public IEnumerable<IEnumerable<string>> ReadRows()
4040

4141
public static bool TryGetNextRow(StreamReader source, out IEnumerable<string> row)
4242
{
43-
if (source == null)
43+
if (source is null)
4444
throw new ArgumentNullException(nameof(source));
4545
Contract.EndContractBlock();
4646

@@ -50,24 +50,24 @@ public static bool TryGetNextRow(StreamReader source, out IEnumerable<string> ro
5050
return true;
5151
}
5252

53-
row = null;
53+
row = null!;
5454
return false;
5555
}
5656

57-
public static ValueTask<IEnumerable<string>> GetNextRowAsync(StreamReader source)
57+
public static ValueTask<IEnumerable<string>?> GetNextRowAsync(StreamReader source)
5858
{
59-
if (source == null)
59+
if (source is null)
6060
throw new ArgumentNullException(nameof(source));
6161
Contract.EndContractBlock();
6262

6363
if (source.EndOfStream)
64-
return new ValueTask<IEnumerable<string>>(default(IEnumerable<string>));
64+
return new ValueTask<IEnumerable<string>?>(default(IEnumerable<string>));
6565

6666
return GetNextRowAsyncCore(source);
6767
}
6868

69-
static async ValueTask<IEnumerable<string>> GetNextRowAsyncCore(StreamReader source)
70-
=> source.EndOfStream ? null : CsvUtility.GetLine(await source.ReadLineAsync());
69+
static async ValueTask<IEnumerable<string>?> GetNextRowAsyncCore(StreamReader source)
70+
=> source.EndOfStream ? null : CsvUtility.GetLine(await source.ReadLineAsync().ConfigureAwait(false));
7171

7272
public static IEnumerable<string> GetNextRow(StreamReader source)
7373
{
@@ -77,7 +77,7 @@ public static IEnumerable<string> GetNextRow(StreamReader source)
7777

7878
public static IEnumerable<IEnumerable<string>> GetRows(StreamReader source)
7979
{
80-
if (source == null)
80+
if (source is null)
8181
throw new ArgumentNullException(nameof(source));
8282
Contract.EndContractBlock();
8383

@@ -87,18 +87,16 @@ public static IEnumerable<IEnumerable<string>> GetRows(StreamReader source)
8787

8888
public static IEnumerable<IEnumerable<string>> GetRowsFromFile(string filepath)
8989
{
90-
if (filepath == null)
90+
if (filepath is null)
9191
throw new ArgumentNullException(nameof(filepath));
9292
if (string.IsNullOrWhiteSpace(filepath))
9393
throw new ArgumentException("Cannot be empty or only whitespace.", nameof(filepath));
9494
Contract.EndContractBlock();
9595

96-
if (File.Exists(filepath))
97-
{
98-
using (var s = new StreamReader(new FileInfo(filepath).OpenRead()))
99-
foreach (var line in GetRows(s))
100-
yield return line;
101-
}
96+
if (!File.Exists(filepath)) yield break;
97+
using var s = new StreamReader(new FileInfo(filepath).OpenRead());
98+
foreach (var line in GetRows(s))
99+
yield return line;
102100
}
103101
}
104102
}

CsvRow.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ public CsvRow(in ReadOnlyMemory<string> headerRow)
1515

1616
public ReadOnlySpan<string> HeaderRow => _headerRow.Span;
1717

18-
public IEnumerable<string> GetRow(IDictionary<string, object> values)
18+
public IEnumerable<string?> GetRow(IDictionary<string, object> values)
1919
{
20-
if (values == null) throw new ArgumentNullException(nameof(values));
20+
if (values is null) throw new ArgumentNullException(nameof(values));
2121
Contract.EndContractBlock();
2222

2323
var len = _headerRow.Length;

CsvUtility.cs

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,18 @@ static IEnumerable<IEnumerable<string>> GetLinesCore(string csv)
6161

6262
public static string WrapQuotes(string value)
6363
{
64-
if (value == null)
64+
if (value is null)
6565
return string.Empty;
66-
66+
#if NETSTANDARD2_1
67+
return '"' + value.Replace("\"", "\"\"", StringComparison.Ordinal) + '"';
68+
#else
6769
return '"' + value.Replace("\"", "\"\"") + '"';
70+
#endif
6871
}
6972

7073
public static string FormatValue(string value, bool forceQuotes = false)
7174
{
72-
if (value == null)
75+
if (value is null)
7376
return string.Empty;
7477

7578
if (!string.IsNullOrEmpty(value) && forceQuotes || QUOTESNEEDED.IsMatch(value))
@@ -78,25 +81,14 @@ public static string FormatValue(string value, bool forceQuotes = false)
7881
return value;
7982
}
8083

81-
public static string ExportValue(object value, bool forceQuotes = false)
82-
{
83-
var result = string.Empty;
84-
switch (value)
84+
public static string ExportValue(object? value, bool forceQuotes = false)
85+
=> FormatValue(value switch
8586
{
86-
case null:
87-
return FormatValue(result, forceQuotes) + ",";
88-
case DateTime datetime:
89-
result = datetime.TimeOfDay == TimeSpan.Zero ?
90-
datetime.ToString("d") : // Use short date.
91-
datetime.ToString(CultureInfo.InvariantCulture);
92-
break;
93-
default:
94-
result = value.ToString();
95-
break;
96-
}
97-
98-
return FormatValue(result, forceQuotes) + ",";
99-
}
87+
DateTime datetime => datetime.TimeOfDay == TimeSpan.Zero ?
88+
datetime.ToString("d", CultureInfo.InvariantCulture) : // Use short date.
89+
datetime.ToString(CultureInfo.InvariantCulture),
90+
_ => value is null ? string.Empty : value.ToString(),
91+
}, forceQuotes) + ",";
10092

10193
}
10294
}

CsvWriter.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,41 @@ namespace Open.Text.CSV
77
{
88
public class CsvWriter : IDisposable
99
{
10-
internal TextWriter Target;
10+
TextWriter? _target;
11+
internal TextWriter Target => _target ?? throw new ObjectDisposedException(GetType().ToString());
1112

1213
public CsvWriter(TextWriter target)
1314
{
14-
Target = target ?? throw new ArgumentNullException(nameof(target));
15+
_target = target ?? throw new ArgumentNullException(nameof(target));
1516
}
1617

1718
public void Dispose()
1819
{
19-
Target = null; // The intention here is if this object is disposed, then prevent further writing.
20+
_target = null; // The intention here is if this object is disposed, then prevent further writing.
2021
}
2122

2223
public void WriteRow<T>(IEnumerable<T> row, bool forceQuotes = false)
2324
{
24-
WriteRow(Target ?? throw new ObjectDisposedException(GetType().ToString()), row, forceQuotes);
25+
WriteRow(Target, row, forceQuotes);
2526
}
2627

2728
public void WriteRows<T>(IEnumerable<IEnumerable<T>> rows, bool forceQuotes = false)
2829
{
29-
WriteRows(Target ?? throw new ObjectDisposedException(GetType().ToString()), rows, forceQuotes);
30+
WriteRows(Target, rows, forceQuotes);
3031
}
3132

32-
public static void WriteValue(TextWriter writer, object value = null, bool forceQuotes = false)
33+
public static void WriteValue(TextWriter writer, object? value = null, bool forceQuotes = false)
3334
{
34-
if (writer == null) throw new ArgumentNullException(nameof(writer));
35+
if (writer is null) throw new ArgumentNullException(nameof(writer));
3536
Contract.EndContractBlock();
3637

3738
writer.Write(CsvUtility.ExportValue(value, forceQuotes));
3839
}
3940

4041
public static void WriteRow<T>(TextWriter writer, IEnumerable<T> row, bool forceQuotes = false)
4142
{
42-
if (writer == null) throw new ArgumentNullException(nameof(writer));
43-
if (row == null) throw new ArgumentNullException(nameof(row));
43+
if (writer is null) throw new ArgumentNullException(nameof(writer));
44+
if (row is null) throw new ArgumentNullException(nameof(row));
4445
Contract.EndContractBlock();
4546

4647
foreach (var o in row)
@@ -51,8 +52,8 @@ public static void WriteRow<T>(TextWriter writer, IEnumerable<T> row, bool force
5152

5253
public static void WriteRows<T>(TextWriter writer, IEnumerable<IEnumerable<T>> rows, bool forceQuotes = false)
5354
{
54-
if (writer == null) throw new ArgumentNullException(nameof(writer));
55-
if (rows == null) throw new ArgumentNullException(nameof(rows));
55+
if (writer is null) throw new ArgumentNullException(nameof(writer));
56+
if (rows is null) throw new ArgumentNullException(nameof(rows));
5657
Contract.EndContractBlock();
5758

5859
foreach (var row in rows)

ExcelCsvExtensions.cs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics.Contracts;
23
using System.IO;
34

45
namespace Open.Text.CSV.Excel
@@ -7,35 +8,51 @@ public static class ExcelCsvExtensions
78
{
89
public static void WriteCsvExcelValue(this TextWriter writer, string value)
910
{
11+
if (writer is null) throw new NullReferenceException();
12+
Contract.EndContractBlock();
13+
1014
CsvWriter.WriteValue(writer, '=' + CsvUtility.WrapQuotes(value));
1115
}
1216

13-
public static void WriteCsvExcelHyperlink(this TextWriter writer, Uri link, string text = null)
17+
public static void WriteCsvExcelHyperlink(this TextWriter writer, Uri link, string? text = null)
1418
{
15-
if (link == null) throw new ArgumentNullException(nameof(link));
19+
if (writer is null) throw new NullReferenceException();
20+
if (link is null) throw new ArgumentNullException(nameof(link));
21+
Contract.EndContractBlock();
1622

1723
writer.WriteCsvExcelHyperlink(link.ToString(), text);
1824
}
1925

20-
public static void WriteCsvExcelHyperlink(this TextWriter writer, string link, string text = null)
26+
public static void WriteCsvExcelHyperlink(this TextWriter writer, string link, string? text = null)
2127
{
22-
WriteCsvExcelValue(writer, $"=HYPERLINK({CsvUtility.WrapQuotes(link)}{(text == null ? string.Empty : (',' + CsvUtility.WrapQuotes(text)))})");
28+
if (writer is null) throw new NullReferenceException();
29+
Contract.EndContractBlock();
30+
31+
WriteCsvExcelValue(writer, $"=HYPERLINK({CsvUtility.WrapQuotes(link)}{(text is null ? string.Empty : (',' + CsvUtility.WrapQuotes(text)))})");
2332
}
2433

2534
public static void WriteExcelValue(this CsvWriter writer, string value)
2635
{
36+
if (writer is null) throw new NullReferenceException();
37+
Contract.EndContractBlock();
38+
2739
CsvWriter.WriteValue(writer.Target, '=' + CsvUtility.WrapQuotes(value));
2840
}
2941

30-
public static void WriteExcelHyperlink(this CsvWriter writer, Uri link, string text = null)
42+
public static void WriteExcelHyperlink(this CsvWriter writer, Uri link, string? text = null)
3143
{
32-
if (link == null) throw new ArgumentNullException(nameof(link));
44+
if (writer is null) throw new NullReferenceException();
45+
if (link is null) throw new ArgumentNullException(nameof(link));
46+
Contract.EndContractBlock();
3347

3448
WriteExcelHyperlink(writer, link.ToString(), text);
3549
}
3650

37-
public static void WriteExcelHyperlink(this CsvWriter writer, string link, string text = null)
51+
public static void WriteExcelHyperlink(this CsvWriter writer, string link, string? text = null)
3852
{
53+
if (writer is null) throw new NullReferenceException();
54+
Contract.EndContractBlock();
55+
3956
WriteCsvExcelHyperlink(writer.Target, link, text);
4057
}
4158
}

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2017
3+
Copyright (c) 2020 electricessence (Oren F.)
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

0 commit comments

Comments
 (0)