| | | 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.Diagnostics.CodeAnalysis; |
| | | 6 | | using System.Reflection; |
| | | 7 | | using System.Runtime.CompilerServices; |
| | | 8 | | using System.Runtime.InteropServices; |
| | | 9 | | using System.Runtime.Serialization; |
| | | 10 | | using System.Text; |
| | | 11 | | |
| | | 12 | | namespace System.Globalization |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// This class implements a set of methods for comparing strings. |
| | | 16 | | /// </summary> |
| | | 17 | | [Serializable] |
| | | 18 | | [TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] |
| | | 19 | | public sealed partial class CompareInfo : IDeserializationCallback |
| | | 20 | | { |
| | | 21 | | // Mask used to check if IndexOf()/LastIndexOf()/IsPrefix()/IsPostfix() has the right flags. |
| | | 22 | | private const CompareOptions ValidIndexMaskOffFlags = |
| | | 23 | | ~(CompareOptions.IgnoreCase | CompareOptions.IgnoreSymbols | CompareOptions.IgnoreNonSpace | |
| | | 24 | | CompareOptions.IgnoreWidth | CompareOptions.IgnoreKanaType); |
| | | 25 | | |
| | | 26 | | // Mask used to check if Compare() / GetHashCode(string) / GetSortKey has the right flags. |
| | | 27 | | private const CompareOptions ValidCompareMaskOffFlags = |
| | | 28 | | ~(CompareOptions.IgnoreCase | CompareOptions.IgnoreSymbols | CompareOptions.IgnoreNonSpace | |
| | | 29 | | CompareOptions.IgnoreWidth | CompareOptions.IgnoreKanaType | CompareOptions.StringSort | |
| | | 30 | | CompareOptions.NumericOrdering); |
| | | 31 | | |
| | | 32 | | // Cache the invariant CompareInfo |
| | 0 | 33 | | internal static readonly CompareInfo Invariant = CultureInfo.InvariantCulture.CompareInfo; |
| | | 34 | | |
| | | 35 | | // CompareInfos have an interesting identity. They are attached to the locale that created them, |
| | | 36 | | // ie: en-US would have an en-US sort. For haw-US (custom), then we serialize it as haw-US. |
| | | 37 | | // The interesting part is that since haw-US doesn't have its own sort, it has to point at another |
| | | 38 | | // locale, which is what SCOMPAREINFO does. |
| | | 39 | | [OptionalField(VersionAdded = 2)] |
| | | 40 | | private string m_name; // The name used to construct this CompareInfo. Do not rename (binary serialization) |
| | | 41 | | |
| | | 42 | | [NonSerialized] |
| | | 43 | | private IntPtr _sortHandle; |
| | | 44 | | |
| | | 45 | | [NonSerialized] |
| | | 46 | | private string _sortName; // The name that defines our behavior |
| | | 47 | | |
| | | 48 | | [OptionalField(VersionAdded = 3)] |
| | | 49 | | private SortVersion? m_SortVersion; // Do not rename (binary serialization) |
| | | 50 | | |
| | | 51 | | private int culture; // Do not rename (binary serialization). The fields sole purpose is to support Desktop seri |
| | | 52 | | |
| | 0 | 53 | | internal CompareInfo(CultureInfo culture) |
| | | 54 | | { |
| | 0 | 55 | | m_name = culture._name; |
| | 0 | 56 | | InitSort(culture); |
| | 0 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Get the CompareInfo constructed from the data table in the specified |
| | | 61 | | /// assembly for the specified culture. |
| | | 62 | | /// Warning: The assembly versioning mechanism is dead! |
| | | 63 | | /// </summary> |
| | | 64 | | public static CompareInfo GetCompareInfo(int culture, Assembly assembly) |
| | | 65 | | { |
| | 0 | 66 | | ArgumentNullException.ThrowIfNull(assembly); |
| | | 67 | | |
| | | 68 | | // Parameter checking. |
| | 0 | 69 | | if (assembly != typeof(object).Module.Assembly) |
| | | 70 | | { |
| | 0 | 71 | | throw new ArgumentException(SR.Argument_OnlyMscorlib, nameof(assembly)); |
| | | 72 | | } |
| | | 73 | | |
| | 0 | 74 | | return GetCompareInfo(culture); |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Get the CompareInfo constructed from the data table in the specified |
| | | 79 | | /// assembly for the specified culture. |
| | | 80 | | /// The purpose of this method is to provide version for CompareInfo tables. |
| | | 81 | | /// </summary> |
| | | 82 | | public static CompareInfo GetCompareInfo(string name, Assembly assembly) |
| | | 83 | | { |
| | 0 | 84 | | ArgumentNullException.ThrowIfNull(name); |
| | 0 | 85 | | ArgumentNullException.ThrowIfNull(assembly); |
| | | 86 | | |
| | 0 | 87 | | if (assembly != typeof(object).Module.Assembly) |
| | | 88 | | { |
| | 0 | 89 | | throw new ArgumentException(SR.Argument_OnlyMscorlib, nameof(assembly)); |
| | | 90 | | } |
| | | 91 | | |
| | 0 | 92 | | return GetCompareInfo(name); |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Get the CompareInfo for the specified culture. |
| | | 97 | | /// This method is provided for ease of integration with NLS-based software. |
| | | 98 | | /// </summary> |
| | | 99 | | public static CompareInfo GetCompareInfo(int culture) |
| | | 100 | | { |
| | 0 | 101 | | if (CultureData.IsCustomCultureId(culture)) |
| | | 102 | | { |
| | 0 | 103 | | throw new ArgumentException(SR.Argument_CustomCultureCannotBePassedByNumber, nameof(culture)); |
| | | 104 | | } |
| | | 105 | | |
| | 0 | 106 | | return CultureInfo.GetCultureInfo(culture).CompareInfo; |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Get the CompareInfo for the specified culture. |
| | | 111 | | /// </summary> |
| | | 112 | | public static CompareInfo GetCompareInfo(string name) |
| | | 113 | | { |
| | 0 | 114 | | ArgumentNullException.ThrowIfNull(name); |
| | | 115 | | |
| | 0 | 116 | | return CultureInfo.GetCultureInfo(name).CompareInfo; |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | public static bool IsSortable(char ch) |
| | | 120 | | { |
| | 0 | 121 | | return IsSortable(new ReadOnlySpan<char>(in ch)); |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | public static bool IsSortable(string text) |
| | | 125 | | { |
| | 0 | 126 | | ArgumentNullException.ThrowIfNull(text); |
| | | 127 | | |
| | 0 | 128 | | return IsSortable(text.AsSpan()); |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | /// <summary> |
| | | 132 | | /// Indicates whether a specified Unicode string is sortable. |
| | | 133 | | /// </summary> |
| | | 134 | | /// <param name="text">A string of zero or more Unicode characters.</param> |
| | | 135 | | /// <returns> |
| | | 136 | | /// <see langword="true"/> if <paramref name="text"/> is non-empty and contains |
| | | 137 | | /// only sortable Unicode characters; otherwise, <see langword="false"/>. |
| | | 138 | | /// </returns> |
| | | 139 | | public static bool IsSortable(ReadOnlySpan<char> text) |
| | | 140 | | { |
| | 0 | 141 | | if (text.Length == 0) |
| | | 142 | | { |
| | 0 | 143 | | return false; |
| | | 144 | | } |
| | | 145 | | |
| | 0 | 146 | | if (GlobalizationMode.Invariant) |
| | | 147 | | { |
| | 0 | 148 | | return true; // all chars are sortable in invariant mode |
| | | 149 | | } |
| | | 150 | | |
| | 0 | 151 | | return (GlobalizationMode.UseNls) ? NlsIsSortable(text) : IcuIsSortable(text); |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | /// <summary> |
| | | 155 | | /// Indicates whether a specified <see cref="Rune"/> is sortable. |
| | | 156 | | /// </summary> |
| | | 157 | | /// <param name="value">A Unicode scalar value.</param> |
| | | 158 | | /// <returns> |
| | | 159 | | /// <see langword="true"/> if <paramref name="value"/> is a sortable Unicode scalar |
| | | 160 | | /// value; otherwise, <see langword="false"/>. |
| | | 161 | | /// </returns> |
| | | 162 | | public static unsafe bool IsSortable(Rune value) |
| | | 163 | | { |
| | 0 | 164 | | Span<char> valueAsUtf16 = stackalloc char[Rune.MaxUtf16CharsPerRune]; |
| | 0 | 165 | | int charCount = value.EncodeToUtf16(valueAsUtf16); |
| | 0 | 166 | | return IsSortable(valueAsUtf16.Slice(0, charCount)); |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | [MemberNotNull(nameof(_sortName))] |
| | | 170 | | private void InitSort(CultureInfo culture) |
| | | 171 | | { |
| | 0 | 172 | | _sortName = culture.SortName; |
| | | 173 | | |
| | 0 | 174 | | if (GlobalizationMode.UseNls) |
| | | 175 | | { |
| | 0 | 176 | | NlsInitSortHandle(); |
| | | 177 | | } |
| | | 178 | | else |
| | | 179 | | { |
| | 0 | 180 | | IcuInitSortHandle(culture.InteropName!); |
| | | 181 | | } |
| | 0 | 182 | | } |
| | | 183 | | |
| | | 184 | | [OnDeserializing] |
| | | 185 | | private void OnDeserializing(StreamingContext ctx) |
| | | 186 | | { |
| | | 187 | | // this becomes null for a brief moment before deserialization |
| | | 188 | | // after serialization is finished it is never null. |
| | 0 | 189 | | m_name = null!; |
| | 0 | 190 | | } |
| | | 191 | | |
| | | 192 | | void IDeserializationCallback.OnDeserialization(object? sender) |
| | | 193 | | { |
| | 0 | 194 | | OnDeserialized(); |
| | 0 | 195 | | } |
| | | 196 | | |
| | | 197 | | [OnDeserialized] |
| | | 198 | | private void OnDeserialized(StreamingContext ctx) |
| | | 199 | | { |
| | 0 | 200 | | OnDeserialized(); |
| | 0 | 201 | | } |
| | | 202 | | |
| | | 203 | | private void OnDeserialized() |
| | | 204 | | { |
| | | 205 | | // If we didn't have a name, use the LCID |
| | 0 | 206 | | if (m_name == null) |
| | | 207 | | { |
| | | 208 | | // From whidbey, didn't have a name |
| | 0 | 209 | | m_name = CultureInfo.GetCultureInfo(culture)._name; |
| | | 210 | | } |
| | | 211 | | else |
| | | 212 | | { |
| | 0 | 213 | | InitSort(CultureInfo.GetCultureInfo(m_name)); |
| | | 214 | | } |
| | 0 | 215 | | } |
| | | 216 | | |
| | | 217 | | [OnSerializing] |
| | | 218 | | private void OnSerializing(StreamingContext ctx) |
| | | 219 | | { |
| | | 220 | | // This is merely for serialization compatibility with Whidbey/Orcas, it can go away when we don't want that |
| | 0 | 221 | | culture = CultureInfo.GetCultureInfo(Name).LCID; // This is the lcid of the constructing culture (still have |
| | 0 | 222 | | Debug.Assert(m_name != null, "CompareInfo.OnSerializing - expected m_name to be set already"); |
| | 0 | 223 | | } |
| | | 224 | | |
| | | 225 | | /// <summary> |
| | | 226 | | /// Returns the name of the culture (well actually, of the sort). |
| | | 227 | | /// Very important for providing a non-LCID way of identifying |
| | | 228 | | /// what the sort is. |
| | | 229 | | /// |
| | | 230 | | /// Note that this name isn't dereferenced in case the CompareInfo is a different locale |
| | | 231 | | /// which is consistent with the behaviors of earlier versions. (so if you ask for a sort |
| | | 232 | | /// and the locale's changed behavior, then you'll get changed behavior, which is like |
| | | 233 | | /// what happens for a version update) |
| | | 234 | | /// </summary> |
| | | 235 | | public string Name |
| | | 236 | | { |
| | | 237 | | get |
| | | 238 | | { |
| | 0 | 239 | | Debug.Assert(m_name != null, "CompareInfo.Name Expected _name to be set"); |
| | 0 | 240 | | if (m_name == "zh-CHT" || m_name == "zh-CHS") |
| | | 241 | | { |
| | 0 | 242 | | return m_name; |
| | | 243 | | } |
| | | 244 | | |
| | 0 | 245 | | return _sortName; |
| | | 246 | | } |
| | | 247 | | } |
| | | 248 | | |
| | | 249 | | /// <summary> |
| | | 250 | | /// Compares the two strings with the given options. Returns 0 if the |
| | | 251 | | /// two strings are equal, a number less than 0 if string1 is less |
| | | 252 | | /// than string2, and a number greater than 0 if string1 is greater |
| | | 253 | | /// than string2. |
| | | 254 | | /// </summary> |
| | | 255 | | public int Compare(string? string1, string? string2) |
| | | 256 | | { |
| | 0 | 257 | | return Compare(string1, string2, CompareOptions.None); |
| | | 258 | | } |
| | | 259 | | |
| | | 260 | | public int Compare(string? string1, string? string2, CompareOptions options) |
| | | 261 | | { |
| | | 262 | | int retVal; |
| | | 263 | | |
| | | 264 | | // Our paradigm is that null sorts less than any other string and |
| | | 265 | | // that two nulls sort as equal. |
| | | 266 | | |
| | 0 | 267 | | if (string1 == null) |
| | | 268 | | { |
| | 0 | 269 | | retVal = (string2 == null) ? 0 : -1; |
| | 0 | 270 | | goto CheckOptionsAndReturn; |
| | | 271 | | } |
| | 0 | 272 | | if (string2 == null) |
| | | 273 | | { |
| | 0 | 274 | | retVal = 1; |
| | 0 | 275 | | goto CheckOptionsAndReturn; |
| | | 276 | | } |
| | | 277 | | |
| | 0 | 278 | | return Compare(string1.AsSpan(), string2.AsSpan(), options); |
| | | 279 | | |
| | | 280 | | CheckOptionsAndReturn: |
| | | 281 | | |
| | | 282 | | // If we're short-circuiting the globalization logic, we still need to check that |
| | | 283 | | // the provided options were valid. |
| | | 284 | | |
| | 0 | 285 | | CheckCompareOptionsForCompare(options); |
| | 0 | 286 | | return retVal; |
| | | 287 | | } |
| | | 288 | | |
| | | 289 | | internal int CompareOptionIgnoreCase(ReadOnlySpan<char> string1, ReadOnlySpan<char> string2) => |
| | 0 | 290 | | GlobalizationMode.Invariant ? |
| | 0 | 291 | | InvariantModeCasing.CompareStringIgnoreCase(ref MemoryMarshal.GetReference(string1), string1.Length, ref |
| | 0 | 292 | | CompareStringCore(string1, string2, CompareOptions.IgnoreCase); |
| | | 293 | | |
| | | 294 | | /// <summary> |
| | | 295 | | /// Compares the specified regions of the two strings with the given |
| | | 296 | | /// options. |
| | | 297 | | /// Returns 0 if the two strings are equal, a number less than 0 if |
| | | 298 | | /// string1 is less than string2, and a number greater than 0 if |
| | | 299 | | /// string1 is greater than string2. |
| | | 300 | | /// </summary> |
| | | 301 | | public int Compare(string? string1, int offset1, int length1, string? string2, int offset2, int length2) |
| | | 302 | | { |
| | 0 | 303 | | return Compare(string1, offset1, length1, string2, offset2, length2, CompareOptions.None); |
| | | 304 | | } |
| | | 305 | | |
| | | 306 | | public int Compare(string? string1, int offset1, string? string2, int offset2, CompareOptions options) |
| | | 307 | | { |
| | 0 | 308 | | return Compare(string1, offset1, string1 == null ? 0 : string1.Length - offset1, |
| | 0 | 309 | | string2, offset2, string2 == null ? 0 : string2.Length - offset2, options); |
| | | 310 | | } |
| | | 311 | | |
| | | 312 | | public int Compare(string? string1, int offset1, string? string2, int offset2) |
| | | 313 | | { |
| | 0 | 314 | | return Compare(string1, offset1, string2, offset2, CompareOptions.None); |
| | | 315 | | } |
| | | 316 | | |
| | | 317 | | public int Compare(string? string1, int offset1, int length1, string? string2, int offset2, int length2, Compare |
| | | 318 | | { |
| | 0 | 319 | | ReadOnlySpan<char> span1 = default; |
| | 0 | 320 | | ReadOnlySpan<char> span2 = default; |
| | | 321 | | |
| | 0 | 322 | | if (string1 == null) |
| | | 323 | | { |
| | 0 | 324 | | if (offset1 != 0 || length1 != 0) |
| | | 325 | | { |
| | 0 | 326 | | goto BoundsCheckError; |
| | | 327 | | } |
| | | 328 | | } |
| | 0 | 329 | | else if (!string1.TryGetSpan(offset1, length1, out span1)) |
| | | 330 | | { |
| | | 331 | | goto BoundsCheckError; |
| | | 332 | | } |
| | | 333 | | |
| | 0 | 334 | | if (string2 == null) |
| | | 335 | | { |
| | 0 | 336 | | if (offset2 != 0 || length2 != 0) |
| | | 337 | | { |
| | 0 | 338 | | goto BoundsCheckError; |
| | | 339 | | } |
| | | 340 | | } |
| | 0 | 341 | | else if (!string2.TryGetSpan(offset2, length2, out span2)) |
| | | 342 | | { |
| | | 343 | | goto BoundsCheckError; |
| | | 344 | | } |
| | | 345 | | |
| | | 346 | | // At this point both string1 and string2 have been bounds-checked. |
| | | 347 | | |
| | | 348 | | int retVal; |
| | | 349 | | |
| | | 350 | | // Our paradigm is that null sorts less than any other string and |
| | | 351 | | // that two nulls sort as equal. |
| | | 352 | | |
| | 0 | 353 | | if (string1 == null) |
| | | 354 | | { |
| | 0 | 355 | | retVal = (string2 == null) ? 0 : -1; |
| | 0 | 356 | | goto CheckOptionsAndReturn; |
| | | 357 | | } |
| | 0 | 358 | | if (string2 == null) |
| | | 359 | | { |
| | 0 | 360 | | retVal = 1; |
| | 0 | 361 | | goto CheckOptionsAndReturn; |
| | | 362 | | } |
| | | 363 | | |
| | | 364 | | // At this point we know both string1 and string2 weren't null, |
| | | 365 | | // though they may have been empty. |
| | | 366 | | |
| | 0 | 367 | | Debug.Assert(!Unsafe.IsNullRef(ref MemoryMarshal.GetReference(span1))); |
| | 0 | 368 | | Debug.Assert(!Unsafe.IsNullRef(ref MemoryMarshal.GetReference(span2))); |
| | | 369 | | |
| | 0 | 370 | | return Compare(span1, span2, options); |
| | | 371 | | |
| | | 372 | | CheckOptionsAndReturn: |
| | | 373 | | |
| | | 374 | | // If we're short-circuiting the globalization logic, we still need to check that |
| | | 375 | | // the provided options were valid. |
| | | 376 | | |
| | 0 | 377 | | CheckCompareOptionsForCompare(options); |
| | 0 | 378 | | return retVal; |
| | | 379 | | |
| | | 380 | | BoundsCheckError: |
| | | 381 | | |
| | | 382 | | // We know a bounds check error occurred. Now we just need to figure |
| | | 383 | | // out the correct error message to surface. |
| | | 384 | | |
| | 0 | 385 | | ArgumentOutOfRangeException.ThrowIfNegative(length1); |
| | 0 | 386 | | ArgumentOutOfRangeException.ThrowIfNegative(length2); |
| | | 387 | | |
| | 0 | 388 | | ArgumentOutOfRangeException.ThrowIfNegative(offset1); |
| | 0 | 389 | | ArgumentOutOfRangeException.ThrowIfNegative(offset2); |
| | | 390 | | |
| | 0 | 391 | | if (offset1 > (string1 == null ? 0 : string1.Length) - length1) |
| | | 392 | | { |
| | 0 | 393 | | throw new ArgumentOutOfRangeException(nameof(string1), SR.ArgumentOutOfRange_OffsetLength); |
| | | 394 | | } |
| | | 395 | | |
| | 0 | 396 | | Debug.Assert(offset2 > (string2 == null ? 0 : string2.Length) - length2); |
| | 0 | 397 | | throw new ArgumentOutOfRangeException(nameof(string2), SR.ArgumentOutOfRange_OffsetLength); |
| | | 398 | | } |
| | | 399 | | |
| | | 400 | | /// <summary> |
| | | 401 | | /// Compares two strings. |
| | | 402 | | /// </summary> |
| | | 403 | | /// <param name="string1">The first string to compare.</param> |
| | | 404 | | /// <param name="string2">The second string to compare.</param> |
| | | 405 | | /// <param name="options">The <see cref="CompareOptions"/> to use during the comparison.</param> |
| | | 406 | | /// <returns> |
| | | 407 | | /// Zero if <paramref name="string1"/> and <paramref name="string2"/> are equal; |
| | | 408 | | /// or a negative value if <paramref name="string1"/> sorts before <paramref name="string2"/>; |
| | | 409 | | /// or a positive value if <paramref name="string1"/> sorts after <paramref name="string2"/>. |
| | | 410 | | /// </returns> |
| | | 411 | | /// <exception cref="ArgumentException"> |
| | | 412 | | /// <paramref name="options"/> contains an unsupported combination of flags. |
| | | 413 | | /// </exception> |
| | | 414 | | public int Compare(ReadOnlySpan<char> string1, ReadOnlySpan<char> string2, CompareOptions options = CompareOptio |
| | | 415 | | { |
| | 0 | 416 | | if (string1 == string2) // referential equality + length |
| | | 417 | | { |
| | 0 | 418 | | CheckCompareOptionsForCompare(options); |
| | 0 | 419 | | return 0; |
| | | 420 | | } |
| | | 421 | | |
| | 0 | 422 | | if ((options & ValidCompareMaskOffFlags) == 0) |
| | | 423 | | { |
| | | 424 | | // Common case: caller is attempting to perform linguistic comparison. |
| | | 425 | | // Pass the flags down to NLS or ICU unless we're running in invariant |
| | | 426 | | // mode, at which point we normalize the flags to Ordinal[IgnoreCase]. |
| | | 427 | | |
| | 0 | 428 | | if (!GlobalizationMode.Invariant) |
| | | 429 | | { |
| | 0 | 430 | | return CompareStringCore(string1, string2, options); |
| | | 431 | | } |
| | | 432 | | |
| | 0 | 433 | | if ((options & CompareOptions.IgnoreCase) == 0) |
| | | 434 | | { |
| | 0 | 435 | | return string1.SequenceCompareTo(string2); |
| | | 436 | | } |
| | | 437 | | |
| | 0 | 438 | | return Ordinal.CompareStringIgnoreCase(ref MemoryMarshal.GetReference(string1), string1.Length, ref Memo |
| | | 439 | | } |
| | | 440 | | else |
| | | 441 | | { |
| | | 442 | | // Less common case: caller is attempting to perform non-linguistic comparison, |
| | | 443 | | // or an invalid combination of flags was supplied. |
| | | 444 | | |
| | 0 | 445 | | if (options == CompareOptions.Ordinal) |
| | | 446 | | { |
| | 0 | 447 | | return string1.SequenceCompareTo(string2); |
| | | 448 | | } |
| | | 449 | | |
| | 0 | 450 | | if (options == CompareOptions.OrdinalIgnoreCase) |
| | | 451 | | { |
| | 0 | 452 | | return Ordinal.CompareStringIgnoreCase(ref MemoryMarshal.GetReference(string1), string1.Length, ref |
| | | 453 | | } |
| | | 454 | | |
| | 0 | 455 | | ThrowCompareOptionsCheckFailed(options); |
| | | 456 | | |
| | | 457 | | return -1; // make the compiler happy; |
| | | 458 | | } |
| | | 459 | | } |
| | | 460 | | |
| | | 461 | | // Checks that 'CompareOptions' is valid for a call to Compare, throwing the appropriate |
| | | 462 | | // exception if the check fails. |
| | | 463 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 464 | | [StackTraceHidden] |
| | | 465 | | private static void CheckCompareOptionsForCompare(CompareOptions options) |
| | | 466 | | { |
| | | 467 | | // Any combination of defined CompareOptions flags is valid, except for |
| | | 468 | | // Ordinal and OrdinalIgnoreCase, which may only be used in isolation. |
| | | 469 | | |
| | 0 | 470 | | if ((options & ValidCompareMaskOffFlags) != 0) |
| | | 471 | | { |
| | 0 | 472 | | if (options != CompareOptions.Ordinal && options != CompareOptions.OrdinalIgnoreCase) |
| | | 473 | | { |
| | 0 | 474 | | ThrowCompareOptionsCheckFailed(options); |
| | | 475 | | } |
| | | 476 | | } |
| | 0 | 477 | | } |
| | | 478 | | |
| | | 479 | | [DoesNotReturn] |
| | | 480 | | [StackTraceHidden] |
| | | 481 | | private static void ThrowCompareOptionsCheckFailed(CompareOptions options) |
| | | 482 | | { |
| | 0 | 483 | | throw new ArgumentException( |
| | 0 | 484 | | paramName: nameof(options), |
| | 0 | 485 | | message: ((options & CompareOptions.Ordinal) != 0) ? SR.Argument_CompareOptionOrdinal : SR.Argument_Inva |
| | | 486 | | } |
| | | 487 | | |
| | | 488 | | private int CompareStringCore(ReadOnlySpan<char> string1, ReadOnlySpan<char> string2, CompareOptions options) => |
| | 0 | 489 | | GlobalizationMode.UseNls ? |
| | 0 | 490 | | NlsCompareString(string1, string2, options) : |
| | 0 | 491 | | #if TARGET_MACCATALYST || TARGET_IOS || TARGET_TVOS |
| | 0 | 492 | | GlobalizationMode.Hybrid ? |
| | 0 | 493 | | CompareStringNative(string1, string2, options) : |
| | 0 | 494 | | #endif |
| | 0 | 495 | | IcuCompareString(string1, string2, options); |
| | | 496 | | |
| | | 497 | | /// <summary> |
| | | 498 | | /// Determines whether prefix is a prefix of string. If prefix equals |
| | | 499 | | /// string.Empty, true is returned. |
| | | 500 | | /// </summary> |
| | | 501 | | public bool IsPrefix(string source, string prefix, CompareOptions options) |
| | | 502 | | { |
| | 0 | 503 | | if (source == null) |
| | | 504 | | { |
| | 0 | 505 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); |
| | | 506 | | } |
| | 0 | 507 | | if (prefix == null) |
| | | 508 | | { |
| | 0 | 509 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.prefix); |
| | | 510 | | } |
| | | 511 | | |
| | 0 | 512 | | return IsPrefix(source.AsSpan(), prefix.AsSpan(), options); |
| | | 513 | | } |
| | | 514 | | |
| | | 515 | | /// <summary> |
| | | 516 | | /// Determines whether a string starts with a specific prefix. |
| | | 517 | | /// </summary> |
| | | 518 | | /// <param name="source">The string to search within.</param> |
| | | 519 | | /// <param name="prefix">The prefix to attempt to match at the start of <paramref name="source"/>.</param> |
| | | 520 | | /// <param name="options">The <see cref="CompareOptions"/> to use during the match.</param> |
| | | 521 | | /// <returns> |
| | | 522 | | /// <see langword="true"/> if <paramref name="prefix"/> occurs at the start of <paramref name="source"/>; |
| | | 523 | | /// otherwise, <see langword="false"/>. |
| | | 524 | | /// </returns> |
| | | 525 | | /// <exception cref="ArgumentException"> |
| | | 526 | | /// <paramref name="options"/> contains an unsupported combination of flags. |
| | | 527 | | /// </exception> |
| | | 528 | | public unsafe bool IsPrefix(ReadOnlySpan<char> source, ReadOnlySpan<char> prefix, CompareOptions options = Compa |
| | | 529 | | { |
| | | 530 | | // The empty string is trivially a prefix of every other string. For compat with |
| | | 531 | | // earlier versions of the Framework we'll early-exit here before validating the |
| | | 532 | | // 'options' argument. |
| | | 533 | | |
| | 0 | 534 | | if (prefix.IsEmpty) |
| | | 535 | | { |
| | 0 | 536 | | return true; |
| | | 537 | | } |
| | | 538 | | |
| | 0 | 539 | | if ((options & ValidIndexMaskOffFlags) == 0) |
| | | 540 | | { |
| | | 541 | | // Common case: caller is attempting to perform a linguistic search. |
| | | 542 | | // Pass the flags down to NLS or ICU unless we're running in invariant |
| | | 543 | | // mode, at which point we normalize the flags to Ordinal[IgnoreCase]. |
| | | 544 | | |
| | 0 | 545 | | if (!GlobalizationMode.Invariant) |
| | | 546 | | { |
| | 0 | 547 | | return StartsWithCore(source, prefix, options, matchLengthPtr: null); |
| | | 548 | | } |
| | | 549 | | |
| | 0 | 550 | | if ((options & CompareOptions.IgnoreCase) == 0) |
| | | 551 | | { |
| | 0 | 552 | | return source.StartsWith(prefix); |
| | | 553 | | } |
| | | 554 | | |
| | 0 | 555 | | return source.StartsWithOrdinalIgnoreCase(prefix); |
| | | 556 | | } |
| | | 557 | | else |
| | | 558 | | { |
| | | 559 | | // Less common case: caller is attempting to perform non-linguistic comparison, |
| | | 560 | | // or an invalid combination of flags was supplied. |
| | | 561 | | |
| | 0 | 562 | | if (options == CompareOptions.Ordinal) |
| | | 563 | | { |
| | 0 | 564 | | return source.StartsWith(prefix); |
| | | 565 | | } |
| | | 566 | | |
| | 0 | 567 | | if (options == CompareOptions.OrdinalIgnoreCase) |
| | | 568 | | { |
| | 0 | 569 | | return source.StartsWithOrdinalIgnoreCase(prefix); |
| | | 570 | | } |
| | | 571 | | |
| | 0 | 572 | | ThrowCompareOptionsCheckFailed(options); |
| | | 573 | | |
| | | 574 | | return false; // make the compiler happy; |
| | | 575 | | } |
| | | 576 | | } |
| | | 577 | | |
| | | 578 | | /// <summary> |
| | | 579 | | /// Determines whether a string starts with a specific prefix. |
| | | 580 | | /// </summary> |
| | | 581 | | /// <param name="source">The string to search within.</param> |
| | | 582 | | /// <param name="prefix">The prefix to attempt to match at the start of <paramref name="source"/>.</param> |
| | | 583 | | /// <param name="options">The <see cref="CompareOptions"/> to use during the match.</param> |
| | | 584 | | /// <param name="matchLength">When this method returns, contains the number of characters of |
| | | 585 | | /// <paramref name="source"/> that matched the desired prefix. This may be different than the |
| | | 586 | | /// length of <paramref name="prefix"/> if a linguistic comparison is performed. Set to 0 |
| | | 587 | | /// if the prefix did not match.</param> |
| | | 588 | | /// <returns> |
| | | 589 | | /// <see langword="true"/> if <paramref name="prefix"/> occurs at the start of <paramref name="source"/>; |
| | | 590 | | /// otherwise, <see langword="false"/>. |
| | | 591 | | /// </returns> |
| | | 592 | | /// <exception cref="ArgumentException"> |
| | | 593 | | /// <paramref name="options"/> contains an unsupported combination of flags. |
| | | 594 | | /// </exception> |
| | | 595 | | /// <remarks> |
| | | 596 | | /// This method has greater overhead than other <see cref="IsPrefix"/> overloads which don't |
| | | 597 | | /// take a <paramref name="matchLength"/> argument. Call this overload only if you require |
| | | 598 | | /// the match length information. |
| | | 599 | | /// </remarks> |
| | | 600 | | public unsafe bool IsPrefix(ReadOnlySpan<char> source, ReadOnlySpan<char> prefix, CompareOptions options, out in |
| | | 601 | | { |
| | | 602 | | bool matched; |
| | | 603 | | |
| | 0 | 604 | | if (GlobalizationMode.Invariant || prefix.IsEmpty || (options & ValidIndexMaskOffFlags) != 0) |
| | | 605 | | { |
| | | 606 | | // Non-linguistic (ordinal) comparison requested, or options are invalid. |
| | | 607 | | // Delegate to other overload, which validates options and throws on failure. |
| | | 608 | | // If success, non-linguistic matches will always preserve prefix length. |
| | | 609 | | |
| | 0 | 610 | | matched = IsPrefix(source, prefix, options); |
| | 0 | 611 | | matchLength = (matched) ? prefix.Length : 0; |
| | | 612 | | } |
| | | 613 | | else |
| | | 614 | | { |
| | | 615 | | // Linguistic comparison requested and we don't need to special-case any args. |
| | | 616 | | #if TARGET_MACCATALYST || TARGET_IOS || TARGET_TVOS |
| | | 617 | | if (GlobalizationMode.Hybrid) |
| | | 618 | | { |
| | | 619 | | throw new PlatformNotSupportedException(SR.PlatformNotSupported_HybridGlobalizationWithMatchLength); |
| | | 620 | | } |
| | | 621 | | #endif |
| | 0 | 622 | | int tempMatchLength = 0; |
| | 0 | 623 | | matched = StartsWithCore(source, prefix, options, &tempMatchLength); |
| | 0 | 624 | | matchLength = tempMatchLength; |
| | | 625 | | } |
| | | 626 | | |
| | 0 | 627 | | return matched; |
| | | 628 | | } |
| | | 629 | | |
| | | 630 | | private unsafe bool StartsWithCore(ReadOnlySpan<char> source, ReadOnlySpan<char> prefix, CompareOptions options, |
| | 0 | 631 | | GlobalizationMode.UseNls ? |
| | 0 | 632 | | NlsStartsWith(source, prefix, options, matchLengthPtr) : |
| | 0 | 633 | | IcuStartsWith(source, prefix, options, matchLengthPtr); |
| | | 634 | | |
| | | 635 | | public bool IsPrefix(string source, string prefix) |
| | | 636 | | { |
| | 0 | 637 | | return IsPrefix(source, prefix, CompareOptions.None); |
| | | 638 | | } |
| | | 639 | | |
| | | 640 | | /// <summary> |
| | | 641 | | /// Determines whether suffix is a suffix of string. If suffix equals |
| | | 642 | | /// string.Empty, true is returned. |
| | | 643 | | /// </summary> |
| | | 644 | | public bool IsSuffix(string source, string suffix, CompareOptions options) |
| | | 645 | | { |
| | 0 | 646 | | if (source == null) |
| | | 647 | | { |
| | 0 | 648 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); |
| | | 649 | | } |
| | 0 | 650 | | if (suffix == null) |
| | | 651 | | { |
| | 0 | 652 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.suffix); |
| | | 653 | | } |
| | | 654 | | |
| | 0 | 655 | | return IsSuffix(source.AsSpan(), suffix.AsSpan(), options); |
| | | 656 | | } |
| | | 657 | | |
| | | 658 | | /// <summary> |
| | | 659 | | /// Determines whether a string ends with a specific suffix. |
| | | 660 | | /// </summary> |
| | | 661 | | /// <param name="source">The string to search within.</param> |
| | | 662 | | /// <param name="suffix">The suffix to attempt to match at the end of <paramref name="source"/>.</param> |
| | | 663 | | /// <param name="options">The <see cref="CompareOptions"/> to use during the match.</param> |
| | | 664 | | /// <returns> |
| | | 665 | | /// <see langword="true"/> if <paramref name="suffix"/> occurs at the end of <paramref name="source"/>; |
| | | 666 | | /// otherwise, <see langword="false"/>. |
| | | 667 | | /// </returns> |
| | | 668 | | /// <exception cref="ArgumentException"> |
| | | 669 | | /// <paramref name="options"/> contains an unsupported combination of flags. |
| | | 670 | | /// </exception> |
| | | 671 | | public unsafe bool IsSuffix(ReadOnlySpan<char> source, ReadOnlySpan<char> suffix, CompareOptions options = Compa |
| | | 672 | | { |
| | | 673 | | // The empty string is trivially a suffix of every other string. For compat with |
| | | 674 | | // earlier versions of the Framework we'll early-exit here before validating the |
| | | 675 | | // 'options' argument. |
| | | 676 | | |
| | 0 | 677 | | if (suffix.IsEmpty) |
| | | 678 | | { |
| | 0 | 679 | | return true; |
| | | 680 | | } |
| | | 681 | | |
| | 0 | 682 | | if ((options & ValidIndexMaskOffFlags) == 0) |
| | | 683 | | { |
| | | 684 | | // Common case: caller is attempting to perform a linguistic search. |
| | | 685 | | // Pass the flags down to NLS or ICU unless we're running in invariant |
| | | 686 | | // mode, at which point we normalize the flags to Ordinal[IgnoreCase]. |
| | | 687 | | |
| | 0 | 688 | | if (!GlobalizationMode.Invariant) |
| | | 689 | | { |
| | 0 | 690 | | return EndsWithCore(source, suffix, options, matchLengthPtr: null); |
| | | 691 | | } |
| | | 692 | | |
| | 0 | 693 | | if ((options & CompareOptions.IgnoreCase) == 0) |
| | | 694 | | { |
| | 0 | 695 | | return source.EndsWith(suffix); |
| | | 696 | | } |
| | | 697 | | |
| | 0 | 698 | | return source.EndsWithOrdinalIgnoreCase(suffix); |
| | | 699 | | } |
| | | 700 | | else |
| | | 701 | | { |
| | | 702 | | // Less common case: caller is attempting to perform non-linguistic comparison, |
| | | 703 | | // or an invalid combination of flags was supplied. |
| | | 704 | | |
| | 0 | 705 | | if (options == CompareOptions.Ordinal) |
| | | 706 | | { |
| | 0 | 707 | | return source.EndsWith(suffix); |
| | | 708 | | } |
| | | 709 | | |
| | 0 | 710 | | if (options == CompareOptions.OrdinalIgnoreCase) |
| | | 711 | | { |
| | 0 | 712 | | return source.EndsWithOrdinalIgnoreCase(suffix); |
| | | 713 | | } |
| | | 714 | | |
| | 0 | 715 | | ThrowCompareOptionsCheckFailed(options); |
| | | 716 | | |
| | | 717 | | return false; // make the compiler happy; |
| | | 718 | | } |
| | | 719 | | } |
| | | 720 | | |
| | | 721 | | /// <summary> |
| | | 722 | | /// Determines whether a string ends with a specific suffix. |
| | | 723 | | /// </summary> |
| | | 724 | | /// <param name="source">The string to search within.</param> |
| | | 725 | | /// <param name="suffix">The suffix to attempt to match at the end of <paramref name="source"/>.</param> |
| | | 726 | | /// <param name="options">The <see cref="CompareOptions"/> to use during the match.</param> |
| | | 727 | | /// <param name="matchLength">When this method returns, contains the number of characters of |
| | | 728 | | /// <paramref name="source"/> that matched the desired suffix. This may be different than the |
| | | 729 | | /// length of <paramref name="suffix"/> if a linguistic comparison is performed. Set to 0 |
| | | 730 | | /// if the suffix did not match.</param> |
| | | 731 | | /// <returns> |
| | | 732 | | /// <see langword="true"/> if <paramref name="suffix"/> occurs at the end of <paramref name="source"/>; |
| | | 733 | | /// otherwise, <see langword="false"/>. |
| | | 734 | | /// </returns> |
| | | 735 | | /// <exception cref="ArgumentException"> |
| | | 736 | | /// <paramref name="options"/> contains an unsupported combination of flags. |
| | | 737 | | /// </exception> |
| | | 738 | | /// <remarks> |
| | | 739 | | /// This method has greater overhead than other <see cref="IsSuffix"/> overloads which don't |
| | | 740 | | /// take a <paramref name="matchLength"/> argument. Call this overload only if you require |
| | | 741 | | /// the match length information. |
| | | 742 | | /// </remarks> |
| | | 743 | | public unsafe bool IsSuffix(ReadOnlySpan<char> source, ReadOnlySpan<char> suffix, CompareOptions options, out in |
| | | 744 | | { |
| | | 745 | | bool matched; |
| | | 746 | | |
| | 0 | 747 | | if (GlobalizationMode.Invariant || suffix.IsEmpty || (options & ValidIndexMaskOffFlags) != 0) |
| | | 748 | | { |
| | | 749 | | // Non-linguistic (ordinal) comparison requested, or options are invalid. |
| | | 750 | | // Delegate to other overload, which validates options and throws on failure. |
| | | 751 | | // If success, non-linguistic matches will always preserve prefix length. |
| | | 752 | | |
| | 0 | 753 | | matched = IsSuffix(source, suffix, options); |
| | 0 | 754 | | matchLength = (matched) ? suffix.Length : 0; |
| | | 755 | | } |
| | | 756 | | else |
| | | 757 | | { |
| | | 758 | | // Linguistic comparison requested and we don't need to special-case any args. |
| | | 759 | | #if TARGET_MACCATALYST || TARGET_IOS || TARGET_TVOS |
| | | 760 | | if (GlobalizationMode.Hybrid) |
| | | 761 | | { |
| | | 762 | | throw new PlatformNotSupportedException(SR.PlatformNotSupported_HybridGlobalizationWithMatchLength); |
| | | 763 | | } |
| | | 764 | | #endif |
| | 0 | 765 | | int tempMatchLength = 0; |
| | 0 | 766 | | matched = EndsWithCore(source, suffix, options, &tempMatchLength); |
| | 0 | 767 | | matchLength = tempMatchLength; |
| | | 768 | | } |
| | | 769 | | |
| | 0 | 770 | | return matched; |
| | | 771 | | } |
| | | 772 | | |
| | | 773 | | public bool IsSuffix(string source, string suffix) |
| | | 774 | | { |
| | 0 | 775 | | return IsSuffix(source, suffix, CompareOptions.None); |
| | | 776 | | } |
| | | 777 | | |
| | | 778 | | private unsafe bool EndsWithCore(ReadOnlySpan<char> source, ReadOnlySpan<char> suffix, CompareOptions options, i |
| | 0 | 779 | | GlobalizationMode.UseNls ? |
| | 0 | 780 | | NlsEndsWith(source, suffix, options, matchLengthPtr) : |
| | 0 | 781 | | IcuEndsWith(source, suffix, options, matchLengthPtr); |
| | | 782 | | |
| | | 783 | | /// <summary> |
| | | 784 | | /// Returns the first index where value is found in string. The |
| | | 785 | | /// search starts from startIndex and ends at endIndex. Returns -1 if |
| | | 786 | | /// the specified value is not found. If value equals string.Empty, |
| | | 787 | | /// startIndex is returned. Throws IndexOutOfRange if startIndex or |
| | | 788 | | /// endIndex is less than zero or greater than the length of string. |
| | | 789 | | /// Throws ArgumentException if value (as a string) is null. |
| | | 790 | | /// </summary> |
| | | 791 | | public int IndexOf(string source, char value) |
| | | 792 | | { |
| | 0 | 793 | | return IndexOf(source, value, CompareOptions.None); |
| | | 794 | | } |
| | | 795 | | |
| | | 796 | | public int IndexOf(string source, string value) |
| | | 797 | | { |
| | 0 | 798 | | return IndexOf(source, value, CompareOptions.None); |
| | | 799 | | } |
| | | 800 | | |
| | | 801 | | public int IndexOf(string source, char value, CompareOptions options) |
| | | 802 | | { |
| | 0 | 803 | | if (source == null) |
| | | 804 | | { |
| | 0 | 805 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); |
| | | 806 | | } |
| | | 807 | | |
| | 0 | 808 | | return IndexOf(source, new ReadOnlySpan<char>(in value), options); |
| | | 809 | | } |
| | | 810 | | |
| | | 811 | | public int IndexOf(string source, string value, CompareOptions options) |
| | | 812 | | { |
| | 0 | 813 | | if (source == null) |
| | | 814 | | { |
| | 0 | 815 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); |
| | | 816 | | } |
| | 0 | 817 | | if (value == null) |
| | | 818 | | { |
| | 0 | 819 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value); |
| | | 820 | | } |
| | | 821 | | |
| | 0 | 822 | | return IndexOf(source.AsSpan(), value.AsSpan(), options); |
| | | 823 | | } |
| | | 824 | | |
| | | 825 | | public int IndexOf(string source, char value, int startIndex) |
| | | 826 | | { |
| | 0 | 827 | | return IndexOf(source, value, startIndex, CompareOptions.None); |
| | | 828 | | } |
| | | 829 | | |
| | | 830 | | public int IndexOf(string source, string value, int startIndex) |
| | | 831 | | { |
| | 0 | 832 | | return IndexOf(source, value, startIndex, CompareOptions.None); |
| | | 833 | | } |
| | | 834 | | |
| | | 835 | | public int IndexOf(string source, char value, int startIndex, CompareOptions options) |
| | | 836 | | { |
| | 0 | 837 | | if (source == null) |
| | | 838 | | { |
| | 0 | 839 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); |
| | | 840 | | } |
| | | 841 | | |
| | 0 | 842 | | return IndexOf(source, value, startIndex, source.Length - startIndex, options); |
| | | 843 | | |
| | | 844 | | } |
| | | 845 | | |
| | | 846 | | public int IndexOf(string source, string value, int startIndex, CompareOptions options) |
| | | 847 | | { |
| | 0 | 848 | | if (source == null) |
| | | 849 | | { |
| | 0 | 850 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); |
| | | 851 | | } |
| | | 852 | | |
| | 0 | 853 | | return IndexOf(source, value, startIndex, source.Length - startIndex, options); |
| | | 854 | | } |
| | | 855 | | |
| | | 856 | | public int IndexOf(string source, char value, int startIndex, int count) |
| | | 857 | | { |
| | 0 | 858 | | return IndexOf(source, value, startIndex, count, CompareOptions.None); |
| | | 859 | | } |
| | | 860 | | |
| | | 861 | | public int IndexOf(string source, string value, int startIndex, int count) |
| | | 862 | | { |
| | 0 | 863 | | return IndexOf(source, value, startIndex, count, CompareOptions.None); |
| | | 864 | | } |
| | | 865 | | |
| | | 866 | | public int IndexOf(string source, char value, int startIndex, int count, CompareOptions options) |
| | | 867 | | { |
| | 0 | 868 | | if (source == null) |
| | | 869 | | { |
| | 0 | 870 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); |
| | | 871 | | } |
| | | 872 | | |
| | 0 | 873 | | if (!source.TryGetSpan(startIndex, count, out ReadOnlySpan<char> sourceSpan)) |
| | | 874 | | { |
| | | 875 | | // Bounds check failed - figure out exactly what went wrong so that we can |
| | | 876 | | // surface the correct argument exception. |
| | | 877 | | |
| | 0 | 878 | | if ((uint)startIndex > (uint)source.Length) |
| | | 879 | | { |
| | 0 | 880 | | ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.Argumen |
| | | 881 | | } |
| | | 882 | | else |
| | | 883 | | { |
| | 0 | 884 | | ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutO |
| | | 885 | | } |
| | | 886 | | } |
| | | 887 | | |
| | 0 | 888 | | int result = IndexOf(sourceSpan, new ReadOnlySpan<char>(in value), options); |
| | 0 | 889 | | if (result >= 0) |
| | | 890 | | { |
| | 0 | 891 | | result += startIndex; |
| | | 892 | | } |
| | 0 | 893 | | return result; |
| | | 894 | | } |
| | | 895 | | |
| | | 896 | | public int IndexOf(string source, string value, int startIndex, int count, CompareOptions options) |
| | | 897 | | { |
| | 0 | 898 | | if (source == null) |
| | | 899 | | { |
| | 0 | 900 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); |
| | | 901 | | } |
| | 0 | 902 | | if (value == null) |
| | | 903 | | { |
| | 0 | 904 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value); |
| | | 905 | | } |
| | | 906 | | |
| | 0 | 907 | | if (!source.TryGetSpan(startIndex, count, out ReadOnlySpan<char> sourceSpan)) |
| | | 908 | | { |
| | | 909 | | // Bounds check failed - figure out exactly what went wrong so that we can |
| | | 910 | | // surface the correct argument exception. |
| | | 911 | | |
| | 0 | 912 | | if ((uint)startIndex > (uint)source.Length) |
| | | 913 | | { |
| | 0 | 914 | | ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.Argumen |
| | | 915 | | } |
| | | 916 | | else |
| | | 917 | | { |
| | 0 | 918 | | ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutO |
| | | 919 | | } |
| | | 920 | | } |
| | | 921 | | |
| | 0 | 922 | | int result = IndexOf(sourceSpan, value, options); |
| | 0 | 923 | | if (result >= 0) |
| | | 924 | | { |
| | 0 | 925 | | result += startIndex; |
| | | 926 | | } |
| | 0 | 927 | | return result; |
| | | 928 | | } |
| | | 929 | | |
| | | 930 | | /// <summary> |
| | | 931 | | /// Searches for the first occurrence of a substring within a source string. |
| | | 932 | | /// </summary> |
| | | 933 | | /// <param name="source">The string to search within.</param> |
| | | 934 | | /// <param name="value">The substring to locate within <paramref name="source"/>.</param> |
| | | 935 | | /// <param name="options">The <see cref="CompareOptions"/> to use during the search.</param> |
| | | 936 | | /// <returns> |
| | | 937 | | /// The zero-based index into <paramref name="source"/> where the substring <paramref name="value"/> |
| | | 938 | | /// first appears; or -1 if <paramref name="value"/> cannot be found within <paramref name="source"/>. |
| | | 939 | | /// </returns> |
| | | 940 | | /// <exception cref="ArgumentException"> |
| | | 941 | | /// <paramref name="options"/> contains an unsupported combination of flags. |
| | | 942 | | /// </exception> |
| | | 943 | | public unsafe int IndexOf(ReadOnlySpan<char> source, ReadOnlySpan<char> value, CompareOptions options = CompareO |
| | | 944 | | { |
| | 0 | 945 | | if ((options & ValidIndexMaskOffFlags) == 0) |
| | | 946 | | { |
| | | 947 | | // Common case: caller is attempting to perform a linguistic search. |
| | | 948 | | // Pass the flags down to NLS or ICU unless we're running in invariant |
| | | 949 | | // mode, at which point we normalize the flags to Ordinal[IgnoreCase]. |
| | | 950 | | |
| | 0 | 951 | | if (!GlobalizationMode.Invariant) |
| | | 952 | | { |
| | 0 | 953 | | if (value.IsEmpty) |
| | | 954 | | { |
| | 0 | 955 | | return 0; // Empty target string trivially occurs at index 0 of every search space. |
| | | 956 | | } |
| | | 957 | | else |
| | | 958 | | { |
| | 0 | 959 | | return IndexOfCore(source, value, options, matchLengthPtr: null, fromBeginning: true); |
| | | 960 | | } |
| | | 961 | | } |
| | | 962 | | |
| | 0 | 963 | | if ((options & CompareOptions.IgnoreCase) == 0) |
| | | 964 | | { |
| | 0 | 965 | | return source.IndexOf(value); |
| | | 966 | | } |
| | | 967 | | |
| | 0 | 968 | | return Ordinal.IndexOfOrdinalIgnoreCase(source, value); |
| | | 969 | | } |
| | | 970 | | else |
| | | 971 | | { |
| | | 972 | | // Less common case: caller is attempting to perform non-linguistic comparison, |
| | | 973 | | // or an invalid combination of flags was supplied. |
| | | 974 | | |
| | 0 | 975 | | if (options == CompareOptions.Ordinal) |
| | | 976 | | { |
| | 0 | 977 | | return source.IndexOf(value); |
| | | 978 | | } |
| | | 979 | | |
| | 0 | 980 | | if (options == CompareOptions.OrdinalIgnoreCase) |
| | | 981 | | { |
| | 0 | 982 | | return Ordinal.IndexOfOrdinalIgnoreCase(source, value); |
| | | 983 | | } |
| | | 984 | | |
| | 0 | 985 | | ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidFlag, ExceptionArgument.options); |
| | | 986 | | |
| | | 987 | | return -1; // make the compiler happy; |
| | | 988 | | } |
| | | 989 | | } |
| | | 990 | | |
| | | 991 | | /// <summary> |
| | | 992 | | /// Searches for the first occurrence of a substring within a source string. |
| | | 993 | | /// </summary> |
| | | 994 | | /// <param name="source">The string to search within.</param> |
| | | 995 | | /// <param name="value">The substring to locate within <paramref name="source"/>.</param> |
| | | 996 | | /// <param name="options">The <see cref="CompareOptions"/> to use during the search.</param> |
| | | 997 | | /// <param name="matchLength">When this method returns, contains the number of characters of |
| | | 998 | | /// <paramref name="source"/> that matched the desired value. This may be different than the |
| | | 999 | | /// length of <paramref name="value"/> if a linguistic comparison is performed. Set to 0 |
| | | 1000 | | /// if <paramref name="value"/> is not found within <paramref name="source"/>.</param> |
| | | 1001 | | /// <returns> |
| | | 1002 | | /// The zero-based index into <paramref name="source"/> where the substring <paramref name="value"/> |
| | | 1003 | | /// first appears; or -1 if <paramref name="value"/> cannot be found within <paramref name="source"/>. |
| | | 1004 | | /// </returns> |
| | | 1005 | | /// <exception cref="ArgumentException"> |
| | | 1006 | | /// <paramref name="options"/> contains an unsupported combination of flags. |
| | | 1007 | | /// </exception> |
| | | 1008 | | /// <remarks> |
| | | 1009 | | /// This method has greater overhead than other <see cref="IndexOf"/> overloads which don't |
| | | 1010 | | /// take a <paramref name="matchLength"/> argument. Call this overload only if you require |
| | | 1011 | | /// the match length information. |
| | | 1012 | | /// </remarks> |
| | | 1013 | | public unsafe int IndexOf(ReadOnlySpan<char> source, ReadOnlySpan<char> value, CompareOptions options, out int m |
| | | 1014 | | { |
| | | 1015 | | int tempMatchLength; |
| | 0 | 1016 | | int retVal = IndexOf(source, value, &tempMatchLength, options, fromBeginning: true); |
| | 0 | 1017 | | matchLength = tempMatchLength; |
| | 0 | 1018 | | return retVal; |
| | | 1019 | | } |
| | | 1020 | | |
| | | 1021 | | /// <summary> |
| | | 1022 | | /// Searches for the first occurrence of a <see cref="Rune"/> within a source string. |
| | | 1023 | | /// </summary> |
| | | 1024 | | /// <param name="source">The string to search within.</param> |
| | | 1025 | | /// <param name="value">The <see cref="Rune"/> to locate within <paramref name="source"/>.</param> |
| | | 1026 | | /// <param name="options">The <see cref="CompareOptions"/> to use during the search.</param> |
| | | 1027 | | /// <returns> |
| | | 1028 | | /// The zero-based index into <paramref name="source"/> where <paramref name="value"/> |
| | | 1029 | | /// first appears; or -1 if <paramref name="value"/> cannot be found within <paramref name="source"/>. |
| | | 1030 | | /// </returns> |
| | | 1031 | | /// <exception cref="ArgumentException"> |
| | | 1032 | | /// <paramref name="options"/> contains an unsupported combination of flags. |
| | | 1033 | | /// </exception> |
| | | 1034 | | public unsafe int IndexOf(ReadOnlySpan<char> source, Rune value, CompareOptions options = CompareOptions.None) |
| | | 1035 | | { |
| | 0 | 1036 | | Span<char> valueAsUtf16 = stackalloc char[Rune.MaxUtf16CharsPerRune]; |
| | 0 | 1037 | | int charCount = value.EncodeToUtf16(valueAsUtf16); |
| | 0 | 1038 | | return IndexOf(source, valueAsUtf16.Slice(0, charCount), options); |
| | | 1039 | | } |
| | | 1040 | | |
| | | 1041 | | /// <summary> |
| | | 1042 | | /// IndexOf overload used when the caller needs the length of the matching substring. |
| | | 1043 | | /// Caller needs to ensure <paramref name="matchLengthPtr"/> is non-null and points |
| | | 1044 | | /// to a valid address. This method will validate <paramref name="options"/>. |
| | | 1045 | | /// </summary> |
| | | 1046 | | private unsafe int IndexOf(ReadOnlySpan<char> source, ReadOnlySpan<char> value, int* matchLengthPtr, CompareOpti |
| | | 1047 | | { |
| | 0 | 1048 | | Debug.Assert(matchLengthPtr != null); |
| | 0 | 1049 | | *matchLengthPtr = 0; |
| | | 1050 | | |
| | 0 | 1051 | | int retVal = 0; |
| | | 1052 | | |
| | 0 | 1053 | | if ((options & ValidIndexMaskOffFlags) == 0) |
| | | 1054 | | { |
| | | 1055 | | // Common case: caller is attempting to perform a linguistic search. |
| | | 1056 | | // Pass the flags down to NLS or ICU unless we're running in invariant |
| | | 1057 | | // mode, at which point we normalize the flags to Ordinal[IgnoreCase]. |
| | | 1058 | | |
| | 0 | 1059 | | if (!GlobalizationMode.Invariant) |
| | | 1060 | | { |
| | 0 | 1061 | | if (value.IsEmpty) |
| | | 1062 | | { |
| | | 1063 | | // empty target substring trivially occurs at beginning / end of search space |
| | 0 | 1064 | | return (fromBeginning) ? 0 : source.Length; |
| | | 1065 | | } |
| | | 1066 | | else |
| | | 1067 | | { |
| | 0 | 1068 | | return IndexOfCore(source, value, options, matchLengthPtr, fromBeginning); |
| | | 1069 | | } |
| | | 1070 | | } |
| | | 1071 | | |
| | 0 | 1072 | | if ((options & CompareOptions.IgnoreCase) == 0) |
| | | 1073 | | { |
| | 0 | 1074 | | retVal = (fromBeginning) ? source.IndexOf(value) : source.LastIndexOf(value); |
| | | 1075 | | } |
| | | 1076 | | else |
| | | 1077 | | { |
| | 0 | 1078 | | retVal = fromBeginning ? Ordinal.IndexOfOrdinalIgnoreCase(source, value) : Ordinal.LastIndexOfOrdina |
| | | 1079 | | } |
| | | 1080 | | } |
| | | 1081 | | else |
| | | 1082 | | { |
| | | 1083 | | // Less common case: caller is attempting to perform non-linguistic comparison, |
| | | 1084 | | // or an invalid combination of flags was supplied. |
| | | 1085 | | |
| | 0 | 1086 | | if (options == CompareOptions.Ordinal) |
| | | 1087 | | { |
| | 0 | 1088 | | retVal = (fromBeginning) ? source.IndexOf(value) : source.LastIndexOf(value); |
| | | 1089 | | } |
| | 0 | 1090 | | else if (options == CompareOptions.OrdinalIgnoreCase) |
| | | 1091 | | { |
| | 0 | 1092 | | retVal = fromBeginning ? Ordinal.IndexOfOrdinalIgnoreCase(source, value) : Ordinal.LastIndexOfOrdina |
| | | 1093 | | } |
| | | 1094 | | else |
| | | 1095 | | { |
| | 0 | 1096 | | ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidFlag, ExceptionArgument.options |
| | | 1097 | | } |
| | | 1098 | | } |
| | | 1099 | | |
| | | 1100 | | // Both Ordinal and OrdinalIgnoreCase match by individual code points in a non-linguistic manner. |
| | | 1101 | | // Non-BMP code points will never match BMP code points, so given UTF-16 inputs the match length |
| | | 1102 | | // will always be equivalent to the target string length. |
| | | 1103 | | |
| | 0 | 1104 | | if (retVal >= 0) |
| | | 1105 | | { |
| | 0 | 1106 | | *matchLengthPtr = value.Length; |
| | | 1107 | | } |
| | 0 | 1108 | | return retVal; |
| | | 1109 | | } |
| | | 1110 | | |
| | | 1111 | | private unsafe int IndexOfCore(ReadOnlySpan<char> source, ReadOnlySpan<char> target, CompareOptions options, int |
| | 0 | 1112 | | GlobalizationMode.UseNls ? |
| | 0 | 1113 | | NlsIndexOfCore(source, target, options, matchLengthPtr, fromBeginning) : |
| | 0 | 1114 | | IcuIndexOfCore(source, target, options, matchLengthPtr, fromBeginning); |
| | | 1115 | | |
| | | 1116 | | /// <summary> |
| | | 1117 | | /// Returns the last index where value is found in string. The |
| | | 1118 | | /// search starts from startIndex and ends at endIndex. Returns -1 if |
| | | 1119 | | /// the specified value is not found. If value equals string.Empty, |
| | | 1120 | | /// endIndex is returned. Throws IndexOutOfRange if startIndex or |
| | | 1121 | | /// endIndex is less than zero or greater than the length of string. |
| | | 1122 | | /// Throws ArgumentException if value (as a string) is null. |
| | | 1123 | | /// </summary> |
| | | 1124 | | public int LastIndexOf(string source, char value) |
| | | 1125 | | { |
| | 0 | 1126 | | return LastIndexOf(source, value, CompareOptions.None); |
| | | 1127 | | } |
| | | 1128 | | |
| | | 1129 | | public int LastIndexOf(string source, string value) |
| | | 1130 | | { |
| | 0 | 1131 | | return LastIndexOf(source, value, CompareOptions.None); |
| | | 1132 | | } |
| | | 1133 | | |
| | | 1134 | | public int LastIndexOf(string source, char value, CompareOptions options) |
| | | 1135 | | { |
| | 0 | 1136 | | if (source == null) |
| | | 1137 | | { |
| | 0 | 1138 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); |
| | | 1139 | | } |
| | | 1140 | | |
| | 0 | 1141 | | return LastIndexOf(source, new ReadOnlySpan<char>(in value), options); |
| | | 1142 | | } |
| | | 1143 | | |
| | | 1144 | | public int LastIndexOf(string source, string value, CompareOptions options) |
| | | 1145 | | { |
| | 0 | 1146 | | if (source == null) |
| | | 1147 | | { |
| | 0 | 1148 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); |
| | | 1149 | | } |
| | 0 | 1150 | | if (value == null) |
| | | 1151 | | { |
| | 0 | 1152 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value); |
| | | 1153 | | } |
| | | 1154 | | |
| | 0 | 1155 | | return LastIndexOf(source.AsSpan(), value.AsSpan(), options); |
| | | 1156 | | } |
| | | 1157 | | |
| | | 1158 | | public int LastIndexOf(string source, char value, int startIndex) |
| | | 1159 | | { |
| | 0 | 1160 | | return LastIndexOf(source, value, startIndex, startIndex + 1, CompareOptions.None); |
| | | 1161 | | } |
| | | 1162 | | |
| | | 1163 | | public int LastIndexOf(string source, string value, int startIndex) |
| | | 1164 | | { |
| | 0 | 1165 | | return LastIndexOf(source, value, startIndex, startIndex + 1, CompareOptions.None); |
| | | 1166 | | } |
| | | 1167 | | |
| | | 1168 | | public int LastIndexOf(string source, char value, int startIndex, CompareOptions options) |
| | | 1169 | | { |
| | 0 | 1170 | | return LastIndexOf(source, value, startIndex, startIndex + 1, options); |
| | | 1171 | | } |
| | | 1172 | | |
| | | 1173 | | public int LastIndexOf(string source, string value, int startIndex, CompareOptions options) |
| | | 1174 | | { |
| | 0 | 1175 | | return LastIndexOf(source, value, startIndex, startIndex + 1, options); |
| | | 1176 | | } |
| | | 1177 | | |
| | | 1178 | | public int LastIndexOf(string source, char value, int startIndex, int count) |
| | | 1179 | | { |
| | 0 | 1180 | | return LastIndexOf(source, value, startIndex, count, CompareOptions.None); |
| | | 1181 | | } |
| | | 1182 | | |
| | | 1183 | | public int LastIndexOf(string source, string value, int startIndex, int count) |
| | | 1184 | | { |
| | 0 | 1185 | | return LastIndexOf(source, value, startIndex, count, CompareOptions.None); |
| | | 1186 | | } |
| | | 1187 | | |
| | | 1188 | | public int LastIndexOf(string source, char value, int startIndex, int count, CompareOptions options) |
| | | 1189 | | { |
| | 0 | 1190 | | if (source == null) |
| | | 1191 | | { |
| | 0 | 1192 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); |
| | | 1193 | | } |
| | | 1194 | | |
| | | 1195 | | TryAgain: |
| | | 1196 | | |
| | | 1197 | | // Previous versions of the Framework special-cased empty 'source' to allow startIndex = -1 or startIndex = |
| | | 1198 | | // ignoring 'count' and short-circuiting the entire operation. We'll silently fix up the 'count' parameter |
| | | 1199 | | // if this occurs. |
| | | 1200 | | // |
| | | 1201 | | // See the comments just before string.IndexOf(string) for more information on how these computations are |
| | | 1202 | | // performed. |
| | | 1203 | | |
| | 0 | 1204 | | if ((uint)startIndex >= (uint)source.Length) |
| | | 1205 | | { |
| | 0 | 1206 | | if (startIndex == -1 && source.Length == 0) |
| | | 1207 | | { |
| | 0 | 1208 | | count = 0; // normalize |
| | | 1209 | | } |
| | 0 | 1210 | | else if (startIndex == source.Length) |
| | | 1211 | | { |
| | | 1212 | | // The caller likely had an off-by-one error when invoking the API. The Framework has historically |
| | | 1213 | | // allowed for this and tried to fix up the parameters, so we'll continue to do so for compat. |
| | | 1214 | | |
| | 0 | 1215 | | startIndex--; |
| | 0 | 1216 | | if (count > 0) |
| | | 1217 | | { |
| | 0 | 1218 | | count--; |
| | | 1219 | | } |
| | | 1220 | | |
| | 0 | 1221 | | goto TryAgain; // guaranteed never to loop more than once |
| | | 1222 | | } |
| | | 1223 | | else |
| | | 1224 | | { |
| | 0 | 1225 | | ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.Argumen |
| | | 1226 | | } |
| | | 1227 | | } |
| | | 1228 | | |
| | 0 | 1229 | | startIndex = startIndex - count + 1; // this will be the actual index where we begin our search |
| | | 1230 | | |
| | 0 | 1231 | | if (!source.TryGetSpan(startIndex, count, out ReadOnlySpan<char> sourceSpan)) |
| | | 1232 | | { |
| | 0 | 1233 | | ThrowHelper.ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count(); |
| | | 1234 | | } |
| | | 1235 | | |
| | 0 | 1236 | | int retVal = LastIndexOf(sourceSpan, new ReadOnlySpan<char>(in value), options); |
| | 0 | 1237 | | if (retVal >= 0) |
| | | 1238 | | { |
| | 0 | 1239 | | retVal += startIndex; |
| | | 1240 | | } |
| | 0 | 1241 | | return retVal; |
| | | 1242 | | } |
| | | 1243 | | |
| | | 1244 | | public int LastIndexOf(string source, string value, int startIndex, int count, CompareOptions options) |
| | | 1245 | | { |
| | 0 | 1246 | | if (source == null) |
| | | 1247 | | { |
| | 0 | 1248 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); |
| | | 1249 | | } |
| | 0 | 1250 | | if (value == null) |
| | | 1251 | | { |
| | 0 | 1252 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value); |
| | | 1253 | | } |
| | | 1254 | | |
| | | 1255 | | TryAgain: |
| | | 1256 | | |
| | | 1257 | | // Previous versions of the Framework special-cased empty 'source' to allow startIndex = -1 or startIndex = |
| | | 1258 | | // ignoring 'count' and short-circuiting the entire operation. We'll silently fix up the 'count' parameter |
| | | 1259 | | // if this occurs. |
| | | 1260 | | // |
| | | 1261 | | // See the comments just before string.IndexOf(string) for more information on how these computations are |
| | | 1262 | | // performed. |
| | | 1263 | | |
| | 0 | 1264 | | if ((uint)startIndex >= (uint)source.Length) |
| | | 1265 | | { |
| | 0 | 1266 | | if (startIndex == -1 && source.Length == 0) |
| | | 1267 | | { |
| | 0 | 1268 | | count = 0; // normalize |
| | | 1269 | | } |
| | 0 | 1270 | | else if (startIndex == source.Length) |
| | | 1271 | | { |
| | | 1272 | | // The caller likely had an off-by-one error when invoking the API. The Framework has historically |
| | | 1273 | | // allowed for this and tried to fix up the parameters, so we'll continue to do so for compat. |
| | | 1274 | | |
| | 0 | 1275 | | startIndex--; |
| | 0 | 1276 | | if (count > 0) |
| | | 1277 | | { |
| | 0 | 1278 | | count--; |
| | | 1279 | | } |
| | | 1280 | | |
| | 0 | 1281 | | goto TryAgain; // guaranteed never to loop more than once |
| | | 1282 | | } |
| | | 1283 | | else |
| | | 1284 | | { |
| | 0 | 1285 | | ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.Argumen |
| | | 1286 | | } |
| | | 1287 | | } |
| | | 1288 | | |
| | 0 | 1289 | | startIndex = startIndex - count + 1; // this will be the actual index where we begin our search |
| | | 1290 | | |
| | 0 | 1291 | | if (!source.TryGetSpan(startIndex, count, out ReadOnlySpan<char> sourceSpan)) |
| | | 1292 | | { |
| | 0 | 1293 | | ThrowHelper.ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count(); |
| | | 1294 | | } |
| | | 1295 | | |
| | 0 | 1296 | | int retVal = LastIndexOf(sourceSpan, value, options); |
| | 0 | 1297 | | if (retVal >= 0) |
| | | 1298 | | { |
| | 0 | 1299 | | retVal += startIndex; |
| | | 1300 | | } |
| | 0 | 1301 | | return retVal; |
| | | 1302 | | } |
| | | 1303 | | |
| | | 1304 | | /// <summary> |
| | | 1305 | | /// Searches for the last occurrence of a substring within a source string. |
| | | 1306 | | /// </summary> |
| | | 1307 | | /// <param name="source">The string to search within.</param> |
| | | 1308 | | /// <param name="value">The substring to locate within <paramref name="source"/>.</param> |
| | | 1309 | | /// <param name="options">The <see cref="CompareOptions"/> to use during the search.</param> |
| | | 1310 | | /// <returns> |
| | | 1311 | | /// The zero-based index into <paramref name="source"/> where the substring <paramref name="value"/> |
| | | 1312 | | /// last appears; or -1 if <paramref name="value"/> cannot be found within <paramref name="source"/>. |
| | | 1313 | | /// </returns> |
| | | 1314 | | /// <exception cref="ArgumentException"> |
| | | 1315 | | /// <paramref name="options"/> contains an unsupported combination of flags. |
| | | 1316 | | /// </exception> |
| | | 1317 | | public unsafe int LastIndexOf(ReadOnlySpan<char> source, ReadOnlySpan<char> value, CompareOptions options = Comp |
| | | 1318 | | { |
| | 0 | 1319 | | if ((options & ValidIndexMaskOffFlags) == 0) |
| | | 1320 | | { |
| | | 1321 | | // Common case: caller is attempting to perform a linguistic search. |
| | | 1322 | | // Pass the flags down to NLS or ICU unless we're running in invariant |
| | | 1323 | | // mode, at which point we normalize the flags to Ordinal[IgnoreCase]. |
| | | 1324 | | |
| | 0 | 1325 | | if (!GlobalizationMode.Invariant) |
| | | 1326 | | { |
| | 0 | 1327 | | if (value.IsEmpty) |
| | | 1328 | | { |
| | 0 | 1329 | | return source.Length; // Empty target string trivially occurs at the last index of every search |
| | | 1330 | | } |
| | | 1331 | | else |
| | | 1332 | | { |
| | 0 | 1333 | | return IndexOfCore(source, value, options, matchLengthPtr: null, fromBeginning: false); |
| | | 1334 | | } |
| | | 1335 | | } |
| | | 1336 | | |
| | 0 | 1337 | | if ((options & CompareOptions.IgnoreCase) == 0) |
| | | 1338 | | { |
| | 0 | 1339 | | return source.LastIndexOf(value); |
| | | 1340 | | } |
| | | 1341 | | |
| | 0 | 1342 | | return Ordinal.LastIndexOfOrdinalIgnoreCase(source, value); |
| | | 1343 | | } |
| | | 1344 | | else |
| | | 1345 | | { |
| | | 1346 | | // Less common case: caller is attempting to perform non-linguistic comparison, |
| | | 1347 | | // or an invalid combination of flags was supplied. |
| | | 1348 | | |
| | 0 | 1349 | | if (options == CompareOptions.Ordinal) |
| | | 1350 | | { |
| | 0 | 1351 | | return source.LastIndexOf(value); |
| | | 1352 | | } |
| | | 1353 | | |
| | 0 | 1354 | | if (options == CompareOptions.OrdinalIgnoreCase) |
| | | 1355 | | { |
| | 0 | 1356 | | return Ordinal.LastIndexOfOrdinalIgnoreCase(source, value); |
| | | 1357 | | } |
| | | 1358 | | |
| | 0 | 1359 | | throw new ArgumentException(paramName: nameof(options), message: SR.Argument_InvalidFlag); |
| | | 1360 | | } |
| | | 1361 | | } |
| | | 1362 | | |
| | | 1363 | | /// <summary> |
| | | 1364 | | /// Searches for the last occurrence of a substring within a source string. |
| | | 1365 | | /// </summary> |
| | | 1366 | | /// <param name="source">The string to search within.</param> |
| | | 1367 | | /// <param name="value">The substring to locate within <paramref name="source"/>.</param> |
| | | 1368 | | /// <param name="options">The <see cref="CompareOptions"/> to use during the search.</param> |
| | | 1369 | | /// <param name="matchLength">When this method returns, contains the number of characters of |
| | | 1370 | | /// <paramref name="source"/> that matched the desired value. This may be different than the |
| | | 1371 | | /// length of <paramref name="value"/> if a linguistic comparison is performed. Set to 0 |
| | | 1372 | | /// if <paramref name="value"/> is not found within <paramref name="source"/>.</param> |
| | | 1373 | | /// <returns> |
| | | 1374 | | /// The zero-based index into <paramref name="source"/> where the substring <paramref name="value"/> |
| | | 1375 | | /// last appears; or -1 if <paramref name="value"/> cannot be found within <paramref name="source"/>. |
| | | 1376 | | /// </returns> |
| | | 1377 | | /// <exception cref="ArgumentException"> |
| | | 1378 | | /// <paramref name="options"/> contains an unsupported combination of flags. |
| | | 1379 | | /// </exception> |
| | | 1380 | | /// <remarks> |
| | | 1381 | | /// This method has greater overhead than other <see cref="IndexOf"/> overloads which don't |
| | | 1382 | | /// take a <paramref name="matchLength"/> argument. Call this overload only if you require |
| | | 1383 | | /// the match length information. |
| | | 1384 | | /// </remarks> |
| | | 1385 | | public unsafe int LastIndexOf(ReadOnlySpan<char> source, ReadOnlySpan<char> value, CompareOptions options, out i |
| | | 1386 | | { |
| | | 1387 | | int tempMatchLength; |
| | 0 | 1388 | | int retVal = IndexOf(source, value, &tempMatchLength, options, fromBeginning: false); |
| | 0 | 1389 | | matchLength = tempMatchLength; |
| | 0 | 1390 | | return retVal; |
| | | 1391 | | } |
| | | 1392 | | |
| | | 1393 | | /// <summary> |
| | | 1394 | | /// Searches for the last occurrence of a <see cref="Rune"/> within a source string. |
| | | 1395 | | /// </summary> |
| | | 1396 | | /// <param name="source">The string to search within.</param> |
| | | 1397 | | /// <param name="value">The <see cref="Rune"/> to locate within <paramref name="source"/>.</param> |
| | | 1398 | | /// <param name="options">The <see cref="CompareOptions"/> to use during the search.</param> |
| | | 1399 | | /// <returns> |
| | | 1400 | | /// The zero-based index into <paramref name="source"/> where <paramref name="value"/> |
| | | 1401 | | /// last appears; or -1 if <paramref name="value"/> cannot be found within <paramref name="source"/>. |
| | | 1402 | | /// </returns> |
| | | 1403 | | /// <exception cref="ArgumentException"> |
| | | 1404 | | /// <paramref name="options"/> contains an unsupported combination of flags. |
| | | 1405 | | /// </exception> |
| | | 1406 | | public unsafe int LastIndexOf(ReadOnlySpan<char> source, Rune value, CompareOptions options = CompareOptions.Non |
| | | 1407 | | { |
| | 0 | 1408 | | Span<char> valueAsUtf16 = stackalloc char[Rune.MaxUtf16CharsPerRune]; |
| | 0 | 1409 | | int charCount = value.EncodeToUtf16(valueAsUtf16); |
| | 0 | 1410 | | return LastIndexOf(source, valueAsUtf16.Slice(0, charCount), options); |
| | | 1411 | | } |
| | | 1412 | | |
| | | 1413 | | /// <summary> |
| | | 1414 | | /// Gets the SortKey for the given string with the given options. |
| | | 1415 | | /// </summary> |
| | | 1416 | | public SortKey GetSortKey(string source, CompareOptions options) |
| | | 1417 | | { |
| | 0 | 1418 | | if (GlobalizationMode.Invariant) |
| | | 1419 | | { |
| | 0 | 1420 | | return InvariantCreateSortKey(source, options); |
| | | 1421 | | } |
| | | 1422 | | |
| | 0 | 1423 | | return CreateSortKeyCore(source, options); |
| | | 1424 | | } |
| | | 1425 | | |
| | | 1426 | | public SortKey GetSortKey(string source) |
| | | 1427 | | { |
| | 0 | 1428 | | if (GlobalizationMode.Invariant) |
| | | 1429 | | { |
| | 0 | 1430 | | return InvariantCreateSortKey(source, CompareOptions.None); |
| | | 1431 | | } |
| | | 1432 | | |
| | 0 | 1433 | | return CreateSortKeyCore(source, CompareOptions.None); |
| | | 1434 | | } |
| | | 1435 | | |
| | | 1436 | | private SortKey CreateSortKeyCore(string source, CompareOptions options) => |
| | 0 | 1437 | | GlobalizationMode.UseNls ? |
| | 0 | 1438 | | NlsCreateSortKey(source, options) : |
| | 0 | 1439 | | IcuCreateSortKey(source, options); |
| | | 1440 | | |
| | | 1441 | | /// <summary> |
| | | 1442 | | /// Computes a sort key over the specified input. |
| | | 1443 | | /// </summary> |
| | | 1444 | | /// <param name="source">The text over which to compute the sort key.</param> |
| | | 1445 | | /// <param name="destination">The buffer into which to write the resulting sort key bytes.</param> |
| | | 1446 | | /// <param name="options">The <see cref="CompareOptions"/> used for computing the sort key.</param> |
| | | 1447 | | /// <returns>The number of bytes written to <paramref name="destination"/>.</returns> |
| | | 1448 | | /// <remarks> |
| | | 1449 | | /// Use <see cref="GetSortKeyLength"/> to query the required size of <paramref name="destination"/>. |
| | | 1450 | | /// It is acceptable to provide a larger-than-necessary output buffer to this method. |
| | | 1451 | | /// </remarks> |
| | | 1452 | | /// <exception cref="ArgumentException"> |
| | | 1453 | | /// <paramref name="destination"/> is too small to contain the resulting sort key; |
| | | 1454 | | /// or <paramref name="options"/> contains an unsupported flag; |
| | | 1455 | | /// or <paramref name="source"/> cannot be processed using the desired <see cref="CompareOptions"/> |
| | | 1456 | | /// under the current <see cref="CompareInfo"/>. |
| | | 1457 | | /// </exception> |
| | | 1458 | | public int GetSortKey(ReadOnlySpan<char> source, Span<byte> destination, CompareOptions options = CompareOptions |
| | | 1459 | | { |
| | 0 | 1460 | | if ((options & ValidCompareMaskOffFlags) != 0) |
| | | 1461 | | { |
| | 0 | 1462 | | ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidFlag, ExceptionArgument.options); |
| | | 1463 | | } |
| | | 1464 | | |
| | 0 | 1465 | | if (GlobalizationMode.Invariant) |
| | | 1466 | | { |
| | 0 | 1467 | | return InvariantGetSortKey(source, destination, options); |
| | | 1468 | | } |
| | | 1469 | | else |
| | | 1470 | | { |
| | 0 | 1471 | | return GetSortKeyCore(source, destination, options); |
| | | 1472 | | } |
| | | 1473 | | } |
| | | 1474 | | |
| | | 1475 | | private int GetSortKeyCore(ReadOnlySpan<char> source, Span<byte> destination, CompareOptions options) => |
| | 0 | 1476 | | GlobalizationMode.UseNls ? |
| | 0 | 1477 | | NlsGetSortKey(source, destination, options) : |
| | 0 | 1478 | | IcuGetSortKey(source, destination, options); |
| | | 1479 | | |
| | | 1480 | | /// <summary> |
| | | 1481 | | /// Returns the length (in bytes) of the sort key that would be produced from the specified input. |
| | | 1482 | | /// </summary> |
| | | 1483 | | /// <param name="source">The text over which to compute the sort key.</param> |
| | | 1484 | | /// <param name="options">The <see cref="CompareOptions"/> used for computing the sort key.</param> |
| | | 1485 | | /// <returns>The length (in bytes) of the sort key.</returns> |
| | | 1486 | | /// <exception cref="ArgumentException"> |
| | | 1487 | | /// <paramref name="options"/> contains an unsupported flag; |
| | | 1488 | | /// or <paramref name="source"/> cannot be processed using the desired <see cref="CompareOptions"/> |
| | | 1489 | | /// under the current <see cref="CompareInfo"/>. |
| | | 1490 | | /// </exception> |
| | | 1491 | | public int GetSortKeyLength(ReadOnlySpan<char> source, CompareOptions options = CompareOptions.None) |
| | | 1492 | | { |
| | 0 | 1493 | | if ((options & ValidCompareMaskOffFlags) != 0) |
| | | 1494 | | { |
| | 0 | 1495 | | ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidFlag, ExceptionArgument.options); |
| | | 1496 | | } |
| | | 1497 | | |
| | 0 | 1498 | | if (GlobalizationMode.Invariant) |
| | | 1499 | | { |
| | 0 | 1500 | | return InvariantGetSortKeyLength(source, options); |
| | | 1501 | | } |
| | | 1502 | | else |
| | | 1503 | | { |
| | 0 | 1504 | | return GetSortKeyLengthCore(source, options); |
| | | 1505 | | } |
| | | 1506 | | } |
| | | 1507 | | |
| | | 1508 | | private int GetSortKeyLengthCore(ReadOnlySpan<char> source, CompareOptions options) => |
| | 0 | 1509 | | GlobalizationMode.UseNls ? |
| | 0 | 1510 | | NlsGetSortKeyLength(source, options) : |
| | 0 | 1511 | | IcuGetSortKeyLength(source, options); |
| | | 1512 | | |
| | | 1513 | | public override bool Equals([NotNullWhen(true)] object? value) |
| | | 1514 | | { |
| | 0 | 1515 | | return value is CompareInfo otherCompareInfo |
| | 0 | 1516 | | && Name == otherCompareInfo.Name; |
| | | 1517 | | } |
| | | 1518 | | |
| | 0 | 1519 | | public override int GetHashCode() => Name.GetHashCode(); |
| | | 1520 | | |
| | | 1521 | | /// <summary> |
| | | 1522 | | /// This method performs the equivalent of of creating a Sortkey for a string from CompareInfo, |
| | | 1523 | | /// then generates a randomized hashcode value from the sort key. |
| | | 1524 | | /// |
| | | 1525 | | /// The hash code is guaranteed to be the same for string A and B where A.Equals(B) is true and both |
| | | 1526 | | /// the CompareInfo and the CompareOptions are the same. If two different CompareInfo objects |
| | | 1527 | | /// treat the string the same way, this implementation will treat them differently (the same way that |
| | | 1528 | | /// Sortkey does at the moment). |
| | | 1529 | | /// </summary> |
| | | 1530 | | public int GetHashCode(string source, CompareOptions options) |
| | | 1531 | | { |
| | 0 | 1532 | | if (source == null) |
| | | 1533 | | { |
| | 0 | 1534 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); |
| | | 1535 | | } |
| | | 1536 | | |
| | 0 | 1537 | | return GetHashCode(source.AsSpan(), options); |
| | | 1538 | | } |
| | | 1539 | | |
| | | 1540 | | public int GetHashCode(ReadOnlySpan<char> source, CompareOptions options) |
| | | 1541 | | { |
| | 0 | 1542 | | if ((options & ValidCompareMaskOffFlags) == 0) |
| | | 1543 | | { |
| | | 1544 | | // Common case: caller is attempting to get a linguistic sort key. |
| | | 1545 | | // Pass the flags down to NLS or ICU unless we're running in invariant |
| | | 1546 | | // mode, at which point we normalize the flags to Ordinal[IgnoreCase]. |
| | | 1547 | | |
| | 0 | 1548 | | if (!GlobalizationMode.Invariant) |
| | | 1549 | | { |
| | 0 | 1550 | | return GetHashCodeOfStringCore(source, options); |
| | | 1551 | | } |
| | | 1552 | | |
| | 0 | 1553 | | return InvariantGetHashCode(source, options); |
| | | 1554 | | } |
| | | 1555 | | else |
| | | 1556 | | { |
| | | 1557 | | // Less common case: caller is attempting to get a non-linguistic sort key, |
| | | 1558 | | // or an invalid combination of flags was supplied. |
| | | 1559 | | |
| | 0 | 1560 | | if (options == CompareOptions.Ordinal) |
| | | 1561 | | { |
| | 0 | 1562 | | return string.GetHashCode(source); |
| | | 1563 | | } |
| | | 1564 | | |
| | 0 | 1565 | | if (options == CompareOptions.OrdinalIgnoreCase) |
| | | 1566 | | { |
| | 0 | 1567 | | return string.GetHashCodeOrdinalIgnoreCase(source); |
| | | 1568 | | } |
| | | 1569 | | |
| | 0 | 1570 | | ThrowCompareOptionsCheckFailed(options); |
| | | 1571 | | |
| | | 1572 | | return -1; // make the compiler happy; |
| | | 1573 | | } |
| | | 1574 | | } |
| | | 1575 | | |
| | | 1576 | | private int GetHashCodeOfStringCore(ReadOnlySpan<char> source, CompareOptions options) => |
| | 0 | 1577 | | GlobalizationMode.UseNls ? |
| | 0 | 1578 | | NlsGetHashCodeOfString(source, options) : |
| | 0 | 1579 | | IcuGetHashCodeOfString(source, options); |
| | | 1580 | | |
| | 0 | 1581 | | public override string ToString() => "CompareInfo - " + Name; |
| | | 1582 | | |
| | | 1583 | | public SortVersion Version |
| | | 1584 | | { |
| | | 1585 | | get |
| | | 1586 | | { |
| | 0 | 1587 | | if (m_SortVersion == null) |
| | | 1588 | | { |
| | 0 | 1589 | | if (GlobalizationMode.Invariant) |
| | | 1590 | | { |
| | 0 | 1591 | | m_SortVersion = new SortVersion(0, CultureInfo.LOCALE_INVARIANT, new Guid(0, 0, 0, 0, 0, 0, 0, |
| | 0 | 1592 | | (byte)(CultureInfo.LOCALE_INVARIANT >> 24), |
| | 0 | 1593 | | (byte)((CultureInfo.LOCALE_INVARIANT & 0x00FF000 |
| | 0 | 1594 | | (byte)((CultureInfo.LOCALE_INVARIANT & 0x0000FF0 |
| | 0 | 1595 | | (byte)(CultureInfo.LOCALE_INVARIANT & 0xFF))); |
| | | 1596 | | } |
| | | 1597 | | else |
| | | 1598 | | { |
| | | 1599 | | #if TARGET_MACCATALYST || TARGET_IOS || TARGET_TVOS |
| | | 1600 | | if (GlobalizationMode.Hybrid) |
| | | 1601 | | { |
| | | 1602 | | throw new PlatformNotSupportedException(GetPNSEText("SortVersion")); |
| | | 1603 | | } |
| | | 1604 | | #endif |
| | 0 | 1605 | | m_SortVersion = GlobalizationMode.UseNls ? NlsGetSortVersion() : IcuGetSortVersion(); |
| | | 1606 | | } |
| | | 1607 | | } |
| | | 1608 | | |
| | 0 | 1609 | | return m_SortVersion; |
| | | 1610 | | } |
| | | 1611 | | } |
| | | 1612 | | |
| | 0 | 1613 | | public int LCID => CultureInfo.GetCultureInfo(Name).LCID; |
| | | 1614 | | |
| | | 1615 | | #if TARGET_MACCATALYST || TARGET_IOS || TARGET_TVOS |
| | | 1616 | | private static string GetPNSEText(string funcName) => SR.Format(SR.PlatformNotSupported_HybridGlobalization, fun |
| | | 1617 | | private static string GetPNSEWithReason(string funcName, string reason) => SR.Format(SR.PlatformNotSupportedWith |
| | | 1618 | | #endif |
| | | 1619 | | } |
| | | 1620 | | } |
| | | 1621 | | |