Skip to content

Commit

Permalink
refactor: support new Path methods from .NET9 (#1192)
Browse files Browse the repository at this point in the history
Add the two new methods to `Path` introduced in .NET 9:
- `string Combine(params ReadOnlySpan<string> paths)`
- `string Join(params ReadOnlySpan<string?> paths)`
  • Loading branch information
vbreuss authored Jan 26, 2025
1 parent 21fd787 commit 5d1885d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
1 change: 1 addition & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<DefineConstants Condition="'$(TargetFramework)' == 'net9.0' OR '$(TargetFramework)' == 'net8.0' OR '$(TargetFramework)' == 'net7.0' OR '$(TargetFramework)' == 'net6.0' OR '$(TargetFramework)' == 'netstandard2.1'">$(DefineConstants);FEATURE_ASYNC_FILE;FEATURE_ENUMERATION_OPTIONS;FEATURE_ADVANCED_PATH_OPERATIONS;FEATURE_PATH_JOIN_WITH_SPAN;FEATURE_SPAN</DefineConstants>
<DefineConstants Condition="'$(TargetFramework)' == 'net9.0' OR '$(TargetFramework)' == 'net8.0' OR '$(TargetFramework)' == 'net7.0' OR '$(TargetFramework)' == 'net6.0'">$(DefineConstants);FEATURE_FILE_MOVE_WITH_OVERWRITE;FEATURE_SUPPORTED_OS_ATTRIBUTE;FEATURE_FILE_SYSTEM_WATCHER_FILTERS;FEATURE_ENDS_IN_DIRECTORY_SEPARATOR;FEATURE_PATH_JOIN_WITH_PARAMS;FEATURE_PATH_JOIN_WITH_FOUR_PATHS;FEATURE_FILE_SYSTEM_INFO_LINK_TARGET;FEATURE_CREATE_SYMBOLIC_LINK;FEATURE_FILESTREAM_OPTIONS</DefineConstants>
<DefineConstants Condition="'$(TargetFramework)' == 'net9.0' OR '$(TargetFramework)' == 'net8.0' OR '$(TargetFramework)' == 'net7.0'">$(DefineConstants);FEATURE_PATH_EXISTS;FEATURE_FILE_SYSTEM_WATCHER_WAIT_WITH_TIMESPAN;FEATURE_FILE_ATTRIBUTES_VIA_HANDLE;FEATURE_CREATE_TEMP_SUBDIRECTORY;FEATURE_READ_LINES_ASYNC;FEATURE_UNIX_FILE_MODE</DefineConstants>
<DefineConstants Condition="'$(TargetFramework)' == 'net9.0'">$(DefineConstants);FEATURE_PATH_SPAN</DefineConstants>
<DefineConstants>$(DefineConstants);FEATURE_SERIALIZABLE</DefineConstants>
</PropertyGroup>
<ItemGroup>
Expand Down
10 changes: 10 additions & 0 deletions src/TestableIO.System.IO.Abstractions.Wrappers/PathBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ internal PathBase() { }

/// <inheritdoc cref="Path.Combine(string[])"/>
public abstract string Combine(params string[] paths);

#if FEATURE_PATH_SPAN
/// <inheritdoc cref="Path.Combine(ReadOnlySpan{string})"/>
public abstract string Combine(params ReadOnlySpan<string> paths);
#endif

/// <inheritdoc cref="Path.Combine(string,string)"/>
public abstract string Combine(string path1, string path2);
Expand Down Expand Up @@ -117,6 +122,11 @@ internal PathBase() { }
/// <inheritdoc />
public abstract string Join(ReadOnlySpan<char> path1, ReadOnlySpan<char> path2, ReadOnlySpan<char> path3);

#if FEATURE_PATH_SPAN
/// <inheritdoc cref="Path.Join(ReadOnlySpan{string})"/>
public abstract string Join(params ReadOnlySpan<string> paths);
#endif

/// <inheritdoc />
public abstract bool TryJoin(ReadOnlySpan<char> path1, ReadOnlySpan<char> path2, ReadOnlySpan<char> path3, Span<char> destination, out int charsWritten);

Expand Down
16 changes: 16 additions & 0 deletions src/TestableIO.System.IO.Abstractions.Wrappers/PathWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ public override string Combine(params string[] paths)
return Path.Combine(paths);
}

#if FEATURE_PATH_SPAN
/// <inheritdoc />
public override string Combine(params ReadOnlySpan<string> paths)
{
return Path.Combine(paths);
}
#endif

/// <inheritdoc />
public override string Combine(string path1, string path2)
{
Expand Down Expand Up @@ -185,6 +193,14 @@ public override string Join(ReadOnlySpan<char> path1, ReadOnlySpan<char> path2)
public override string Join(ReadOnlySpan<char> path1, ReadOnlySpan<char> path2, ReadOnlySpan<char> path3) =>
Path.Join(path1, path2, path3);

#if FEATURE_PATH_SPAN
/// <inheritdoc />
public override string Join(params ReadOnlySpan<string> paths)
{
return Path.Join(paths);
}
#endif

/// <inheritdoc />
public override bool TryJoin(ReadOnlySpan<char> path1, ReadOnlySpan<char> path2, Span<char> destination, out int charsWritten) =>
Path.TryJoin(path1, path2, destination, out charsWritten);
Expand Down
10 changes: 10 additions & 0 deletions src/TestableIO.System.IO.Abstractions/IPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public interface IPath : IFileSystemEntity
/// <inheritdoc cref="Path.Combine(string[])" />
string Combine(params string[] paths);

#if FEATURE_PATH_SPAN
/// <inheritdoc cref="Path.Combine(ReadOnlySpan{string})" />
string Combine(params ReadOnlySpan<string> paths);
#endif

#if FEATURE_ENDS_IN_DIRECTORY_SEPARATOR
/// <inheritdoc cref="Path.EndsInDirectorySeparator(ReadOnlySpan{char})" />
bool EndsInDirectorySeparator(ReadOnlySpan<char> path);
Expand Down Expand Up @@ -148,6 +153,11 @@ string Join(ReadOnlySpan<char> path1,
ReadOnlySpan<char> path2,
ReadOnlySpan<char> path3);

#if FEATURE_PATH_SPAN
/// <inheritdoc cref="Path.Join(ReadOnlySpan{string?})" />
string Join(params ReadOnlySpan<string?> paths);
#endif

/// <inheritdoc cref="Path.TryJoin(ReadOnlySpan{char}, ReadOnlySpan{char}, Span{char}, out int)" />
bool TryJoin(ReadOnlySpan<char> path1,
ReadOnlySpan<char> path2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,5 @@
"Char get_VolumeSeparatorChar()",
"Char[] get_InvalidPathChars()"
],
"MissingMembers": [
"System.String Combine(System.ReadOnlySpan`1[System.String])",
"System.String Join(System.ReadOnlySpan`1[System.String])"
]
"MissingMembers": []
}

0 comments on commit 5d1885d

Please sign in to comment.