From 1e86041b7858b8e81c9f5e0949ebebd4d3221962 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20G=C3=B6ls?= <6608231+Abrynos@users.noreply.github.com> Date: Mon, 22 May 2023 10:35:58 +0200 Subject: [PATCH] Add Path.ChangeExtension(string?, string?) (#29) * Add Path.ChangeExtension(string, string) * Try fix build for netstandard2.1 or higher * One more fix * And another one --- JustArchiNET.Madness/PathMadness/Path.cs | 41 ++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/JustArchiNET.Madness/PathMadness/Path.cs b/JustArchiNET.Madness/PathMadness/Path.cs index 4ad10d3..e3e5b37 100644 --- a/JustArchiNET.Madness/PathMadness/Path.cs +++ b/JustArchiNET.Madness/PathMadness/Path.cs @@ -168,6 +168,47 @@ public static string GetRelativePath(string relativeTo, string path) { [MadnessType(EMadnessType.Proxy)] public static string GetTempPath() => System.IO.Path.GetTempPath(); +#if NETSTANDARD2_1_OR_GREATER + [MadnessType(EMadnessType.Proxy)] + public static string ChangeExtension(string? path, string? extension) => System.IO.Path.ChangeExtension(path, extension); +#else + [MadnessType(EMadnessType.Implementation)] + [Pure] + public static string? ChangeExtension(string? path, string? extension) + { + if (path == null) { + return null; + } + + int subLength = path.Length; + if (subLength == 0) { + return string.Empty; + } + + for (int i = path.Length - 1; i >= 0; i--) { + char ch = path[i]; + + if (ch == '.') { + subLength = i; + break; + } + + if (PathInternalNetCore.IsDirectorySeparator(ch)) { + break; + } + } + + if (extension == null) { + return path.Substring(0, subLength); + } + + string subpath = path.Substring(0, subLength); + return extension.StartsWith(".") ? + string.Concat(subpath, extension) : + string.Concat(subpath, ".", extension); + } +#endif + [MadnessType(EMadnessType.Proxy)] [Pure] public static bool IsPathRooted(string? path) => System.IO.Path.IsPathRooted(path);