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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296// Copyright 2020 Heath Stewart.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
// Derived from https://github.com/dotnet/extensions/blob/master/src/Primitives/src/StringSegment.cs
// licensed under the Apache License, version 2.0. See ThirdPartyNotices.txt in the project root for license information.
using System;
using System.Runtime.CompilerServices;
internal readonly struct StringSegment : IEquatable<StringSegment>, IEquatable<string>
{
public static readonly StringSegment Empty = string.Empty;
public StringSegment(string buffer)
{
Buffer = buffer;
Offset = 0;
Length = buffer?.Length ?? 0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public StringSegment(string buffer, int offset, int length)
{
// TODO: Throw argument exceptions.
Buffer = buffer;
Offset = offset;
Length = length;
}
public string Buffer { get; }
public int Offset { get; }
public int Length { get; }
public string Value => HasValue ? Buffer.Substring(Offset, Length) : null;
public bool HasValue => Buffer != null;
public char this[int index]
{
get
{
if ((uint)index >= (uint)Length)
{
throw new ArgumentOutOfRangeException(nameof(index));
}
return Buffer[Offset + index];
}
}
public static bool IsNullOrEmpty(StringSegment value) => !value.HasValue || value.Length == 0;
public static bool operator ==(StringSegment left, StringSegment right) => left.Equals(right);
public static bool operator !=(StringSegment left, StringSegment right) => !left.Equals(right);
public static implicit operator StringSegment(string value) => new StringSegment(value);
public static implicit operator ReadOnlySpan<char>(StringSegment value) => value.AsSpan();
public static implicit operator ReadOnlyMemory<char>(StringSegment value) => value.AsMemory();
public ReadOnlySpan<char> AsSpan() => Buffer.AsSpan(Offset, Length);
public ReadOnlyMemory<char> AsMemory() => Buffer.AsMemory(Offset, Length);
public int IndexOfAny(char[] separators, int startIndex = 0) => IndexOfAny(separators, startIndex, Length - startIndex);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int IndexOfAny(char[] separators, int startIndex, int count)
{
int index = -1;
if (HasValue)
{
if (startIndex < 0 || Offset + startIndex > Buffer.Length)
{
throw new ArgumentOutOfRangeException(nameof(startIndex));
}
if (count < 0 || Offset + startIndex + count > Buffer.Length)
{
throw new ArgumentOutOfRangeException(nameof(count));
}
index = Buffer.IndexOfAny(separators, Offset + startIndex, count);
if (index != -1)
{
index -= Offset;
}
}
return index;
}
public StringTokenizer Split(char[] separators, StringSplitOptions options = default) => new StringTokenizer(this, separators, options);
public StringSegment Subsegment(int offset) => Subsegment(offset, Length - offset);
public StringSegment Subsegment(int offset, int length)
{
// TODO: Throw argument exceptions.
return new StringSegment(Buffer, Offset + offset, length);
}
public StringSegment Trim() => TrimStart().TrimEnd();
public StringSegment UnsafeTrim() => UnsafeTrimStart().UnsafeTrimEnd();
public StringSegment TrimStart()
{
int trimmedStart = Offset;
int length = Offset + Length;
while (trimmedStart < length)
{
char c = Buffer[trimmedStart];
if (!char.IsWhiteSpace(c))
{
break;
}
trimmedStart++;
}
return new StringSegment(Buffer, trimmedStart, length - trimmedStart);
}
public unsafe StringSegment UnsafeTrimStart()
{
int trimmedStart = Offset;
int length = Offset + Length;
fixed (char* p = Buffer)
{
while (trimmedStart < length)
{
char c = p[trimmedStart];
if (!char.IsWhiteSpace(c))
{
break;
}
trimmedStart++;
}
}
return new StringSegment(Buffer, trimmedStart, length - trimmedStart);
}
public StringSegment TrimEnd()
{
int offset = Offset;
int trimmedEnd = offset + Length - 1;
while (trimmedEnd >= offset)
{
char c = Buffer[trimmedEnd];
if (!char.IsWhiteSpace(c))
{
break;
}
trimmedEnd--;
}
return new StringSegment(Buffer, offset, trimmedEnd- offset + 1);
}
public unsafe StringSegment UnsafeTrimEnd()
{
int offset = Offset;
int trimmedEnd = offset + Length - 1;
fixed (char* p = Buffer)
{
while (trimmedEnd >= offset)
{
char c = p[trimmedEnd];
if (!char.IsWhiteSpace(c))
{
break;
}
trimmedEnd--;
}
}
return new StringSegment(Buffer, offset, trimmedEnd- offset + 1);
}
public static bool Equals(StringSegment left, StringSegment right, StringComparison comparisonType) => left.Equals(right, comparisonType);
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}
return obj is StringSegment value && Equals(value);
}
public bool Equals(string other) => Equals(other, StringComparison.Ordinal);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(string other, StringComparison comparisonType)
{
if (other is null)
{
throw new ArgumentNullException(nameof(other));
}
int length = other.Length;
if (!HasValue || Length != length)
{
return false;
}
return string.Compare(Buffer, Offset, other, 0, length, comparisonType) == 0;
}
public bool Equals(StringSegment other) => Equals(other, StringComparison.Ordinal);
public bool Equals(StringSegment other, StringComparison comparisonType)
{
if (Length != other.Length)
{
return false;
}
return string.Compare(Buffer, Offset, other.Buffer, other.Offset, other.Length, comparisonType) == 0;
}
public override int GetHashCode() => Value?.GetHashCode() ?? 0;
public override string ToString() => Value ?? string.Empty;
internal void Skip(ref int index, char[] skipChars)
{
while (index < Offset + Length)
{
bool found = false;
char c = Buffer[index];
for (int i = 0; i < skipChars.Length; ++i)
{
if (c == skipChars[i])
{
found = true;
index++;
break;
}
}
if (!found)
{
break;
}
}
}
internal unsafe void UnsafeSkip(ref int index, char[] skipChars)
{
fixed (char* p = Buffer)
{
while (index < Offset + Length)
{
bool found = false;
char c = p[index];
for (int i = 0; i < skipChars.Length; ++i)
{
if (c == skipChars[i])
{
found = true;
index++;
break;
}
}
if (!found)
{
break;
}
}
}
}
}