Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eliminate use of LINQ within GetVersion #524

Merged
merged 1 commit into from
May 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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