/
SpanRuneEnumerator.cs
50 lines (42 loc) · 1.73 KB
/
SpanRuneEnumerator.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace System.Text
{
// An enumerator for retrieving System.Text.Rune instances from a ROS<char>.
// Methods are pattern-matched by compiler to allow using foreach pattern.
public ref struct SpanRuneEnumerator
{
private ReadOnlySpan<char> _remaining;
private Rune _current;
internal SpanRuneEnumerator(ReadOnlySpan<char> buffer)
{
_remaining = buffer;
_current = default;
}
public Rune Current => _current;
public SpanRuneEnumerator GetEnumerator() => this;
public bool MoveNext()
{
if (_remaining.IsEmpty)
{
// reached the end of the buffer
_current = default;
return false;
}
int scalarValue = Rune.ReadFirstRuneFromUtf16Buffer(_remaining);
if (scalarValue < 0)
{
// replace invalid sequences with U+FFFD
scalarValue = Rune.ReplacementChar.Value;
}
// In UTF-16 specifically, invalid sequences always have length 1, which is the same
// length as the replacement character U+FFFD. This means that we can always bump the
// next index by the current scalar's UTF-16 sequence length. This optimization is not
// generally applicable; for example, enumerating scalars from UTF-8 cannot utilize
// this same trick.
_current = Rune.UnsafeCreate((uint)scalarValue);
_remaining = _remaining.Slice(_current.Utf16SequenceLength);
return true;
}
}
}