/
TextElementEnumerator.cs
92 lines (73 loc) · 3.11 KB
/
TextElementEnumerator.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections;
using System.Diagnostics;
using System.Text.Unicode;
namespace System.Globalization
{
public class TextElementEnumerator : IEnumerator
{
private readonly string _str;
private readonly int _strStartIndex; // where in _str the enumeration should begin
private int _currentTextElementOffset;
private int _currentTextElementLength;
private string? _currentTextElementSubstr;
internal TextElementEnumerator(string str, int startIndex)
{
Debug.Assert(str != null, "TextElementEnumerator(): str != null");
Debug.Assert(startIndex >= 0 && startIndex <= str.Length, "TextElementEnumerator(): startIndex >= 0 && startIndex <= str.Length");
_str = str;
_strStartIndex = startIndex;
Reset();
}
public bool MoveNext()
{
_currentTextElementSubstr = null; // clear any cached substr
int newOffset = _currentTextElementOffset + _currentTextElementLength;
_currentTextElementOffset = newOffset; // advance
_currentTextElementLength = 0; // prevent future calls to MoveNext() or get_Current from succeeding if we've hit end of data
if (newOffset >= _str.Length)
{
return false; // reached the end of the data
}
_currentTextElementLength = TextSegmentationUtility.GetLengthOfFirstUtf16ExtendedGraphemeCluster(_str.AsSpan(newOffset));
return true;
}
public object Current => GetTextElement();
public string GetTextElement()
{
// Returned the cached substr if we've already generated it.
// Otherwise perform the substr operation now.
string? currentSubstr = _currentTextElementSubstr;
if (currentSubstr is null)
{
if (_currentTextElementOffset >= _str.Length)
{
throw new InvalidOperationException(SR.InvalidOperation_EnumOpCantHappen);
}
currentSubstr = _str.Substring(_currentTextElementOffset, _currentTextElementLength);
_currentTextElementSubstr = currentSubstr;
}
return currentSubstr;
}
public int ElementIndex
{
get
{
if (_currentTextElementOffset >= _str.Length)
{
throw new InvalidOperationException(SR.InvalidOperation_EnumOpCantHappen);
}
return _currentTextElementOffset - _strStartIndex;
}
}
public void Reset()
{
// These first two fields are set to intentionally out-of-range values.
// They'll be fixed up once the enumerator starts.
_currentTextElementOffset = _str.Length;
_currentTextElementLength = _strStartIndex - _str.Length;
_currentTextElementSubstr = null;
}
}
}