@@ -14,119 +14,90 @@ namespace Microsoft.Toolkit.HighPerformance.Enumerables
14
14
/// </summary>
15
15
/// <typeparam name="T">The type of items to enumerate.</typeparam>
16
16
[ EditorBrowsable ( EditorBrowsableState . Never ) ]
17
- public readonly ref struct ReadOnlySpanTokenizer < T >
17
+ public ref struct ReadOnlySpanTokenizer < T >
18
18
where T : IEquatable < T >
19
19
{
20
20
/// <summary>
21
- /// The source <see cref="ReadOnlySpan{T}"/> instance
21
+ /// The source <see cref="ReadOnlySpan{T}"/> instance.
22
22
/// </summary>
23
23
private readonly ReadOnlySpan < T > span ;
24
24
25
25
/// <summary>
26
- /// The separator <typeparamref name="T"/> item to use.
26
+ /// The separator item to use.
27
27
/// </summary>
28
28
private readonly T separator ;
29
29
30
+ /// <summary>
31
+ /// The current initial offset.
32
+ /// </summary>
33
+ private int start ;
34
+
35
+ /// <summary>
36
+ /// The current final offset.
37
+ /// </summary>
38
+ private int end ;
39
+
30
40
/// <summary>
31
41
/// Initializes a new instance of the <see cref="ReadOnlySpanTokenizer{T}"/> struct.
32
42
/// </summary>
33
- /// <param name="span">The source <see cref="ReadOnlySpan{T}"/> to tokenize.</param>
34
- /// <param name="separator">The separator <typeparamref name="T"/> item to use.</param>
35
- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
43
+ /// <param name="span">The source <see cref="ReadOnlySpan{T}"/> instance.</param>
44
+ /// <param name="separator">The separator item to use.</param>
36
45
public ReadOnlySpanTokenizer ( ReadOnlySpan < T > span , T separator )
37
46
{
38
47
this . span = span ;
39
48
this . separator = separator ;
49
+ this . start = 0 ;
50
+ this . end = - 1 ;
40
51
}
41
52
42
53
/// <summary>
43
54
/// Implements the duck-typed <see cref="IEnumerable{T}.GetEnumerator"/> method.
44
55
/// </summary>
45
- /// <returns>An <see cref="Enumerator "/> instance targeting the current <see cref="ReadOnlySpan{T}"/> value.</returns>
56
+ /// <returns>An <see cref="ReadOnlySpanTokenizer{T} "/> instance targeting the current <see cref="ReadOnlySpan{T}"/> value.</returns>
46
57
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
47
- public Enumerator GetEnumerator ( ) => new Enumerator ( this . span , this . separator ) ;
58
+ public ReadOnlySpanTokenizer < T > GetEnumerator ( ) => this ;
48
59
49
60
/// <summary>
50
- /// An enumerator for a source <see cref="ReadOnlySpan{T} "/> instance .
61
+ /// Implements the duck-typed <see cref="System.Collections.IEnumerator.MoveNext "/> method .
51
62
/// </summary>
52
- [ EditorBrowsable ( EditorBrowsableState . Never ) ]
53
- public ref struct Enumerator
63
+ /// <returns><see langword="true"/> whether a new element is available, <see langword="false"/> otherwise</returns>
64
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
65
+ public bool MoveNext ( )
54
66
{
55
- /// <summary>
56
- /// The source <see cref="ReadOnlySpan{T}"/> instance.
57
- /// </summary>
58
- private readonly ReadOnlySpan < T > span ;
59
-
60
- /// <summary>
61
- /// The separator item to use.
62
- /// </summary>
63
- private readonly T separator ;
64
-
65
- /// <summary>
66
- /// The current initial offset.
67
- /// </summary>
68
- private int start ;
69
-
70
- /// <summary>
71
- /// The current final offset.
72
- /// </summary>
73
- private int end ;
74
-
75
- /// <summary>
76
- /// Initializes a new instance of the <see cref="Enumerator"/> struct.
77
- /// </summary>
78
- /// <param name="span">The source <see cref="ReadOnlySpan{T}"/> instance.</param>
79
- /// <param name="separator">The separator item to use.</param>
80
- public Enumerator ( ReadOnlySpan < T > span , T separator )
81
- {
82
- this . span = span ;
83
- this . separator = separator ;
84
- this . start = 0 ;
85
- this . end = - 1 ;
86
- }
67
+ int
68
+ newEnd = this . end + 1 ,
69
+ length = this . span . Length ;
87
70
88
- /// <summary>
89
- /// Implements the duck-typed <see cref="System.Collections.IEnumerator.MoveNext"/> method.
90
- /// </summary>
91
- /// <returns><see langword="true"/> whether a new element is available, <see langword="false"/> otherwise</returns>
92
- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
93
- public bool MoveNext ( )
71
+ // Additional check if the separator is not the last character
72
+ if ( newEnd <= length )
94
73
{
95
- int
96
- newEnd = this . end + 1 ,
97
- length = this . span . Length ;
98
-
99
- // Additional check if the separator is not the last character
100
- if ( newEnd <= length )
101
- {
102
- this . start = newEnd ;
74
+ this . start = newEnd ;
103
75
104
- int index = this . span . Slice ( newEnd ) . IndexOf ( this . separator ) ;
76
+ int index = this . span . Slice ( newEnd ) . IndexOf ( this . separator ) ;
105
77
106
- // Extract the current subsequence
107
- if ( index >= 0 )
108
- {
109
- this . end = newEnd + index ;
110
-
111
- return true ;
112
- }
113
-
114
- this . end = length ;
78
+ // Extract the current subsequence
79
+ if ( index >= 0 )
80
+ {
81
+ this . end = newEnd + index ;
115
82
116
83
return true ;
117
84
}
118
85
119
- return false ;
120
- }
86
+ this . end = length ;
121
87
122
- /// <summary>
123
- /// Gets the duck-typed <see cref="IEnumerator{T}.Current"/> property.
124
- /// </summary>
125
- public ReadOnlySpan < T > Current
126
- {
127
- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
128
- get => this . span . Slice ( this . start , this . end - this . start ) ;
88
+ return true ;
129
89
}
90
+
91
+ return false ;
92
+ }
93
+
94
+ /// <summary>
95
+ /// Gets the duck-typed <see cref="IEnumerator{T}.Current"/> property.
96
+ /// </summary>
97
+ public ReadOnlySpan < T > Current
98
+ {
99
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
100
+ get => this . span . Slice ( this . start , this . end - this . start ) ;
130
101
}
131
102
}
132
103
}
0 commit comments