Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various things #9

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
67 changes: 67 additions & 0 deletions DALib/Comparers/NaturalStringComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System.Collections.Generic;

namespace DALib.Comparers;

/// <summary>
/// A natural string comparer intended to work similarly to how windows orders files. Strings with numbers will be
/// compared numerically, rather than lexicographically.
/// </summary>
public sealed class NaturalStringComparer : IComparer<string>
{
public static IComparer<string> Instance { get; } = new NaturalStringComparer();

/// <inheritdoc />
/// <inheritdoc />
public int Compare(string? x, string? y)
{
// ReSharper disable once ConvertIfStatementToSwitchStatement
if ((x == null) && (y == null))
return 0;

if (x == null)
return -1;

if (y == null)
return 1;

var ix = 0;
var iy = 0;

while ((ix < x.Length) && (iy < y.Length))
if (char.IsDigit(x[ix]) && char.IsDigit(y[iy]))
{
var lx = 0;
var ly = 0;

while ((ix < x.Length) && char.IsDigit(x[ix]))
{
lx = lx * 10 + (x[ix] - '0');
ix++;
}

while ((iy < y.Length) && char.IsDigit(y[iy]))
{
ly = ly * 10 + (y[iy] - '0');
iy++;
}

if (lx != ly)
return lx.CompareTo(ly);
}
else
{
// Convert both characters to lower case for case-insensitive comparison
var cx = char.ToLowerInvariant(x[ix]);
var cy = char.ToLowerInvariant(y[iy]);

if (cx != cy)
return cx.CompareTo(cy);

ix++;
iy++;
}

return x.Length.CompareTo(y.Length);
}

}
52 changes: 52 additions & 0 deletions DALib/Comparers/PreferUnderscoreIgnoreCaseStringComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;

namespace DALib.Comparers;

/// <summary>
/// Compares two strings and returns a value indicating whether one is less than, equal to, or greater than the other.
/// Underscore will be considered less than any other character
/// </summary>
public sealed class PreferUnderscoreIgnoreCaseStringComparer : IComparer<string>
{
/// <summary>
/// Gets the default instance of the <see cref="PreferUnderscoreIgnoreCaseStringComparer" /> class.
/// </summary>
public static IComparer<string> Instance { get; } = new PreferUnderscoreIgnoreCaseStringComparer();

/// <inheritdoc />
public int Compare(string? x, string? y)
{
if (StringComparer.OrdinalIgnoreCase.Equals(x, y))
return 0;

if (x == null)
return -1;

if (y == null)
return 1;

var xLength = x.Length;
var yLength = y.Length;
var minLength = xLength < yLength ? xLength : yLength;

for (var i = 0; i < minLength; i++)
{
var xChar = char.ToUpperInvariant(x[i]);
var yChar = char.ToUpperInvariant(y[i]);

if (xChar == yChar)
continue;

if (xChar == '_')
return -1;

if (yChar == '_')
return 1;

return xChar.CompareTo(yChar);
}

return xLength.CompareTo(yLength);
}
}
7 changes: 4 additions & 3 deletions DALib/DALib.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!--suppress MsbuildTargetFrameworkTagInspection -->
<TargetFrameworks>net8.0</TargetFrameworks>
<PackageId>DALib</PackageId>
<PackageVersion>0.5.3</PackageVersion>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand All @@ -22,6 +21,8 @@
<Nullable>enable</Nullable>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TargetFramework>net9.0</TargetFramework>
<LangVersion>latestmajor</LangVersion>
</PropertyGroup>
<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)README.md" Pack="true" PackagePath=""/>
Expand All @@ -30,8 +31,8 @@
<None Include="hybrasyl-512x512.png" Pack="true" PackagePath=""/>
</ItemGroup>
<ItemGroup>
<PackageReference Include="KGySoft.Drawing.SkiaSharp" Version="7.2.0"/>
<PackageReference Include="SkiaSharp" Version="2.88.6"/>
<PackageReference Include="KGySoft.Drawing.SkiaSharp" Version="8.1.0"/>
<PackageReference Include="SkiaSharp" Version="3.116.0"/>
</ItemGroup>
<ItemGroup>
<SupportedPlatform Include="Linux"/>
Expand Down
Loading
Loading