From 7065b0c567b6ad4abf22e5cf631b804012107ce2 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Mon, 16 Apr 2018 16:54:03 -0400 Subject: [PATCH] feat: Adding util methods for path comparisons --- ICD.Common.Utils/IO/IcdPath.cs | 4 ++ ICD.Common.Utils/PathUtils.cs | 18 +++++++++ ICD.Common.Utils/StringUtils.cs | 72 +++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+) diff --git a/ICD.Common.Utils/IO/IcdPath.cs b/ICD.Common.Utils/IO/IcdPath.cs index 37d0bad..6ee4a52 100644 --- a/ICD.Common.Utils/IO/IcdPath.cs +++ b/ICD.Common.Utils/IO/IcdPath.cs @@ -10,6 +10,10 @@ namespace ICD.Common.Utils.IO { public static class IcdPath { + public static char DirectorySeparatorChar { get { return Path.DirectorySeparatorChar; } } + + public static char AltDirectorySeparatorChar { get { return Path.AltDirectorySeparatorChar; } } + public static string GetFileName(string path) { if (path == null) diff --git a/ICD.Common.Utils/PathUtils.cs b/ICD.Common.Utils/PathUtils.cs index 7a691e1..3d473a6 100644 --- a/ICD.Common.Utils/PathUtils.cs +++ b/ICD.Common.Utils/PathUtils.cs @@ -185,6 +185,24 @@ namespace ICD.Common.Utils return IcdFile.Exists(path) || IcdDirectory.Exists(path); } + /// + /// Returns the path if the given path is already a directory or has a trailing slash. + /// Otherwise returns the parent directory name. + /// + /// + /// + [PublicAPI] + public static string GetDirectoryNameFromPath(string path) + { + if (IcdDirectory.Exists(path)) + return path; + + if (path.EndsWith(IcdPath.DirectorySeparatorChar) || path.EndsWith(IcdPath.AltDirectorySeparatorChar)) + return path; + + return IcdPath.GetDirectoryName(path); + } + #endregion } } diff --git a/ICD.Common.Utils/StringUtils.cs b/ICD.Common.Utils/StringUtils.cs index 36008e7..3235b7b 100644 --- a/ICD.Common.Utils/StringUtils.cs +++ b/ICD.Common.Utils/StringUtils.cs @@ -610,5 +610,77 @@ namespace ICD.Common.Utils { return value == null ? null : value.ToUpper(); } + + /// + /// Compares the given chars for equality. + /// + /// + /// + /// + /// + [PublicAPI] + public static bool Compare(char a, char b, bool ignoreCase) + { + if (ignoreCase) + { + a = char.ToUpper(a, CultureInfo.InvariantCulture); + b = char.ToUpper(b, CultureInfo.InvariantCulture); + } + + return a == b; + } + + /// + /// Find the longest common string between the matches. + /// E.g. + /// + /// C:\\Workspace + /// C:\\Workshop + /// + /// Results in + /// + /// C:\\Work + /// + /// + /// + /// + [PublicAPI] + public static string GetLongestCommonIntersectionFromStart(IEnumerable items, bool ignoreCase) + { + if (items == null) + throw new ArgumentNullException("items"); + + string output = null; + + foreach (string item in items) + { + // If there is a null in the sequence that's the best match we can make + if (string.IsNullOrEmpty(item)) + return null; + + // Seed our first item + if (output == null) + { + output = item; + continue; + } + + // Find the common substring + for (int index = 0; index < output.Length; index++) + { + if (index >= item.Length || !Compare(output[index], item[index], ignoreCase)) + { + output = output.Substring(0, index); + break; + } + } + + // Abandon the search if there is no common substring + if (string.IsNullOrEmpty(output)) + break; + } + + return output; + } } }