Simplified and clarified small different pieces of code in a few languages.

pull/39/merge
Project Nayuki 6 years ago
parent 6f45d3b573
commit 6841e3716c

@ -368,8 +368,7 @@ class QrCode(object):
ecc = rs.get_remainder(dat) ecc = rs.get_remainder(dat)
if i < numshortblocks: if i < numshortblocks:
dat.append(0) dat.append(0)
dat.extend(ecc) blocks.append(dat + ecc)
blocks.append(dat)
assert k == len(data) assert k == len(data)
# Interleave (not concatenate) the bytes from every block into a single sequence # Interleave (not concatenate) the bytes from every block into a single sequence

@ -240,7 +240,7 @@ impl QrCode {
// Note that Unix newlines (\n) are always used, regardless of the platform. // Note that Unix newlines (\n) are always used, regardless of the platform.
pub fn to_svg_string(&self, border: i32) -> String { pub fn to_svg_string(&self, border: i32) -> String {
assert!(border >= 0, "Border must be non-negative"); assert!(border >= 0, "Border must be non-negative");
let mut result: String = String::new(); let mut result = String::new();
result += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; result += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
result += "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n"; result += "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n";
let dimension = self.size.checked_add(border.checked_mul(2).unwrap()).unwrap(); let dimension = self.size.checked_add(border.checked_mul(2).unwrap()).unwrap();
@ -308,7 +308,7 @@ impl QrCode {
// Calculate error correction code and pack bits // Calculate error correction code and pack bits
let size: i32 = self.size; let size: i32 = self.size;
// errcorrlvl is uint2, mask is uint3 // errcorrlvl is uint2, mask is uint3
let mut data: u32 = self.errorcorrectionlevel.format_bits() << 3 | (mask.value() as u32); let data: u32 = self.errorcorrectionlevel.format_bits() << 3 | (mask.value() as u32);
let mut rem: u32 = data; let mut rem: u32 = data;
for _ in 0 .. 10 { for _ in 0 .. 10 {
rem = (rem << 1) ^ ((rem >> 9) * 0x537); rem = (rem << 1) ^ ((rem >> 9) * 0x537);
@ -420,8 +420,7 @@ impl QrCode {
let rs = ReedSolomonGenerator::new(blockecclen); let rs = ReedSolomonGenerator::new(blockecclen);
let mut k: usize = 0; let mut k: usize = 0;
for i in 0 .. numblocks { for i in 0 .. numblocks {
let mut dat = Vec::<u8>::with_capacity(shortblocklen + 1); let mut dat = data[k .. k + shortblocklen - blockecclen + ((i >= numshortblocks) as usize)].to_vec();
dat.extend_from_slice(&data[k .. k + shortblocklen - blockecclen + ((i >= numshortblocks) as usize)]);
k += dat.len(); k += dat.len();
let ecc: Vec<u8> = rs.get_remainder(&dat); let ecc: Vec<u8> = rs.get_remainder(&dat);
if i < numshortblocks { if i < numshortblocks {

@ -286,11 +286,11 @@ namespace qrcodegen {
// based on the given mask and this object's error correction level field. // based on the given mask and this object's error correction level field.
private drawFormatBits(mask: int): void { private drawFormatBits(mask: int): void {
// Calculate error correction code and pack bits // Calculate error correction code and pack bits
let data: int = this.errorCorrectionLevel.formatBits << 3 | mask; // errCorrLvl is uint2, mask is uint3 const data: int = this.errorCorrectionLevel.formatBits << 3 | mask; // errCorrLvl is uint2, mask is uint3
let rem: int = data; let rem: int = data;
for (let i = 0; i < 10; i++) for (let i = 0; i < 10; i++)
rem = (rem << 1) ^ ((rem >>> 9) * 0x537); rem = (rem << 1) ^ ((rem >>> 9) * 0x537);
let bits = (data << 10 | rem) ^ 0x5412; // uint15 const bits = (data << 10 | rem) ^ 0x5412; // uint15
if (bits >>> 15 != 0) if (bits >>> 15 != 0)
throw "Assertion error"; throw "Assertion error";
@ -322,7 +322,7 @@ namespace qrcodegen {
let rem: int = this.version; // version is uint6, in the range [7, 40] let rem: int = this.version; // version is uint6, in the range [7, 40]
for (let i = 0; i < 12; i++) for (let i = 0; i < 12; i++)
rem = (rem << 1) ^ ((rem >>> 11) * 0x1F25); rem = (rem << 1) ^ ((rem >>> 11) * 0x1F25);
let bits: int = this.version << 12 | rem; // uint18 const bits: int = this.version << 12 | rem; // uint18
if (bits >>> 18 != 0) if (bits >>> 18 != 0)
throw "Assertion error"; throw "Assertion error";

Loading…
Cancel
Save