From 4f82b30370f3fd766063b4052382fa6175c529e3 Mon Sep 17 00:00:00 2001 From: Morilli <35152647+Morilli@users.noreply.github.com> Date: Sun, 20 Oct 2024 21:24:09 +0200 Subject: [PATCH] cleanup some SequenceEqual related code --- .../Consoles/Atari/2600/Atari2600.RomHeuristics.cs | 2 +- .../Consoles/Nintendo/NES/FDS/FDS.cs | 10 ++++------ .../Consoles/Nintendo/NES/NES.cs | 13 +++++-------- .../Consoles/Nintendo/NES/NES.iNES.cs | 10 +++------- .../Consoles/Nintendo/NES/Unif.cs | 5 ++--- src/BizHawk.Emulation.DiscSystem/DiscIdentifier.cs | 5 ++--- src/BizHawk.Tests/Common/checksums/SHA1Tests.cs | 5 +++-- 7 files changed, 20 insertions(+), 30 deletions(-) diff --git a/src/BizHawk.Emulation.Cores/Consoles/Atari/2600/Atari2600.RomHeuristics.cs b/src/BizHawk.Emulation.Cores/Consoles/Atari/2600/Atari2600.RomHeuristics.cs index 743e568155..72ef083e68 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Atari/2600/Atari2600.RomHeuristics.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Atari/2600/Atari2600.RomHeuristics.cs @@ -40,7 +40,7 @@ namespace BizHawk.Emulation.Cores.Atari.Atari2600 return "F8SC"; } - if (rom.Take(4096).SequenceEqual(rom.Skip(4096).Take(4096))) + if (rom.AsSpan(start: 0, length: 4096).SequenceEqual(rom.AsSpan(start: 4096, length: 4096))) { return "4K"; // Again if it is simply the same 4k twice. Got this scenario from Stella logic. Will assume a good reason for it } diff --git a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/FDS/FDS.cs b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/FDS/FDS.cs index cff9525cbf..508e0fd061 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/FDS/FDS.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/FDS/FDS.cs @@ -1,8 +1,8 @@ -using System.Linq; using System.Text; using System.IO; using BizHawk.Common; +using BizHawk.Common.CollectionExtensions; using BizHawk.Emulation.Common; namespace BizHawk.Emulation.Cores.Nintendo.NES @@ -89,12 +89,12 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES public void SetDiskImage(byte[] diskimage) { // each FDS format is worse than the last - if (diskimage.Take(4).SequenceEqual(Encoding.ASCII.GetBytes("\x01*NI"))) + if (diskimage.AsSpan(start: 0, length: 4).SequenceEqual("\x01*NI"u8)) { int nsides = diskimage.Length / 65500; MemoryStream ms = new MemoryStream(); - ms.Write(Encoding.ASCII.GetBytes("FDS\x1A"), 0, 4); + ms.Write("FDS\x1A"u8.ToArray(), 0, 4); ms.WriteByte((byte)nsides); byte[] nulls = new byte[11]; ms.Write(nulls, 0, 11); @@ -194,9 +194,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES // throw new Exception("FDS Saveram: Can't load when a disk is active!"); MemoryStream ms = new MemoryStream(data, false); BinaryReader br = new BinaryReader(ms); - byte[] cmp = Encoding.ASCII.GetBytes("FDSS"); - byte[] tmp = br.ReadBytes(cmp.Length); - if (!cmp.SequenceEqual(tmp)) + if (!br.ReadBytes(4).SequenceEqual("FDSS"u8)) throw new Exception("FDS Saveram: bad header"); int n = br.ReadInt32(); if (n != NumSides) diff --git a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.cs b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.cs index 9411b62b91..d1d4dc49bf 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.cs @@ -339,7 +339,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES origin = EDetectionOrigin.None; if (file.Length < 16) throw new Exception("Alleged NES rom too small to be anything useful"); - if (file.Take(4).SequenceEqual(System.Text.Encoding.ASCII.GetBytes("UNIF"))) + if (file.AsSpan(start: 0, length: 4).SequenceEqual("UNIF"u8)) { unif = new Unif(new MemoryStream(file)); LoadWriteLine("Found UNIF header:"); @@ -349,7 +349,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES hash_sha1_several.Add(hash_sha1); LoadWriteLine("headerless rom hash: {0}", hash_sha1); } - else if(file.Take(5).SequenceEqual(System.Text.Encoding.ASCII.GetBytes("NESM\x1A"))) + else if(file.AsSpan(0, 5).SequenceEqual("NESM\x1A"u8)) { origin = EDetectionOrigin.NSF; LoadWriteLine("Loading as NSF"); @@ -375,8 +375,8 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES return; } - else if (file.Take(4).SequenceEqual(System.Text.Encoding.ASCII.GetBytes("FDS\x1A")) - || file.Take(4).SequenceEqual(System.Text.Encoding.ASCII.GetBytes("\x01*NI"))) + else if (file.AsSpan(start: 0, length: 4).SequenceEqual("FDS\x1A"u8) + || file.AsSpan(start: 0, length: 4).SequenceEqual("\x01*NI"u8)) { // danger! this is a different codepath with an early return. accordingly, some // code is duplicated twice... @@ -415,12 +415,9 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES } else { - byte[] nesheader = new byte[16]; - Buffer.BlockCopy(file, 0, nesheader, 0, 16); - bool exists = true; - if (!DetectFromINES(nesheader, out iNesHeaderInfo, out iNesHeaderInfoV2)) + if (!DetectFromINES(file.AsSpan(start: 0, length: 16), out iNesHeaderInfo, out iNesHeaderInfoV2)) { // we don't have an ines header, check if the game hash is in the game db exists = false; diff --git a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.iNES.cs b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.iNES.cs index 0fb18f3d86..e1787db5ab 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.iNES.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.iNES.cs @@ -1,5 +1,3 @@ -using System.Linq; -using System.Text; using BizHawk.Common.NumberExtensions; namespace BizHawk.Emulation.Cores.Nintendo.NES @@ -13,11 +11,9 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES return 1 << (i + 6); } - public static bool DetectFromINES(byte[] data, out CartInfo Cart, out CartInfo CartV2) + public static bool DetectFromINES(ReadOnlySpan data, out CartInfo Cart, out CartInfo CartV2) { - byte[] ID = new byte[4]; - Buffer.BlockCopy(data, 0, ID, 0, 4); - if (!ID.SequenceEqual(Encoding.ASCII.GetBytes("NES\x1A"))) + if (!data[..4].SequenceEqual("NES\x1A"u8)) { Cart = null; CartV2 = null; @@ -115,4 +111,4 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES return true; } } -} \ No newline at end of file +} diff --git a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/Unif.cs b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/Unif.cs index 4fab8b9a78..7b2c1c4a38 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/Unif.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/Unif.cs @@ -1,10 +1,10 @@ using System.Collections.Generic; -using System.Linq; using System.Text; using System.IO; using BizHawk.Common; using BizHawk.Common.BufferExtensions; +using BizHawk.Common.CollectionExtensions; using BizHawk.Common.IOExtensions; @@ -31,8 +31,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES { var br = new BinaryReader(s, Encoding.ASCII); - if (!Encoding.ASCII.GetBytes("UNIF") - .SequenceEqual(br.ReadBytes(4))) + if (!br.ReadBytes(4).SequenceEqual("UNIF"u8)) { throw new Exception("Missing \"UNIF\" header mark!"); } diff --git a/src/BizHawk.Emulation.DiscSystem/DiscIdentifier.cs b/src/BizHawk.Emulation.DiscSystem/DiscIdentifier.cs index a75a1a7f0c..783b367bd5 100644 --- a/src/BizHawk.Emulation.DiscSystem/DiscIdentifier.cs +++ b/src/BizHawk.Emulation.DiscSystem/DiscIdentifier.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Text; using BizHawk.Common; +using BizHawk.Common.CollectionExtensions; using BizHawk.Common.StringExtensions; using ISOParser; @@ -445,9 +446,7 @@ namespace BizHawk.Emulation.DiscSystem var data = ReadDataSectorCached(lba); if (data == null) return false; var cmp = Encoding.ASCII.GetBytes(s); - var cmp2 = new byte[cmp.Length]; - Buffer.BlockCopy(data, offset, cmp2, 0, cmp.Length); - return cmp.SequenceEqual(cmp2); + return cmp.SequenceEqual(data.AsSpan(start: offset, length: cmp.Length)); } private bool SectorContains(string s, int lba = 0) diff --git a/src/BizHawk.Tests/Common/checksums/SHA1Tests.cs b/src/BizHawk.Tests/Common/checksums/SHA1Tests.cs index 3bca144f90..c4b81ac269 100644 --- a/src/BizHawk.Tests/Common/checksums/SHA1Tests.cs +++ b/src/BizHawk.Tests/Common/checksums/SHA1Tests.cs @@ -1,6 +1,7 @@ -using System.Linq; using System.Text; + using BizHawk.Common; +using BizHawk.Common.CollectionExtensions; namespace BizHawk.Tests.Common.checksums { @@ -46,7 +47,7 @@ namespace BizHawk.Tests.Common.checksums Encoding.ASCII.GetBytes(testString).CopyTo(data, 0); byte[] expectedSha2 = [ 0x65, 0x87, 0x84, 0xE2, 0x68, 0xBF, 0xB1, 0x67, 0x94, 0x7B, 0xB7, 0xF3, 0xFB, 0x76, 0x69, 0x62, 0x79, 0x3E, 0x8C, 0x46 ]; - Assert.IsTrue(expectedSha2.SequenceEqual(SHA1Checksum.Compute(new Span(data, 0, 64)))); + Assert.IsTrue(expectedSha2.SequenceEqual(SHA1Checksum.Compute(data.AsSpan(0, 64)))); byte[] expectedSha3 = [ 0x34, 0xF3, 0xA2, 0x57, 0xBD, 0x12, 0x5E, 0x6E, 0x0E, 0x28, 0xD0, 0xE5, 0xDA, 0xBE, 0x22, 0x28, 0x97, 0xFA, 0x69, 0x55 ]; Assert.IsTrue(expectedSha3.SequenceEqual(SHA1Checksum.Compute(data)));