Skip to content

Commit 66ccc89

Browse files
committed
(#20) Comparers: upgrade docs and naming, move to separate namespace
1 parent bc66235 commit 66ccc89

File tree

9 files changed

+88
-97
lines changed

9 files changed

+88
-97
lines changed

TruePath.Tests/AbsolutePathTests.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// SPDX-License-Identifier: MIT
44

55
using System.Diagnostics;
6+
using TruePath.Comparers;
67

78
namespace TruePath.Tests;
89

@@ -219,7 +220,7 @@ public void EqualsUseStrictStringPathComparer_SamePaths_True()
219220
var path2 = new AbsolutePath(nonCanonicalPath);
220221

221222
// Act
222-
var equals = path1.Equals(path2, StrictStringPathComparer.Comparer);
223+
var equals = path1.Equals(path2, StrictPathComparer.Instance);
223224

224225
// Assert
225226
Assert.True(equals);
@@ -230,13 +231,13 @@ public void EqualsUseStrictStringPathComparer_NotSamePaths_False()
230231
{
231232
// Arrange
232233
var currentDirectory = Environment.CurrentDirectory;
233-
var nonCanonicalPath = new string(currentDirectory.MakeNonCanonicalPath().ToArray());
234+
var nonCanonicalPath = new string(currentDirectory.ToNonCanonicalCase().ToArray());
234235

235236
var path1 = new AbsolutePath(currentDirectory);
236237
var path2 = new AbsolutePath(nonCanonicalPath);
237238

238239
// Act
239-
var equals = path1.Equals(path2, StrictStringPathComparer.Comparer);
240+
var equals = path1.Equals(path2, StrictPathComparer.Instance);
240241

241242
// Assert
242243
Assert.False(equals);
@@ -252,7 +253,7 @@ public void OnLinux_EqualsDefault_CaseSensitive_False()
252253
}
253254

254255
var currentDirectory = Environment.CurrentDirectory;
255-
var nonCanonicalPath = new string(currentDirectory.MakeNonCanonicalPath().ToArray());
256+
var nonCanonicalPath = new string(currentDirectory.ToNonCanonicalCase().ToArray());
256257

257258
var path1 = new AbsolutePath(currentDirectory);
258259
var path2 = new AbsolutePath(nonCanonicalPath);
@@ -274,7 +275,7 @@ public void OnWindowsOrOsx_EqualsDefault_CaseInsensitive_True()
274275
}
275276

276277
var currentDirectory = Environment.CurrentDirectory;
277-
var nonCanonicalPath = new string(currentDirectory.MakeNonCanonicalPath().ToArray());
278+
var nonCanonicalPath = new string(currentDirectory.ToNonCanonicalCase().ToArray());
278279

279280
var path1 = new AbsolutePath(currentDirectory);
280281
var path2 = new AbsolutePath(nonCanonicalPath);

TruePath.Tests/LocalPathTests.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//
33
// SPDX-License-Identifier: MIT
44

5+
using TruePath.Comparers;
56
using Xunit.Abstractions;
67

78
namespace TruePath.Tests;
@@ -102,7 +103,7 @@ public void EqualsUseStrictStringPathComparer_SamePaths_True()
102103
var path2 = new LocalPath(nonCanonicalPath);
103104

104105
// Act
105-
var equals = path1.Equals(path2, StrictStringPathComparer.Comparer);
106+
var equals = path1.Equals(path2, StrictPathComparer.Instance);
106107

107108
// Assert
108109
Assert.True(equals);
@@ -113,13 +114,13 @@ public void EqualsUseStrictStringPathComparer_NotSamePaths_False()
113114
{
114115
// Arrange
115116
var currentDirectory = Environment.CurrentDirectory;
116-
var nonCanonicalPath = new string(currentDirectory.MakeNonCanonicalPath().ToArray());
117+
var nonCanonicalPath = new string(currentDirectory.ToNonCanonicalCase().ToArray());
117118

118119
var path1 = new LocalPath(currentDirectory);
119120
var path2 = new LocalPath(nonCanonicalPath);
120121

121122
// Act
122-
var equals = path1.Equals(path2, StrictStringPathComparer.Comparer);
123+
var equals = path1.Equals(path2, StrictPathComparer.Instance);
123124

124125
// Assert
125126
Assert.False(equals);
@@ -135,7 +136,7 @@ public void OnLinux_EqualsDefault_CaseSensitive_False()
135136
}
136137

137138
var currentDirectory = Environment.CurrentDirectory;
138-
var nonCanonicalPath = new string(currentDirectory.MakeNonCanonicalPath().ToArray());
139+
var nonCanonicalPath = new string(currentDirectory.ToNonCanonicalCase().ToArray());
139140

140141
var path1 = new LocalPath(currentDirectory);
141142
var path2 = new LocalPath(nonCanonicalPath);
@@ -157,7 +158,7 @@ public void OnWindowsOrOsx_EqualsDefault_CaseInsensitive_True()
157158
}
158159

159160
var currentDirectory = Environment.CurrentDirectory;
160-
var nonCanonicalPath = new string(currentDirectory.MakeNonCanonicalPath().ToArray());
161+
var nonCanonicalPath = new string(currentDirectory.ToNonCanonicalCase().ToArray());
161162

162163
var path1 = new LocalPath(currentDirectory);
163164
var path2 = new LocalPath(nonCanonicalPath);

TruePath.Tests/Utils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace TruePath.Tests;
66

77
public static class Utils
88
{
9-
internal static string MakeNonCanonicalPath(this string path)
9+
internal static string ToNonCanonicalCase(this string path)
1010
{
1111
var result = new char[path.Length];
1212
for (var i = 0; i < path.Length; i++)

TruePath/AbsolutePath.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// SPDX-License-Identifier: MIT
44

55
using System.Runtime.InteropServices;
6+
using TruePath.Comparers;
67

78
namespace TruePath;
89

@@ -103,7 +104,7 @@ public bool IsPrefixOf(AbsolutePath other)
103104
/// <remarks>Note that currently this comparison is case-sensitive.</remarks>
104105
public bool Equals(AbsolutePath other)
105106
{
106-
var comparer = PlatformDefaultPathComparer.Comparer;
107+
var comparer = PlatformDefaultPathComparer.Instance;
107108
return comparer.Compare(Underlying.Value, other.Underlying.Value) == 0;
108109
}
109110

@@ -115,9 +116,6 @@ public bool Equals(AbsolutePath other)
115116
/// <returns>
116117
/// <see langword="true"/> if the specified <see cref="AbsolutePath"/> is equal to the current <see cref="AbsolutePath"/> using the specified string comparer; otherwise, <see langword="false"/>.
117118
/// </returns>
118-
/// <remarks>
119-
/// If the comparer is null, this method returns <see langword="false"/>.
120-
/// </remarks>
121119
public bool Equals(AbsolutePath other, IComparer<string> comparer)
122120
{
123121
return comparer.Compare(Value, other.Value) == 0;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// SPDX-FileCopyrightText: 2024 TruePath contributors <https://github.com/ForNeVeR/TruePath>
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
using System.Runtime.InteropServices;
6+
7+
namespace TruePath.Comparers;
8+
9+
/// <summary>
10+
/// <para>Provides a default comparer for comparing file paths, aware of the current platform.</para>
11+
/// <para>
12+
/// On <b>Windows</b> and <b>macOS</b>, this will perform <b>case-insensitive</b> comparison, since the file
13+
/// systems are case-insensitive on these operating systems by default.
14+
/// </para>
15+
/// <para>On <b>Linux</b>, the comparison will be <b>case-sensitive</b>.</para>
16+
/// </summary>
17+
/// <remarks>
18+
/// Note that this comparison <b>does not guarantee correctness</b>: in practice, on any platform to control
19+
/// case-sensitiveness of either the whole file system or a part of it. This class does not take this into account,
20+
/// having a benefit of no accessing the file system for any of the comparisons.
21+
/// </remarks>
22+
public class PlatformDefaultPathComparer : IComparer<string>
23+
{
24+
/// <summary>
25+
/// Gets the singleton instance of the <see cref="PlatformDefaultPathComparer"/> class.
26+
/// </summary>
27+
public static readonly PlatformDefaultPathComparer Instance = new();
28+
29+
private readonly StringComparer _comparisonType;
30+
31+
/// <summary>
32+
/// Initializes a new instance of the <see cref="PlatformDefaultPathComparer"/> class.
33+
/// </summary>
34+
private PlatformDefaultPathComparer()
35+
{
36+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
37+
{
38+
_comparisonType = StringComparer.OrdinalIgnoreCase;
39+
}
40+
else
41+
{
42+
_comparisonType = StringComparer.Ordinal;
43+
}
44+
}
45+
46+
/// <inheritdoc cref="IComparer{T}.Compare"/>
47+
public int Compare(string? x, string? y)
48+
{
49+
return _comparisonType.Compare(x, y);
50+
}
51+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// SPDX-FileCopyrightText: 2024 TruePath contributors <https://github.com/ForNeVeR/TruePath>
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
namespace TruePath.Comparers;
6+
7+
/// <summary>A strict comparer for comparing file paths using ordinal comparison.</summary>
8+
public class StrictPathComparer : IComparer<string>
9+
{
10+
/// <summary>
11+
/// Gets the singleton instance of the <see cref="StrictPathComparer"/> class.
12+
/// </summary>
13+
public static readonly StrictPathComparer Instance = new();
14+
15+
/// <inheritdoc cref="IComparer{T}.Compare"/>
16+
public int Compare(string? x, string? y)
17+
{
18+
return StringComparer.Ordinal.Compare(x, y);
19+
}
20+
}

TruePath/LocalPath.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// SPDX-License-Identifier: MIT
44

5-
using System.Runtime.InteropServices;
5+
using TruePath.Comparers;
66

77
namespace TruePath;
88

@@ -44,7 +44,7 @@ public readonly struct LocalPath(string value) : IEquatable<LocalPath>, IPath, I
4444
/// <remarks>Note that currently this comparison is case-sensitive.</remarks>
4545
public bool Equals(LocalPath other)
4646
{
47-
var comparer = PlatformDefaultPathComparer.Comparer;
47+
var comparer = PlatformDefaultPathComparer.Instance;
4848
return comparer.Compare(Value, other.Value) == 0;
4949
}
5050

@@ -56,9 +56,6 @@ public bool Equals(LocalPath other)
5656
/// <returns>
5757
/// <see langword="true"/> if the specified <see cref="LocalPath"/> is equal to the current <see cref="LocalPath"/> using the specified string comparer; otherwise, <see langword="false"/>.
5858
/// </returns>
59-
/// <remarks>
60-
/// If the comparer is null, this method returns <see langword="false"/>.
61-
/// </remarks>
6259
public bool Equals(LocalPath other, IComparer<string> comparer)
6360
{
6461
return comparer.Compare(Value, other.Value) == 0;

TruePath/PlatformDefaultPathComparer.cs

Lines changed: 0 additions & 48 deletions
This file was deleted.

TruePath/StrictStringPathComparer.cs

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)