forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringSegment.cs
More file actions
439 lines (384 loc) · 18.1 KB
/
StringSegment.cs
File metadata and controls
439 lines (384 loc) · 18.1 KB
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#if !NETSTANDARD1_1
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Runtime.CompilerServices;
namespace ServiceStack.Text
{
/// <summary>
/// An optimized representation of a substring.
/// </summary>
public struct StringSegment : IEquatable<StringSegment>, IEquatable<string>
{
/// <summary>
/// Initializes an instance of the <see cref="StringSegment"/> struct.
/// </summary>
/// <param name="buffer">
/// The original <see cref="string"/>. The <see cref="StringSegment"/> includes the whole <see cref="string"/>.
/// </param>
public StringSegment(string buffer)
{
Buffer = buffer;
Offset = 0;
Length = buffer?.Length ?? 0;
}
/// <summary>
/// Initializes an instance of the <see cref="StringSegment"/> struct.
/// </summary>
/// <param name="buffer">The original <see cref="string"/> used as buffer.</param>
/// <param name="offset">The offset of the segment within the <paramref name="buffer"/>.</param>
/// <param name="length">The length of the segment.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public StringSegment(string buffer, int offset, int length)
{
// Validate arguments, check is minimal instructions with reduced branching for inlinable fast-path
// Negative values discovered though conversion to high values when converted to unsigned
// Failure should be rare and location determination and message is delegated to failure functions
if (buffer == null || (uint)offset > (uint)buffer.Length || (uint)length > (uint)(buffer.Length - offset))
{
ThrowInvalidArguments(buffer, offset, length);
}
Buffer = buffer;
Offset = offset;
Length = length;
}
private static void ThrowInvalidArguments(string buffer, int offset, int length)
{
// Only have single throw in method so is marked as "does not return" and isn't inlined to caller
throw GetInvalidArgumentException(buffer, offset, length);
}
private static Exception GetInvalidArgumentException(string buffer, int offset, int length)
{
if (buffer == null)
{
return new ArgumentNullException(nameof(buffer));
}
if (offset < 0)
{
return new ArgumentOutOfRangeException(nameof(offset));
}
if (length < 0)
{
return new ArgumentOutOfRangeException(nameof(length));
}
return new ArgumentException("invalid offset or length");
}
/// <summary>
/// Gets the <see cref="string"/> buffer for this <see cref="StringSegment"/>.
/// </summary>
public string Buffer { get; }
/// <summary>
/// Gets the offset within the buffer for this <see cref="StringSegment"/>.
/// </summary>
public int Offset { get; }
/// <summary>
/// Gets the length of this <see cref="StringSegment"/>.
/// </summary>
public int Length { get; }
/// <summary>
/// Gets the value of this segment as a <see cref="string"/>.
/// </summary>
public string Value
{
get
{
if (!HasValue)
{
return null;
}
else
{
return Buffer.Substring(Offset, Length);
}
}
}
/// <summary>
/// Gets whether or not this <see cref="StringSegment"/> contains a valid value.
/// </summary>
public bool HasValue
{
get { return Buffer != null; }
}
/// <inheritdoc />
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}
return obj is StringSegment && Equals((StringSegment)obj);
}
/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns><code>true</code> if the current object is equal to the other parameter; otherwise, <code>false</code>.</returns>
public bool Equals(StringSegment other)
{
return Equals(other, StringComparison.Ordinal);
}
/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">An object to compare with this object.</param>
/// <param name="comparisonType">One of the enumeration values that specifies the rules to use in the comparison.</param>
/// <returns><code>true</code> if the current object is equal to the other parameter; otherwise, <code>false</code>.</returns>
public bool Equals(StringSegment other, StringComparison comparisonType)
{
int textLength = other.Length;
if (Length != textLength)
{
return false;
}
return string.Compare(Buffer, Offset, other.Buffer, other.Offset, textLength, comparisonType) == 0;
}
/// <summary>
/// Checks if the specified <see cref="string"/> is equal to the current <see cref="StringSegment"/>.
/// </summary>
/// <param name="text">The <see cref="string"/> to compare with the current <see cref="StringSegment"/>.</param>
/// <returns><code>true</code> if the specified <see cref="string"/> is equal to the current <see cref="StringSegment"/>; otherwise, <code>false</code>.</returns>
public bool Equals(string text)
{
return Equals(text, StringComparison.Ordinal);
}
/// <summary>
/// Checks if the specified <see cref="string"/> is equal to the current <see cref="StringSegment"/>.
/// </summary>
/// <param name="text">The <see cref="string"/> to compare with the current <see cref="StringSegment"/>.</param>
/// <param name="comparisonType">One of the enumeration values that specifies the rules to use in the comparison.</param>
/// <returns><code>true</code> if the specified <see cref="string"/> is equal to the current <see cref="StringSegment"/>; otherwise, <code>false</code>.</returns>
public bool Equals(string text, StringComparison comparisonType)
{
if (text == null)
{
throw new ArgumentNullException(nameof(text));
}
int textLength = text.Length;
if (!HasValue || Length != textLength)
{
return false;
}
return string.Compare(Buffer, Offset, text, 0, textLength, comparisonType) == 0;
}
/// <inheritdoc />
/// <remarks>
/// This GetHashCode is expensive since it allocates on every call.
/// However this is required to ensure we retain any behavior (such as hash code randomization) that
/// string.GetHashCode has.
/// </remarks>
public override int GetHashCode()
{
if (!HasValue)
{
return 0;
}
else
{
return Value.GetHashCode();
}
}
/// <summary>
/// Checks if two specified <see cref="StringSegment"/> have the same value.
/// </summary>
/// <param name="left">The first <see cref="StringSegment"/> to compare, or <code>null</code>.</param>
/// <param name="right">The second <see cref="StringSegment"/> to compare, or <code>null</code>.</param>
/// <returns><code>true</code> if the value of <paramref name="left"/> is the same as the value of <paramref name="right"/>; otherwise, <code>false</code>.</returns>
public static bool operator ==(StringSegment left, StringSegment right)
{
return left.Equals(right);
}
/// <summary>
/// Checks if two specified <see cref="StringSegment"/> have different values.
/// </summary>
/// <param name="left">The first <see cref="StringSegment"/> to compare, or <code>null</code>.</param>
/// <param name="right">The second <see cref="StringSegment"/> to compare, or <code>null</code>.</param>
/// <returns><code>true</code> if the value of <paramref name="left"/> is different from the value of <paramref name="right"/>; otherwise, <code>false</code>.</returns>
public static bool operator !=(StringSegment left, StringSegment right)
{
return !left.Equals(right);
}
/// <summary>
/// Checks if the beginning of this <see cref="StringSegment"/> matches the specified <see cref="string"/> when compared using the specified <paramref name="comparisonType"/>.
/// </summary>
/// <param name="text">The <see cref="string"/>to compare.</param>
/// <param name="comparisonType">One of the enumeration values that specifies the rules to use in the comparison.</param>
/// <returns><code>true</code> if <paramref name="text"/> matches the beginning of this <see cref="StringSegment"/>; otherwise, <code>false</code>.</returns>
public bool StartsWith(string text, StringComparison comparisonType)
{
if (text == null)
{
throw new ArgumentNullException(nameof(text));
}
var textLength = text.Length;
if (!HasValue || Length < textLength)
{
return false;
}
return string.Compare(Buffer, Offset, text, 0, textLength, comparisonType) == 0;
}
/// <summary>
/// Checks if the end of this <see cref="StringSegment"/> matches the specified <see cref="string"/> when compared using the specified <paramref name="comparisonType"/>.
/// </summary>
/// <param name="text">The <see cref="string"/>to compare.</param>
/// <param name="comparisonType">One of the enumeration values that specifies the rules to use in the comparison.</param>
/// <returns><code>true</code> if <paramref name="text"/> matches the end of this <see cref="StringSegment"/>; otherwise, <code>false</code>.</returns>
public bool EndsWith(string text, StringComparison comparisonType)
{
if (text == null)
{
throw new ArgumentNullException(nameof(text));
}
var textLength = text.Length;
if (!HasValue || Length < textLength)
{
return false;
}
return string.Compare(Buffer, Offset + Length - textLength, text, 0, textLength, comparisonType) == 0;
}
/// <summary>
/// Retrieves a substring from this <see cref="StringSegment"/>.
/// The substring starts at the position specified by <paramref name="offset"/> and has the specified <paramref name="length"/>.
/// </summary>
/// <param name="offset">The zero-based starting character position of a substring in this <see cref="StringSegment"/>.</param>
/// <param name="length">The number of characters in the substring.</param>
/// <returns>A <see cref="string"/> that is equivalent to the substring of length <paramref name="length"/> that begins at <paramref name="offset"/> in this <see cref="StringSegment"/></returns>
public string Substring(int offset, int length)
{
if (!HasValue)
{
throw new ArgumentOutOfRangeException(nameof(offset));
}
if (offset < 0 || offset + length > Length)
{
throw new ArgumentOutOfRangeException(nameof(offset));
}
if (length < 0 || Offset + offset + length > Buffer.Length)
{
throw new ArgumentOutOfRangeException(nameof(length));
}
return Buffer.Substring(Offset + offset, length);
}
/// <summary>
/// Retrieves a <see cref="StringSegment"/> that represents a substring from this <see cref="StringSegment"/>.
/// The <see cref="StringSegment"/> starts at the position specified by <paramref name="offset"/> and has the specified <paramref name="length"/>.
/// </summary>
/// <param name="offset">The zero-based starting character position of a substring in this <see cref="StringSegment"/>.</param>
/// <param name="length">The number of characters in the substring.</param>
/// <returns>A <see cref="StringSegment"/> that is equivalent to the substring of length <paramref name="length"/> that begins at <paramref name="offset"/> in this <see cref="StringSegment"/></returns>
public StringSegment Subsegment(int offset, int length)
{
if (!HasValue)
{
throw new ArgumentOutOfRangeException(nameof(offset));
}
if (offset < 0 || offset + length > Length)
{
throw new ArgumentOutOfRangeException(nameof(offset));
}
if (length < 0 || Offset + offset + length > Buffer.Length)
{
throw new ArgumentOutOfRangeException(nameof(length));
}
return new StringSegment(Buffer, Offset + offset, length);
}
/// <summary>
/// Gets the zero-based index of the first occurrence of the character <paramref name="c"/> in this <see cref="StringSegment"/>.
/// The search starts at <paramref name="start"/> and examines a specified number of <paramref name="count"/> character positions.
/// </summary>
/// <param name="c">The Unicode character to seek.</param>
/// <param name="start">The zero-based index position at which the search starts. </param>
/// <param name="count">The number of characters to examine.</param>
/// <returns>The zero-based index position of <paramref name="c"/> from the beginning of the <see cref="StringSegment"/> if that character is found, or -1 if it is not.</returns>
public int IndexOf(char c, int start, int count)
{
if (start < 0 || Offset + start > Buffer.Length)
{
throw new ArgumentOutOfRangeException(nameof(start));
}
if (count < 0 || Offset + start + count > Buffer.Length)
{
throw new ArgumentOutOfRangeException(nameof(count));
}
var index = Buffer.IndexOf(c, start + Offset, count);
if (index != -1)
{
return index - Offset;
}
else
{
return index;
}
}
/// <summary>
/// Gets the zero-based index of the first occurrence of the character <paramref name="c"/> in this <see cref="StringSegment"/>.
/// The search starts at <paramref name="start"/>.
/// </summary>
/// <param name="c">The Unicode character to seek.</param>
/// <param name="start">The zero-based index position at which the search starts. </param>
/// <returns>The zero-based index position of <paramref name="c"/> from the beginning of the <see cref="StringSegment"/> if that character is found, or -1 if it is not.</returns>
public int IndexOf(char c, int start)
{
return IndexOf(c, start, Length - start);
}
/// <summary>
/// Gets the zero-based index of the first occurrence of the character <paramref name="c"/> in this <see cref="StringSegment"/>.
/// </summary>
/// <param name="c">The Unicode character to seek.</param>
/// <returns>The zero-based index position of <paramref name="c"/> from the beginning of the <see cref="StringSegment"/> if that character is found, or -1 if it is not.</returns>
public int IndexOf(char c)
{
return IndexOf(c, 0, Length);
}
/// <summary>
/// Removes all leading and trailing whitespaces.
/// </summary>
/// <returns>The trimmed <see cref="StringSegment"/>.</returns>
public StringSegment Trim()
{
return TrimStart().TrimEnd();
}
/// <summary>
/// Removes all leading whitespaces.
/// </summary>
/// <returns>The trimmed <see cref="StringSegment"/>.</returns>
public StringSegment TrimStart()
{
var trimmedStart = Offset;
while (trimmedStart < Offset + Length)
{
if (!char.IsWhiteSpace(Buffer, trimmedStart))
{
break;
}
trimmedStart++;
}
return new StringSegment(Buffer, trimmedStart, Offset + Length - trimmedStart);
}
/// <summary>
/// Removes all trailing whitespaces.
/// </summary>
/// <returns>The trimmed <see cref="StringSegment"/>.</returns>
public StringSegment TrimEnd()
{
var trimmedEnd = Offset + Length - 1;
while (trimmedEnd >= Offset)
{
if (!char.IsWhiteSpace(Buffer, trimmedEnd))
{
break;
}
trimmedEnd--;
}
return new StringSegment(Buffer, Offset, trimmedEnd - Offset + 1);
}
/// <summary>
/// Returns the <see cref="string"/> represented by this <see cref="StringSegment"/> or <code>String.Empty</code> if the <see cref="StringSegment"/> does not contain a value.
/// </summary>
/// <returns>The <see cref="string"/> represented by this <see cref="StringSegment"/> or <code>String.Empty</code> if the <see cref="StringSegment"/> does not contain a value.</returns>
public override string ToString()
{
return Value ?? string.Empty;
}
}
}
#endif