From 1d7fce85c11aa99752c97e36a7c3c1664d141cf8 Mon Sep 17 00:00:00 2001 From: Shane32 Date: Tue, 14 May 2024 00:55:59 -0400 Subject: [PATCH] Eliminate use of LINQ within GetVersion --- QRCoder/QRCodeGenerator.cs | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/QRCoder/QRCodeGenerator.cs b/QRCoder/QRCodeGenerator.cs index bc2c3108..43a69768 100644 --- a/QRCoder/QRCodeGenerator.cs +++ b/QRCoder/QRCodeGenerator.cs @@ -524,23 +524,21 @@ private static void ConvertToDecNotationInPlace(Polynom poly) /// The minimum version of the QR code that can accommodate the given data and settings. private static int GetVersion(int length, EncodingMode encMode, ECCLevel eccLevel) { + // capacity table is already sorted by version number ascending, so the smallest version that can hold the data is the first one found + foreach (var x in capacityTable) + { + // find the requested ECC level and encoding mode in the capacity table + foreach (var y in x.Details) + { + if (y.ErrorCorrectionLevel == eccLevel && y.CapacityDict[encMode] >= length) + { + // if the capacity of the current version is enough, return the version number + return x.Version; + } + } + } - var fittingVersions = capacityTable.Where( - x => x.Details.Any( - y => (y.ErrorCorrectionLevel == eccLevel - && y.CapacityDict[encMode] >= Convert.ToInt32(length) - ) - ) - ).Select(x => new - { - version = x.Version, - capacity = x.Details.Single(y => y.ErrorCorrectionLevel == eccLevel) - .CapacityDict[encMode] - }); - - if (fittingVersions.Any()) - return fittingVersions.Min(x => x.version); - + // if no version was found, throw an exception var maxSizeByte = capacityTable.Where( x => x.Details.Any( y => (y.ErrorCorrectionLevel == eccLevel))