From 1439e8e4a5a966ba3fa38c2771d5c5bc3c46ba31 Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Mon, 27 Jan 2020 00:53:27 +0000 Subject: [PATCH] Simplified some Rust code. --- rust/src/lib.rs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 56b2eff..0d4c018 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -648,7 +648,6 @@ impl QrCode { let mut runcolor = false; let mut runx: i32 = 0; let mut runhistory = FinderPenalty::new(size); - let mut padrun = size; // Add white border to initial run for x in 0 .. size { if self.module(x, y) == runcolor { runx += 1; @@ -658,8 +657,7 @@ impl QrCode { result += 1; } } else { - runhistory.add_history(runx + padrun); - padrun = 0; + runhistory.add_history(runx); if !runcolor { result += runhistory.count_patterns() * PENALTY_N3; } @@ -667,14 +665,13 @@ impl QrCode { runx = 1; } } - result += runhistory.terminate_and_count(runcolor, runx + padrun) * PENALTY_N3; + result += runhistory.terminate_and_count(runcolor, runx) * PENALTY_N3; } // Adjacent modules in column having same color, and finder-like patterns for x in 0 .. size { let mut runcolor = false; let mut runy: i32 = 0; let mut runhistory = FinderPenalty::new(size); - let mut padrun = size; // Add white border to initial run for y in 0 .. size { if self.module(x, y) == runcolor { runy += 1; @@ -684,8 +681,7 @@ impl QrCode { result += 1; } } else { - runhistory.add_history(runy + padrun); - padrun = 0; + runhistory.add_history(runy); if !runcolor { result += runhistory.count_patterns() * PENALTY_N3; } @@ -693,7 +689,7 @@ impl QrCode { runy = 1; } } - result += runhistory.terminate_and_count(runcolor, runy + padrun) * PENALTY_N3; + result += runhistory.terminate_and_count(runcolor, runy) * PENALTY_N3; } // 2*2 blocks of modules having same color @@ -849,7 +845,10 @@ impl FinderPenalty { // Pushes the given value to the front and drops the last value. - pub fn add_history(&mut self, currentrunlength: i32) { + pub fn add_history(&mut self, mut currentrunlength: i32) { + if self.run_history[0] == 0 { + currentrunlength += self.qr_size; // Add white border to initial run + } let rh = &mut self.run_history; for i in (0 .. rh.len()-1).rev() { rh[i + 1] = rh[i];