-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCharRange.cs
275 lines (217 loc) · 8.99 KB
/
CharRange.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace System
{
[Serializable]
public readonly struct CharRange : IRange<char, CharRange.Enumerator>, IEquatableReadOnlyStruct<CharRange>, ISerializable
{
public char Start { get; }
public char End { get; }
public bool IsFromEnd { get; }
public CharRange(char start, char end)
{
this.Start = start;
this.End = end;
this.IsFromEnd = false;
}
public CharRange(char start, char end, bool fromEnd)
{
this.Start = start;
this.End = end;
this.IsFromEnd = fromEnd;
}
private CharRange(SerializationInfo info, StreamingContext context)
{
this.Start = info.GetCharOrDefault(nameof(this.Start));
this.End = info.GetCharOrDefault(nameof(this.End));
this.IsFromEnd = info.GetBooleanOrDefault(nameof(this.IsFromEnd));
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue(nameof(this.Start), this.Start);
info.AddValue(nameof(this.End), this.End);
info.AddValue(nameof(this.IsFromEnd), this.IsFromEnd);
}
public void Deconstruct(out char start, out char end)
{
start = this.Start;
end = this.End;
}
public void Deconstruct(out char start, out char end, out bool fromEnd)
{
start = this.Start;
end = this.End;
fromEnd = this.IsFromEnd;
}
public CharRange With(in char? Start = null, in char? End = null, bool? IsFromEnd = null)
=> new CharRange(
Start ?? this.Start,
End ?? this.End,
IsFromEnd ?? this.IsFromEnd
);
public CharRange FromStart()
=> new CharRange(this.Start, this.End, false);
public CharRange FromEnd()
=> new CharRange(this.Start, this.End, true);
IRange<char> IRange<char>.FromStart()
=> FromStart();
IRange<char> IRange<char>.FromEnd()
=> FromEnd();
public char Count()
{
if (this.End > this.Start)
return (char)(this.End - this.Start + 1);
return (char)(this.Start - this.End + 1);
}
public bool Contains(char value)
=> this.Start < this.End
? value >= this.Start && value <= this.End
: value >= this.End && value <= this.Start;
public override bool Equals(object obj)
=> obj is CharRange other &&
this.Start == other.Start && this.End == other.End &&
this.IsFromEnd == other.IsFromEnd;
public bool Equals(in CharRange other)
=> this.Start == other.Start && this.End == other.End &&
this.IsFromEnd == other.IsFromEnd;
public bool Equals(CharRange other)
=> this.Start == other.Start && this.End == other.End &&
this.IsFromEnd == other.IsFromEnd;
public override int GetHashCode()
{
#if USE_SYSTEM_HASHCODE
return HashCode.Combine(this.Start, this.End, this.IsFromEnd);
#endif
#pragma warning disable CS0162 // Unreachable code detected
var hashCode = -1418356749;
hashCode = hashCode * -1521134295 + this.Start.GetHashCode();
hashCode = hashCode * -1521134295 + this.End.GetHashCode();
hashCode = hashCode * -1521134295 + this.IsFromEnd.GetHashCode();
return hashCode;
#pragma warning restore CS0162 // Unreachable code detected
}
public override string ToString()
=> $"{{ {nameof(this.Start)}={this.Start}, {nameof(this.End)}={this.End}, {nameof(this.IsFromEnd)}={this.IsFromEnd} }}";
public Enumerator GetEnumerator()
=> new Enumerator(this);
public Enumerator Range()
=> GetEnumerator();
IEnumerator<char> IRange<char>.Range()
=> GetEnumerator();
public CharRange Normalize()
=> Normal(this.Start, this.End);
/// <summary>
/// Create a normal range from (a, b) where <see cref="Start"/> is lesser than <see cref="End"/>.
/// </summary>
public static CharRange Normal(char a, char b)
=> a > b ? new CharRange(b, a) : new CharRange(a, b);
/// <summary>
/// Create a range from a size which is greater than 0
/// </summary>
/// <exception cref="InvalidOperationException">Size must be greater than 0</exception>
public static CharRange FromSize(char value, bool fromEnd = false)
{
if (value == 0)
throw new InvalidOperationException("Size must be greater than 0");
return new CharRange((char)0, (char)(value > 0 ? value - 1 : value), fromEnd);
}
public static CharRange FromStart(char start, char end)
=> new CharRange(start, end, false);
public static CharRange FromEnd(char start, char end)
=> new CharRange(start, end, true);
public static implicit operator CharRange(in (char start, char end) value)
=> new CharRange(value.start, value.end);
public static implicit operator CharRange(in (char start, char end, bool fromEnd) value)
=> new CharRange(value.start, value.end, value.fromEnd);
public static implicit operator ReadRange<char, Enumerator.Default>(in CharRange value)
=> new ReadRange<char, Enumerator.Default>(value.Start, value.End, value.IsFromEnd);
public static implicit operator ReadRange<char>(in CharRange value)
=> new ReadRange<char>(value.Start, value.End, value.IsFromEnd, new Enumerator.Default());
public static implicit operator CharRange(in ReadRange<char> value)
=> new CharRange(value.Start, value.End, value.IsFromEnd);
public static implicit operator CharRange(in ReadRange<char, Enumerator.Default> value)
=> new CharRange(value.Start, value.End, value.IsFromEnd);
public static bool operator ==(in CharRange lhs, in CharRange rhs)
=> lhs.Start == rhs.Start && lhs.End == rhs.End &&
lhs.IsFromEnd == rhs.IsFromEnd;
public static bool operator !=(in CharRange lhs, in CharRange rhs)
=> lhs.Start != rhs.Start || lhs.End != rhs.End ||
lhs.IsFromEnd != rhs.IsFromEnd;
public struct Enumerator : IEnumerator<char>
{
private readonly char start;
private readonly char end;
private readonly sbyte sign;
private char current;
private sbyte flag;
public Enumerator(in CharRange range)
: this(range.Start, range.End, range.IsFromEnd)
{ }
public Enumerator(char start, char end, bool fromEnd)
{
var increasing = start <= end;
if (fromEnd)
{
this.start = end;
this.end = start;
}
else
{
this.start = start;
this.end = end;
}
this.sign = (sbyte)(increasing
? (fromEnd ? -1 : 1)
: (fromEnd ? 1 : -1));
this.current = this.start;
this.flag = -1;
}
public bool MoveNext()
{
if (this.flag == 0)
{
if (this.current == this.end)
{
this.flag = 1;
return false;
}
this.current += (char)this.sign;
return true;
}
if (this.flag < 0)
{
this.flag = 0;
return true;
}
return false;
}
public char Current
{
get
{
if (this.flag < 0)
throw ThrowHelper.GetInvalidOperationException_InvalidOperation_EnumNotStarted();
if (this.flag > 0)
throw ThrowHelper.GetInvalidOperationException_InvalidOperation_EnumEnded();
return this.current;
}
}
public void Dispose()
{
}
object IEnumerator.Current
=> this.Current;
void IEnumerator.Reset()
{
this.current = this.start;
this.flag = -1;
}
public readonly struct Default : IRangeEnumerator<char>
{
public IEnumerator<char> Enumerate(char start, char end, bool fromEnd)
=> new Enumerator(start, end, fromEnd);
}
}
}
}