Reduce Linq usage a little bit more

This commit is contained in:
Matt Nadareski
2025-04-14 13:52:43 -04:00
parent 0f2990d706
commit 7726ef4552
4 changed files with 26 additions and 14 deletions

View File

@@ -1,5 +1,4 @@
using System; using System;
using System.Linq;
namespace SabreTools.Core.Tools namespace SabreTools.Core.Tools
{ {
@@ -156,11 +155,19 @@ namespace SabreTools.Core.Tools
if (value.Length == 0) if (value.Length == 0)
return false; return false;
// Otherwise, make sure that every character is a proper match
for (int i = 0; i < value.Length; i++)
{
char c = value[i];
#if NET7_0_OR_GREATER #if NET7_0_OR_GREATER
return value.All(c => char.IsAsciiHexDigit(c) || c == '.' || c == ','); if (!char.IsAsciiHexDigit(c) && c != '.' && c != ',')
#else #else
return value.All(c => c.IsAsciiHexDigit() || c == '.' || c == ','); if (!c.IsAsciiHexDigit() && c != '.' && c != ',')
#endif #endif
return false;
}
return true;
} }
#if NETFRAMEWORK || NETCOREAPP3_1 || NET5_0 || NET6_0 #if NETFRAMEWORK || NETCOREAPP3_1 || NET5_0 || NET6_0

View File

@@ -1,5 +1,5 @@
using System;
using System.IO; using System.IO;
using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using SabreTools.Hashing; using SabreTools.Hashing;
@@ -107,7 +107,7 @@ namespace SabreTools.Core.Tools
if (string.IsNullOrEmpty(input)) if (string.IsNullOrEmpty(input))
return string.Empty; return string.Empty;
return new string(input!.Where(c => c <= 255).ToArray()); return new string(Array.FindAll(input!.ToCharArray(), c => c <= 255));
} }
#endregion #endregion
@@ -195,12 +195,19 @@ namespace SabreTools.Core.Tools
hash = hash.ToLowerInvariant(); hash = hash.ToLowerInvariant();
// Otherwise, make sure that every character is a proper match // Otherwise, make sure that every character is a proper match
for (int i = 0; i < hash.Length; i++)
{
char c = hash[i];
#if NET7_0_OR_GREATER #if NET7_0_OR_GREATER
if (hash.Any(c => !char.IsAsciiHexDigit(c))) if (!char.IsAsciiHexDigit(c))
#else #else
if (hash.Any(c => !c.IsAsciiHexDigit())) if (!c.IsAsciiHexDigit())
#endif #endif
hash = string.Empty; {
hash = string.Empty;
break;
}
}
return hash; return hash;
} }

View File

@@ -1,6 +1,5 @@
using System; using System;
using System.IO; using System.IO;
using System.Linq;
using SabreTools.Hashing; using SabreTools.Hashing;
using SabreTools.IO.Extensions; using SabreTools.IO.Extensions;
@@ -25,7 +24,7 @@ namespace SabreTools.Core.Tools
return false; return false;
// Otherwise, they need to match exactly // Otherwise, they need to match exactly
return firstHash.SequenceEqual(secondHash); return Matching.Extensions.EqualsExactly(firstHash, secondHash);
} }
/// <summary> /// <summary>

View File

@@ -1069,16 +1069,15 @@ namespace SabreTools.DatFiles
var datItems = itemIndices var datItems = itemIndices
.FindAll(i => _items.ContainsKey(i)) .FindAll(i => _items.ContainsKey(i))
.Select(i => new KeyValuePair<long, DatItem>(i, _items[i])) .ConvertAll(i => new KeyValuePair<long, DatItem>(i, _items[i]));
.ToList();
Sort(ref datItems, norename); Sort(ref datItems, norename);
#if NET40_OR_GREATER || NETCOREAPP #if NET40_OR_GREATER || NETCOREAPP
_buckets.TryAdd(bucketKeys[i], [.. datItems.Select(kvp => kvp.Key)]); _buckets.TryAdd(bucketKeys[i], datItems.ConvertAll(kvp => kvp.Key));
}); });
#else #else
_buckets[bucketKeys[i]] = [.. datItems.Select(kvp => kvp.Key)]; _buckets[bucketKeys[i]] = datItems.ConvertAll(kvp => kvp.Key);
} }
#endif #endif
} }