diff --git a/SabreTools.Core/ConcurrentList.cs b/SabreTools.Core/ConcurrentList.cs index 60a9e699..cf986bbd 100644 --- a/SabreTools.Core/ConcurrentList.cs +++ b/SabreTools.Core/ConcurrentList.cs @@ -7,7 +7,11 @@ namespace SabreTools.Core /// /// Thread-safe list class /// +#if NET20 || NET35 || NET40 + public class ConcurrentList : ICollection, IEnumerable, IEnumerable, IList, ICollection, IList +#else public class ConcurrentList : ICollection, IEnumerable, IEnumerable, IList, IReadOnlyCollection, IReadOnlyList, ICollection, IList +#endif { private List _list = []; private readonly object _lock = new(); diff --git a/SabreTools.Core/DictionaryBaseExtensions.cs b/SabreTools.Core/DictionaryBaseExtensions.cs index b70a53e2..348bc826 100644 --- a/SabreTools.Core/DictionaryBaseExtensions.cs +++ b/SabreTools.Core/DictionaryBaseExtensions.cs @@ -362,11 +362,11 @@ namespace SabreTools.Core /// private static bool HasCommonHash(this Disk self, Disk other) { - bool md5Null = string.IsNullOrWhiteSpace(self.ReadString(Disk.MD5Key)); - md5Null ^= string.IsNullOrWhiteSpace(other.ReadString(Disk.MD5Key)); + bool md5Null = string.IsNullOrEmpty(self.ReadString(Disk.MD5Key)); + md5Null ^= string.IsNullOrEmpty(other.ReadString(Disk.MD5Key)); - bool sha1Null = string.IsNullOrWhiteSpace(self.ReadString(Disk.SHA1Key)); - sha1Null ^= string.IsNullOrWhiteSpace(other.ReadString(Disk.SHA1Key)); + bool sha1Null = string.IsNullOrEmpty(self.ReadString(Disk.SHA1Key)); + sha1Null ^= string.IsNullOrEmpty(other.ReadString(Disk.SHA1Key)); return !md5Null || !sha1Null; } @@ -376,17 +376,17 @@ namespace SabreTools.Core /// private static bool HasCommonHash(this Media self, Media other) { - bool md5Null = string.IsNullOrWhiteSpace(self.ReadString(Media.MD5Key)); - md5Null ^= string.IsNullOrWhiteSpace(other.ReadString(Media.MD5Key)); + bool md5Null = string.IsNullOrEmpty(self.ReadString(Media.MD5Key)); + md5Null ^= string.IsNullOrEmpty(other.ReadString(Media.MD5Key)); - bool sha1Null = string.IsNullOrWhiteSpace(self.ReadString(Media.SHA1Key)); - sha1Null ^= string.IsNullOrWhiteSpace(other.ReadString(Media.SHA1Key)); + bool sha1Null = string.IsNullOrEmpty(self.ReadString(Media.SHA1Key)); + sha1Null ^= string.IsNullOrEmpty(other.ReadString(Media.SHA1Key)); - bool sha256Null = string.IsNullOrWhiteSpace(self.ReadString(Media.SHA256Key)); - sha256Null ^= string.IsNullOrWhiteSpace(other.ReadString(Media.SHA256Key)); + bool sha256Null = string.IsNullOrEmpty(self.ReadString(Media.SHA256Key)); + sha256Null ^= string.IsNullOrEmpty(other.ReadString(Media.SHA256Key)); - bool spamsumNull = string.IsNullOrWhiteSpace(self.ReadString(Media.SpamSumKey)); - spamsumNull ^= string.IsNullOrWhiteSpace(other.ReadString(Media.SpamSumKey)); + bool spamsumNull = string.IsNullOrEmpty(self.ReadString(Media.SpamSumKey)); + spamsumNull ^= string.IsNullOrEmpty(other.ReadString(Media.SpamSumKey)); return !md5Null || !sha1Null || !sha256Null || !spamsumNull; } @@ -396,26 +396,26 @@ namespace SabreTools.Core /// private static bool HasCommonHash(this Rom self, Rom other) { - bool crcNull = string.IsNullOrWhiteSpace(self.ReadString(Rom.CRCKey)); - crcNull ^= string.IsNullOrWhiteSpace(other.ReadString(Rom.CRCKey)); + bool crcNull = string.IsNullOrEmpty(self.ReadString(Rom.CRCKey)); + crcNull ^= string.IsNullOrEmpty(other.ReadString(Rom.CRCKey)); - bool md5Null = string.IsNullOrWhiteSpace(self.ReadString(Rom.MD5Key)); - md5Null ^= string.IsNullOrWhiteSpace(other.ReadString(Rom.MD5Key)); + bool md5Null = string.IsNullOrEmpty(self.ReadString(Rom.MD5Key)); + md5Null ^= string.IsNullOrEmpty(other.ReadString(Rom.MD5Key)); - bool sha1Null = string.IsNullOrWhiteSpace(self.ReadString(Rom.SHA1Key)); - sha1Null ^= string.IsNullOrWhiteSpace(other.ReadString(Rom.SHA1Key)); + bool sha1Null = string.IsNullOrEmpty(self.ReadString(Rom.SHA1Key)); + sha1Null ^= string.IsNullOrEmpty(other.ReadString(Rom.SHA1Key)); - bool sha256Null = string.IsNullOrWhiteSpace(self.ReadString(Rom.SHA256Key)); - sha256Null ^= string.IsNullOrWhiteSpace(other.ReadString(Rom.SHA256Key)); + bool sha256Null = string.IsNullOrEmpty(self.ReadString(Rom.SHA256Key)); + sha256Null ^= string.IsNullOrEmpty(other.ReadString(Rom.SHA256Key)); - bool sha384Null = string.IsNullOrWhiteSpace(self.ReadString(Rom.SHA384Key)); - sha384Null ^= string.IsNullOrWhiteSpace(other.ReadString(Rom.SHA384Key)); + bool sha384Null = string.IsNullOrEmpty(self.ReadString(Rom.SHA384Key)); + sha384Null ^= string.IsNullOrEmpty(other.ReadString(Rom.SHA384Key)); - bool sha512Null = string.IsNullOrWhiteSpace(self.ReadString(Rom.SHA512Key)); - sha512Null ^= string.IsNullOrWhiteSpace(other.ReadString(Rom.SHA512Key)); + bool sha512Null = string.IsNullOrEmpty(self.ReadString(Rom.SHA512Key)); + sha512Null ^= string.IsNullOrEmpty(other.ReadString(Rom.SHA512Key)); - bool spamsumNull = string.IsNullOrWhiteSpace(self.ReadString(Rom.SpamSumKey)); - spamsumNull ^= string.IsNullOrWhiteSpace(other.ReadString(Rom.SpamSumKey)); + bool spamsumNull = string.IsNullOrEmpty(self.ReadString(Rom.SpamSumKey)); + spamsumNull ^= string.IsNullOrEmpty(other.ReadString(Rom.SpamSumKey)); return !crcNull || !md5Null || !sha1Null || !sha256Null || !sha384Null || !sha512Null || !spamsumNull; } @@ -425,8 +425,8 @@ namespace SabreTools.Core /// private static bool HasHashes(this Disk disk) { - bool md5Null = string.IsNullOrWhiteSpace(disk.ReadString(Disk.MD5Key)); - bool sha1Null = string.IsNullOrWhiteSpace(disk.ReadString(Disk.SHA1Key)); + bool md5Null = string.IsNullOrEmpty(disk.ReadString(Disk.MD5Key)); + bool sha1Null = string.IsNullOrEmpty(disk.ReadString(Disk.SHA1Key)); return !md5Null || !sha1Null; } @@ -436,10 +436,10 @@ namespace SabreTools.Core /// private static bool HasHashes(this Media media) { - bool md5Null = string.IsNullOrWhiteSpace(media.ReadString(Media.MD5Key)); - bool sha1Null = string.IsNullOrWhiteSpace(media.ReadString(Media.SHA1Key)); - bool sha256Null = string.IsNullOrWhiteSpace(media.ReadString(Media.SHA256Key)); - bool spamsumNull = string.IsNullOrWhiteSpace(media.ReadString(Media.SpamSumKey)); + bool md5Null = string.IsNullOrEmpty(media.ReadString(Media.MD5Key)); + bool sha1Null = string.IsNullOrEmpty(media.ReadString(Media.SHA1Key)); + bool sha256Null = string.IsNullOrEmpty(media.ReadString(Media.SHA256Key)); + bool spamsumNull = string.IsNullOrEmpty(media.ReadString(Media.SpamSumKey)); return !md5Null || !sha1Null || !sha256Null || !spamsumNull; } @@ -449,13 +449,13 @@ namespace SabreTools.Core /// private static bool HasHashes(this Rom rom) { - bool crcNull = string.IsNullOrWhiteSpace(rom.ReadString(Rom.CRCKey)); - bool md5Null = string.IsNullOrWhiteSpace(rom.ReadString(Rom.MD5Key)); - bool sha1Null = string.IsNullOrWhiteSpace(rom.ReadString(Rom.SHA1Key)); - bool sha256Null = string.IsNullOrWhiteSpace(rom.ReadString(Rom.SHA256Key)); - bool sha384Null = string.IsNullOrWhiteSpace(rom.ReadString(Rom.SHA384Key)); - bool sha512Null = string.IsNullOrWhiteSpace(rom.ReadString(Rom.SHA512Key)); - bool spamsumNull = string.IsNullOrWhiteSpace(rom.ReadString(Rom.SpamSumKey)); + bool crcNull = string.IsNullOrEmpty(rom.ReadString(Rom.CRCKey)); + bool md5Null = string.IsNullOrEmpty(rom.ReadString(Rom.MD5Key)); + bool sha1Null = string.IsNullOrEmpty(rom.ReadString(Rom.SHA1Key)); + bool sha256Null = string.IsNullOrEmpty(rom.ReadString(Rom.SHA256Key)); + bool sha384Null = string.IsNullOrEmpty(rom.ReadString(Rom.SHA384Key)); + bool sha512Null = string.IsNullOrEmpty(rom.ReadString(Rom.SHA512Key)); + bool spamsumNull = string.IsNullOrEmpty(rom.ReadString(Rom.SpamSumKey)); return !crcNull || !md5Null || !sha1Null || !sha256Null || !sha384Null || !sha512Null || !spamsumNull; } @@ -466,10 +466,10 @@ namespace SabreTools.Core private static bool HasZeroHash(this Disk disk) { string? md5 = disk.ReadString(Disk.MD5Key); - bool md5Null = string.IsNullOrWhiteSpace(md5) || string.Equals(md5, Constants.MD5Zero, StringComparison.OrdinalIgnoreCase); + bool md5Null = string.IsNullOrEmpty(md5) || string.Equals(md5, Constants.MD5Zero, StringComparison.OrdinalIgnoreCase); string? sha1 = disk.ReadString(Disk.SHA1Key); - bool sha1Null = string.IsNullOrWhiteSpace(sha1) || string.Equals(sha1, Constants.SHA1Zero, StringComparison.OrdinalIgnoreCase); + bool sha1Null = string.IsNullOrEmpty(sha1) || string.Equals(sha1, Constants.SHA1Zero, StringComparison.OrdinalIgnoreCase); return md5Null && sha1Null; } @@ -480,16 +480,16 @@ namespace SabreTools.Core private static bool HasZeroHash(this Media media) { string? md5 = media.ReadString(Media.MD5Key); - bool md5Null = string.IsNullOrWhiteSpace(md5) || string.Equals(md5, Constants.MD5Zero, StringComparison.OrdinalIgnoreCase); + bool md5Null = string.IsNullOrEmpty(md5) || string.Equals(md5, Constants.MD5Zero, StringComparison.OrdinalIgnoreCase); string? sha1 = media.ReadString(Media.SHA1Key); - bool sha1Null = string.IsNullOrWhiteSpace(sha1) || string.Equals(sha1, Constants.SHA1Zero, StringComparison.OrdinalIgnoreCase); + bool sha1Null = string.IsNullOrEmpty(sha1) || string.Equals(sha1, Constants.SHA1Zero, StringComparison.OrdinalIgnoreCase); string? sha256 = media.ReadString(Media.SHA256Key); - bool sha256Null = string.IsNullOrWhiteSpace(sha256) || string.Equals(sha256, Constants.SHA256Zero, StringComparison.OrdinalIgnoreCase); + bool sha256Null = string.IsNullOrEmpty(sha256) || string.Equals(sha256, Constants.SHA256Zero, StringComparison.OrdinalIgnoreCase); string? spamsum = media.ReadString(Media.SpamSumKey); - bool spamsumNull = string.IsNullOrWhiteSpace(spamsum) || string.Equals(spamsum, Constants.SpamSumZero, StringComparison.OrdinalIgnoreCase); + bool spamsumNull = string.IsNullOrEmpty(spamsum) || string.Equals(spamsum, Constants.SpamSumZero, StringComparison.OrdinalIgnoreCase); return md5Null && sha1Null && sha256Null && spamsumNull; } @@ -500,25 +500,25 @@ namespace SabreTools.Core private static bool HasZeroHash(this Rom rom) { string? crc = rom.ReadString(Rom.CRCKey); - bool crcNull = string.IsNullOrWhiteSpace(crc) || string.Equals(crc, Constants.CRCZero, StringComparison.OrdinalIgnoreCase); + bool crcNull = string.IsNullOrEmpty(crc) || string.Equals(crc, Constants.CRCZero, StringComparison.OrdinalIgnoreCase); string? md5 = rom.ReadString(Rom.MD5Key); - bool md5Null = string.IsNullOrWhiteSpace(md5) || string.Equals(md5, Constants.MD5Zero, StringComparison.OrdinalIgnoreCase); + bool md5Null = string.IsNullOrEmpty(md5) || string.Equals(md5, Constants.MD5Zero, StringComparison.OrdinalIgnoreCase); string? sha1 = rom.ReadString(Rom.SHA1Key); - bool sha1Null = string.IsNullOrWhiteSpace(sha1) || string.Equals(sha1, Constants.SHA1Zero, StringComparison.OrdinalIgnoreCase); + bool sha1Null = string.IsNullOrEmpty(sha1) || string.Equals(sha1, Constants.SHA1Zero, StringComparison.OrdinalIgnoreCase); string? sha256 = rom.ReadString(Rom.SHA256Key); - bool sha256Null = string.IsNullOrWhiteSpace(sha256) || string.Equals(sha256, Constants.SHA256Zero, StringComparison.OrdinalIgnoreCase); + bool sha256Null = string.IsNullOrEmpty(sha256) || string.Equals(sha256, Constants.SHA256Zero, StringComparison.OrdinalIgnoreCase); string? sha384 = rom.ReadString(Rom.SHA384Key); - bool sha384Null = string.IsNullOrWhiteSpace(sha384) || string.Equals(sha384, Constants.SHA384Zero, StringComparison.OrdinalIgnoreCase); + bool sha384Null = string.IsNullOrEmpty(sha384) || string.Equals(sha384, Constants.SHA384Zero, StringComparison.OrdinalIgnoreCase); string? sha512 = rom.ReadString(Rom.SHA512Key); - bool sha512Null = string.IsNullOrWhiteSpace(sha512) || string.Equals(sha512, Constants.SHA512Zero, StringComparison.OrdinalIgnoreCase); + bool sha512Null = string.IsNullOrEmpty(sha512) || string.Equals(sha512, Constants.SHA512Zero, StringComparison.OrdinalIgnoreCase); string? spamsum = rom.ReadString(Rom.SpamSumKey); - bool spamsumNull = string.IsNullOrWhiteSpace(spamsum) || string.Equals(spamsum, Constants.SpamSumZero, StringComparison.OrdinalIgnoreCase); + bool spamsumNull = string.IsNullOrEmpty(spamsum) || string.Equals(spamsum, Constants.SpamSumZero, StringComparison.OrdinalIgnoreCase); return crcNull && md5Null && sha1Null && sha256Null && sha384Null && sha512Null && spamsumNull; } @@ -559,12 +559,12 @@ namespace SabreTools.Core string? selfMd5 = self.ReadString(Disk.MD5Key); string? otherMd5 = other.ReadString(Disk.MD5Key); - if (string.IsNullOrWhiteSpace(selfMd5) && !string.IsNullOrWhiteSpace(otherMd5)) + if (string.IsNullOrEmpty(selfMd5) && !string.IsNullOrEmpty(otherMd5)) self[Disk.MD5Key] = otherMd5; string? selfSha1 = self.ReadString(Disk.SHA1Key); string? otherSha1 = other.ReadString(Disk.SHA1Key); - if (string.IsNullOrWhiteSpace(selfSha1) && !string.IsNullOrWhiteSpace(otherSha1)) + if (string.IsNullOrEmpty(selfSha1) && !string.IsNullOrEmpty(otherSha1)) self[Disk.SHA1Key] = otherSha1; } @@ -578,22 +578,22 @@ namespace SabreTools.Core string? selfMd5 = self.ReadString(Media.MD5Key); string? otherMd5 = other.ReadString(Media.MD5Key); - if (string.IsNullOrWhiteSpace(selfMd5) && !string.IsNullOrWhiteSpace(otherMd5)) + if (string.IsNullOrEmpty(selfMd5) && !string.IsNullOrEmpty(otherMd5)) self[Media.MD5Key] = otherMd5; string? selfSha1 = self.ReadString(Media.SHA1Key); string? otherSha1 = other.ReadString(Media.SHA1Key); - if (string.IsNullOrWhiteSpace(selfSha1) && !string.IsNullOrWhiteSpace(otherSha1)) + if (string.IsNullOrEmpty(selfSha1) && !string.IsNullOrEmpty(otherSha1)) self[Media.SHA1Key] = otherSha1; string? selfSha256 = self.ReadString(Media.SHA256Key); string? otherSha256 = other.ReadString(Media.SHA256Key); - if (string.IsNullOrWhiteSpace(selfSha256) && !string.IsNullOrWhiteSpace(otherSha256)) + if (string.IsNullOrEmpty(selfSha256) && !string.IsNullOrEmpty(otherSha256)) self[Media.SHA256Key] = otherSha256; string? selfSpamSum = self.ReadString(Media.SpamSumKey); string? otherSpamSum = other.ReadString(Media.SpamSumKey); - if (string.IsNullOrWhiteSpace(selfSpamSum) && !string.IsNullOrWhiteSpace(otherSpamSum)) + if (string.IsNullOrEmpty(selfSpamSum) && !string.IsNullOrEmpty(otherSpamSum)) self[Media.SpamSumKey] = otherSpamSum; } @@ -612,37 +612,37 @@ namespace SabreTools.Core string? selfCrc = self.ReadString(Rom.CRCKey); string? otherCrc = other.ReadString(Rom.CRCKey); - if (string.IsNullOrWhiteSpace(selfCrc) && !string.IsNullOrWhiteSpace(otherCrc)) + if (string.IsNullOrEmpty(selfCrc) && !string.IsNullOrEmpty(otherCrc)) self[Rom.CRCKey] = otherCrc; string? selfMd5 = self.ReadString(Rom.MD5Key); string? otherMd5 = other.ReadString(Rom.MD5Key); - if (string.IsNullOrWhiteSpace(selfMd5) && !string.IsNullOrWhiteSpace(otherMd5)) + if (string.IsNullOrEmpty(selfMd5) && !string.IsNullOrEmpty(otherMd5)) self[Rom.MD5Key] = otherMd5; string? selfSha1 = self.ReadString(Rom.SHA1Key); string? otherSha1 = other.ReadString(Rom.SHA1Key); - if (string.IsNullOrWhiteSpace(selfSha1) && !string.IsNullOrWhiteSpace(otherSha1)) + if (string.IsNullOrEmpty(selfSha1) && !string.IsNullOrEmpty(otherSha1)) self[Rom.SHA1Key] = otherSha1; string? selfSha256 = self.ReadString(Rom.SHA256Key); string? otherSha256 = other.ReadString(Rom.SHA256Key); - if (string.IsNullOrWhiteSpace(selfSha256) && !string.IsNullOrWhiteSpace(otherSha256)) + if (string.IsNullOrEmpty(selfSha256) && !string.IsNullOrEmpty(otherSha256)) self[Rom.SHA256Key] = otherSha256; string? selfSha384 = self.ReadString(Rom.SHA384Key); string? otherSha384 = other.ReadString(Rom.SHA384Key); - if (string.IsNullOrWhiteSpace(selfSha384) && !string.IsNullOrWhiteSpace(otherSha384)) + if (string.IsNullOrEmpty(selfSha384) && !string.IsNullOrEmpty(otherSha384)) self[Rom.SHA384Key] = otherSha384; string? selfSha512 = self.ReadString(Rom.SHA512Key); string? otherSha512 = other.ReadString(Rom.SHA512Key); - if (string.IsNullOrWhiteSpace(selfSha512) && !string.IsNullOrWhiteSpace(otherSha512)) + if (string.IsNullOrEmpty(selfSha512) && !string.IsNullOrEmpty(otherSha512)) self[Rom.SHA512Key] = otherSha512; string? selfSpamSum = self.ReadString(Rom.SpamSumKey); string? otherSpamSum = other.ReadString(Rom.SpamSumKey); - if (string.IsNullOrWhiteSpace(selfSpamSum) && !string.IsNullOrWhiteSpace(otherSpamSum)) + if (string.IsNullOrEmpty(selfSpamSum) && !string.IsNullOrEmpty(otherSpamSum)) self[Rom.SpamSumKey] = otherSpamSum; } @@ -676,11 +676,11 @@ namespace SabreTools.Core return string.Empty; string? md5 = self.ReadString(Disk.MD5Key); - if (!string.IsNullOrWhiteSpace(md5)) + if (!string.IsNullOrEmpty(md5)) return $"_{md5}"; string? sha1 = self.ReadString(Disk.SHA1Key); - if (!string.IsNullOrWhiteSpace(sha1)) + if (!string.IsNullOrEmpty(sha1)) return $"_{sha1}"; return "_1"; @@ -695,19 +695,19 @@ namespace SabreTools.Core return string.Empty; string? md5 = self.ReadString(Media.MD5Key); - if (!string.IsNullOrWhiteSpace(md5)) + if (!string.IsNullOrEmpty(md5)) return $"_{md5}"; string? sha1 = self.ReadString(Media.SHA1Key); - if (!string.IsNullOrWhiteSpace(sha1)) + if (!string.IsNullOrEmpty(sha1)) return $"_{sha1}"; string? sha256 = self.ReadString(Media.SHA256Key); - if (!string.IsNullOrWhiteSpace(sha256)) + if (!string.IsNullOrEmpty(sha256)) return $"_{sha256}"; string? spamSum = self.ReadString(Media.SpamSumKey); - if (!string.IsNullOrWhiteSpace(spamSum)) + if (!string.IsNullOrEmpty(spamSum)) return $"_{spamSum}"; return "_1"; @@ -722,31 +722,31 @@ namespace SabreTools.Core return string.Empty; string? crc = self.ReadString(Rom.CRCKey); - if (!string.IsNullOrWhiteSpace(crc)) + if (!string.IsNullOrEmpty(crc)) return $"_{crc}"; string? md5 = self.ReadString(Rom.MD5Key); - if (!string.IsNullOrWhiteSpace(md5)) + if (!string.IsNullOrEmpty(md5)) return $"_{md5}"; string? sha1 = self.ReadString(Rom.SHA1Key); - if (!string.IsNullOrWhiteSpace(sha1)) + if (!string.IsNullOrEmpty(sha1)) return $"_{sha1}"; string? sha256 = self.ReadString(Rom.SHA256Key); - if (!string.IsNullOrWhiteSpace(sha256)) + if (!string.IsNullOrEmpty(sha256)) return $"_{sha256}"; string? sha384 = self.ReadString(Rom.SHA384Key); - if (!string.IsNullOrWhiteSpace(sha384)) + if (!string.IsNullOrEmpty(sha384)) return $"_{sha384}"; string? sha512 = self.ReadString(Rom.SHA512Key); - if (!string.IsNullOrWhiteSpace(sha512)) + if (!string.IsNullOrEmpty(sha512)) return $"_{sha512}"; string? spamSum = self.ReadString(Rom.SpamSumKey); - if (!string.IsNullOrWhiteSpace(spamSum)) + if (!string.IsNullOrEmpty(spamSum)) return $"_{spamSum}"; return "_1"; diff --git a/SabreTools.Core/Globals.cs b/SabreTools.Core/Globals.cs index 0566baba..e8348736 100644 --- a/SabreTools.Core/Globals.cs +++ b/SabreTools.Core/Globals.cs @@ -15,6 +15,7 @@ namespace SabreTools.Core /// public static int MaxThreads { get; set; } = Environment.ProcessorCount; +#if NET452_OR_GREATER || NETCOREAPP /// /// ParallelOptions object for use in parallel operations /// @@ -22,6 +23,7 @@ namespace SabreTools.Core { MaxDegreeOfParallelism = MaxThreads }; +#endif #endregion } diff --git a/SabreTools.Core/Prepare.cs b/SabreTools.Core/Prepare.cs index 551fb86c..4d5b8cb7 100644 --- a/SabreTools.Core/Prepare.cs +++ b/SabreTools.Core/Prepare.cs @@ -27,6 +27,7 @@ namespace SabreTools.Core mid = $"|{mid.PadLeft(((width - mid.Length) / 2) + mid.Length).PadRight(width)}|"; // If we're outputting to console, do fancy things +#if NET452_OR_GREATER || NETCOREAPP if (!Console.IsOutputRedirected) { // Set the console to ready state @@ -47,6 +48,7 @@ namespace SabreTools.Core Console.ForegroundColor = formertext; Console.BackgroundColor = formerback; } +#endif } } } diff --git a/SabreTools.Core/SabreTools.Core.csproj b/SabreTools.Core/SabreTools.Core.csproj index f1f0d761..6055520c 100644 --- a/SabreTools.Core/SabreTools.Core.csproj +++ b/SabreTools.Core/SabreTools.Core.csproj @@ -1,16 +1,38 @@  - net6.0;net8.0 + + net20;net35;net40;net452;net462;net472;net48;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0 + win-x86;win-x64;win-arm64;linux-x64;linux-arm64;osx-x64 + false + false latest enable + true true + 1.1.2 + + + Matt Nadareski + Copyright (c)2016-2023 Matt Nadareski + https://github.com/SabreTools/ + https://github.com/SabreTools/SabreTools + git + + + + + + + + + diff --git a/SabreTools.Core/Tools/Aaru/SpamSumContext.cs b/SabreTools.Core/Tools/Aaru/SpamSumContext.cs index 94e4ea28..ea1d4f5e 100644 --- a/SabreTools.Core/Tools/Aaru/SpamSumContext.cs +++ b/SabreTools.Core/Tools/Aaru/SpamSumContext.cs @@ -49,12 +49,12 @@ namespace Aaru.Checksums /// Implements the SpamSum fuzzy hashing algorithm. public sealed class SpamSumContext : IChecksum, IDisposable { - const uint ROLLING_WINDOW = 7; - const uint MIN_BLOCKSIZE = 3; - const uint HASH_PRIME = 0x01000193; - const uint HASH_INIT = 0x28021967; - const uint NUM_BLOCKHASHES = 31; - const uint SPAMSUM_LENGTH = 64; + const uint ROLLING_WINDOW = 7; + const uint MIN_BLOCKSIZE = 3; + const uint HASH_PRIME = 0x01000193; + const uint HASH_INIT = 0x28021967; + const uint NUM_BLOCKHASHES = 31; + const uint SPAMSUM_LENGTH = 64; const uint FUZZY_MAX_RESULT = (2 * SPAMSUM_LENGTH) + 20; //"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; @@ -76,17 +76,17 @@ namespace Aaru.Checksums Bh = new BlockhashContext[NUM_BLOCKHASHES] }; - for(int i = 0; i < NUM_BLOCKHASHES; i++) + for (int i = 0; i < NUM_BLOCKHASHES; i++) _self.Bh[i].Digest = new byte[SPAMSUM_LENGTH]; - _self.Bhstart = 0; - _self.Bhend = 1; - _self.Bh[0].H = HASH_INIT; - _self.Bh[0].Halfh = HASH_INIT; - _self.Bh[0].Digest[0] = 0; + _self.Bhstart = 0; + _self.Bhend = 1; + _self.Bh[0].H = HASH_INIT; + _self.Bh[0].Halfh = HASH_INIT; + _self.Bh[0].Digest[0] = 0; _self.Bh[0].Halfdigest = 0; - _self.Bh[0].Dlen = 0; - _self.TotalSize = 0; + _self.Bh[0].Dlen = 0; + _self.TotalSize = 0; roll_init(); } @@ -98,7 +98,7 @@ namespace Aaru.Checksums { _self.TotalSize += len; - for(int i = 0; i < len; i++) + for (int i = 0; i < len; i++) fuzzy_engine_step(data[i]); } @@ -125,7 +125,9 @@ namespace Aaru.Checksums return CToString(result); } +#if NET452_OR_GREATER || NETCOREAPP [MethodImpl(MethodImplOptions.AggressiveInlining)] +#endif void roll_init() => _self.Roll = new RollState { Window = new byte[ROLLING_WINDOW] @@ -141,7 +143,9 @@ namespace Aaru.Checksums * h3 is a shift/xor based rolling hash, and is mostly needed to ensure that * we can cope with large blocksize values */ +#if NET452_OR_GREATER || NETCOREAPP [MethodImpl(MethodImplOptions.AggressiveInlining)] +#endif void roll_hash(byte c) { _self.Roll.H2 -= _self.Roll.H1; @@ -157,54 +161,64 @@ namespace Aaru.Checksums * in theory should have no effect. This AND has been removed * for performance (jk) */ _self.Roll.H3 <<= 5; - _self.Roll.H3 ^= c; + _self.Roll.H3 ^= c; } +#if NET452_OR_GREATER || NETCOREAPP [MethodImpl(MethodImplOptions.AggressiveInlining)] +#endif uint roll_sum() => _self.Roll.H1 + _self.Roll.H2 + _self.Roll.H3; /* A simple non-rolling hash, based on the FNV hash. */ +#if NET452_OR_GREATER || NETCOREAPP [MethodImpl(MethodImplOptions.AggressiveInlining)] +#endif static uint sum_hash(byte c, uint h) => (h * HASH_PRIME) ^ c; +#if NET452_OR_GREATER || NETCOREAPP [MethodImpl(MethodImplOptions.AggressiveInlining)] +#endif static uint SSDEEP_BS(uint index) => MIN_BLOCKSIZE << (int)index; +#if NET452_OR_GREATER || NETCOREAPP [MethodImpl(MethodImplOptions.AggressiveInlining)] +#endif void fuzzy_try_fork_blockhash() { - if(_self.Bhend >= NUM_BLOCKHASHES) + if (_self.Bhend >= NUM_BLOCKHASHES) return; - if(_self.Bhend == 0) // assert + if (_self.Bhend == 0) // assert throw new Exception("Assertion failed"); uint obh = _self.Bhend - 1; uint nbh = _self.Bhend; - _self.Bh[nbh].H = _self.Bh[obh].H; - _self.Bh[nbh].Halfh = _self.Bh[obh].Halfh; - _self.Bh[nbh].Digest[0] = 0; + _self.Bh[nbh].H = _self.Bh[obh].H; + _self.Bh[nbh].Halfh = _self.Bh[obh].Halfh; + _self.Bh[nbh].Digest[0] = 0; _self.Bh[nbh].Halfdigest = 0; - _self.Bh[nbh].Dlen = 0; + _self.Bh[nbh].Dlen = 0; ++_self.Bhend; } +#if NET452_OR_GREATER || NETCOREAPP [MethodImpl(MethodImplOptions.AggressiveInlining)] +#endif void fuzzy_try_reduce_blockhash() { - if(_self.Bhstart >= _self.Bhend) + if (_self.Bhstart >= _self.Bhend) throw new Exception("Assertion failed"); - if(_self.Bhend - _self.Bhstart < 2) + if (_self.Bhend - _self.Bhstart < 2) /* Need at least two working hashes. */ return; - if((ulong)SSDEEP_BS(_self.Bhstart) * SPAMSUM_LENGTH >= _self.TotalSize) + if ((ulong)SSDEEP_BS(_self.Bhstart) * SPAMSUM_LENGTH >= _self.TotalSize) /* Initial blocksize estimate would select this or a smaller * blocksize. */ return; - if(_self.Bh[_self.Bhstart + 1].Dlen < SPAMSUM_LENGTH / 2) + if (_self.Bh[_self.Bhstart + 1].Dlen < SPAMSUM_LENGTH / 2) /* Estimate adjustment would select this blocksize. */ return; @@ -213,7 +227,9 @@ namespace Aaru.Checksums ++_self.Bhstart; } +#if NET452_OR_GREATER || NETCOREAPP [MethodImpl(MethodImplOptions.AggressiveInlining)] +#endif void fuzzy_engine_step(byte c) { uint i; @@ -223,16 +239,16 @@ namespace Aaru.Checksums roll_hash(c); ulong h = roll_sum(); - for(i = _self.Bhstart; i < _self.Bhend; ++i) + for (i = _self.Bhstart; i < _self.Bhend; ++i) { - _self.Bh[i].H = sum_hash(c, _self.Bh[i].H); + _self.Bh[i].H = sum_hash(c, _self.Bh[i].H); _self.Bh[i].Halfh = sum_hash(c, _self.Bh[i].Halfh); } - for(i = _self.Bhstart; i < _self.Bhend; ++i) + for (i = _self.Bhstart; i < _self.Bhend; ++i) { /* With growing blocksize almost no runs fail the next test. */ - if(h % SSDEEP_BS(i) != SSDEEP_BS(i) - 1) + if (h % SSDEEP_BS(i) != SSDEEP_BS(i) - 1) /* Once this condition is false for one bs, it is * automatically false for all further bs. I.e. if * h === -1 (mod 2*bs) then h === -1 (mod bs). */ @@ -241,13 +257,13 @@ namespace Aaru.Checksums /* We have hit a reset point. We now emit hashes which are * based on all characters in the piece of the message between * the last reset point and this one */ - if(0 == _self.Bh[i].Dlen) + if (0 == _self.Bh[i].Dlen) fuzzy_try_fork_blockhash(); - _self.Bh[i].Digest[_self.Bh[i].Dlen] = _b64[_self.Bh[i].H % 64]; - _self.Bh[i].Halfdigest = _b64[_self.Bh[i].Halfh % 64]; + _self.Bh[i].Digest[_self.Bh[i].Dlen] = _b64[_self.Bh[i].H % 64]; + _self.Bh[i].Halfdigest = _b64[_self.Bh[i].Halfh % 64]; - if(_self.Bh[i].Dlen < SPAMSUM_LENGTH - 1) + if (_self.Bh[i].Dlen < SPAMSUM_LENGTH - 1) { /* We can have a problem with the tail overflowing. The * easiest way to cope with this is to only reset the @@ -256,12 +272,12 @@ namespace Aaru.Checksums * last few pieces of the message into a single piece * */ _self.Bh[i].Digest[++_self.Bh[i].Dlen] = 0; - _self.Bh[i].H = HASH_INIT; + _self.Bh[i].H = HASH_INIT; - if(_self.Bh[i].Dlen >= SPAMSUM_LENGTH / 2) + if (_self.Bh[i].Dlen >= SPAMSUM_LENGTH / 2) continue; - _self.Bh[i].Halfh = HASH_INIT; + _self.Bh[i].Halfh = HASH_INIT; _self.Bh[i].Halfdigest = 0; } else @@ -270,50 +286,52 @@ namespace Aaru.Checksums } // CLAUNIA: Flags seems to never be used in ssdeep, so I just removed it for code simplicity +#if NET452_OR_GREATER || NETCOREAPP [MethodImpl(MethodImplOptions.AggressiveInlining)] +#endif void FuzzyDigest(out byte[] result) { - var sb = new StringBuilder(); - uint bi = _self.Bhstart; - uint h = roll_sum(); - int remain = (int)(FUZZY_MAX_RESULT - 1); /* Exclude terminating '\0'. */ + var sb = new StringBuilder(); + uint bi = _self.Bhstart; + uint h = roll_sum(); + int remain = (int)(FUZZY_MAX_RESULT - 1); /* Exclude terminating '\0'. */ result = new byte[FUZZY_MAX_RESULT]; /* Verify that our elimination was not overeager. */ - if(!(bi == 0 || ((ulong)SSDEEP_BS(bi) / 2) * SPAMSUM_LENGTH < _self.TotalSize)) + if (!(bi == 0 || ((ulong)SSDEEP_BS(bi) / 2) * SPAMSUM_LENGTH < _self.TotalSize)) throw new Exception("Assertion failed"); int resultOff = 0; /* Initial blocksize guess. */ - while((ulong)SSDEEP_BS(bi) * SPAMSUM_LENGTH < _self.TotalSize) + while ((ulong)SSDEEP_BS(bi) * SPAMSUM_LENGTH < _self.TotalSize) { ++bi; - if(bi >= NUM_BLOCKHASHES) + if (bi >= NUM_BLOCKHASHES) throw new OverflowException("The input exceeds data types."); } /* Adapt blocksize guess to actual digest length. */ - while(bi >= _self.Bhend) + while (bi >= _self.Bhend) --bi; - while(bi > _self.Bhstart && + while (bi > _self.Bhstart && _self.Bh[bi].Dlen < SPAMSUM_LENGTH / 2) --bi; - if(bi > 0 && + if (bi > 0 && _self.Bh[bi].Dlen < SPAMSUM_LENGTH / 2) throw new Exception("Assertion failed"); sb.AppendFormat("{0}:", SSDEEP_BS(bi)); int i = Encoding.ASCII.GetBytes(sb.ToString()).Length; - if(i <= 0) + if (i <= 0) /* Maybe snprintf has set errno here? */ throw new OverflowException("The input exceeds data types."); - if(i >= remain) + if (i >= remain) throw new Exception("Assertion failed"); remain -= i; @@ -324,21 +342,21 @@ namespace Aaru.Checksums i = (int)_self.Bh[bi].Dlen; - if(i > remain) + if (i > remain) throw new Exception("Assertion failed"); Array.Copy(_self.Bh[bi].Digest, 0, result, resultOff, i); resultOff += i; - remain -= i; + remain -= i; - if(h != 0) + if (h != 0) { - if(remain <= 0) + if (remain <= 0) throw new Exception("Assertion failed"); result[resultOff] = _b64[_self.Bh[bi].H % 64]; - if(i < 3 || + if (i < 3 || result[resultOff] != result[resultOff - 1] || result[resultOff] != result[resultOff - 2] || result[resultOff] != result[resultOff - 3]) @@ -347,14 +365,14 @@ namespace Aaru.Checksums --remain; } } - else if(_self.Bh[bi].Digest[i] != 0) + else if (_self.Bh[bi].Digest[i] != 0) { - if(remain <= 0) + if (remain <= 0) throw new Exception("Assertion failed"); result[resultOff] = _self.Bh[bi].Digest[i]; - if(i < 3 || + if (i < 3 || result[resultOff] != result[resultOff - 1] || result[resultOff] != result[resultOff - 2] || result[resultOff] != result[resultOff - 3]) @@ -364,33 +382,33 @@ namespace Aaru.Checksums } } - if(remain <= 0) + if (remain <= 0) throw new Exception("Assertion failed"); result[resultOff++] = 0x3A; // ':' --remain; - if(bi < _self.Bhend - 1) + if (bi < _self.Bhend - 1) { ++bi; i = (int)_self.Bh[bi].Dlen; - if(i > remain) + if (i > remain) throw new Exception("Assertion failed"); Array.Copy(_self.Bh[bi].Digest, 0, result, resultOff, i); resultOff += i; - remain -= i; + remain -= i; - if(h != 0) + if (h != 0) { - if(remain <= 0) + if (remain <= 0) throw new Exception("Assertion failed"); - h = _self.Bh[bi].Halfh; + h = _self.Bh[bi].Halfh; result[resultOff] = _b64[h % 64]; - if(i < 3 || + if (i < 3 || result[resultOff] != result[resultOff - 1] || result[resultOff] != result[resultOff - 2] || result[resultOff] != result[resultOff - 3]) @@ -403,14 +421,14 @@ namespace Aaru.Checksums { i = _self.Bh[bi].Halfdigest; - if(i != 0) + if (i != 0) { - if(remain <= 0) + if (remain <= 0) throw new Exception("Assertion failed"); result[resultOff] = (byte)i; - if(i < 3 || + if (i < 3 || result[resultOff] != result[resultOff - 1] || result[resultOff] != result[resultOff - 2] || result[resultOff] != result[resultOff - 3]) @@ -421,12 +439,12 @@ namespace Aaru.Checksums } } } - else if(h != 0) + else if (h != 0) { - if(_self.Bh[bi].Dlen != 0) + if (_self.Bh[bi].Dlen != 0) throw new Exception("Assertion failed"); - if(remain <= 0) + if (remain <= 0) throw new Exception("Assertion failed"); result[resultOff++] = _b64[_self.Bh[bi].H % 64]; @@ -472,16 +490,18 @@ namespace Aaru.Checksums public static string Data(byte[] data, out byte[]? hash) => Data(data, (uint)data.Length, out hash); // Converts an ASCII null-terminated string to .NET string +#if NET452_OR_GREATER || NETCOREAPP [MethodImpl(MethodImplOptions.AggressiveInlining)] +#endif static string CToString(byte[] cString) { int count = 0; // ReSharper disable once LoopCanBeConvertedToQuery // LINQ is six times slower - foreach(byte c in cString) + foreach (byte c in cString) { - if(c == 0) + if (c == 0) break; count++; @@ -491,7 +511,9 @@ namespace Aaru.Checksums } // Converts an ASCII null-terminated string to .NET string +#if NET452_OR_GREATER || NETCOREAPP [MethodImpl(MethodImplOptions.AggressiveInlining)] +#endif static byte[] CToArray(byte[] cString) { int count = 0; @@ -506,7 +528,13 @@ namespace Aaru.Checksums count++; } +#if NETFRAMEWORK + byte[] temp = new byte[count]; + Array.Copy(cString, temp, count); + return temp; +#else return new ReadOnlySpan(cString, 0, count).ToArray(); +#endif } public void Dispose() @@ -532,8 +560,8 @@ namespace Aaru.Checksums * output hash to stay compatible with ssdeep output. */ struct BlockhashContext { - public uint H; - public uint Halfh; + public uint H; + public uint Halfh; public byte[] Digest; // SPAMSUM_LENGTH @@ -543,12 +571,12 @@ namespace Aaru.Checksums struct FuzzyState { - public uint Bhstart; - public uint Bhend; + public uint Bhstart; + public uint Bhend; public BlockhashContext[] Bh; //NUM_BLOCKHASHES - public ulong TotalSize; + public ulong TotalSize; public RollState Roll; } } diff --git a/SabreTools.Core/Tools/AttributeHelper.cs b/SabreTools.Core/Tools/AttributeHelper.cs index 71296bdb..fdac427a 100644 --- a/SabreTools.Core/Tools/AttributeHelper.cs +++ b/SabreTools.Core/Tools/AttributeHelper.cs @@ -23,7 +23,7 @@ namespace SabreTools.Core.Tools // If the value returns a null on ToString, just return null string? valueStr = value.ToString(); - if (string.IsNullOrWhiteSpace(valueStr)) + if (string.IsNullOrEmpty(valueStr)) return null; // Get the member info array diff --git a/SabreTools.Core/Tools/Converters.cs b/SabreTools.Core/Tools/Converters.cs index 82890133..dae0648e 100644 --- a/SabreTools.Core/Tools/Converters.cs +++ b/SabreTools.Core/Tools/Converters.cs @@ -16,6 +16,22 @@ namespace SabreTools.Core.Tools { List fields = []; +#if NET20 || NET35 + if ((hash & Hash.CRC) != 0) + fields.Add(DatItemField.CRC); + if ((hash & Hash.MD5) != 0) + fields.Add(DatItemField.MD5); + if ((hash & Hash.SHA1) != 0) + fields.Add(DatItemField.SHA1); + if ((hash & Hash.SHA256) != 0) + fields.Add(DatItemField.SHA256); + if ((hash & Hash.SHA384) != 0) + fields.Add(DatItemField.SHA384); + if ((hash & Hash.SHA512) != 0) + fields.Add(DatItemField.SHA512); + if ((hash & Hash.SpamSum) != 0) + fields.Add(DatItemField.SpamSum); +#else if (hash.HasFlag(Hash.CRC)) fields.Add(DatItemField.CRC); if (hash.HasFlag(Hash.MD5)) @@ -30,6 +46,7 @@ namespace SabreTools.Core.Tools fields.Add(DatItemField.SHA512); if (hash.HasFlag(Hash.SpamSum)) fields.Add(DatItemField.SpamSum); +#endif return fields; } @@ -66,7 +83,7 @@ namespace SabreTools.Core.Tools return DatHeaderField.NULL; // Normalize the input - input = input.ToLowerInvariant(); + input = input!.ToLowerInvariant(); // Create regex string headerRegex = @"^(dat|header|datheader)[.\-_\s]"; @@ -96,7 +113,7 @@ namespace SabreTools.Core.Tools return DatItemField.NULL; // Normalize the input - input = input.ToLowerInvariant(); + input = input!.ToLowerInvariant(); // Create regex string datItemRegex = @"^(item|datitem)[.\-_\s]"; @@ -198,7 +215,7 @@ namespace SabreTools.Core.Tools return MachineField.NULL; // Normalize the input - input = input.ToLowerInvariant(); + input = input!.ToLowerInvariant(); // Create regex string machineRegex = @"^(game|machine)[.\-_\s]"; @@ -349,8 +366,12 @@ namespace SabreTools.Core.Tools // Build the output dictionary Dictionary mappings = []; - foreach (T value in values) + foreach (T? value in values) { + // If the value is null + if (value == null) + continue; + // Try to get the mapping attribute MappingAttribute? attr = AttributeHelper.GetAttribute(value); if (attr?.Mappings == null || !attr.Mappings.Any()) @@ -593,8 +614,12 @@ namespace SabreTools.Core.Tools // Build the output dictionary Dictionary mappings = []; - foreach (T value in values) + foreach (T? value in values) { + // If the value is null + if (value == null) + continue; + // Try to get the mapping attribute MappingAttribute? attr = AttributeHelper.GetAttribute(value); if (attr?.Mappings == null || !attr.Mappings.Any()) diff --git a/SabreTools.Core/Tools/NumberHelper.cs b/SabreTools.Core/Tools/NumberHelper.cs index a55881f7..26a6f574 100644 --- a/SabreTools.Core/Tools/NumberHelper.cs +++ b/SabreTools.Core/Tools/NumberHelper.cs @@ -41,7 +41,7 @@ namespace SabreTools.Core.Tools public static double? ConvertToDouble(string? numeric) { // If we don't have a valid string, we can't do anything - if (string.IsNullOrWhiteSpace(numeric)) + if (string.IsNullOrEmpty(numeric)) return null; if (!double.TryParse(numeric, out double doubleValue)) @@ -56,11 +56,11 @@ namespace SabreTools.Core.Tools public static long? ConvertToInt64(string? numeric) { // If we don't have a valid string, we can't do anything - if (string.IsNullOrWhiteSpace(numeric)) + if (string.IsNullOrEmpty(numeric)) return null; // Normalize the string for easier comparison - numeric = numeric.ToLowerInvariant(); + numeric = numeric!.ToLowerInvariant(); // Get the multiplication modifier and trim characters long multiplier = DetermineMultiplier(numeric); @@ -84,11 +84,11 @@ namespace SabreTools.Core.Tools /// public static long DetermineMultiplier(string? numeric) { - if (string.IsNullOrWhiteSpace(numeric)) + if (string.IsNullOrEmpty(numeric)) return 0; long multiplier = 1; - if (numeric.EndsWith("k") || numeric.EndsWith("kb")) + if (numeric!.EndsWith("k") || numeric.EndsWith("kb")) multiplier = KiloByte; else if (numeric.EndsWith("ki") || numeric.EndsWith("kib")) multiplier = KibiByte; @@ -130,18 +130,20 @@ namespace SabreTools.Core.Tools public static bool IsNumeric(string? value) { // If we have no value, it is not numeric - if (string.IsNullOrWhiteSpace(value)) + if (string.IsNullOrEmpty(value)) return false; // If we have a hex value - value = value.ToLowerInvariant(); + value = value!.ToLowerInvariant(); if (value.StartsWith("0x")) - value = value[2..]; + value = value.Substring(2); if (DetermineMultiplier(value) > 1) value = value.TrimEnd(['k', 'm', 'g', 't', 'p', 'e', 'z', 'y', 'i', 'b', ' ']); -#if NET7_0_OR_GREATER +#if NETFRAMEWORK || NETCOREAPP3_1 || NET5_0 + return value.All(c => char.IsNumber(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') || c == '.' || c == ','); +#elif NET7_0_OR_GREATER return value.All(c => char.IsAsciiHexDigit(c) || c == '.' || c == ','); #else return value.All(c => c.IsAsciiHexDigit() || c == '.' || c == ','); diff --git a/SabreTools.Core/Tools/TextHelper.cs b/SabreTools.Core/Tools/TextHelper.cs index f2f716b3..e64aad5c 100644 --- a/SabreTools.Core/Tools/TextHelper.cs +++ b/SabreTools.Core/Tools/TextHelper.cs @@ -36,12 +36,12 @@ namespace SabreTools.Core.Tools public static byte[]? StringToByteArray(string? hex) { // If we get null in, we send null out - if (string.IsNullOrWhiteSpace(hex)) + if (string.IsNullOrEmpty(hex)) return null; try { - int NumberChars = hex.Length; + int NumberChars = hex!.Length; byte[] bytes = new byte[NumberChars / 2]; for (int i = 0; i < NumberChars; i += 2) { @@ -65,11 +65,11 @@ namespace SabreTools.Core.Tools /// public static string? NormalizeCharacters(string? input) { - if (string.IsNullOrWhiteSpace(input)) + if (string.IsNullOrEmpty(input)) return input; ///Run the name through the filters to make sure that it's correct - input = NormalizeChars(input); + input = NormalizeChars(input!); input = RussianToLatin(input); input = SearchPattern(input); @@ -119,10 +119,10 @@ namespace SabreTools.Core.Tools /// public static string? RemovePathUnsafeCharacters(string? input) { - if (string.IsNullOrWhiteSpace(input)) + if (string.IsNullOrEmpty(input)) return input; - input = input.ToLowerInvariant(); + input = input!.ToLowerInvariant(); List invalidPath = [.. Path.GetInvalidPathChars()]; return new string(input.Where(c => !invalidPath.Contains(c)).ToArray()); @@ -133,7 +133,7 @@ namespace SabreTools.Core.Tools /// public static string? RemoveUnicodeCharacters(string? input) { - if (string.IsNullOrWhiteSpace(input)) + if (string.IsNullOrEmpty(input)) return input; return new string(input.Where(c => c <= 255).ToArray()); @@ -200,16 +200,16 @@ namespace SabreTools.Core.Tools private static string? NormalizeHashData(string? hash, int expectedLength) { // If we have a known blank hash, return blank - if (string.IsNullOrWhiteSpace(hash)) + if (string.IsNullOrEmpty(hash)) return null; else if (hash == "-" || hash == "_") return string.Empty; // Check to see if it's a "hex" hash - hash = hash.Trim().Replace("0x", string.Empty); + hash = hash!.Trim().Replace("0x", string.Empty); // If we have a blank hash now, return blank - if (string.IsNullOrWhiteSpace(hash)) + if (string.IsNullOrEmpty(hash)) return string.Empty; // If the hash shorter than the required length, pad it diff --git a/SabreTools.Core/Tools/Utilities.cs b/SabreTools.Core/Tools/Utilities.cs index a4daab70..3998f63b 100644 --- a/SabreTools.Core/Tools/Utilities.cs +++ b/SabreTools.Core/Tools/Utilities.cs @@ -32,7 +32,7 @@ namespace SabreTools.Core.Tools public static bool ConditionalHashEquals(string? firstHash, string? secondHash) { // If either hash is empty, we say they're equal for merging - if (string.IsNullOrWhiteSpace(firstHash) || string.IsNullOrWhiteSpace(secondHash)) + if (string.IsNullOrEmpty(firstHash) || string.IsNullOrEmpty(secondHash)) return true; // If they're different sizes, they can't match @@ -56,7 +56,7 @@ namespace SabreTools.Core.Tools return null; // If the hash isn't the right size, then we return null - if (hash.Length != Constants.SHA1Length) + if (hash!.Length != Constants.SHA1Length) return null; // Cap the depth between 0 and 20, for now