Skip to content

Commit

Permalink
Merge pull request codebude#524 from Shane32/eliminate_linq_getversion
Browse files Browse the repository at this point in the history
Eliminate use of LINQ within GetVersion
  • Loading branch information
codebude authored May 14, 2024
2 parents ffc0a7d + 1d7fce8 commit 283a458
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions QRCoder/QRCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -524,23 +524,21 @@ private static void ConvertToDecNotationInPlace(Polynom poly)
/// <returns>The minimum version of the QR code that can accommodate the given data and settings.</returns>
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))
Expand Down

0 comments on commit 283a458

Please sign in to comment.