Skip to content

Commit 30b9e44

Browse files
committed
fix: removed instance where i was re-implementing chucks_exact
1 parent 90f9a8e commit 30b9e44

2 files changed

Lines changed: 12 additions & 20 deletions

File tree

src/oned/itf_writer.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,28 +47,22 @@ impl OneDimensionalCodeWriter for ITFWriter {
4747

4848
let mut result = vec![false; 9 + 9 * length];
4949
let mut pos = Self::appendPattern(&mut result, 0, &START_PATTERN, true) as usize;
50-
let mut i = 0;
51-
while i < length {
52-
let one = contents
53-
.chars()
54-
.nth(i)
55-
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?
50+
51+
let cached_contents: Vec<char> = contents.chars().collect();
52+
for chunk in cached_contents.chunks_exact(2) {
53+
let one = chunk[0]
5654
.to_digit(10)
5755
.ok_or(Exceptions::PARSE)? as usize;
58-
let two = contents
59-
.chars()
60-
.nth(i + 1)
61-
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?
56+
let two = chunk[1]
6257
.to_digit(10)
6358
.ok_or(Exceptions::PARSE)? as usize;
59+
6460
let mut encoding = [0; 10];
6561
for j in 0..5 {
6662
encoding[2 * j] = PATTERNS[one][j];
6763
encoding[2 * j + 1] = PATTERNS[two][j];
6864
}
6965
pos += Self::appendPattern(&mut result, pos, &encoding, true) as usize;
70-
71-
i += 2;
7266
}
7367
Self::appendPattern(&mut result, pos, &END_PATTERN, true);
7468

src/oned/telepen_common.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,23 @@ pub fn numeric_to_ascii(contents: &str) -> Result<String> {
3535
}
3636

3737
let mut ascii = Vec::with_capacity(contents.chars().count() / 2);
38-
let mut i = 0;
3938

40-
let cached_contents = contents.chars().map(|c| c as u8).collect::<Vec<_>>();
39+
let cached_contents: Vec<u8> = contents.chars().map(|c| c as u8).collect();
4140

42-
while i < cached_contents.len() {
43-
let first = *cached_contents.get(i).unwrap();
44-
let second = *cached_contents.get(i + 1).unwrap();
41+
for (i, chunk) in cached_contents.chunks_exact(2).enumerate() {
42+
let first = chunk[0];
43+
let second = chunk[1];
4544

4645
if second == 88 && (48..=57).contains(&first) {
4746
ascii.push((17 + first - 48) as char);
4847
} else if (48..=57).contains(&second) && (48..=57).contains(&first) {
4948
ascii.push((27 + (first - 48) * 10 + (second - 48)) as char);
5049
} else {
5150
return Err(Exceptions::illegal_argument_with(format!(
52-
"Input contains an invalid character around position {i}."
51+
"Input contains an invalid character around position {}.",
52+
i * 2
5353
)));
5454
}
55-
56-
i += 2;
5755
}
5856

5957
Ok(ascii.iter().collect())

0 commit comments

Comments
 (0)