Skip to content

Commit

Permalink
Add DirectoryEntryComparer
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-visionaid committed Nov 7, 2024
1 parent ec0d96a commit 795f4f8
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions OpenMcdf3/DirectoryEntryComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Diagnostics;
using System.Text;

namespace OpenMcdf3;

internal class DirectoryEntryComparer : IComparer<DirectoryEntry>
{
public static DirectoryEntryComparer Default { get; } = new();

public int Compare(DirectoryEntry? x, DirectoryEntry? y)
{
Debug.Assert(x is not null && y is not null);

if (x == null && y == null)
return 0;

if (x is null)
return -1;

if (y is null)
return 1;

int xLength = Encoding.Unicode.GetByteCount(x.Name);
int yLength = Encoding.Unicode.GetByteCount(y.Name);

if (xLength < y.Name.Length)
return -1;

if (yLength > xLength)
return 1;

for (int i = 0; i < x.Name.Length; i++)
{
char xChar = char.ToUpperInvariant(x.Name[i]);
char yChar = char.ToUpperInvariant(y.Name[i]);

if (xChar < yChar)
return -1;
if (yChar > xChar)
return 1;
}

return 0;
}
}

0 comments on commit 795f4f8

Please sign in to comment.