From f757716c2c84f9a41f2bb94be6bc4ac47f559493 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Tue, 17 Aug 2021 17:51:01 +0100 Subject: [PATCH] Convert part of loop into LINQ. --- Aaru.Decryption | 2 +- Aaru.Devices/Remote/Remote.cs | 31 +++++++++++++------------------ Aaru.Filesystems/AppleHFS/Info.cs | 6 ++---- Aaru.Images/AaruFormat/Read.cs | 7 ++----- Aaru.Images/AaruFormat/Write.cs | 7 ++----- Aaru.Images/AppleNIB/Read.cs | 20 +++++--------------- Aaru.Images/CDRWin/Verify.cs | 9 +++------ Aaru.Images/CloneCD/Read.cs | 9 ++------- 8 files changed, 30 insertions(+), 61 deletions(-) diff --git a/Aaru.Decryption b/Aaru.Decryption index 73b212d3b..a84d4c28f 160000 --- a/Aaru.Decryption +++ b/Aaru.Decryption @@ -1 +1 @@ -Subproject commit 73b212d3bb730fc67876e3f42bd22d1426289c1c +Subproject commit a84d4c28f6b19cb821913145216d436dfbd332ea diff --git a/Aaru.Devices/Remote/Remote.cs b/Aaru.Devices/Remote/Remote.cs index 912328c38..e4f8bac43 100644 --- a/Aaru.Devices/Remote/Remote.cs +++ b/Aaru.Devices/Remote/Remote.cs @@ -1647,32 +1647,27 @@ namespace Aaru.Devices.Remote int off = tmp.Length; - foreach(Device.MmcSingleCommand command in commands) + foreach(AaruCmdSdhci cmd in commands.Select(command => new AaruCmdSdhci + { + application = command.isApplication, + argument = command.argument, + block_size = command.blockSize, + blocks = command.blocks, + buf_len = (uint)(command.buffer?.Length ?? 0), + command = command.command, + flags = command.flags, + timeout = timeout, + write = command.write + })) { - var cmd = new AaruCmdSdhci - { - application = command.isApplication, - argument = command.argument, - block_size = command.blockSize, - blocks = command.blocks, - buf_len = (uint)(command.buffer?.Length ?? 0), - command = command.command, - flags = command.flags, - timeout = timeout, - write = command.write - }; - tmp = Marshal.StructureToByteArrayLittleEndian(cmd); Array.Copy(tmp, 0, buf, off, tmp.Length); off += tmp.Length; } - foreach(Device.MmcSingleCommand command in commands) + foreach(Device.MmcSingleCommand command in commands.Where(command => (command.buffer?.Length ?? 0) != 0)) { - if((command.buffer?.Length ?? 0) == 0) - continue; - Array.Copy(command.buffer, 0, buf, off, command.buffer.Length); off += command.buffer.Length; diff --git a/Aaru.Filesystems/AppleHFS/Info.cs b/Aaru.Filesystems/AppleHFS/Info.cs index b98f03ee6..92f4f28e9 100644 --- a/Aaru.Filesystems/AppleHFS/Info.cs +++ b/Aaru.Filesystems/AppleHFS/Info.cs @@ -31,6 +31,7 @@ // ****************************************************************************/ using System; +using System.Linq; using System.Text; using Aaru.CommonTypes; using Aaru.CommonTypes.Interfaces; @@ -61,11 +62,8 @@ namespace Aaru.Filesystems foreach(int offset in new[] { 0, 0x200, 0x400, 0x600, 0x800, 0xA00 - }) + }.Where(offset => mdbSector.Length >= offset + 0x7C + 2)) { - if(mdbSector.Length < offset + 0x7C + 2) - continue; - drSigWord = BigEndianBitConverter.ToUInt16(mdbSector, offset); if(drSigWord != AppleCommon.HFS_MAGIC) diff --git a/Aaru.Images/AaruFormat/Read.cs b/Aaru.Images/AaruFormat/Read.cs index b328a412d..803dd6615 100644 --- a/Aaru.Images/AaruFormat/Read.cs +++ b/Aaru.Images/AaruFormat/Read.cs @@ -1218,11 +1218,8 @@ namespace Aaru.DiscImages { var lastTrackInSession = new Track(); - foreach(Track trk in Tracks.Where(trk => trk.TrackSession == leadOuts.Key)) - { - if(trk.TrackSequence > lastTrackInSession.TrackSequence) - lastTrackInSession = trk; - } + foreach(Track trk in Tracks.Where(trk => trk.TrackSession == leadOuts.Key).Where(trk => trk.TrackSequence > lastTrackInSession.TrackSequence)) + lastTrackInSession = trk; if(lastTrackInSession.TrackSequence == 0 || lastTrackInSession.TrackEndSector == (ulong)leadOuts.Value - 1) diff --git a/Aaru.Images/AaruFormat/Write.cs b/Aaru.Images/AaruFormat/Write.cs index 6751ec882..88eeb2347 100644 --- a/Aaru.Images/AaruFormat/Write.cs +++ b/Aaru.Images/AaruFormat/Write.cs @@ -1388,11 +1388,8 @@ namespace Aaru.DiscImages { var lastTrackInSession = new Track(); - foreach(Track trk in Tracks.Where(trk => trk.TrackSession == leadOuts.Key)) - { - if(trk.TrackSequence > lastTrackInSession.TrackSequence) - lastTrackInSession = trk; - } + foreach(Track trk in Tracks.Where(trk => trk.TrackSession == leadOuts.Key).Where(trk => trk.TrackSequence > lastTrackInSession.TrackSequence)) + lastTrackInSession = trk; if(lastTrackInSession.TrackSequence == 0 || lastTrackInSession.TrackEndSector == (ulong)leadOuts.Value - 1) diff --git a/Aaru.Images/AppleNIB/Read.cs b/Aaru.Images/AppleNIB/Read.cs index 4007aa923..a8f6695f2 100644 --- a/Aaru.Images/AppleNIB/Read.cs +++ b/Aaru.Images/AppleNIB/Read.cs @@ -77,22 +77,12 @@ namespace Aaru.DiscImages // Detect ProDOS skewed disks if(skewed) - foreach(Apple2.RawSector sector in tracks[17].sectors) + foreach(bool isDos in from sector in tracks[17].sectors where sector.addressField.sector.SequenceEqual(new byte[] + { + 170, 170 + }) select Apple2.DecodeSector(sector) into sector0 where sector0 != null select sector0[0x01] == 17 && sector0[0x02] < 16 && sector0[0x27] <= 122 && + sector0[0x34] == 35 && sector0[0x35] == 16 && sector0[0x36] == 0 && sector0[0x37] == 1) { - if(!sector.addressField.sector.SequenceEqual(new byte[] - { - 170, 170 - })) - continue; - - byte[] sector0 = Apple2.DecodeSector(sector); - - if(sector0 == null) - continue; - - bool isDos = sector0[0x01] == 17 && sector0[0x02] < 16 && sector0[0x27] <= 122 && - sector0[0x34] == 35 && sector0[0x35] == 16 && sector0[0x36] == 0 && sector0[0x37] == 1; - if(isDos) skewing = _dosSkewing; diff --git a/Aaru.Images/CDRWin/Verify.cs b/Aaru.Images/CDRWin/Verify.cs index f954a5d78..79bad595d 100644 --- a/Aaru.Images/CDRWin/Verify.cs +++ b/Aaru.Images/CDRWin/Verify.cs @@ -60,9 +60,8 @@ namespace Aaru.DiscImages { var ctx = new Sha1Context(); - foreach(IFilter filter in filters) + foreach(Stream stream in filters.Select(filter => filter.GetDataForkStream())) { - Stream stream = filter.GetDataForkStream(); readBytes = 0; verifyBytes = new byte[verifySize]; @@ -89,9 +88,8 @@ namespace Aaru.DiscImages { var ctx = new Md5Context(); - foreach(IFilter filter in filters) + foreach(Stream stream in filters.Select(filter => filter.GetDataForkStream())) { - Stream stream = filter.GetDataForkStream(); readBytes = 0; verifyBytes = new byte[verifySize]; @@ -118,9 +116,8 @@ namespace Aaru.DiscImages { var ctx = new Crc32Context(); - foreach(IFilter filter in filters) + foreach(Stream stream in filters.Select(filter => filter.GetDataForkStream())) { - Stream stream = filter.GetDataForkStream(); readBytes = 0; verifyBytes = new byte[verifySize]; diff --git a/Aaru.Images/CloneCD/Read.cs b/Aaru.Images/CloneCD/Read.cs index ca7c8d675..5a55b8afe 100644 --- a/Aaru.Images/CloneCD/Read.cs +++ b/Aaru.Images/CloneCD/Read.cs @@ -613,14 +613,9 @@ namespace Aaru.DiscImages if(trackIndexes.TryGetValue((byte)tmpTrack.TrackSequence, out Dictionary indexes)) { - foreach(KeyValuePair trackIndex in indexes.OrderBy(i => i.Key)) - { - if(trackIndex.Key <= 1) - continue; - + foreach((byte index, int value) in indexes.OrderBy(i => i.Key).Where(trackIndex => trackIndex.Key > 1)) // Untested as of 20210711 - tmpTrack.Indexes[trackIndex.Key] = trackIndex.Value; - } + tmpTrack.Indexes[index] = value; } if(tmpTrack.TrackType == TrackType.Data)