[Style] Implement CompareNumeric again, not external this time

This commit is contained in:
Matt Nadareski
2016-10-28 12:24:17 -07:00
parent a29b3171db
commit 5ba600782b
4 changed files with 70 additions and 11 deletions

View File

@@ -5757,7 +5757,7 @@ namespace SabreTools.Helper.Dats
// Get a properly sorted set of keys // Get a properly sorted set of keys
List<string> keys = Files.Keys.ToList(); List<string> keys = Files.Keys.ToList();
keys.Sort(new NaturalComparer()); keys.Sort(Style.CompareNumeric);
foreach (string key in keys) foreach (string key in keys)
{ {

View File

@@ -538,7 +538,6 @@ namespace SabreTools.Helper.Dats
{ {
try try
{ {
NaturalComparer nc = new NaturalComparer();
if (x.SystemID == y.SystemID) if (x.SystemID == y.SystemID)
{ {
if (x.SourceID == y.SourceID) if (x.SourceID == y.SourceID)
@@ -549,9 +548,9 @@ namespace SabreTools.Helper.Dats
{ {
if (Path.GetDirectoryName(Style.RemovePathUnsafeCharacters(x.Name)) == Path.GetDirectoryName(Style.RemovePathUnsafeCharacters(y.Name))) if (Path.GetDirectoryName(Style.RemovePathUnsafeCharacters(x.Name)) == Path.GetDirectoryName(Style.RemovePathUnsafeCharacters(y.Name)))
{ {
return nc.Compare(Path.GetFileName(Style.RemovePathUnsafeCharacters(x.Name)), Path.GetFileName(Style.RemovePathUnsafeCharacters(y.Name))); return Style.CompareNumeric(Path.GetFileName(Style.RemovePathUnsafeCharacters(x.Name)), Path.GetFileName(Style.RemovePathUnsafeCharacters(y.Name)));
} }
return nc.Compare(Path.GetDirectoryName(Style.RemovePathUnsafeCharacters(x.Name)), Path.GetDirectoryName(Style.RemovePathUnsafeCharacters(y.Name))); return Style.CompareNumeric(Path.GetDirectoryName(Style.RemovePathUnsafeCharacters(x.Name)), Path.GetDirectoryName(Style.RemovePathUnsafeCharacters(y.Name)));
} }
else if ((x.Type == ItemType.Rom || x.Type == ItemType.Disk) && (y.Type != ItemType.Rom && y.Type != ItemType.Disk)) else if ((x.Type == ItemType.Rom || x.Type == ItemType.Disk) && (y.Type != ItemType.Rom && y.Type != ItemType.Disk))
{ {
@@ -565,16 +564,16 @@ namespace SabreTools.Helper.Dats
{ {
if (Path.GetDirectoryName(x.Name) == Path.GetDirectoryName(y.Name)) if (Path.GetDirectoryName(x.Name) == Path.GetDirectoryName(y.Name))
{ {
return nc.Compare(Path.GetFileName(x.Name), Path.GetFileName(y.Name)); return Style.CompareNumeric(Path.GetFileName(x.Name), Path.GetFileName(y.Name));
} }
return nc.Compare(Path.GetDirectoryName(x.Name), Path.GetDirectoryName(y.Name)); return Style.CompareNumeric(Path.GetDirectoryName(x.Name), Path.GetDirectoryName(y.Name));
} }
} }
return nc.Compare(x.Machine.Name, y.Machine.Name); return Style.CompareNumeric(x.Machine.Name, y.Machine.Name);
} }
return (norename ? nc.Compare(x.Machine.Name, y.Machine.Name) : x.SourceID - y.SourceID); return (norename ? Style.CompareNumeric(x.Machine.Name, y.Machine.Name) : x.SourceID - y.SourceID);
} }
return (norename ? nc.Compare(x.Machine.Name, y.Machine.Name) : x.SystemID - y.SystemID); return (norename ? Style.CompareNumeric(x.Machine.Name, y.Machine.Name) : x.SystemID - y.SystemID);
} }
catch (Exception) catch (Exception)
{ {

View File

@@ -44,12 +44,12 @@ namespace SabreTools.Helper.Tools
{ {
// Take care of the files in the top directory // Take care of the files in the top directory
List<string> toadd = Directory.EnumerateFiles(directory, "*", SearchOption.TopDirectoryOnly).ToList(); List<string> toadd = Directory.EnumerateFiles(directory, "*", SearchOption.TopDirectoryOnly).ToList();
toadd.Sort(new NaturalComparer()); toadd.Sort(Style.CompareNumeric);
infiles.AddRange(toadd); infiles.AddRange(toadd);
// Then recurse through and add from the directories // Then recurse through and add from the directories
List<string> dirs = Directory.EnumerateDirectories(directory, "*", SearchOption.TopDirectoryOnly).ToList(); List<string> dirs = Directory.EnumerateDirectories(directory, "*", SearchOption.TopDirectoryOnly).ToList();
dirs.Sort(new NaturalComparer()); dirs.Sort(Style.CompareNumeric);
foreach (string dir in dirs) foreach (string dir in dirs)
{ {
infiles = RetrieveFiles(dir, infiles); infiles = RetrieveFiles(dir, infiles);

View File

@@ -275,6 +275,66 @@ namespace SabreTools.Helper.Tools
return new string(s.Where(c => !invalidPath.Contains(c)).ToArray()); return new string(s.Where(c => !invalidPath.Contains(c)).ToArray());
} }
/// <summary>
/// Compare strings as numeric
/// </summary>
/// <param name="s1"></param>
/// <param name="s2"></param>
/// <returns></returns>
public static int CompareNumeric(string s1, string s2)
{
// If the strings are the same exactly, return
if (s1 == s2)
{
return 0;
}
// If one is null, then say that's less than
if (s1 == null)
{
return -1;
}
if (s2 == null)
{
return 1;
}
// 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;
}
/// <summary> /// <summary>
/// Convert all characters that are not considered XML-safe /// Convert all characters that are not considered XML-safe
/// </summary> /// </summary>