| | 1 | | // <copyright file="Utilities.cs" company="Jérémy Ansel"> |
| | 2 | | // Copyright (c) 2015, 2019 Jérémy Ansel |
| | 3 | | // </copyright> |
| | 4 | | // <license> |
| | 5 | | // Licensed under the MIT license. See LICENSE.txt |
| | 6 | | // </license> |
| | 7 | |
|
| | 8 | | namespace JeremyAnsel.IO.Locator |
| | 9 | | { |
| | 10 | | using System; |
| | 11 | | using System.IO; |
| | 12 | |
|
| | 13 | | /// <summary> |
| | 14 | | /// Utility methods. |
| | 15 | | /// </summary> |
| | 16 | | internal static class Utilities |
| | 17 | | { |
| | 18 | | /// <summary> |
| | 19 | | /// Normalizes a path. |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="path">The path.</param> |
| | 22 | | /// <returns>A normalized path.</returns> |
| | 23 | | public static string PathNormalize(string? path) |
| | 24 | | { |
| 0 | 25 | | if (string.IsNullOrEmpty(path)) |
| | 26 | | { |
| 0 | 27 | | return string.Empty; |
| | 28 | | } |
| | 29 | |
|
| 0 | 30 | | return path! |
| 0 | 31 | | .Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar) |
| 0 | 32 | | .TrimStart(Path.DirectorySeparatorChar); |
| | 33 | | } |
| | 34 | |
|
| | 35 | | /// <summary> |
| | 36 | | /// Indicates whether two paths are equals. |
| | 37 | | /// </summary> |
| | 38 | | /// <param name="path1">The first path.</param> |
| | 39 | | /// <param name="path2">The second path.</param> |
| | 40 | | /// <returns>A boolean.</returns> |
| | 41 | | public static bool PathEquals(string? path1, string? path2) |
| | 42 | | { |
| 0 | 43 | | path1 = Utilities.PathNormalize(path1); |
| 0 | 44 | | path2 = Utilities.PathNormalize(path2); |
| | 45 | |
|
| 0 | 46 | | return string.Equals(path1, path2, StringComparison.OrdinalIgnoreCase); |
| | 47 | | } |
| | 48 | |
|
| | 49 | | /// <summary> |
| | 50 | | /// Indicates whether a path starts with a specified path. |
| | 51 | | /// </summary> |
| | 52 | | /// <param name="path1">The first path.</param> |
| | 53 | | /// <param name="path2">The second path.</param> |
| | 54 | | /// <returns>A boolean.</returns> |
| | 55 | | public static bool PathStartsWith(string? path1, string? path2) |
| | 56 | | { |
| 0 | 57 | | path1 = Utilities.PathNormalize(path1); |
| 0 | 58 | | path2 = Utilities.PathNormalize(path2); |
| | 59 | |
|
| 0 | 60 | | return path1.StartsWith(path2, StringComparison.OrdinalIgnoreCase); |
| | 61 | | } |
| | 62 | | } |
| | 63 | | } |