Use Matching to replace NaturalSort

This commit is contained in:
Matt Nadareski
2024-02-29 21:20:44 -05:00
parent 4ed3c45722
commit 17c023efaa
24 changed files with 23 additions and 385 deletions

View File

@@ -1,104 +0,0 @@
/*
*
* Links for info and original source code:
*
* https://blog.codinghorror.com/sorting-for-humans-natural-sort-order/
* http://www.codeproject.com/Articles/22517/Natural-Sort-Comparer
*
* Exact code implementation used with permission, originally by motoschifo
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
// Ported to SabreTools.Matching. Remove once published.
namespace NaturalSort
{
public class NaturalComparer : Comparer<string>, IDisposable
{
private readonly Dictionary<string, string[]> table;
public NaturalComparer()
{
table = [];
}
public void Dispose()
{
table.Clear();
}
public override int Compare(string? x, string? y)
{
if (x == null || y == null)
{
if (x == null && y != null)
return -1;
else if (x != null && y == null)
return 1;
else
return 0;
}
if (x.ToLowerInvariant() == y.ToLowerInvariant())
{
return x.CompareTo(y);
}
if (!table.TryGetValue(x, out string[]? x1))
{
//x1 = Regex.Split(x.Replace(" ", string.Empty), "([0-9]+)");
x1 = Regex.Split(x.ToLowerInvariant(), "([0-9]+)").Where(s => !string.IsNullOrEmpty(s)).ToArray();
table.Add(x, x1);
}
if (!table.TryGetValue(y, out string[]? y1))
{
//y1 = Regex.Split(y.Replace(" ", string.Empty), "([0-9]+)");
y1 = Regex.Split(y.ToLowerInvariant(), "([0-9]+)").Where(s => !string.IsNullOrEmpty(s)).ToArray();
table.Add(y, y1);
}
for (int i = 0; i < x1.Length && i < y1.Length; i++)
{
if (x1[i] != y1[i])
{
return PartCompare(x1[i], y1[i]);
}
}
if (y1.Length > x1.Length)
{
return 1;
}
else if (x1.Length > y1.Length)
{
return -1;
}
else
{
return x.CompareTo(y);
}
}
private static int PartCompare(string left, string right)
{
if (!long.TryParse(left, out long x))
{
return NaturalComparerUtil.CompareNumeric(left, right);
}
if (!long.TryParse(right, out long y))
{
return NaturalComparerUtil.CompareNumeric(left, right);
}
// If we have an equal part, then make sure that "longer" ones are taken into account
if (x.CompareTo(y) == 0)
{
return left.Length - right.Length;
}
return x.CompareTo(y);
}
}
}

View File

@@ -1,79 +0,0 @@
using System.IO;
// Ported to SabreTools.Matching. Remove once published.
namespace NaturalSort
{
public static class NaturalComparerUtil
{
public static int CompareNumeric(string s1, string s2)
{
// Save the orginal strings, for later comparison
string s1orig = s1;
string s2orig = s2;
// We want to normalize the strings, so we set both to lower case
s1 = s1.ToLowerInvariant();
s2 = s2.ToLowerInvariant();
// If the strings are the same exactly, return
if (s1 == s2)
return s1orig.CompareTo(s2orig);
// If one is null, then say that's less than
if (s1 == null)
return -1;
if (s2 == null)
return 1;
// Now split into path parts after converting AltDirSeparator to DirSeparator
s1 = s1.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
s2 = s2.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
string[] s1parts = s1.Split(Path.DirectorySeparatorChar);
string[] s2parts = s2.Split(Path.DirectorySeparatorChar);
// Then compare each part in turn
for (int j = 0; j < s1parts.Length && j < s2parts.Length; j++)
{
int compared = CompareNumericPart(s1parts[j], s2parts[j]);
if (compared != 0)
return compared;
}
// If we got out here, then it looped through at least one of the strings
if (s1parts.Length > s2parts.Length)
return 1;
if (s1parts.Length < s2parts.Length)
return -1;
return s1orig.CompareTo(s2orig);
}
private static int CompareNumericPart(string s1, string s2)
{
// Otherwise, loop through until we have an answer
for (int i = 0; i < s1.Length && i < s2.Length; i++)
{
int s1c = s1[i];
int s2c = s2[i];
// If the characters are the same, continue
if (s1c == s2c)
continue;
// If they're different, check which one was larger
if (s1c > s2c)
return 1;
if (s1c < s2c)
return -1;
}
// If we got out here, then it looped through at least one of the strings
if (s1.Length > s2.Length)
return 1;
if (s1.Length < s2.Length)
return -1;
return 0;
}
}
}

View File

@@ -1,104 +0,0 @@
/*
*
* Links for info and original source code:
*
* https://blog.codinghorror.com/sorting-for-humans-natural-sort-order/
* http://www.codeproject.com/Articles/22517/Natural-Sort-Comparer
*
* Exact code implementation used with permission, originally by motoschifo
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
// Ported to SabreTools.Matching. Remove once published.
namespace NaturalSort
{
public class NaturalReversedComparer : Comparer<string>, IDisposable
{
private readonly Dictionary<string, string[]> table;
public NaturalReversedComparer()
{
table = [];
}
public void Dispose()
{
table.Clear();
}
public override int Compare(string? x, string? y)
{
if (x == null || y == null)
{
if (x == null && y != null)
return -1;
else if (x != null && y == null)
return 1;
else
return 0;
}
if (y.ToLowerInvariant() == x.ToLowerInvariant())
{
return y.CompareTo(x);
}
if (!table.TryGetValue(x, out string[]? x1))
{
//x1 = Regex.Split(x.Replace(" ", string.Empty), "([0-9]+)");
x1 = Regex.Split(x.ToLowerInvariant(), "([0-9]+)").Where(s => !string.IsNullOrEmpty(s)).ToArray();
table.Add(x, x1);
}
if (!table.TryGetValue(y, out string[]? y1))
{
//y1 = Regex.Split(y.Replace(" ", string.Empty), "([0-9]+)");
y1 = Regex.Split(y.ToLowerInvariant(), "([0-9]+)").Where(s => !string.IsNullOrEmpty(s)).ToArray();
table.Add(y, y1);
}
for (int i = 0; i < x1.Length && i < y1.Length; i++)
{
if (x1[i] != y1[i])
{
return PartCompare(x1[i], y1[i]);
}
}
if (y1.Length > x1.Length)
{
return 1;
}
else if (x1.Length > y1.Length)
{
return -1;
}
else
{
return y.CompareTo(x);
}
}
private static int PartCompare(string left, string right)
{
if (!long.TryParse(left, out long x))
{
return NaturalComparerUtil.CompareNumeric(right, left);
}
if (!long.TryParse(right, out long y))
{
return NaturalComparerUtil.CompareNumeric(right, left);
}
// If we have an equal part, then make sure that "longer" ones are taken into account
if (y.CompareTo(x) == 0)
{
return right.Length - left.Length;
}
return y.CompareTo(x);
}
}
}

View File

@@ -1,28 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!-- Assembly Properties -->
<TargetFrameworks>net20;net35;net40;net452;net462;net472;net48;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0</TargetFrameworks>
<RuntimeIdentifiers>win-x86;win-x64;win-arm64;linux-x64;linux-arm64;osx-x64</RuntimeIdentifiers>
<CheckEolTargetFramework>false</CheckEolTargetFramework>
<IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Version>1.1.2</Version>
<!-- Package Properties -->
<Authors>Matt Nadareski</Authors>
<Copyright>Copyright (c)2016-2024 motoschifo, Matt Nadareski</Copyright>
<PackageProjectUrl>https://github.com/SabreTools/</PackageProjectUrl>
<RepositoryUrl>https://github.com/SabreTools/SabreTools</RepositoryUrl>
<RepositoryType>git</RepositoryType>
</PropertyGroup>
<!-- Support for old .NET versions -->
<ItemGroup Condition="$(TargetFramework.StartsWith(`net2`)) OR $(TargetFramework.StartsWith(`net3`))">
<PackageReference Include="Net30.LinqBridge" Version="1.3.0" />
</ItemGroup>
</Project>

View File

@@ -1,40 +0,0 @@
using System;
// Ported to SabreTools.Matching. Remove once published.
namespace SabreTools.Core
{
public static class ArrayExtensions
{
/// <summary>
/// Indicates whether the specified array is null or has a length of zero
/// </summary>
public static bool IsNullOrEmpty(this Array? array)
{
return array == null || array.Length == 0;
}
//// <summary>
/// Returns if the first byte array starts with the second array
/// </summary>
public static bool StartsWith(this byte[] arr1, byte[] arr2, bool exact = false)
{
// If we have any invalid inputs, we return false
if (arr1 == null || arr2 == null
|| arr1.Length == 0 || arr2.Length == 0
|| arr2.Length > arr1.Length
|| (exact && arr1.Length != arr2.Length))
{
return false;
}
// Otherwise, loop through and see
for (int i = 0; i < arr2.Length; i++)
{
if (arr1[i] != arr2[i])
return false;
}
return true;
}
}
}

View File

@@ -35,6 +35,7 @@
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="SabreTools.Matching" Version="1.3.1" />
<PackageReference Include="SabreTools.Models" Version="1.3.0" />
</ItemGroup>

View File

@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Linq;
using SabreTools.Matching;
namespace SabreTools.Core.Tools
{

View File

@@ -6,13 +6,12 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Serialization;
using Newtonsoft.Json;
using SabreTools.Core;
using SabreTools.DatItems;
using SabreTools.DatItems.Formats;
using SabreTools.Logging;
using NaturalSort;
using Newtonsoft.Json;
using SabreTools.Matching;
namespace SabreTools.DatFiles
{

View File

@@ -8,13 +8,13 @@ using System.Linq;
using System.Threading.Tasks;
using System.Xml.Serialization;
using Microsoft.Data.Sqlite;
using NaturalSort;
using Newtonsoft.Json;
using SabreTools.Core;
using SabreTools.DatItems;
using SabreTools.DatItems.Formats;
using SabreTools.IO;
using SabreTools.Logging;
using SabreTools.Matching;
namespace SabreTools.DatFiles
{

View File

@@ -21,7 +21,6 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\NaturalSort\NaturalSort.csproj" />
<ProjectReference Include="..\SabreTools.Core\SabreTools.Core.csproj" />
<ProjectReference Include="..\SabreTools.DatItems\SabreTools.DatItems.csproj" />
<ProjectReference Include="..\SabreTools.Logging\SabreTools.Logging.csproj" />
@@ -35,6 +34,7 @@
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="SabreTools.IO" Version="1.3.0" />
<PackageReference Include="SabreTools.Matching" Version="1.3.1" />
<PackageReference Include="SabreTools.Models" Version="1.3.0" />
<PackageReference Include="SabreTools.Serialization" Version="1.3.0" />
</ItemGroup>

View File

@@ -1,7 +1,6 @@
using System;
using System.IO;
using System.Xml.Serialization;
using NaturalSort;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using SabreTools.Core;
@@ -9,6 +8,7 @@ using SabreTools.Core.Tools;
using SabreTools.DatItems.Formats;
using SabreTools.FileTypes;
using SabreTools.Logging;
using SabreTools.Matching;
namespace SabreTools.DatItems
{

View File

@@ -5,6 +5,7 @@ using Newtonsoft.Json;
using SabreTools.Core;
using SabreTools.Core.Tools;
using SabreTools.FileTypes;
using SabreTools.Matching;
// TODO: Add item mappings for all fields
namespace SabreTools.DatItems.Formats

View File

@@ -21,7 +21,6 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\NaturalSort\NaturalSort.csproj" />
<ProjectReference Include="..\SabreTools.Core\SabreTools.Core.csproj" />
<ProjectReference Include="..\SabreTools.FileTypes\SabreTools.FileTypes.csproj" />
<ProjectReference Include="..\SabreTools.Logging\SabreTools.Logging.csproj" />
@@ -29,6 +28,7 @@
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="SabreTools.Matching" Version="1.3.1" />
<PackageReference Include="SabreTools.Models" Version="1.3.0" />
</ItemGroup>

View File

@@ -21,7 +21,6 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\NaturalSort\NaturalSort.csproj" />
<ProjectReference Include="..\SabreTools.Core\SabreTools.Core.csproj" />
<ProjectReference Include="..\SabreTools.DatFiles\SabreTools.DatFiles.csproj" />
<ProjectReference Include="..\SabreTools.DatItems\SabreTools.DatItems.csproj" />
@@ -33,6 +32,7 @@
<ItemGroup>
<PackageReference Include="SabreTools.IO" Version="1.3.0" />
<PackageReference Include="SabreTools.Matching" Version="1.3.1" />
<PackageReference Include="SabreTools.Skippers" Version="1.1.2" />
</ItemGroup>

View File

@@ -4,13 +4,13 @@ using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using NaturalSort;
using SabreTools.Core;
using SabreTools.DatFiles;
using SabreTools.DatItems;
using SabreTools.DatItems.Formats;
using SabreTools.IO;
using SabreTools.Logging;
using SabreTools.Matching;
namespace SabreTools.DatTools
{

View File

@@ -1,7 +1,7 @@
using System.IO;
using System.Text;
using SabreTools.Core;
using SabreTools.IO;
using SabreTools.Matching;
namespace SabreTools.FileTypes.Aaru
{

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using SabreTools.Core;
using SabreTools.Matching;
#if NET462_OR_GREATER || NETCOREAPP
using SharpCompress.Archives;
using SharpCompress.Archives.Rar;
@@ -242,7 +243,7 @@ namespace SabreTools.FileTypes.Archives
try
{
SharpCompress.Archives.Rar.RarArchive ra = SharpCompress.Archives.Rar.RarArchive.Open(this.Filename, new ReaderOptions { LeaveStreamOpen = false });
List<RarArchiveEntry> rarEntries = ra.Entries.OrderBy(e => e.Key, new NaturalSort.NaturalReversedComparer()).ToList();
List<RarArchiveEntry> rarEntries = ra.Entries.OrderBy(e => e.Key, new NaturalReversedComparer()).ToList();
string? lastRarEntry = null;
foreach (RarArchiveEntry entry in rarEntries)
{

View File

@@ -2,12 +2,11 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using SabreTools.Core;
using SabreTools.Core.Tools;
using Compress;
using Compress.SevenZip;
using NaturalSort;
using SabreTools.Core;
using SabreTools.Core.Tools;
using SabreTools.Matching;
namespace SabreTools.FileTypes.Archives
{

View File

@@ -2,9 +2,10 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Compress;
using SabreTools.Core;
using SabreTools.Core.Tools;
using Compress;
using SabreTools.Matching;
#if NET462_OR_GREATER || NETCOREAPP
using SharpCompress.Archives;
using SharpCompress.Archives.Tar;
@@ -231,7 +232,7 @@ namespace SabreTools.FileTypes.Archives
try
{
TarArchive ta = TarArchive.Open(this.Filename!, new ReaderOptions { LeaveStreamOpen = false });
List<TarArchiveEntry> tarEntries = ta.Entries.OrderBy(e => e.Key, new NaturalSort.NaturalReversedComparer()).ToList();
List<TarArchiveEntry> tarEntries = ta.Entries.OrderBy(e => e.Key, new NaturalReversedComparer()).ToList();
string? lastTarEntry = null;
foreach (TarArchiveEntry entry in tarEntries)
{

View File

@@ -4,9 +4,9 @@ using System.IO;
using System.Linq;
using Compress;
using Compress.ZipFile;
using NaturalSort;
using SabreTools.Core;
using SabreTools.Core.Tools;
using SabreTools.Matching;
namespace SabreTools.FileTypes.Archives
{

View File

@@ -8,6 +8,7 @@ using SabreTools.FileTypes.Aaru;
using SabreTools.FileTypes.CHD;
using SabreTools.IO;
using SabreTools.Logging;
using SabreTools.Matching;
using SabreTools.Skippers;
namespace SabreTools.FileTypes

View File

@@ -21,7 +21,6 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\NaturalSort\NaturalSort.csproj" />
<ProjectReference Include="..\SabreTools.Core\SabreTools.Core.csproj" />
<ProjectReference Include="..\SabreTools.Logging\SabreTools.Logging.csproj" />
</ItemGroup>
@@ -35,6 +34,7 @@
<ItemGroup>
<PackageReference Include="SabreTools.IO" Version="1.3.0" />
<PackageReference Include="SabreTools.Matching" Version="1.3.1" />
<PackageReference Include="SabreTools.Skippers" Version="1.1.2" />
</ItemGroup>

View File

@@ -36,8 +36,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SabreTools.Test", "SabreToo
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SabreTools.Filter", "SabreTools.Filter\SabreTools.Filter.csproj", "{2A7A27A9-5FB9-4F6D-88F3-67120668A029}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NaturalSort", "NaturalSort\NaturalSort.csproj", "{F7180B1C-7CFB-4526-8FF5-C31E7B049378}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -150,14 +148,6 @@ Global
{2A7A27A9-5FB9-4F6D-88F3-67120668A029}.Release|Any CPU.Build.0 = Release|Any CPU
{2A7A27A9-5FB9-4F6D-88F3-67120668A029}.Release|x64.ActiveCfg = Release|Any CPU
{2A7A27A9-5FB9-4F6D-88F3-67120668A029}.Release|x64.Build.0 = Release|Any CPU
{F7180B1C-7CFB-4526-8FF5-C31E7B049378}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F7180B1C-7CFB-4526-8FF5-C31E7B049378}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F7180B1C-7CFB-4526-8FF5-C31E7B049378}.Debug|x64.ActiveCfg = Debug|Any CPU
{F7180B1C-7CFB-4526-8FF5-C31E7B049378}.Debug|x64.Build.0 = Debug|Any CPU
{F7180B1C-7CFB-4526-8FF5-C31E7B049378}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F7180B1C-7CFB-4526-8FF5-C31E7B049378}.Release|Any CPU.Build.0 = Release|Any CPU
{F7180B1C-7CFB-4526-8FF5-C31E7B049378}.Release|x64.ActiveCfg = Release|Any CPU
{F7180B1C-7CFB-4526-8FF5-C31E7B049378}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@@ -1,6 +1,5 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using SabreTools.Core;
using SabreTools.DatFiles;
using SabreTools.DatTools;