Skip to content

Commit 0c54783

Browse files
committed
Further optimization of SB in GenerateQRCode function
Set fixed capacity to avoid memory reallocation and added test case
1 parent ee3a6e7 commit 0c54783

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

QRCoder/QRCodeGenerator.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,7 @@ public static QRCodeData GenerateQrCode(string plainText, ECCLevel eccLevel, boo
150150
}
151151
modeIndicator += DecToBin((int)encoding, 4);
152152
var countIndicator = DecToBin(dataInputLength, GetCountIndicatorLength(version, encoding));
153-
var bitString = modeIndicator + countIndicator;
154-
155-
bitString += codedText;
153+
var bitString = modeIndicator + countIndicator + codedText;
156154

157155
return GenerateQrCode(bitString, eccLevel, version);
158156
}
@@ -170,9 +168,10 @@ public static QRCodeData GenerateQrCode(byte[] binaryData, ECCLevel eccLevel)
170168
int version = GetVersion(binaryData.Length, EncodingMode.Byte, eccLevel);
171169

172170
string modeIndicator = DecToBin((int)EncodingMode.Byte, 4);
173-
string countIndicator = DecToBin(binaryData.Length, GetCountIndicatorLength(version, EncodingMode.Byte));
171+
int countIndicatorLen = GetCountIndicatorLength(version, EncodingMode.Byte);
172+
string countIndicator = DecToBin(binaryData.Length, countIndicatorLen);
174173

175-
StringBuilder sb = new StringBuilder();
174+
StringBuilder sb = new StringBuilder(capacity: 4 + countIndicatorLen + (binaryData.Length*8));
176175
sb.Append(modeIndicator).Append(countIndicator);
177176
foreach (byte b in binaryData)
178177
{

QRCoderTests/QRGeneratorTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,16 @@ public void can_encode_byte()
122122
var result = string.Join("", qrData.ModuleMatrix.Select(x => x.ToBitString()).ToArray());
123123
result.ShouldBe("0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001111111001011011111110000000010000010011100100000100000000101110101101101011101000000001011101001010010111010000000010111010001010101110100000000100000100000101000001000000001111111010101011111110000000000000000110110000000000000000111011111111011000100000000001001110001100010000010000000010011110001010001001000000000110011010000001000110000000001110001111001010110110000000000000000111101010011100000000111111101111011100110000000001000001010011101110010000000010111010110101110010100000000101110100110001000110000000001011101011001000100010000000010000010100000100011000000000111111101110101010111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
124124
}
125+
126+
[Fact]
127+
[Category("QRGenerator/TextEncoding")]
128+
public void can_generate_from_bytes()
129+
{
130+
var gen = new QRCodeGenerator();
131+
var qrData = gen.CreateQrCode(Encoding.UTF8.GetBytes("123ABC"), QRCodeGenerator.ECCLevel.L);
132+
var result = string.Join("", qrData.ModuleMatrix.Select(x => x.ToBitString()).ToArray());
133+
result.ShouldBe("0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001111111010111011111110000000010000010001100100000100000000101110101101001011101000000001011101011001010111010000000010111010100100101110100000000100000100111101000001000000001111111010101011111110000000000000000000110000000000000000111100101111110011101000000000111100010011110001110000000000100010100100000001000000000011110011111001110011000000001111101110101001000000000000000000000111100100100100000000111111100001100100110000000001000001000100001111110000000010111010010011111010100000000101110101111001011110000000001011101010101011000000000000010000010111001000010000000000111111101010010010010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
134+
}
125135
}
126136

127137
public static class ExtensionMethods

0 commit comments

Comments
 (0)