Skip to content

Commit 66a4e27

Browse files
committed
Add comments for GetFormatString
1 parent f5a4eaa commit 66a4e27

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

QRCoder/QRCodeGenerator.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,11 +303,19 @@ void AddCodeWordBlocks(int blockNum, int blocksInGroup, int codewordsInGroup, Bi
303303

304304
private static readonly BitArray _getFormatGenerator = new BitArray(new bool[] { true, false, true, false, false, true, true, false, true, true, true });
305305
private static readonly BitArray _getFormatMask = new BitArray(new bool[] { true, false, true, false, true, false, false, false, false, false, true, false, false, true, false });
306+
/// <summary>
307+
/// Generates a BitArray containing the format string for a QR code based on the error correction level and mask pattern version.
308+
/// The format string includes the error correction level, mask pattern version, and error correction coding.
309+
/// </summary>
310+
/// <param name="level">The error correction level to be encoded in the format string.</param>
311+
/// <param name="maskVersion">The mask pattern version to be encoded in the format string.</param>
312+
/// <returns>A BitArray containing the 15-bit format string used in QR code generation.</returns>
306313
private static BitArray GetFormatString(ECCLevel level, int maskVersion)
307314
{
308315
var fStrEcc = new BitArray(15); // Total length including space for mask version and padding
309316
WriteEccLevelAndVersion();
310317

318+
// Apply the format generator polynomial to add error correction to the format string.
311319
int index = 0;
312320
int count = 15;
313321
TrimLeadingZeros(fStrEcc, ref index, ref count);
@@ -317,10 +325,16 @@ private static BitArray GetFormatString(ECCLevel level, int maskVersion)
317325
fStrEcc[index + i] ^= _getFormatGenerator[i];
318326
TrimLeadingZeros(fStrEcc, ref index, ref count);
319327
}
328+
329+
// Align bits with the start of the array.
320330
ShiftTowardsBit0(fStrEcc, index);
331+
332+
// Prefix the error correction bits with the ECC level and version number.
321333
fStrEcc.Length = 10 + 5;
322334
ShiftAwayFromBit0(fStrEcc, (10 - count) + 5);
323335
WriteEccLevelAndVersion();
336+
337+
// XOR the format string with a predefined mask to add robustness against errors.
324338
fStrEcc.Xor(_getFormatMask);
325339
return fStrEcc;
326340

@@ -341,6 +355,8 @@ void WriteEccLevelAndVersion()
341355
default: // M: 00
342356
break;
343357
}
358+
359+
// Insert the 3-bit mask version directly after the error correction level bits.
344360
DecToBin(maskVersion, 3, fStrEcc, 2);
345361
}
346362
}

0 commit comments

Comments
 (0)