cleanup some SequenceEqual related code

This commit is contained in:
Morilli 2024-10-20 21:24:09 +02:00
parent b7b8788354
commit 4f82b30370
7 changed files with 20 additions and 30 deletions

View File

@ -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
}

View File

@ -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)

View File

@ -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;

View File

@ -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<byte> 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;
}
}
}
}

View File

@ -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!");
}

View File

@ -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)

View File

@ -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<byte>(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)));