/
SpanGraphemeEnumerator.cs
227 lines (194 loc) · 8.08 KB
/
SpanGraphemeEnumerator.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
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
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
namespace SixLabors.Fonts.Unicode;
/// <summary>
/// An enumerator for retrieving Grapheme instances from a <see cref="ReadOnlySpan{Char}"/>.
/// <br/>
/// Implements the Unicode Grapheme Cluster Algorithm. UAX:29
/// <see href="https://www.unicode.org/reports/tr29/tr29-37.html"/>
/// <br/>
/// Methods are pattern-matched by compiler to allow using foreach pattern.
/// </summary>
public ref struct SpanGraphemeEnumerator
{
private ReadOnlySpan<char> source;
/// <summary>
/// Initializes a new instance of the <see cref="SpanGraphemeEnumerator"/> struct.
/// </summary>
/// <param name="source">The buffer to read from.</param>
public SpanGraphemeEnumerator(ReadOnlySpan<char> source)
{
this.source = source;
this.Current = default;
}
/// <summary>
/// Gets the element in the collection at the current position of the enumerator.
/// </summary>
public ReadOnlySpan<char> Current { get; private set; }
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>An enumerator that iterates through the collection.</returns>
public readonly SpanGraphemeEnumerator GetEnumerator() => this;
/// <summary>
/// Advances the enumerator to the next element of the collection.
/// </summary>
/// <returns>
/// <see langword="true"/> if the enumerator was successfully advanced to the next element;
/// <see langword="false"/> if the enumerator has passed the end of the collection.
/// </returns>
public bool MoveNext()
{
if (this.source.IsEmpty)
{
return false;
}
// Algorithm given at https://www.unicode.org/reports/tr29/#Grapheme_Cluster_Boundary_Rules.
var processor = new Processor(this.source);
processor.MoveNext();
// First, consume as many Prepend scalars as we can (rule GB9b).
while (processor.CurrentType == GraphemeClusterClass.Prepend)
{
processor.MoveNext();
}
// Next, make sure we're not about to violate control character restrictions.
// Essentially, if we saw Prepend data, we can't have Control | CR | LF data afterward (rule GB5).
if (processor.CharsConsumed > 0)
{
if (processor.CurrentType is GraphemeClusterClass.Control
or GraphemeClusterClass.CarriageReturn
or GraphemeClusterClass.LineFeed)
{
goto Return;
}
}
// Now begin the main state machine.
GraphemeClusterClass previousClusterBreakType = processor.CurrentType;
processor.MoveNext();
switch (previousClusterBreakType)
{
case GraphemeClusterClass.CarriageReturn:
if (processor.CurrentType != GraphemeClusterClass.LineFeed)
{
goto Return; // rules GB3 & GB4 (only <LF> can follow <CR>)
}
processor.MoveNext();
goto case GraphemeClusterClass.LineFeed;
case GraphemeClusterClass.Control:
case GraphemeClusterClass.LineFeed:
goto Return; // rule GB4 (no data after Control | LF)
case GraphemeClusterClass.HangulLead:
if (processor.CurrentType == GraphemeClusterClass.HangulLead)
{
processor.MoveNext(); // rule GB6 (L x L)
goto case GraphemeClusterClass.HangulLead;
}
else if (processor.CurrentType == GraphemeClusterClass.HangulVowel)
{
processor.MoveNext(); // rule GB6 (L x V)
goto case GraphemeClusterClass.HangulVowel;
}
else if (processor.CurrentType == GraphemeClusterClass.HangulLeadVowel)
{
processor.MoveNext(); // rule GB6 (L x LV)
goto case GraphemeClusterClass.HangulLeadVowel;
}
else if (processor.CurrentType == GraphemeClusterClass.HangulLeadVowelTail)
{
processor.MoveNext(); // rule GB6 (L x LVT)
goto case GraphemeClusterClass.HangulLeadVowelTail;
}
else
{
break;
}
case GraphemeClusterClass.HangulLeadVowel:
case GraphemeClusterClass.HangulVowel:
if (processor.CurrentType == GraphemeClusterClass.HangulVowel)
{
processor.MoveNext(); // rule GB7 (LV | V x V)
goto case GraphemeClusterClass.HangulVowel;
}
else if (processor.CurrentType == GraphemeClusterClass.HangulTail)
{
processor.MoveNext(); // rule GB7 (LV | V x T)
goto case GraphemeClusterClass.HangulTail;
}
else
{
break;
}
case GraphemeClusterClass.HangulLeadVowelTail:
case GraphemeClusterClass.HangulTail:
if (processor.CurrentType == GraphemeClusterClass.HangulTail)
{
processor.MoveNext(); // rule GB8 (LVT | T x T)
goto case GraphemeClusterClass.HangulTail;
}
else
{
break;
}
case GraphemeClusterClass.ExtendedPictographic:
// Attempt processing extended pictographic (rules GB11, GB9).
// First, drain any Extend scalars that might exist
while (processor.CurrentType == GraphemeClusterClass.Extend)
{
processor.MoveNext();
}
// Now see if there's a ZWJ + extended pictograph again.
if (processor.CurrentType != GraphemeClusterClass.ZeroWidthJoiner)
{
break;
}
processor.MoveNext();
if (processor.CurrentType != GraphemeClusterClass.ExtendedPictographic)
{
break;
}
processor.MoveNext();
goto case GraphemeClusterClass.ExtendedPictographic;
case GraphemeClusterClass.RegionalIndicator:
// We've consumed a single RI scalar. Try to consume another (to make it a pair).
if (processor.CurrentType == GraphemeClusterClass.RegionalIndicator)
{
processor.MoveNext();
}
// Standalone RI scalars (or a single pair of RI scalars) can only be followed by trailers.
break; // nothing but trailers after the final RI
default:
break;
}
// rules GB9, GB9a
while (processor.CurrentType is GraphemeClusterClass.Extend
or GraphemeClusterClass.ZeroWidthJoiner
or GraphemeClusterClass.SpacingMark)
{
processor.MoveNext();
}
Return:
this.Current = this.source.Slice(0, processor.CharsConsumed);
this.source = this.source.Slice(processor.CharsConsumed);
return true; // rules GB2, GB999
}
private ref struct Processor
{
private readonly ReadOnlySpan<char> source;
private int charsConsumed;
public Processor(ReadOnlySpan<char> source)
{
this.source = source;
this.CurrentType = GraphemeClusterClass.Any;
this.charsConsumed = 0;
this.CharsConsumed = 0;
}
public GraphemeClusterClass CurrentType { get; private set; }
public int CharsConsumed { get; private set; }
public void MoveNext()
{
this.CharsConsumed += this.charsConsumed;
var codePoint = CodePoint.DecodeFromUtf16At(this.source, this.CharsConsumed, out this.charsConsumed);
this.CurrentType = CodePoint.GetGraphemeClusterClass(codePoint);
}
}
}