| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | // Copied from https://github.com/aspnet/Extensions/tree/master/src/Primitives/src |
| | | 5 | | |
| | | 6 | | using System; |
| | | 7 | | using System.Collections.Generic; |
| | | 8 | | |
| | | 9 | | namespace Azure.Core.Http.Multipart |
| | | 10 | | { |
| | | 11 | | internal class StringSegmentComparer : IComparer<StringSegment>, IEqualityComparer<StringSegment> |
| | | 12 | | { |
| | 0 | 13 | | public static StringSegmentComparer Ordinal { get; } |
| | 0 | 14 | | = new StringSegmentComparer(StringComparison.Ordinal, StringComparer.Ordinal); |
| | | 15 | | |
| | 0 | 16 | | public static StringSegmentComparer OrdinalIgnoreCase { get; } |
| | 0 | 17 | | = new StringSegmentComparer(StringComparison.OrdinalIgnoreCase, StringComparer.OrdinalIgnoreCase); |
| | | 18 | | |
| | 0 | 19 | | private StringSegmentComparer(StringComparison comparison, StringComparer comparer) |
| | | 20 | | { |
| | 0 | 21 | | Comparison = comparison; |
| | 0 | 22 | | Comparer = comparer; |
| | 0 | 23 | | } |
| | | 24 | | |
| | 0 | 25 | | private StringComparison Comparison { get; } |
| | 0 | 26 | | private StringComparer Comparer { get; } |
| | | 27 | | |
| | | 28 | | public int Compare(StringSegment x, StringSegment y) |
| | | 29 | | { |
| | 0 | 30 | | return StringSegment.Compare(x, y, Comparison); |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | public bool Equals(StringSegment x, StringSegment y) |
| | | 34 | | { |
| | 0 | 35 | | return StringSegment.Equals(x, y, Comparison); |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | public int GetHashCode(StringSegment obj) |
| | | 39 | | { |
| | | 40 | | #if NETCOREAPP |
| | | 41 | | return string.GetHashCode(obj.AsSpan(), Comparison); |
| | | 42 | | #else |
| | 0 | 43 | | if (!obj.HasValue) |
| | | 44 | | { |
| | 0 | 45 | | return 0; |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | // .NET Core strings use randomized hash codes for security reasons. Consequently we must materialize the St |
| | 0 | 49 | | return Comparer.GetHashCode(obj.Value); |
| | | 50 | | #endif |
| | | 51 | | } |
| | | 52 | | } |
| | | 53 | | } |