Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

Commit

Permalink
Add Path.ChangeExtension(string?, string?) (#29)
Browse files Browse the repository at this point in the history
* Add Path.ChangeExtension(string, string)

* Try fix build for netstandard2.1 or higher

* One more fix

* And another one
  • Loading branch information
Abrynos authored May 22, 2023
1 parent e6e081f commit 1e86041
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions JustArchiNET.Madness/PathMadness/Path.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 1e86041

Please sign in to comment.