| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Buffers; |
| | | 5 | | using System.Globalization; |
| | | 6 | | using System.Runtime.InteropServices; |
| | | 7 | | |
| | | 8 | | namespace System.Text.Unicode |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Provides helper utilities for computing text segmentation boundaries |
| | | 12 | | /// as specified in UAX#29 (https://www.unicode.org/reports/tr29/). |
| | | 13 | | /// </summary> |
| | | 14 | | /// <remarks> |
| | | 15 | | /// The current implementation is compliant per Rev. 35, https://www.unicode.org/reports/tr29/tr29-35.html. |
| | | 16 | | /// </remarks> |
| | | 17 | | internal static class TextSegmentationUtility |
| | | 18 | | { |
| | | 19 | | private delegate OperationStatus DecodeFirstRune<T>(ReadOnlySpan<T> input, out Rune rune, out int elementsConsum |
| | | 20 | | |
| | 0 | 21 | | private static readonly DecodeFirstRune<char> _utf16Decoder = Rune.DecodeFromUtf16; |
| | | 22 | | |
| | | 23 | | private static int GetLengthOfFirstExtendedGraphemeCluster<T>(ReadOnlySpan<T> input, DecodeFirstRune<T> decoder) |
| | | 24 | | { |
| | | 25 | | // Algorithm given at https://www.unicode.org/reports/tr29/#Grapheme_Cluster_Boundary_Rules. |
| | | 26 | | |
| | 0 | 27 | | Processor<T> processor = new Processor<T>(input, decoder); |
| | 0 | 28 | | processor.MoveNext(); |
| | | 29 | | |
| | | 30 | | // First, consume as many Prepend scalars as we can (rule GB9b). |
| | | 31 | | |
| | 0 | 32 | | while (processor.CurrentType == GraphemeClusterBreakType.Prepend) |
| | | 33 | | { |
| | 0 | 34 | | processor.MoveNext(); |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | // Next, make sure we're not about to violate control character restrictions. |
| | | 38 | | // Essentially, if we saw Prepend data, we can't have Control | CR | LF data afterward (rule GB5). |
| | | 39 | | |
| | 0 | 40 | | if (processor.CurrentCodeUnitOffset > 0) |
| | | 41 | | { |
| | 0 | 42 | | if (processor.CurrentType == GraphemeClusterBreakType.Control |
| | 0 | 43 | | || processor.CurrentType == GraphemeClusterBreakType.CR |
| | 0 | 44 | | || processor.CurrentType == GraphemeClusterBreakType.LF) |
| | | 45 | | { |
| | | 46 | | goto Return; |
| | | 47 | | } |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | // Now begin the main state machine. |
| | | 51 | | |
| | 0 | 52 | | GraphemeClusterBreakType previousClusterBreakType = processor.CurrentType; |
| | 0 | 53 | | processor.MoveNext(); |
| | | 54 | | |
| | | 55 | | switch (previousClusterBreakType) |
| | | 56 | | { |
| | | 57 | | case GraphemeClusterBreakType.CR: |
| | 0 | 58 | | if (processor.CurrentType != GraphemeClusterBreakType.LF) |
| | | 59 | | { |
| | | 60 | | goto Return; // rules GB3 & GB4 (only <LF> can follow <CR>) |
| | | 61 | | } |
| | | 62 | | |
| | 0 | 63 | | processor.MoveNext(); |
| | 0 | 64 | | goto case GraphemeClusterBreakType.LF; |
| | | 65 | | |
| | | 66 | | case GraphemeClusterBreakType.Control: |
| | | 67 | | case GraphemeClusterBreakType.LF: |
| | | 68 | | goto Return; // rule GB4 (no data after Control | LF) |
| | | 69 | | |
| | | 70 | | case GraphemeClusterBreakType.L: |
| | 0 | 71 | | if (processor.CurrentType == GraphemeClusterBreakType.L) |
| | | 72 | | { |
| | 0 | 73 | | processor.MoveNext(); // rule GB6 (L x L) |
| | 0 | 74 | | goto case GraphemeClusterBreakType.L; |
| | | 75 | | } |
| | 0 | 76 | | else if (processor.CurrentType == GraphemeClusterBreakType.V) |
| | | 77 | | { |
| | 0 | 78 | | processor.MoveNext(); // rule GB6 (L x V) |
| | 0 | 79 | | goto case GraphemeClusterBreakType.V; |
| | | 80 | | } |
| | 0 | 81 | | else if (processor.CurrentType == GraphemeClusterBreakType.LV) |
| | | 82 | | { |
| | 0 | 83 | | processor.MoveNext(); // rule GB6 (L x LV) |
| | 0 | 84 | | goto case GraphemeClusterBreakType.LV; |
| | | 85 | | } |
| | 0 | 86 | | else if (processor.CurrentType == GraphemeClusterBreakType.LVT) |
| | | 87 | | { |
| | 0 | 88 | | processor.MoveNext(); // rule GB6 (L x LVT) |
| | 0 | 89 | | goto case GraphemeClusterBreakType.LVT; |
| | | 90 | | } |
| | | 91 | | else |
| | | 92 | | { |
| | | 93 | | break; |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | case GraphemeClusterBreakType.LV: |
| | | 97 | | case GraphemeClusterBreakType.V: |
| | 0 | 98 | | if (processor.CurrentType == GraphemeClusterBreakType.V) |
| | | 99 | | { |
| | 0 | 100 | | processor.MoveNext(); // rule GB7 (LV | V x V) |
| | 0 | 101 | | goto case GraphemeClusterBreakType.V; |
| | | 102 | | } |
| | 0 | 103 | | else if (processor.CurrentType == GraphemeClusterBreakType.T) |
| | | 104 | | { |
| | 0 | 105 | | processor.MoveNext(); // rule GB7 (LV | V x T) |
| | | 106 | | goto case GraphemeClusterBreakType.T; |
| | | 107 | | } |
| | | 108 | | else |
| | | 109 | | { |
| | | 110 | | break; |
| | | 111 | | } |
| | | 112 | | |
| | | 113 | | case GraphemeClusterBreakType.LVT: |
| | | 114 | | case GraphemeClusterBreakType.T: |
| | 0 | 115 | | if (processor.CurrentType == GraphemeClusterBreakType.T) |
| | | 116 | | { |
| | 0 | 117 | | processor.MoveNext(); // rule GB8 (LVT | T x T) |
| | 0 | 118 | | goto case GraphemeClusterBreakType.T; |
| | | 119 | | } |
| | | 120 | | else |
| | | 121 | | { |
| | | 122 | | break; |
| | | 123 | | } |
| | | 124 | | |
| | | 125 | | case GraphemeClusterBreakType.Extended_Pictograph: |
| | | 126 | | // Attempt processing extended pictographic (rules GB11, GB9). |
| | | 127 | | // First, drain any Extend scalars that might exist |
| | | 128 | | |
| | 0 | 129 | | while (processor.CurrentType == GraphemeClusterBreakType.Extend) |
| | | 130 | | { |
| | 0 | 131 | | processor.MoveNext(); |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | // Now see if there's a ZWJ + extended pictograph again. |
| | | 135 | | |
| | 0 | 136 | | if (processor.CurrentType != GraphemeClusterBreakType.ZWJ) |
| | | 137 | | { |
| | | 138 | | break; |
| | | 139 | | } |
| | | 140 | | |
| | 0 | 141 | | processor.MoveNext(); |
| | 0 | 142 | | if (processor.CurrentType != GraphemeClusterBreakType.Extended_Pictograph) |
| | | 143 | | { |
| | | 144 | | break; |
| | | 145 | | } |
| | | 146 | | |
| | 0 | 147 | | processor.MoveNext(); |
| | 0 | 148 | | goto case GraphemeClusterBreakType.Extended_Pictograph; |
| | | 149 | | |
| | | 150 | | case GraphemeClusterBreakType.Regional_Indicator: |
| | | 151 | | // We've consumed a single RI scalar. Try to consume another (to make it a pair). |
| | | 152 | | |
| | 0 | 153 | | if (processor.CurrentType == GraphemeClusterBreakType.Regional_Indicator) |
| | | 154 | | { |
| | 0 | 155 | | processor.MoveNext(); |
| | | 156 | | } |
| | | 157 | | |
| | | 158 | | // Standalone RI scalars (or a single pair of RI scalars) can only be followed by trailers. |
| | | 159 | | |
| | 0 | 160 | | break; // nothing but trailers after the final RI |
| | | 161 | | |
| | | 162 | | default: |
| | | 163 | | break; |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | // rules GB9, GB9a |
| | 0 | 167 | | while (processor.CurrentType == GraphemeClusterBreakType.Extend |
| | 0 | 168 | | || processor.CurrentType == GraphemeClusterBreakType.ZWJ |
| | 0 | 169 | | || processor.CurrentType == GraphemeClusterBreakType.SpacingMark) |
| | | 170 | | { |
| | 0 | 171 | | processor.MoveNext(); |
| | | 172 | | } |
| | | 173 | | |
| | | 174 | | Return: |
| | | 175 | | |
| | 0 | 176 | | return processor.CurrentCodeUnitOffset; // rules GB2, GB999 |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | /// <summary> |
| | | 180 | | /// Given UTF-16 input text, returns the length (in chars) of the first extended grapheme cluster. |
| | | 181 | | /// The slice [0..length] represents the first standalone extended grapheme cluster in the text. |
| | | 182 | | /// If the input is empty, returns 0. |
| | | 183 | | /// </summary> |
| | | 184 | | public static int GetLengthOfFirstUtf16ExtendedGraphemeCluster(ReadOnlySpan<char> input) |
| | | 185 | | { |
| | 0 | 186 | | return GetLengthOfFirstExtendedGraphemeCluster(input, _utf16Decoder); |
| | | 187 | | } |
| | | 188 | | |
| | | 189 | | [StructLayout(LayoutKind.Auto)] |
| | | 190 | | private ref struct Processor<T> |
| | | 191 | | { |
| | | 192 | | private readonly ReadOnlySpan<T> _buffer; |
| | | 193 | | private readonly DecodeFirstRune<T> _decoder; |
| | | 194 | | private int _codeUnitLengthOfCurrentScalar; |
| | | 195 | | |
| | | 196 | | internal Processor(ReadOnlySpan<T> buffer, DecodeFirstRune<T> decoder) |
| | | 197 | | { |
| | 0 | 198 | | _buffer = buffer; |
| | 0 | 199 | | _decoder = decoder; |
| | 0 | 200 | | _codeUnitLengthOfCurrentScalar = 0; |
| | 0 | 201 | | CurrentType = GraphemeClusterBreakType.Other; |
| | 0 | 202 | | CurrentCodeUnitOffset = 0; |
| | 0 | 203 | | } |
| | | 204 | | |
| | 0 | 205 | | public int CurrentCodeUnitOffset { get; private set; } |
| | | 206 | | |
| | | 207 | | /// <summary> |
| | | 208 | | /// Will be <see cref="GraphemeClusterBreakType.Other"/> if invalid data or EOF reached. |
| | | 209 | | /// Caller shouldn't need to special-case this since the normal rules will halt on this condition. |
| | | 210 | | /// </summary> |
| | 0 | 211 | | public GraphemeClusterBreakType CurrentType { get; private set; } |
| | | 212 | | |
| | | 213 | | public void MoveNext() |
| | | 214 | | { |
| | | 215 | | // For ill-formed subsequences (like unpaired UTF-16 surrogate code points), we rely on |
| | | 216 | | // the decoder's default behavior of interpreting these ill-formed subsequences as |
| | | 217 | | // equivalent to U+FFFD REPLACEMENT CHARACTER. This code point has a boundary property |
| | | 218 | | // of Other (XX), which matches the modifications made to UAX#29, Rev. 35. |
| | | 219 | | // See: https://www.unicode.org/reports/tr29/tr29-35.html#Modifications |
| | | 220 | | // This change is also reflected in the UCD files. For example, Unicode 11.0's UCD file |
| | | 221 | | // https://www.unicode.org/Public/11.0.0/ucd/auxiliary/GraphemeBreakProperty.txt |
| | | 222 | | // has the line "D800..DFFF ; Control # Cs [2048] <surrogate-D800>..<surrogate-DFFF>", |
| | | 223 | | // but starting with Unicode 12.0 that line has been removed. |
| | | 224 | | // |
| | | 225 | | // If a later version of the Unicode Standard further modifies this guidance we should reflect |
| | | 226 | | // that here. |
| | | 227 | | |
| | 0 | 228 | | CurrentCodeUnitOffset += _codeUnitLengthOfCurrentScalar; |
| | 0 | 229 | | _decoder(_buffer.Slice(CurrentCodeUnitOffset), out Rune thisRune, out _codeUnitLengthOfCurrentScalar); |
| | 0 | 230 | | CurrentType = CharUnicodeInfo.GetGraphemeClusterBreakType(thisRune); |
| | 0 | 231 | | } |
| | | 232 | | } |
| | | 233 | | } |
| | | 234 | | } |
| | | 235 | | |