Use ReadOnlySpan instead of Span if appropriate

This commit is contained in:
Kurt 2022-06-13 00:04:20 -07:00
parent 600c8f704f
commit 0093927e15
12 changed files with 24 additions and 24 deletions

View file

@ -97,7 +97,7 @@ public static class MoveShopRecordApplicator
shop.SetMasteredRecordFlag(index, true); shop.SetMasteredRecordFlag(index, true);
} }
public static void SetEncounterMasteryFlags(this IMoveShop8Mastery shop, Span<int> moves, Learnset mastery, int level) public static void SetEncounterMasteryFlags(this IMoveShop8Mastery shop, ReadOnlySpan<int> moves, Learnset mastery, int level)
{ {
var possible = shop.MoveShopPermitIndexes; var possible = shop.MoveShopPermitIndexes;
var permit = shop.MoveShopPermitFlags; var permit = shop.MoveShopPermitFlags;

View file

@ -86,7 +86,7 @@ namespace PKHeX.Core
/// </summary> /// </summary>
/// <param name="pk">Pokémon to modify.</param> /// <param name="pk">Pokémon to modify.</param>
/// <param name="moves">Moves to apply.</param> /// <param name="moves">Moves to apply.</param>
public static ModifyResult SetMoves(PKM pk, Span<int> moves) public static ModifyResult SetMoves(PKM pk, ReadOnlySpan<int> moves)
{ {
pk.SetMoves(moves); pk.SetMoves(moves);
pk.HealPP(); pk.HealPP();

View file

@ -58,7 +58,7 @@ namespace PKHeX.Core
/// <param name="pk"></param> /// <param name="pk"></param>
/// <param name="h">History of evolutions present as</param> /// <param name="h">History of evolutions present as</param>
/// <param name="IVs"><see cref="PKM.IVs"/> to use (if already known). Will fetch the current <see cref="PKM.IVs"/> if not provided.</param> /// <param name="IVs"><see cref="PKM.IVs"/> to use (if already known). Will fetch the current <see cref="PKM.IVs"/> if not provided.</param>
public static void SetSuggestedHyperTrainingData(this PKM pk, EvolutionHistory h, Span<int> IVs) public static void SetSuggestedHyperTrainingData(this PKM pk, EvolutionHistory h, ReadOnlySpan<int> IVs)
{ {
if (pk is not IHyperTrain t) if (pk is not IHyperTrain t)
return; return;
@ -83,7 +83,7 @@ namespace PKHeX.Core
pb.ResetCP(); pb.ResetCP();
} }
/// <inheritdoc cref="SetSuggestedHyperTrainingData(PKM,EvolutionHistory,Span{int})"/> /// <inheritdoc cref="SetSuggestedHyperTrainingData(PKM,EvolutionHistory,ReadOnlySpan{int})"/>
public static void SetSuggestedHyperTrainingData(this PKM pk, int[]? IVs = null) => pk.SetSuggestedHyperTrainingData(EvolutionHistory.Empty, IVs ?? pk.IVs); public static void SetSuggestedHyperTrainingData(this PKM pk, int[]? IVs = null) => pk.SetSuggestedHyperTrainingData(EvolutionHistory.Empty, IVs ?? pk.IVs);
/// <summary> /// <summary>

View file

@ -12,7 +12,7 @@ namespace PKHeX.Core
public string Summary => $"{ID:00}: {Offset:X5}-{Offset + Length - 1:X5}, {Length:X5}"; public string Summary => $"{ID:00}: {Offset:X5}-{Offset + Length - 1:X5}, {Length:X5}";
protected abstract bool ChecksumValid(Span<byte> data); protected abstract bool ChecksumValid(ReadOnlySpan<byte> data);
protected abstract void SetChecksum(Span<byte> data); protected abstract void SetChecksum(Span<byte> data);
/// <summary> /// <summary>
@ -21,7 +21,7 @@ namespace PKHeX.Core
/// <param name="blocks">Block info objects used for offset/length</param> /// <param name="blocks">Block info objects used for offset/length</param>
/// <param name="data">Complete data array</param> /// <param name="data">Complete data array</param>
/// <returns>True if checksums are valid, false if anything is invalid.</returns> /// <returns>True if checksums are valid, false if anything is invalid.</returns>
public static bool GetChecksumsValid(IEnumerable<BlockInfo> blocks, Span<byte> data) public static bool GetChecksumsValid(IEnumerable<BlockInfo> blocks, ReadOnlySpan<byte> data)
{ {
foreach (var b in blocks) foreach (var b in blocks)
{ {

View file

@ -32,9 +32,9 @@ namespace PKHeX.Core
} }
private int ChecksumOffset => BlockInfoOffset + 0x14 + ((int)ID * 8) + 6; private int ChecksumOffset => BlockInfoOffset + 0x14 + ((int)ID * 8) + 6;
protected abstract ushort GetChecksum(Span<byte> data); protected abstract ushort GetChecksum(ReadOnlySpan<byte> data);
protected override bool ChecksumValid(Span<byte> data) protected override bool ChecksumValid(ReadOnlySpan<byte> data)
{ {
ushort chk = GetChecksum(data); ushort chk = GetChecksum(data);
var old = ReadUInt16LittleEndian(data[ChecksumOffset..]); var old = ReadUInt16LittleEndian(data[ChecksumOffset..]);
@ -51,18 +51,18 @@ namespace PKHeX.Core
public sealed class BlockInfo6 : BlockInfo3DS public sealed class BlockInfo6 : BlockInfo3DS
{ {
public BlockInfo6(int bo, uint id, int ofs, int len) : base(bo, id, ofs, len) { } public BlockInfo6(int bo, uint id, int ofs, int len) : base(bo, id, ofs, len) { }
protected override ushort GetChecksum(Span<byte> data) => Checksums.CRC16_CCITT(data.Slice(Offset, Length)); protected override ushort GetChecksum(ReadOnlySpan<byte> data) => Checksums.CRC16_CCITT(data.Slice(Offset, Length));
} }
public sealed class BlockInfo7 : BlockInfo3DS public sealed class BlockInfo7 : BlockInfo3DS
{ {
public BlockInfo7(int bo, uint id, int ofs, int len) : base(bo, id, ofs, len) { } public BlockInfo7(int bo, uint id, int ofs, int len) : base(bo, id, ofs, len) { }
protected override ushort GetChecksum(Span<byte> data) => Checksums.CRC16Invert(data.Slice(Offset, Length)); protected override ushort GetChecksum(ReadOnlySpan<byte> data) => Checksums.CRC16Invert(data.Slice(Offset, Length));
} }
public sealed class BlockInfo7b : BlockInfo3DS public sealed class BlockInfo7b : BlockInfo3DS
{ {
public BlockInfo7b(int bo, uint id, int ofs, int len) : base(bo, id, ofs, len) { } public BlockInfo7b(int bo, uint id, int ofs, int len) : base(bo, id, ofs, len) { }
protected override ushort GetChecksum(Span<byte> data) => Checksums.CRC16NoInvert(data.Slice(Offset, Length)); protected override ushort GetChecksum(ReadOnlySpan<byte> data) => Checksums.CRC16NoInvert(data.Slice(Offset, Length));
} }
} }

View file

@ -19,9 +19,9 @@ namespace PKHeX.Core
ChecksumMirror = chkMirror; ChecksumMirror = chkMirror;
} }
private ushort GetChecksum(Span<byte> data) => Checksums.CRC16_CCITT(data.Slice(Offset, Length)); private ushort GetChecksum(ReadOnlySpan<byte> data) => Checksums.CRC16_CCITT(data.Slice(Offset, Length));
protected override bool ChecksumValid(Span<byte> data) protected override bool ChecksumValid(ReadOnlySpan<byte> data)
{ {
ushort chk = GetChecksum(data); ushort chk = GetChecksum(data);
if (chk != ReadUInt16LittleEndian(data[ChecksumOffset..])) if (chk != ReadUInt16LittleEndian(data[ChecksumOffset..]))

View file

@ -21,7 +21,7 @@ namespace PKHeX.Core
SaveCount = ReadUInt32BigEndian(data[(Offset + 8)..]); SaveCount = ReadUInt32BigEndian(data[(Offset + 8)..]);
} }
protected override bool ChecksumValid(Span<byte> data) protected override bool ChecksumValid(ReadOnlySpan<byte> data)
{ {
var chk = GetChecksum(data); var chk = GetChecksum(data);
var old = ReadUInt32BigEndian(data[Offset..]); var old = ReadUInt32BigEndian(data[Offset..]);
@ -35,7 +35,7 @@ namespace PKHeX.Core
WriteUInt32BigEndian(span, chk); WriteUInt32BigEndian(span, chk);
} }
private uint GetChecksum(Span<byte> data) private uint GetChecksum(ReadOnlySpan<byte> data)
{ {
var span = data.Slice(Offset + 4, ChecksumRegionSize); var span = data.Slice(Offset + 4, ChecksumRegionSize);
return Checksums.CheckSum16BigInvert(span); return Checksums.CheckSum16BigInvert(span);

View file

@ -175,11 +175,11 @@ namespace PKHeX.Core
subkey[0xF] ^= 0x87; subkey[0xF] ^= 0x87;
} }
private static void Xor(ReadOnlySpan<byte> b1, ReadOnlySpan<byte> b2, Span<byte> x) private static void Xor(ReadOnlySpan<byte> b1, ReadOnlySpan<byte> b2, Span<byte> result)
{ {
Debug.Assert(b1.Length == b2.Length); Debug.Assert(b1.Length == b2.Length);
for (var i = 0; i < b1.Length; i++) for (var i = 0; i < b1.Length; i++)
x[i] = (byte)(b1[i] ^ b2[i]); result[i] = (byte)(b1[i] ^ b2[i]);
} }
/// <summary> /// <summary>

View file

@ -44,18 +44,18 @@ public sealed class MyItem8a : MyItem
value[1].SetPouch(access.GetBlock(SaveBlockAccessor8LA.KItemKey).Data); value[1].SetPouch(access.GetBlock(SaveBlockAccessor8LA.KItemKey).Data);
value[2].SetPouch(access.GetBlock(SaveBlockAccessor8LA.KItemStored).Data); value[2].SetPouch(access.GetBlock(SaveBlockAccessor8LA.KItemStored).Data);
value[3].SetPouch(access.GetBlock(SaveBlockAccessor8LA.KItemRecipe).Data); value[3].SetPouch(access.GetBlock(SaveBlockAccessor8LA.KItemRecipe).Data);
SaveFavorites(value, access); SaveFavorites((InventoryPouch8a[])value, access);
} }
} }
private static void LoadFavorites(IEnumerable<InventoryPouch8a> pouches, SCBlockAccessor access) private static void LoadFavorites(ReadOnlySpan<InventoryPouch8a> pouches, SCBlockAccessor access)
{ {
var favorites = access.GetBlock(SaveBlockAccessor8LA.KItemFavorite).Data.AsSpan(); var favorites = access.GetBlock(SaveBlockAccessor8LA.KItemFavorite).Data.AsSpan();
foreach (var arr in pouches) foreach (var arr in pouches)
LoadFavorites(arr.Items, favorites); LoadFavorites(arr.Items, favorites);
} }
private static void SaveFavorites(IEnumerable<InventoryPouch> pouches, SCBlockAccessor access) private static void SaveFavorites(ReadOnlySpan<InventoryPouch8a> pouches, SCBlockAccessor access)
{ {
var favorites = access.GetBlock(SaveBlockAccessor8LA.KItemFavorite).Data.AsSpan(); var favorites = access.GetBlock(SaveBlockAccessor8LA.KItemFavorite).Data.AsSpan();
favorites.Clear(); favorites.Clear();
@ -63,7 +63,7 @@ public sealed class MyItem8a : MyItem
SaveFavorites(arr.Items, favorites); SaveFavorites(arr.Items, favorites);
} }
private static void LoadFavorites(IEnumerable<InventoryItem> items, Span<byte> favorites) private static void LoadFavorites(ReadOnlySpan<InventoryItem> items, ReadOnlySpan<byte> favorites)
{ {
foreach (var z in items) foreach (var z in items)
{ {

View file

@ -189,7 +189,7 @@ namespace PKHeX.Core
return result; return result;
} }
internal static T[] ConcatAll<T>(T[] arr1, T[] arr2, Span<T> arr3) internal static T[] ConcatAll<T>(T[] arr1, T[] arr2, ReadOnlySpan<T> arr3)
{ {
int len = arr1.Length + arr2.Length + arr3.Length; int len = arr1.Length + arr2.Length + arr3.Length;
var result = new T[len]; var result = new T[len];

View file

@ -84,7 +84,7 @@ namespace PKHeX.Core
cbList.Sort(beginCount, allowed.Length, Comparer); cbList.Sort(beginCount, allowed.Length, Comparer);
} }
public static void AddCBWithOffset(List<ComboItem> cbList, Span<string> inStrings, int offset) public static void AddCBWithOffset(List<ComboItem> cbList, ReadOnlySpan<string> inStrings, int offset)
{ {
int beginCount = cbList.Count; int beginCount = cbList.Count;
cbList.Capacity += inStrings.Length; cbList.Capacity += inStrings.Length;

View file

@ -196,7 +196,7 @@ namespace PKHeX.Tests.Legality.Shadow
VerifyResultsAntiShiny(Mawile, Encounters3Teams.Mawile, 12345, 51882, stackalloc[] {31, 30, 29, 31, 23, 27}); VerifyResultsAntiShiny(Mawile, Encounters3Teams.Mawile, 12345, 51882, stackalloc[] {31, 30, 29, 31, 23, 27});
} }
private static void VerifyResultsAntiShiny(ReadOnlySpan<uint> resultPIDs, TeamLock[] team, int tid, int sid, Span<int> ivs) private static void VerifyResultsAntiShiny(ReadOnlySpan<uint> resultPIDs, TeamLock[] team, int tid, int sid, ReadOnlySpan<int> ivs)
{ {
var pk3 = new PK3 var pk3 = new PK3
{ {