-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ec0d96a
commit 795f4f8
Showing
1 changed file
with
45 additions
and
0 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
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; | ||
} | ||
} |