|
| 1 | +#region -- License Terms -- |
| 2 | +// |
| 3 | +// MessagePack for CLI |
| 4 | +// |
| 5 | +// Copyright (C) 2014 FUJIWARA, Yusuke |
| 6 | +// |
| 7 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +// you may not use this file except in compliance with the License. |
| 9 | +// You may obtain a copy of the License at |
| 10 | +// |
| 11 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +// |
| 13 | +// Unless required by applicable law or agreed to in writing, software |
| 14 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +// See the License for the specific language governing permissions and |
| 17 | +// limitations under the License. |
| 18 | +// |
| 19 | +#endregion -- License Terms -- |
| 20 | + |
| 21 | +using System; |
| 22 | +using System.Collections.Generic; |
| 23 | +using System.Globalization; |
| 24 | +using System.IO; |
| 25 | + |
| 26 | +namespace mpu |
| 27 | +{ |
| 28 | + internal abstract class PathEqualityComparer : IEqualityComparer<string> |
| 29 | + { |
| 30 | + private static readonly PathEqualityComparer _instance = DetermineInstance(); |
| 31 | + |
| 32 | + private static PathEqualityComparer DetermineInstance() |
| 33 | + { |
| 34 | + switch ( Environment.OSVersion.Platform ) |
| 35 | + { |
| 36 | + case PlatformID.Win32NT: |
| 37 | + case PlatformID.Win32S: |
| 38 | + case PlatformID.Win32Windows: |
| 39 | + case PlatformID.WinCE: |
| 40 | + case PlatformID.Xbox: |
| 41 | + { |
| 42 | + return OrdinalPathEqualityComparer.CaseInsensitive; |
| 43 | + } |
| 44 | + case PlatformID.MacOSX: // Mac's NFD problem may not affect in assembly probling... |
| 45 | + case PlatformID.Unix: |
| 46 | + { |
| 47 | + return OrdinalPathEqualityComparer.CaseSensitive; |
| 48 | + } |
| 49 | + default: |
| 50 | + { |
| 51 | + throw new PlatformNotSupportedException(String.Format( CultureInfo.CurrentCulture, "Unknown platform '{0:G}({0:D})'.", Environment.OSVersion.Platform )); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public static PathEqualityComparer Instance |
| 57 | + { |
| 58 | + get { return _instance; } |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + public bool Equals( string x, string y ) |
| 63 | + { |
| 64 | + return EqualsCore( SafeGetFullPath( x ), SafeGetFullPath( y ) ); |
| 65 | + } |
| 66 | + |
| 67 | + protected abstract bool EqualsCore( string x, string y ); |
| 68 | + |
| 69 | + public int GetHashCode( string obj ) |
| 70 | + { |
| 71 | + return SafeGetFullPath( obj ).GetHashCode(); |
| 72 | + } |
| 73 | + |
| 74 | + private static string SafeGetFullPath( string mayBePath ) |
| 75 | + { |
| 76 | + if ( String.IsNullOrEmpty( mayBePath ) ) |
| 77 | + { |
| 78 | + return String.Empty; |
| 79 | + } |
| 80 | + |
| 81 | + return Path.GetFullPath( mayBePath.Trim() ).Replace( Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar ); |
| 82 | + } |
| 83 | + |
| 84 | + private sealed class OrdinalPathEqualityComparer : PathEqualityComparer |
| 85 | + { |
| 86 | + public static readonly OrdinalPathEqualityComparer CaseSensitive = |
| 87 | + new OrdinalPathEqualityComparer( StringComparer.Ordinal ); |
| 88 | + |
| 89 | + public static readonly OrdinalPathEqualityComparer CaseInsensitive = |
| 90 | + new OrdinalPathEqualityComparer( StringComparer.OrdinalIgnoreCase ); |
| 91 | + |
| 92 | + private readonly IEqualityComparer<string> _pathComparer; |
| 93 | + |
| 94 | + private OrdinalPathEqualityComparer( IEqualityComparer<string> pathComparer ) |
| 95 | + { |
| 96 | + this._pathComparer = pathComparer; |
| 97 | + } |
| 98 | + |
| 99 | + protected override bool EqualsCore( string x, string y ) |
| 100 | + { |
| 101 | + return this._pathComparer.Equals( x, y ); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments