From 169db55a8201e72fb0642e4c6d853879b07cfc3c Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Mon, 3 Apr 2023 12:52:01 -0500 Subject: [PATCH] begin cleanup --- src/common/cpp_essentials/pattern.rs | 8 ++++++-- src/qrcode/cpp_port/detector.rs | 11 +++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/common/cpp_essentials/pattern.rs b/src/common/cpp_essentials/pattern.rs index be4aef6..e356204 100644 --- a/src/common/cpp_essentials/pattern.rs +++ b/src/common/cpp_essentials/pattern.rs @@ -116,7 +116,11 @@ impl<'a> PatternView<'_> { Some(*self.data.0.get(self.start)?) } pub fn end(&self) -> Option { - Some(self.data.0[self.start + self.count]) + if self.start + self.count < self.data.0.len() { + Some(self.data.0[self.start + self.count]) + }else { + None + } } // int sum(int n = 0) const { return std::accumulate(_data, _data + (n == 0 ? _size : n), 0); } @@ -473,7 +477,7 @@ pub fn FindLeftGuardBy<'a, const LEN: usize, Pred: Fn(&PatternView, Option) if window.isAtFirstBar() && isGuard(&window, None) { return Ok(window); } - let end = Into::::into(view.end().unwrap()) - minSize; + let end = Into::::into(view.end().ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?) - minSize; while window.start < end { if isGuard(&window, Some(Into::::into(window[PREV_IDX]))) { return Ok(window); diff --git a/src/qrcode/cpp_port/detector.rs b/src/qrcode/cpp_port/detector.rs index 2afd886..cc0f090 100644 --- a/src/qrcode/cpp_port/detector.rs +++ b/src/qrcode/cpp_port/detector.rs @@ -61,8 +61,15 @@ pub fn FindFinderPatterns(image: &BitMatrix, tryHarder: bool) -> FinderPatterns let mut next: PatternView = PatternView::new(&row); while { - let next = FindLeftGuard(&next, 0, &PATTERN, 0.5).unwrap(); - next.isValid() + if let Ok(next) = FindLeftGuard(&next, 0, &PATTERN, 0.5) { + next.isValid() + }else { + false + } + // let Ok(next) = FindLeftGuard(&next, 0, &PATTERN, 0.5) else { + // break; + // }; + // next.isValid() } { let p = point( next.pixelsInFront() as f32