-
Notifications
You must be signed in to change notification settings - Fork 34
/
Sep.cs
63 lines (50 loc) · 1.72 KB
/
Sep.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Diagnostics.Contracts;
namespace nietras.SeparatedValues;
public readonly record struct Sep
{
const char _min = (char)32;
const char _max = (char)126;
readonly char _separator;
public Sep() : this(SepDefaults.Separator) { }
public Sep(char separator)
{
Validate(separator);
_separator = separator;
}
public char Separator
{
get => _separator;
init { Validate(value); _separator = value; }
}
public static Sep Default { get; } = new(SepDefaults.Separator);
public static Sep? Auto => null;
internal static Sep Min { get; } = new(_min);
internal static Sep Max { get; } = new(_max);
public static Sep New(char separator) => new(separator);
public static SepReaderOptions Reader() => new(null);
public static SepReaderOptions Reader(Func<SepReaderOptions, SepReaderOptions> configure)
{
Contract.Assume(configure != null);
return configure(Reader());
}
public static SepWriterOptions Writer() => new(Default);
public static SepWriterOptions Writer(Func<SepWriterOptions, SepWriterOptions> configure)
{
Contract.Assume(configure != null);
return configure(Writer());
}
internal static void Validate(char separator)
{
if (separator != '\t' && (separator < _min || separator > _max))
{
SepThrow.ArgumentOutOfRangeException_Separator(separator);
}
if (separator == SepDefaults.Comment ||
separator == SepDefaults.Quote)
{
SepThrow.ArgumentException_Separator(separator);
}
}
internal string[] Split(string line) => line.Split(Separator, StringSplitOptions.None);
}