| | | 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 | | |
| | | 7 | | namespace System.Globalization |
| | | 8 | | { |
| | | 9 | | /// <summary>Represents the result of mapping a string to its sort key.</summary> |
| | | 10 | | public sealed partial class SortKey |
| | | 11 | | { |
| | | 12 | | private readonly CompareInfo _compareInfo; |
| | | 13 | | private readonly CompareOptions _options; |
| | | 14 | | private readonly string _string; |
| | | 15 | | private readonly byte[] _keyData; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// The following constructor is designed to be called from CompareInfo to get the |
| | | 19 | | /// the sort key of specific string for synthetic culture |
| | | 20 | | /// </summary> |
| | 0 | 21 | | internal SortKey(CompareInfo compareInfo, string str, CompareOptions options, byte[] keyData) |
| | | 22 | | { |
| | 0 | 23 | | _keyData = keyData; |
| | 0 | 24 | | _compareInfo = compareInfo; |
| | 0 | 25 | | _options = options; |
| | 0 | 26 | | _string = str; |
| | 0 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Returns the original string used to create the current instance |
| | | 31 | | /// of SortKey. |
| | | 32 | | /// </summary> |
| | 0 | 33 | | public string OriginalString => _string; |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Returns a byte array representing the current instance of the |
| | | 37 | | /// sort key. |
| | | 38 | | /// </summary> |
| | 0 | 39 | | public byte[] KeyData => (byte[])_keyData.Clone(); |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Compares the two sort keys. Returns 0 if the two sort keys are |
| | | 43 | | /// equal, a number less than 0 if sortkey1 is less than sortkey2, |
| | | 44 | | /// and a number greater than 0 if sortkey1 is greater than sortkey2. |
| | | 45 | | /// </summary> |
| | | 46 | | public static int Compare(SortKey sortkey1, SortKey sortkey2) |
| | | 47 | | { |
| | 0 | 48 | | ArgumentNullException.ThrowIfNull(sortkey1); |
| | 0 | 49 | | ArgumentNullException.ThrowIfNull(sortkey2); |
| | | 50 | | |
| | 0 | 51 | | byte[] key1Data = sortkey1._keyData; |
| | 0 | 52 | | byte[] key2Data = sortkey2._keyData; |
| | | 53 | | |
| | 0 | 54 | | Debug.Assert(key1Data != null); |
| | 0 | 55 | | Debug.Assert(key2Data != null); |
| | | 56 | | |
| | | 57 | | // SortKey comparisons are done as an ordinal comparison by the raw sort key bytes. |
| | | 58 | | |
| | 0 | 59 | | return new ReadOnlySpan<byte>(key1Data).SequenceCompareTo(key2Data); |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | public override bool Equals([NotNullWhen(true)] object? value) => |
| | 0 | 63 | | value is SortKey other && new ReadOnlySpan<byte>(_keyData).SequenceEqual(other._keyData); |
| | | 64 | | |
| | 0 | 65 | | public override int GetHashCode() => _compareInfo.GetHashCode(_string, _options); |
| | | 66 | | |
| | 0 | 67 | | public override string ToString() => $"SortKey - {_compareInfo.Name}, {_options}, {_string}"; |
| | | 68 | | } |
| | | 69 | | } |
| | | 70 | | |