-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'refs/heads/feature/get-file-extension'
- Loading branch information
Showing
8 changed files
with
111 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// SPDX-FileCopyrightText: 2024 TruePath contributors <https://github.com/ForNeVeR/TruePath> | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
namespace TruePath.Tests; | ||
|
||
public class PathExtensionsTests | ||
{ | ||
[Theory] | ||
[InlineData("..", ".")] | ||
[InlineData("foo/bar.txt", ".txt")] | ||
[InlineData("/foo/bar.txt", ".txt")] | ||
[InlineData("foo/bar.", ".")] | ||
[InlineData("foo/bar", "")] | ||
[InlineData(".gitignore", ".gitignore")] | ||
public void GetExtensionWithDotTests(string path, string expected) | ||
{ | ||
IPath local = new LocalPath(path); | ||
Assert.Equal(expected, local.GetExtensionWithDot()); | ||
|
||
if (!path.StartsWith('/')) return; | ||
|
||
IPath a = new AbsolutePath(path); | ||
Assert.Equal(expected, a.GetExtensionWithDot()); | ||
} | ||
|
||
[Theory] | ||
[InlineData(".", "")] | ||
[InlineData("..", "")] | ||
[InlineData("foo/bar.txt", "txt")] | ||
[InlineData("/foo/bar.txt", "txt")] | ||
[InlineData("foo/bar.", "")] | ||
[InlineData("foo/bar", "")] | ||
[InlineData(".gitignore", "gitignore")] | ||
public void GetExtensionWithoutDotTests(string path, string expected) | ||
{ | ||
IPath l = new LocalPath(path); | ||
Assert.Equal(expected, l.GetExtensionWithoutDot()); | ||
|
||
if (!path.StartsWith('/')) return; | ||
|
||
IPath a = new AbsolutePath(path); | ||
Assert.Equal(expected, a.GetExtensionWithoutDot()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// SPDX-FileCopyrightText: 2024 Friedrich von Never <[email protected]> | ||
// SPDX-FileCopyrightText: 2024 TruePath contributors <https://github.com/ForNeVeR/TruePath> | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
|
@@ -31,7 +31,7 @@ public AbsolutePath(string value) | |
/// <exception cref="ArgumentException">Thrown if the passed path is not absolute.</exception> | ||
public AbsolutePath(LocalPath localPath) : this(localPath.Value) {} | ||
|
||
/// <summary>The normalized path string.</summary> | ||
/// <inheritdoc cref="IPath.Value"/> | ||
public string Value => Underlying.Value; | ||
|
||
/// <inheritdoc cref="IPath.Parent"/> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// SPDX-FileCopyrightText: 2024 Friedrich von Never <[email protected]> | ||
// SPDX-FileCopyrightText: 2024 TruePath contributors <https://github.com/ForNeVeR/TruePath> | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
|
@@ -7,7 +7,10 @@ namespace TruePath; | |
/// <summary>Represents a path in a file system.</summary> | ||
public interface IPath | ||
{ | ||
/// <summary>The full name of the last component of this path.</summary> | ||
/// <summary>The normalized path string.</summary> | ||
string Value { get; } | ||
|
||
/// <summary>The name of the last component of this path.</summary> | ||
string FileName { get; } | ||
|
||
/// <summary> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// SPDX-FileCopyrightText: 2024 Friedrich von Never <[email protected]> | ||
// SPDX-FileCopyrightText: 2024 TruePath contributors <https://github.com/ForNeVeR/TruePath> | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
|
@@ -14,7 +14,7 @@ namespace TruePath; | |
/// </summary> | ||
public readonly struct LocalPath(string value) : IEquatable<LocalPath>, IPath, IPath<LocalPath> | ||
{ | ||
/// <summary>The normalized path string.</summary> | ||
/// <inheritdoc cref="IPath.Value"/> | ||
public string Value { get; } = PathStrings.Normalize(value); | ||
|
||
/// <summary> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// SPDX-FileCopyrightText: 2024 TruePath contributors <https://github.com/ForNeVeR/TruePath> | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
namespace TruePath; | ||
|
||
/// <summary> | ||
/// Extension methods for <see cref="IPath"/> and <see cref="IPath{TPath}"/>. | ||
/// </summary> | ||
public static class PathExtensions | ||
{ | ||
/// <summary> | ||
/// <para>Gets the extension of the file name of the <paramref name="path"/> with the dot character.</para> | ||
/// <para>For example, for the path <c>file.txt</c>, this method will return a string <c>.txt</c>.</para> | ||
/// <para>File name entirely consisting of extension, such as <c>.gitignore</c>, is returned as-is.</para> | ||
/// </summary> | ||
/// <returns>The extension of the file name of the path with the dot character (if present).</returns> | ||
/// <remarks> | ||
/// This method will return an empty string for paths without extensions, and will return a dot for paths whose | ||
/// names end with a dot (even though it is an unusual path). This behavior allows to distinguish such paths, | ||
/// and potentially reconstruct the file name from its part without the extension and the "extension with dot". | ||
/// </remarks> | ||
public static string GetExtensionWithDot(this IPath path) => | ||
path.FileName.EndsWith('.') ? "." : Path.GetExtension(path.FileName); | ||
|
||
/// <summary> | ||
/// <para>Gets the extension of the file name of the <paramref name="path"/> without the dot character.</para> | ||
/// <para>For example, for the path <c>file.txt</c>, this method will return a string <c>txt</c>.</para> | ||
/// <para> | ||
/// File name entirely consisting of extension, such as <c>.gitignore</c>, is returned with its leading dot | ||
/// trimmed. | ||
/// </para> | ||
/// </summary> | ||
/// <returns>The extension of the file name of the path without the dot.</returns> | ||
/// <remarks> | ||
/// This method will return an empty string for paths without extensions and with empty extensions (ending with | ||
/// dot, which may be unusual). This behavior doesn't allow to distinguish such paths using this method, to | ||
/// reconstruct the original name from its name without extension and its extension without dot. | ||
/// </remarks> | ||
public static string GetExtensionWithoutDot(this IPath path) => GetExtensionWithDot(path).TrimStart('.'); | ||
} |