Skip to content

Commit 283a458

Browse files
authored
Merge pull request #524 from Shane32/eliminate_linq_getversion
Eliminate use of LINQ within GetVersion
2 parents ffc0a7d + 1d7fce8 commit 283a458

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

QRCoder/QRCodeGenerator.cs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -524,23 +524,21 @@ private static void ConvertToDecNotationInPlace(Polynom poly)
524524
/// <returns>The minimum version of the QR code that can accommodate the given data and settings.</returns>
525525
private static int GetVersion(int length, EncodingMode encMode, ECCLevel eccLevel)
526526
{
527+
// capacity table is already sorted by version number ascending, so the smallest version that can hold the data is the first one found
528+
foreach (var x in capacityTable)
529+
{
530+
// find the requested ECC level and encoding mode in the capacity table
531+
foreach (var y in x.Details)
532+
{
533+
if (y.ErrorCorrectionLevel == eccLevel && y.CapacityDict[encMode] >= length)
534+
{
535+
// if the capacity of the current version is enough, return the version number
536+
return x.Version;
537+
}
538+
}
539+
}
527540

528-
var fittingVersions = capacityTable.Where(
529-
x => x.Details.Any(
530-
y => (y.ErrorCorrectionLevel == eccLevel
531-
&& y.CapacityDict[encMode] >= Convert.ToInt32(length)
532-
)
533-
)
534-
).Select(x => new
535-
{
536-
version = x.Version,
537-
capacity = x.Details.Single(y => y.ErrorCorrectionLevel == eccLevel)
538-
.CapacityDict[encMode]
539-
});
540-
541-
if (fittingVersions.Any())
542-
return fittingVersions.Min(x => x.version);
543-
541+
// if no version was found, throw an exception
544542
var maxSizeByte = capacityTable.Where(
545543
x => x.Details.Any(
546544
y => (y.ErrorCorrectionLevel == eccLevel))

0 commit comments

Comments
 (0)