/
StringTokenizer.cs
124 lines (102 loc) · 3.84 KB
/
StringTokenizer.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections;
using System.Collections.Generic;
namespace Microsoft.Extensions.Primitives
{
/// <summary>
/// Tokenizes a <see cref="string"/> into <see cref="StringSegment"/>s.
/// </summary>
public readonly struct StringTokenizer : IEnumerable<StringSegment>
{
private readonly StringSegment _value;
private readonly char[] _separators;
/// <summary>
/// Initializes a new instance of <see cref="StringTokenizer"/>.
/// </summary>
/// <param name="value">The <see cref="string"/> to tokenize.</param>
/// <param name="separators">The characters to tokenize by.</param>
public StringTokenizer(string value, char[] separators)
{
if (value == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value);
}
if (separators == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.separators);
}
_value = value;
_separators = separators;
}
/// <summary>
/// Initializes a new instance of <see cref="StringTokenizer"/>.
/// </summary>
/// <param name="value">The <see cref="StringSegment"/> to tokenize.</param>
/// <param name="separators">The characters to tokenize by.</param>
public StringTokenizer(StringSegment value, char[] separators)
{
if (!value.HasValue)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value);
}
if (separators == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.separators);
}
_value = value;
_separators = separators;
}
public Enumerator GetEnumerator() => new Enumerator(in _value, _separators);
IEnumerator<StringSegment> IEnumerable<StringSegment>.GetEnumerator() => GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public struct Enumerator : IEnumerator<StringSegment>
{
private readonly StringSegment _value;
private readonly char[] _separators;
private int _index;
internal Enumerator(in StringSegment value, char[] separators)
{
_value = value;
_separators = separators;
Current = default;
_index = 0;
}
public Enumerator(ref StringTokenizer tokenizer)
{
_value = tokenizer._value;
_separators = tokenizer._separators;
Current = default(StringSegment);
_index = 0;
}
public StringSegment Current { get; private set; }
object IEnumerator.Current => Current;
public void Dispose()
{
}
public bool MoveNext()
{
if (!_value.HasValue || _index > _value.Length)
{
Current = default(StringSegment);
return false;
}
int next = _value.IndexOfAny(_separators, _index);
if (next == -1)
{
// No separator found. Consume the remainder of the string.
next = _value.Length;
}
Current = _value.Subsegment(_index, next - _index);
_index = next + 1;
return true;
}
public void Reset()
{
Current = default(StringSegment);
_index = 0;
}
}
}
}