From 59de2aeecb87d23e892c7589ae3b5d89e90850c8 Mon Sep 17 00:00:00 2001 From: Waldi Ravens Date: Wed, 8 May 2019 09:00:03 +0200 Subject: [PATCH] Add FilePathComparer (Unix, Windows, Native) --- libse/FilePathComparer.cs | 69 +++++++++++++++++++++++++++++++++++++++ libse/LibSE.csproj | 1 + 2 files changed, 70 insertions(+) create mode 100644 libse/FilePathComparer.cs diff --git a/libse/FilePathComparer.cs b/libse/FilePathComparer.cs new file mode 100644 index 000000000..e1c7f612f --- /dev/null +++ b/libse/FilePathComparer.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.IO; + +namespace Nikse.SubtitleEdit.Core +{ + public abstract class FilePathComparer : IEqualityComparer, IComparer + { + private sealed class WindowsFilePathComparer: FilePathComparer + { + public override int Compare(string path1, string path2) + { + return ReferenceEquals(path1, path2) + ? 0 + : ReferenceEquals(path2, null) + ? 1 + : ReferenceEquals(path1, null) + ? -1 + : string.Compare(path1.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar), path2.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar), StringComparison.OrdinalIgnoreCase); + } + + public override bool Equals(string path1, string path2) + { + return ReferenceEquals(path1, path2) || + (!(ReferenceEquals(path1, null) || ReferenceEquals(path2, null)) && + string.Equals(path1.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar), path2.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar), StringComparison.OrdinalIgnoreCase)); + } + + public override int GetHashCode(string path) + { + return path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar).ToUpperInvariant().GetHashCode(); + } + } + + private sealed class UnixFilePathComparer : FilePathComparer + { + public override int Compare(string path1, string path2) + { + return ReferenceEquals(path1, path2) + ? 0 + : ReferenceEquals(path2, null) + ? 1 + : ReferenceEquals(path1, null) + ? -1 + : string.Compare(path1.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar), path2.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar), StringComparison.Ordinal); + } + + public override bool Equals(string path1, string path2) + { + return ReferenceEquals(path1, path2) || + (!(ReferenceEquals(path1, null) || ReferenceEquals(path2, null)) && + string.Equals(path1.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar), path2.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar), StringComparison.Ordinal)); + } + + public override int GetHashCode(string path) + { + return path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar).GetHashCode(); + } + } + + public static FilePathComparer Native => Configuration.IsRunningOnWindows ? Windows : Unix; + public static FilePathComparer Windows => new WindowsFilePathComparer(); + public static FilePathComparer Unix => new UnixFilePathComparer(); + + public abstract int Compare(string path1, string path2); + public abstract bool Equals(string path1, string path2); + public abstract int GetHashCode(string path); + } +} diff --git a/libse/LibSE.csproj b/libse/LibSE.csproj index 192ca2cc5..a9054bdb6 100644 --- a/libse/LibSE.csproj +++ b/libse/LibSE.csproj @@ -144,6 +144,7 @@ +