/
StringInfo.cs
230 lines (199 loc) · 10.2 KB
/
StringInfo.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
228
229
230
// 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.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text.Unicode;
namespace System.Globalization
{
/// <summary>
/// This class defines behaviors specific to a writing system.
/// A writing system is the collection of scripts and orthographic rules
/// required to represent a language as text.
/// </summary>
public class StringInfo
{
private string _str;
private int[]? _indexes;
public StringInfo() : this(string.Empty)
{
}
public StringInfo(string value)
{
this.String = value;
}
public override bool Equals([NotNullWhen(true)] object? value)
{
return value is StringInfo otherStringInfo
&& _str.Equals(otherStringInfo._str);
}
public override int GetHashCode() => _str.GetHashCode();
/// <summary>
/// Our zero-based array of index values into the string. Initialize if
/// our private array is not yet, in fact, initialized.
/// </summary>
private int[]? Indexes
{
get
{
if (_indexes == null && String.Length > 0)
{
_indexes = StringInfo.ParseCombiningCharacters(String);
}
return _indexes;
}
}
public string String
{
get => _str;
[MemberNotNull(nameof(_str))]
set
{
ArgumentNullException.ThrowIfNull(value);
_str = value;
_indexes = null;
}
}
public int LengthInTextElements => Indexes?.Length ?? 0;
public string SubstringByTextElements(int startingTextElement)
{
return SubstringByTextElements(startingTextElement, (Indexes?.Length ?? 0) - startingTextElement);
}
public string SubstringByTextElements(int startingTextElement, int lengthInTextElements)
{
int[] indexes = Indexes ?? Array.Empty<int>();
if ((uint)startingTextElement >= (uint)indexes.Length)
{
throw new ArgumentOutOfRangeException(nameof(startingTextElement), startingTextElement, SR.Arg_ArgumentOutOfRangeException);
}
if ((uint)lengthInTextElements > (uint)(indexes.Length - startingTextElement))
{
throw new ArgumentOutOfRangeException(nameof(lengthInTextElements), lengthInTextElements, SR.Arg_ArgumentOutOfRangeException);
}
int start = indexes[startingTextElement];
Index end = ^0; // assume reading to end of the string unless the caller told us to stop early
if ((uint)(startingTextElement + lengthInTextElements) < (uint)indexes.Length)
{
end = indexes[startingTextElement + lengthInTextElements];
}
return String[start..end];
}
/// <summary>
/// Returns the first text element (extended grapheme cluster) that occurs in the input string.
/// </summary>
/// <remarks>
/// A grapheme cluster is a sequence of one or more Unicode code points that should be treated as a single unit.
/// </remarks>
/// <param name="str">The input string to analyze.</param>
/// <returns>The substring corresponding to the first text element within <paramref name="str"/>,
/// or the empty string if <paramref name="str"/> is empty.</returns>
/// <exception cref="ArgumentNullException"><paramref name="str"/> is null.</exception>
public static string GetNextTextElement(string str) => GetNextTextElement(str, 0);
/// <summary>
/// Returns the first text element (extended grapheme cluster) that occurs in the input string
/// starting at the specified index.
/// </summary>
/// <remarks>
/// A grapheme cluster is a sequence of one or more Unicode code points that should be treated as a single unit.
/// </remarks>
/// <param name="str">The input string to analyze.</param>
/// <param name="index">The char offset in <paramref name="str"/> at which to begin analysis.</param>
/// <returns>The substring corresponding to the first text element within <paramref name="str"/> starting
/// at index <paramref name="index"/>, or the empty string if <paramref name="index"/> corresponds to
/// the end of <paramref name="str"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="str"/> is null.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is negative or beyond the end of <paramref name="str"/>.</exception>
public static string GetNextTextElement(string str, int index)
{
int nextTextElementLength = GetNextTextElementLength(str, index);
return str.Substring(index, nextTextElementLength);
}
/// <summary>
/// Returns the length of the first text element (extended grapheme cluster) that occurs in the input string.
/// </summary>
/// <remarks>
/// A grapheme cluster is a sequence of one or more Unicode code points that should be treated as a single unit.
/// </remarks>
/// <param name="str">The input string to analyze.</param>
/// <returns>The length (in chars) of the substring corresponding to the first text element within <paramref name="str"/>,
/// or 0 if <paramref name="str"/> is empty.</returns>
/// <exception cref="ArgumentNullException"><paramref name="str"/> is null.</exception>
public static int GetNextTextElementLength(string str) => GetNextTextElementLength(str, 0);
/// <summary>
/// Returns the length of the first text element (extended grapheme cluster) that occurs in the input string
/// starting at the specified index.
/// </summary>
/// <remarks>
/// A grapheme cluster is a sequence of one or more Unicode code points that should be treated as a single unit.
/// </remarks>
/// <param name="str">The input string to analyze.</param>
/// <param name="index">The char offset in <paramref name="str"/> at which to begin analysis.</param>
/// <returns>The length (in chars) of the substring corresponding to the first text element within <paramref name="str"/> starting
/// at index <paramref name="index"/>, or 0 if <paramref name="index"/> corresponds to the end of <paramref name="str"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="str"/> is null.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is negative or beyond the end of <paramref name="str"/>.</exception>
public static int GetNextTextElementLength(string str, int index)
{
if (str is null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.str);
}
if ((uint)index > (uint)str.Length)
{
ThrowHelper.ThrowArgumentOutOfRange_IndexMustBeLessOrEqualException();
}
return GetNextTextElementLength(str.AsSpan(index));
}
/// <summary>
/// Returns the length of the first text element (extended grapheme cluster) that occurs in the input span.
/// </summary>
/// <remarks>
/// A grapheme cluster is a sequence of one or more Unicode code points that should be treated as a single unit.
/// </remarks>
/// <param name="str">The input span to analyze.</param>
/// <returns>The length (in chars) of the substring corresponding to the first text element within <paramref name="str"/>,
/// or 0 if <paramref name="str"/> is empty.</returns>
public static int GetNextTextElementLength(ReadOnlySpan<char> str) => TextSegmentationUtility.GetLengthOfFirstUtf16ExtendedGraphemeCluster(str);
public static TextElementEnumerator GetTextElementEnumerator(string str) => GetTextElementEnumerator(str, 0);
public static TextElementEnumerator GetTextElementEnumerator(string str, int index)
{
if (str is null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.str);
}
if ((uint)index > (uint)str.Length)
{
ThrowHelper.ThrowArgumentOutOfRange_IndexMustBeLessOrEqualException();
}
return new TextElementEnumerator(str, index);
}
/// <summary>
/// Returns the indices of each base character or properly formed surrogate
/// pair within the str. It recognizes a base character plus one or more
/// combining characters or a properly formed surrogate pair as a text
/// element and returns the index of the base character or high surrogate.
/// Each index is the beginning of a text element within a str. The length
/// of each element is easily computed as the difference between successive
/// indices. The length of the array will always be less than or equal to
/// the length of the str. For example, given the str
/// \u4f00\u302a\ud800\udc00\u4f01, this method would return the indices:
/// 0, 2, 4.
/// </summary>
public static int[] ParseCombiningCharacters(string str)
{
if (str is null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.str);
}
ValueListBuilder<int> builder = new ValueListBuilder<int>(stackalloc int[64]); // 64 arbitrarily chosen
ReadOnlySpan<char> remaining = str;
while (!remaining.IsEmpty)
{
builder.Append(str.Length - remaining.Length); // a new extended grapheme cluster begins at this offset
remaining = remaining.Slice(GetNextTextElementLength(remaining)); // consume this cluster
}
int[] retVal = builder.AsSpan().ToArray();
builder.Dispose();
return retVal;
}
}
}