| | | 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.Diagnostics; |
| | | 5 | | using System.Runtime.CompilerServices; |
| | | 6 | | using System.Runtime.InteropServices; |
| | | 7 | | using System.Text; |
| | | 8 | | |
| | | 9 | | namespace System.Globalization |
| | | 10 | | { |
| | | 11 | | internal static class InvariantModeCasing |
| | | 12 | | { |
| | | 13 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 0 | 14 | | internal static char ToLower(char c) => CharUnicodeInfo.ToLower(c); |
| | | 15 | | |
| | | 16 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 0 | 17 | | internal static char ToUpper(char c) => CharUnicodeInfo.ToUpper(c); |
| | | 18 | | |
| | | 19 | | internal static string ToLower(string s) |
| | | 20 | | { |
| | 0 | 21 | | if (s.Length == 0) |
| | | 22 | | { |
| | 0 | 23 | | return string.Empty; |
| | | 24 | | } |
| | | 25 | | |
| | 0 | 26 | | ReadOnlySpan<char> source = s; |
| | | 27 | | |
| | 0 | 28 | | int i = 0; |
| | 0 | 29 | | while (i < s.Length) |
| | | 30 | | { |
| | 0 | 31 | | if (char.IsHighSurrogate(source[i]) && i < s.Length - 1 && char.IsLowSurrogate(source[i + 1])) |
| | | 32 | | { |
| | 0 | 33 | | SurrogateCasing.ToLower(source[i], source[i + 1], out char h, out char l); |
| | 0 | 34 | | if (source[i] != h || source[i + 1] != l) |
| | | 35 | | { |
| | | 36 | | break; |
| | | 37 | | } |
| | | 38 | | |
| | 0 | 39 | | i += 2; |
| | 0 | 40 | | continue; |
| | | 41 | | } |
| | | 42 | | |
| | 0 | 43 | | if (ToLower(source[i]) != source[i]) |
| | | 44 | | { |
| | | 45 | | break; |
| | | 46 | | } |
| | | 47 | | |
| | 0 | 48 | | i++; |
| | | 49 | | } |
| | | 50 | | |
| | 0 | 51 | | if (i >= s.Length) |
| | | 52 | | { |
| | 0 | 53 | | return s; |
| | | 54 | | } |
| | | 55 | | |
| | 0 | 56 | | string result = string.FastAllocateString(s.Length); |
| | 0 | 57 | | var destination = new Span<char>(ref result.GetRawStringData(), result.Length); |
| | 0 | 58 | | ReadOnlySpan<char> src = s; |
| | 0 | 59 | | src.Slice(0, i).CopyTo(destination); |
| | 0 | 60 | | ToLower(src.Slice(i), destination.Slice(i)); |
| | | 61 | | |
| | 0 | 62 | | return result; |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | internal static string ToUpper(string s) |
| | | 66 | | { |
| | 0 | 67 | | if (s.Length == 0) |
| | | 68 | | { |
| | 0 | 69 | | return string.Empty; |
| | | 70 | | } |
| | | 71 | | |
| | 0 | 72 | | ReadOnlySpan<char> source = s; |
| | | 73 | | |
| | 0 | 74 | | int i = 0; |
| | 0 | 75 | | while (i < s.Length) |
| | | 76 | | { |
| | 0 | 77 | | if (char.IsHighSurrogate(source[i]) && i < s.Length - 1 && char.IsLowSurrogate(source[i + 1])) |
| | | 78 | | { |
| | 0 | 79 | | SurrogateCasing.ToUpper(source[i], source[i + 1], out char h, out char l); |
| | 0 | 80 | | if (source[i] != h || source[i + 1] != l) |
| | | 81 | | { |
| | | 82 | | break; |
| | | 83 | | } |
| | | 84 | | |
| | 0 | 85 | | i += 2; |
| | 0 | 86 | | continue; |
| | | 87 | | } |
| | | 88 | | |
| | 0 | 89 | | if (ToUpper(source[i]) != source[i]) |
| | | 90 | | { |
| | | 91 | | break; |
| | | 92 | | } |
| | | 93 | | |
| | 0 | 94 | | i++; |
| | | 95 | | } |
| | | 96 | | |
| | 0 | 97 | | if (i >= s.Length) |
| | | 98 | | { |
| | 0 | 99 | | return s; |
| | | 100 | | } |
| | | 101 | | |
| | 0 | 102 | | string result = string.FastAllocateString(s.Length); |
| | 0 | 103 | | var destination = new Span<char>(ref result.GetRawStringData(), result.Length); |
| | 0 | 104 | | ReadOnlySpan<char> src = s; |
| | 0 | 105 | | src.Slice(0, i).CopyTo(destination); |
| | 0 | 106 | | ToUpper(src.Slice(i), destination.Slice(i)); |
| | | 107 | | |
| | 0 | 108 | | return result; |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | internal static void ToUpper(ReadOnlySpan<char> source, Span<char> destination) |
| | | 112 | | { |
| | 0 | 113 | | Debug.Assert(GlobalizationMode.Invariant); |
| | 0 | 114 | | Debug.Assert(source.Length <= destination.Length); |
| | | 115 | | |
| | 0 | 116 | | for (int i = 0; i < source.Length; i++) |
| | | 117 | | { |
| | 0 | 118 | | char c = source[i]; |
| | 0 | 119 | | if (char.IsHighSurrogate(c) && i < source.Length - 1) |
| | | 120 | | { |
| | 0 | 121 | | char cl = source[i + 1]; |
| | 0 | 122 | | if (char.IsLowSurrogate(cl)) |
| | | 123 | | { |
| | | 124 | | // well formed surrogates |
| | 0 | 125 | | SurrogateCasing.ToUpper(c, cl, out char h, out char l); |
| | 0 | 126 | | destination[i] = h; |
| | 0 | 127 | | destination[i + 1] = l; |
| | 0 | 128 | | i++; // skip the low surrogate |
| | 0 | 129 | | continue; |
| | | 130 | | } |
| | | 131 | | } |
| | | 132 | | |
| | 0 | 133 | | destination[i] = ToUpper(c); |
| | | 134 | | } |
| | 0 | 135 | | } |
| | | 136 | | |
| | | 137 | | internal static void ToLower(ReadOnlySpan<char> source, Span<char> destination) |
| | | 138 | | { |
| | 0 | 139 | | Debug.Assert(GlobalizationMode.Invariant); |
| | 0 | 140 | | Debug.Assert(source.Length <= destination.Length); |
| | | 141 | | |
| | 0 | 142 | | for (int i = 0; i < source.Length; i++) |
| | | 143 | | { |
| | 0 | 144 | | char c = source[i]; |
| | 0 | 145 | | if (char.IsHighSurrogate(c) && i < source.Length - 1) |
| | | 146 | | { |
| | 0 | 147 | | char cl = source[i + 1]; |
| | 0 | 148 | | if (char.IsLowSurrogate(cl)) |
| | | 149 | | { |
| | | 150 | | // well formed surrogates |
| | 0 | 151 | | SurrogateCasing.ToLower(c, cl, out char h, out char l); |
| | 0 | 152 | | destination[i] = h; |
| | 0 | 153 | | destination[i + 1] = l; |
| | 0 | 154 | | i++; // skip the low surrogate |
| | 0 | 155 | | continue; |
| | | 156 | | } |
| | | 157 | | } |
| | | 158 | | |
| | 0 | 159 | | destination[i] = ToLower(c); |
| | | 160 | | } |
| | 0 | 161 | | } |
| | | 162 | | |
| | | 163 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 164 | | private static (uint, int) GetScalar(ref char source, int index, int length) |
| | | 165 | | { |
| | 0 | 166 | | char charA = source; |
| | 0 | 167 | | if (!char.IsHighSurrogate(charA) || index >= length - 1) |
| | | 168 | | { |
| | 0 | 169 | | return ((uint)charA, 1); |
| | | 170 | | } |
| | | 171 | | |
| | 0 | 172 | | char charB = Unsafe.Add(ref source, 1); |
| | 0 | 173 | | if (!char.IsLowSurrogate(charB)) |
| | | 174 | | { |
| | 0 | 175 | | return ((uint)charA, 1); |
| | | 176 | | } |
| | | 177 | | |
| | 0 | 178 | | return (UnicodeUtility.GetScalarFromUtf16SurrogatePair(charA, charB), 2); |
| | | 179 | | } |
| | | 180 | | |
| | | 181 | | internal static int CompareStringIgnoreCase(ref char strA, int lengthA, ref char strB, int lengthB) |
| | | 182 | | { |
| | 0 | 183 | | Debug.Assert(GlobalizationMode.Invariant); |
| | | 184 | | |
| | 0 | 185 | | int length = Math.Min(lengthA, lengthB); |
| | | 186 | | |
| | 0 | 187 | | ref char charA = ref strA; |
| | 0 | 188 | | ref char charB = ref strB; |
| | | 189 | | |
| | 0 | 190 | | int index = 0; |
| | | 191 | | |
| | 0 | 192 | | while (index < length) |
| | | 193 | | { |
| | 0 | 194 | | (uint codePointA, int codePointLengthA) = GetScalar(ref charA, index, lengthA); |
| | 0 | 195 | | (uint codePointB, int codePointLengthB) = GetScalar(ref charB, index, lengthB); |
| | | 196 | | |
| | 0 | 197 | | if (codePointA == codePointB) |
| | | 198 | | { |
| | 0 | 199 | | Debug.Assert(codePointLengthA == codePointLengthB); |
| | 0 | 200 | | index += codePointLengthA; |
| | 0 | 201 | | charA = ref Unsafe.Add(ref charA, codePointLengthA); |
| | 0 | 202 | | charB = ref Unsafe.Add(ref charB, codePointLengthB); |
| | 0 | 203 | | continue; |
| | | 204 | | } |
| | | 205 | | |
| | 0 | 206 | | uint aUpper = CharUnicodeInfo.ToUpper(codePointA); |
| | 0 | 207 | | uint bUpper = CharUnicodeInfo.ToUpper(codePointB); |
| | | 208 | | |
| | 0 | 209 | | if (aUpper == bUpper) |
| | | 210 | | { |
| | 0 | 211 | | Debug.Assert(codePointLengthA == codePointLengthB); |
| | 0 | 212 | | index += codePointLengthA; |
| | 0 | 213 | | charA = ref Unsafe.Add(ref charA, codePointLengthA); |
| | 0 | 214 | | charB = ref Unsafe.Add(ref charB, codePointLengthB); |
| | 0 | 215 | | continue; |
| | | 216 | | } |
| | | 217 | | |
| | 0 | 218 | | return (int)aUpper - (int)bUpper; |
| | | 219 | | } |
| | | 220 | | |
| | 0 | 221 | | return lengthA - lengthB; |
| | | 222 | | } |
| | | 223 | | |
| | | 224 | | internal static unsafe int IndexOfIgnoreCase(ReadOnlySpan<char> source, ReadOnlySpan<char> value) |
| | | 225 | | { |
| | 0 | 226 | | Debug.Assert(value.Length > 0); |
| | 0 | 227 | | Debug.Assert(value.Length <= source.Length); |
| | 0 | 228 | | Debug.Assert(GlobalizationMode.Invariant); |
| | | 229 | | |
| | 0 | 230 | | fixed (char* pSource = &MemoryMarshal.GetReference(source)) |
| | 0 | 231 | | fixed (char* pValue = &MemoryMarshal.GetReference(value)) |
| | | 232 | | { |
| | 0 | 233 | | char* pSourceLimit = pSource + (source.Length - value.Length); |
| | 0 | 234 | | char* pValueLimit = pValue + value.Length - 1; |
| | 0 | 235 | | char* pCurrentSource = pSource; |
| | | 236 | | |
| | 0 | 237 | | while (pCurrentSource <= pSourceLimit) |
| | | 238 | | { |
| | 0 | 239 | | char *pVal = pValue; |
| | 0 | 240 | | char *pSrc = pCurrentSource; |
| | | 241 | | |
| | 0 | 242 | | while (pVal <= pValueLimit) |
| | | 243 | | { |
| | 0 | 244 | | if (!char.IsHighSurrogate(*pVal) || pVal == pValueLimit) |
| | | 245 | | { |
| | 0 | 246 | | if (*pVal != *pSrc && ToUpper(*pVal) != ToUpper(*pSrc)) |
| | | 247 | | break; // no match |
| | | 248 | | |
| | 0 | 249 | | pVal++; |
| | 0 | 250 | | pSrc++; |
| | 0 | 251 | | continue; |
| | | 252 | | } |
| | | 253 | | |
| | 0 | 254 | | if (char.IsHighSurrogate(*pSrc) && char.IsLowSurrogate(*(pSrc + 1)) && char.IsLowSurrogate(*(pVa |
| | | 255 | | { |
| | | 256 | | // Well formed surrogates |
| | | 257 | | // both the source and the Value have well-formed surrogates. |
| | 0 | 258 | | if (!SurrogateCasing.Equal(*pSrc, *(pSrc + 1), *pVal, *(pVal + 1))) |
| | | 259 | | break; // no match |
| | | 260 | | |
| | 0 | 261 | | pSrc += 2; |
| | 0 | 262 | | pVal += 2; |
| | 0 | 263 | | continue; |
| | | 264 | | } |
| | | 265 | | |
| | 0 | 266 | | if (*pVal != *pSrc) |
| | | 267 | | break; // no match |
| | | 268 | | |
| | 0 | 269 | | pSrc++; |
| | 0 | 270 | | pVal++; |
| | | 271 | | } |
| | | 272 | | |
| | 0 | 273 | | if (pVal > pValueLimit) |
| | | 274 | | { |
| | | 275 | | // Found match. |
| | 0 | 276 | | return (int)(pCurrentSource - pSource); |
| | | 277 | | } |
| | | 278 | | |
| | 0 | 279 | | pCurrentSource++; |
| | | 280 | | } |
| | | 281 | | |
| | 0 | 282 | | return -1; |
| | | 283 | | } |
| | | 284 | | } |
| | | 285 | | |
| | | 286 | | internal static unsafe int LastIndexOfIgnoreCase(ReadOnlySpan<char> source, ReadOnlySpan<char> value) |
| | | 287 | | { |
| | 0 | 288 | | Debug.Assert(value.Length > 0); |
| | 0 | 289 | | Debug.Assert(value.Length <= source.Length); |
| | 0 | 290 | | Debug.Assert(GlobalizationMode.Invariant); |
| | | 291 | | |
| | 0 | 292 | | fixed (char* pSource = &MemoryMarshal.GetReference(source)) |
| | 0 | 293 | | fixed (char* pValue = &MemoryMarshal.GetReference(value)) |
| | | 294 | | { |
| | 0 | 295 | | char* pValueLimit = pValue + value.Length - 1; |
| | 0 | 296 | | char* pCurrentSource = pSource + (source.Length - value.Length); |
| | | 297 | | |
| | 0 | 298 | | while (pCurrentSource >= pSource) |
| | | 299 | | { |
| | 0 | 300 | | char *pVal = pValue; |
| | 0 | 301 | | char *pSrc = pCurrentSource; |
| | | 302 | | |
| | 0 | 303 | | while (pVal <= pValueLimit) |
| | | 304 | | { |
| | 0 | 305 | | if (!char.IsHighSurrogate(*pVal) || pVal == pValueLimit) |
| | | 306 | | { |
| | 0 | 307 | | if (*pVal != *pSrc && ToUpper(*pVal) != ToUpper(*pSrc)) |
| | | 308 | | break; // no match |
| | | 309 | | |
| | 0 | 310 | | pVal++; |
| | 0 | 311 | | pSrc++; |
| | 0 | 312 | | continue; |
| | | 313 | | } |
| | | 314 | | |
| | 0 | 315 | | if (char.IsHighSurrogate(*pSrc) && char.IsLowSurrogate(*(pSrc + 1)) && char.IsLowSurrogate(*(pVa |
| | | 316 | | { |
| | | 317 | | // Well formed surrogates |
| | | 318 | | // both the source and the Value have well-formed surrogates. |
| | 0 | 319 | | if (!SurrogateCasing.Equal(*pSrc, *(pSrc + 1), *pVal, *(pVal + 1))) |
| | | 320 | | break; // no match |
| | | 321 | | |
| | 0 | 322 | | pSrc += 2; |
| | 0 | 323 | | pVal += 2; |
| | 0 | 324 | | continue; |
| | | 325 | | } |
| | | 326 | | |
| | 0 | 327 | | if (*pVal != *pSrc) |
| | | 328 | | break; // no match |
| | | 329 | | |
| | 0 | 330 | | pSrc++; |
| | 0 | 331 | | pVal++; |
| | | 332 | | } |
| | | 333 | | |
| | 0 | 334 | | if (pVal > pValueLimit) |
| | | 335 | | { |
| | | 336 | | // Found match. |
| | 0 | 337 | | return (int)(pCurrentSource - pSource); |
| | | 338 | | } |
| | | 339 | | |
| | 0 | 340 | | pCurrentSource--; |
| | | 341 | | } |
| | | 342 | | |
| | 0 | 343 | | return -1; |
| | | 344 | | } |
| | | 345 | | } |
| | | 346 | | } |
| | | 347 | | } |
| | | 348 | | |