Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions TruePath.Tests/PathStringsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,35 @@ public void WindowsSpecificDotFoldersAreTraversed(string input, string expected)
}
}

[Theory]
[InlineData("C:/", "C:")]
[InlineData("C:/../file", "file")]
public void Normalize_DriveLetterCheckDisabled_TreatsColonAsRegularPathCharacter(string input, string expected)
{
var actual = PathStrings.Normalize(input, driveBasedSystem: false);

Assert.Equal(NormalizeSeparators(expected), actual);
}

[Theory]
[InlineData("C:/", "C:/")]
[InlineData("C:/../file", "C:/../file")]
public void Normalize_DriveLetterCheckEnabled_PreservesWindowsDrivePrefix(string input, string expected)
{
var actual = PathStrings.Normalize(input, driveBasedSystem: true);

Assert.Equal(NormalizeSeparators(expected), actual);
}

[Fact]
public void Normalize_DriveLetterHandling_MatchesCurrentPlatform()
{
var actual = PathStrings.Normalize("C:/../file");
var expected = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "C:/../file" : "file";

Assert.Equal(NormalizeSeparators(expected), actual);
}

private static string NormalizeSeparators(string path) =>
path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
}
4 changes: 2 additions & 2 deletions TruePath/LocalPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace TruePath;
/// (e.g. the parent of <c>foo</c>).
/// </para>
/// <para>
/// Always stored in a normalized form. Read the documentation on <see cref="TruePath.PathStrings.Normalize"/> to
/// know what form of normalization the path uses.
/// Always stored in a normalized form. Read the documentation on <see cref="TruePath.PathStrings.Normalize(string)"/>
/// to know what form of normalization the path uses.
/// </para>
/// </summary>
public readonly struct LocalPath(string value) : IEquatable<LocalPath>, IComparable<LocalPath>, IPath, IPath<LocalPath>
Expand Down
12 changes: 9 additions & 3 deletions TruePath/PathStrings.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// SPDX-FileCopyrightText: 2024 TruePath contributors <https://github.com/ForNeVeR/TruePath>
// SPDX-FileCopyrightText: 2024-2026 TruePath contributors <https://github.com/ForNeVeR/TruePath>
//
// SPDX-License-Identifier: MIT

using System.Buffers;
using System.Runtime.InteropServices;
#if NET8_0_OR_GREATER
using System.Runtime.CompilerServices;
#endif

namespace TruePath;

Expand Down Expand Up @@ -46,12 +49,15 @@ public static class PathStrings
/// Note that this operation will never perform any file IO, and is purely string manipulation.
/// </para>
/// </summary>
public static string Normalize(string path) =>
Normalize(path, driveBasedSystem: RuntimeInformation.IsOSPlatform(OSPlatform.Windows));

#if NET8_0_OR_GREATER
[SkipLocalsInit] // is necessary to prevent the CLR from filling stackalloc with zeros.
#endif
public static string Normalize(string path)
internal static string Normalize(string path, bool driveBasedSystem)
{
bool containsDriveLetter = SourceContainsDriveLetter(path.AsSpan());
bool containsDriveLetter = driveBasedSystem && SourceContainsDriveLetter(path.AsSpan());

int written = 0;

Expand Down
Loading