Horizon – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System; |
2 | using System.IO; |
||
3 | |||
4 | namespace Horizon.Utilities |
||
5 | { |
||
6 | /// <summary> |
||
7 | /// Various extensions for dealing with subpaths. |
||
8 | /// </summary> |
||
9 | /// <remarks>https://stackoverflow.com/questions/5617320/given-full-path-check-if-path-is-subdirectory-of-some-other-path-or-otherwise</remarks> |
||
10 | public static class Subpaths |
||
11 | { |
||
12 | #region Public Methods |
||
13 | |||
14 | public static string NormalizePath(this string path) |
||
15 | { |
||
16 | return Path.GetFullPath(path.Replace('/', '\\').WithEnding("\\")); |
||
17 | } |
||
18 | |||
19 | /// <summary> |
||
20 | /// Returns true if <paramref name="path" /> starts with the path <paramref name="baseDirPath" />. |
||
21 | /// The comparison is case-insensitive, handles / and \ slashes as folder separators and |
||
22 | /// only matches if the base dir folder name is matched exactly ("c:\foobar\file.txt" is not a sub path of "c:\foo"). |
||
23 | /// </summary> |
||
24 | public static bool IsSubPathOf(this string path, string baseDirPath) |
||
25 | { |
||
26 | var normalizedPath = NormalizePath(path); |
||
27 | |||
28 | var normalizedBaseDirPath = NormalizePath(baseDirPath); |
||
29 | |||
30 | return normalizedPath.StartsWith(normalizedBaseDirPath, StringComparison.OrdinalIgnoreCase); |
||
31 | } |
||
32 | |||
33 | public static bool IsPathEqual(this string path, string that) |
||
34 | { |
||
35 | return string.Equals(NormalizePath(path), NormalizePath(that), StringComparison.OrdinalIgnoreCase); |
||
36 | } |
||
37 | |||
38 | #endregion |
||
39 | |||
40 | #region Private Methods |
||
41 | |||
42 | /// <summary> |
||
43 | /// Returns <paramref name="str" /> with the minimal concatenation of <paramref name="ending" /> (starting from end) |
||
44 | /// that |
||
45 | /// results in satisfying .EndsWith(ending). |
||
46 | /// </summary> |
||
47 | /// <example>"hel".WithEnding("llo") returns "hello", which is the result of "hel" + "lo".</example> |
||
48 | private static string WithEnding(this string str, string ending) |
||
49 | { |
||
50 | if (str == null) |
||
51 | { |
||
52 | return ending; |
||
53 | } |
||
54 | |||
55 | var result = str; |
||
56 | |||
57 | // Right() is 1-indexed, so include these cases |
||
58 | // * Append no characters |
||
59 | // * Append up to N characters, where N is ending length |
||
60 | for (var i = 0; i <= ending.Length; i++) |
||
61 | { |
||
62 | var tmp = result + ending.Right(i); |
||
63 | if (tmp.EndsWith(ending)) |
||
64 | { |
||
65 | return tmp; |
||
66 | } |
||
67 | } |
||
68 | |||
69 | return result; |
||
70 | } |
||
71 | |||
72 | /// <summary>Gets the rightmost <paramref name="length" /> characters from a string.</summary> |
||
73 | /// <param name="value">The string to retrieve the substring from.</param> |
||
74 | /// <param name="length">The number of characters to retrieve.</param> |
||
75 | /// <returns>The substring.</returns> |
||
76 | private static string Right(this string value, int length) |
||
77 | { |
||
78 | if (value == null) |
||
79 | { |
||
80 | throw new ArgumentNullException("value"); |
||
81 | } |
||
82 | |||
83 | if (length < 0) |
||
84 | { |
||
85 | throw new ArgumentOutOfRangeException("length", length, "Length is less than zero"); |
||
86 | } |
||
87 | |||
88 | return length < value.Length ? value.Substring(value.Length - length) : value; |
||
89 | } |
||
90 | |||
91 | #endregion |
||
92 | } |
||
93 | } |