diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2020-09-05 15:00:38 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2020-09-05 15:00:38 -0400 |
commit | 4f3d7eaafc056a7a2b17b1657e069eb456f60f52 (patch) | |
tree | 25bb83dca0f4db5f53451908c6b44b7a6681a4bd /src/SMAPI/Utilities | |
parent | a9ec5a6e9165fe18cc6fca60b81a330bce0e1904 (diff) | |
download | SMAPI-4f3d7eaafc056a7a2b17b1657e069eb456f60f52.tar.gz SMAPI-4f3d7eaafc056a7a2b17b1657e069eb456f60f52.tar.bz2 SMAPI-4f3d7eaafc056a7a2b17b1657e069eb456f60f52.zip |
make PathUtilities available to mods
Diffstat (limited to 'src/SMAPI/Utilities')
-rw-r--r-- | src/SMAPI/Utilities/PathUtilities.cs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/SMAPI/Utilities/PathUtilities.cs b/src/SMAPI/Utilities/PathUtilities.cs new file mode 100644 index 00000000..ea134468 --- /dev/null +++ b/src/SMAPI/Utilities/PathUtilities.cs @@ -0,0 +1,45 @@ +using System.Diagnostics.Contracts; +using ToolkitPathUtilities = StardewModdingAPI.Toolkit.Utilities.PathUtilities; + +namespace StardewModdingAPI.Utilities +{ + /// <summary>Provides utilities for normalizing file paths.</summary> + public static class PathUtilities + { + /********* + ** Public methods + *********/ + /// <summary>Get the segments from a path (e.g. <c>/usr/bin/example</c> => <c>usr</c>, <c>bin</c>, and <c>example</c>).</summary> + /// <param name="path">The path to split.</param> + /// <param name="limit">The number of segments to match. Any additional segments will be merged into the last returned part.</param> + [Pure] + public static string[] GetSegments(string path, int? limit = null) + { + return ToolkitPathUtilities.GetSegments(path, limit); + } + + /// <summary>Normalize path separators in a file path.</summary> + /// <param name="path">The file path to normalize.</param> + [Pure] + public static string NormalizePathSeparators(string path) + { + return ToolkitPathUtilities.NormalizePathSeparators(path); + } + + /// <summary>Get whether a path is relative and doesn't try to climb out of its containing folder (e.g. doesn't contain <c>../</c>).</summary> + /// <param name="path">The path to check.</param> + [Pure] + public static bool IsSafeRelativePath(string path) + { + return ToolkitPathUtilities.IsSafeRelativePath(path); + } + + /// <summary>Get whether a string is a valid 'slug', containing only basic characters that are safe in all contexts (e.g. filenames, URLs, etc).</summary> + /// <param name="str">The string to check.</param> + [Pure] + public static bool IsSlug(string str) + { + return ToolkitPathUtilities.IsSlug(str); + } + } +} |