diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2020-09-05 14:51:52 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2020-09-05 14:51:52 -0400 |
commit | be1df8e7050ff5872799f6bee7f8cb419d7a3f38 (patch) | |
tree | 3906e8f08b8bebb22754ca295fdda8eda428515c /src/SMAPI.Toolkit/Utilities/PathUtilities.cs | |
parent | 5d1c77884f6686a59f121639dc177b7095e8c477 (diff) | |
download | SMAPI-be1df8e7050ff5872799f6bee7f8cb419d7a3f38.tar.gz SMAPI-be1df8e7050ff5872799f6bee7f8cb419d7a3f38.tar.bz2 SMAPI-be1df8e7050ff5872799f6bee7f8cb419d7a3f38.zip |
simplify path separator normalization
It no longer tries to clean up the path (e.g. "path/to///file/" => "path/to/file"), which means it can more intuitively handle cases like this:
asset.AssetName.StartsWith(PathUtilities.NormalizePathSeparators("Characters/Dialogue/"))
Diffstat (limited to 'src/SMAPI.Toolkit/Utilities/PathUtilities.cs')
-rw-r--r-- | src/SMAPI.Toolkit/Utilities/PathUtilities.cs | 32 |
1 files changed, 13 insertions, 19 deletions
diff --git a/src/SMAPI.Toolkit/Utilities/PathUtilities.cs b/src/SMAPI.Toolkit/Utilities/PathUtilities.cs index 34940d4f..bd5fafbc 100644 --- a/src/SMAPI.Toolkit/Utilities/PathUtilities.cs +++ b/src/SMAPI.Toolkit/Utilities/PathUtilities.cs @@ -45,18 +45,10 @@ namespace StardewModdingAPI.Toolkit.Utilities [Pure] public static string NormalizePathSeparators(string path) { - string normalized = string.Join(PathUtilities.PreferredPathSeparator, PathUtilities.GetSegments(path)); - - // keep root -#if SMAPI_FOR_WINDOWS - if (path.StartsWith(PathUtilities.WindowsUncRoot)) - normalized = PathUtilities.WindowsUncRoot + normalized; - else -#endif - if (path.StartsWith(PathUtilities.PreferredPathSeparator) || path.StartsWith(PathUtilities.WindowsUncRoot)) - normalized = PathUtilities.PreferredPathSeparator + normalized; + if (string.IsNullOrWhiteSpace(path)) + return path; - return normalized; + return string.Join(PathUtilities.PreferredPathSeparator, path.Split(PathUtilities.PossiblePathSeparators)); } /// <summary>Get a directory or file path relative to a given source path. If no relative path is possible (e.g. the paths are on different drives), an absolute path is returned.</summary> @@ -79,16 +71,18 @@ namespace StardewModdingAPI.Toolkit.Utilities // get relative path string relative = PathUtilities.NormalizePathSeparators(Uri.UnescapeDataString(from.MakeRelativeUri(to).ToString())); - // set empty path to './' + // normalize if (relative == "") - relative = "./"; - - // fix root - if (relative.StartsWith("file:") && !targetPath.Contains("file:")) + relative = "."; + else { - relative = relative.Substring("file:".Length); - if (targetPath.StartsWith(PathUtilities.WindowsUncRoot) && !relative.StartsWith(PathUtilities.WindowsUncRoot)) - relative = PathUtilities.WindowsUncRoot + relative.TrimStart('\\'); + // trim trailing slash from URL + if (relative.EndsWith(PathUtilities.PreferredPathSeparator)) + relative = relative.Substring(0, relative.Length - PathUtilities.PreferredPathSeparator.Length); + + // fix root + if (relative.StartsWith("file:") && !targetPath.Contains("file:")) + relative = relative.Substring("file:".Length); } return relative; |