From f40366c40d8d1956081f7ec643d240c02a81df52 Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Mon, 18 Nov 2024 05:22:06 +0000 Subject: [PATCH] Simplified some Rust code by using slice.chunks(). --- rust-no-heap/src/lib.rs | 38 +++++++++----------------------------- rust/src/lib.rs | 38 +++++++++----------------------------- 2 files changed, 18 insertions(+), 58 deletions(-) diff --git a/rust-no-heap/src/lib.rs b/rust-no-heap/src/lib.rs index 48da775..720e30b 100644 --- a/rust-no-heap/src/lib.rs +++ b/rust-no-heap/src/lib.rs @@ -1111,21 +1111,12 @@ impl<'a> QrSegment<'a> { /// /// Panics if the string contains non-digit characters. pub fn make_numeric(text: &str, buf: &'a mut [u8]) -> Self { + assert!(text.bytes().all(|b| (b'0' ..= b'9').contains(&b)), "String contains non-numeric characters"); let mut bb = BitBuffer::new(buf); - let mut accumdata: u32 = 0; - let mut accumcount: u8 = 0; - for b in text.bytes() { - assert!((b'0' ..= b'9').contains(&b), "String contains non-numeric characters"); - accumdata = accumdata * 10 + u32::from(b - b'0'); - accumcount += 1; - if accumcount == 3 { - bb.append_bits(accumdata, 10); - accumdata = 0; - accumcount = 0; - } - } - if accumcount > 0 { // 1 or 2 digits remaining - bb.append_bits(accumdata, accumcount * 3 + 1); + for chunk in text.as_bytes().chunks(3) { + let data: u32 = chunk.iter().fold(0u32, + |acc, &b| acc * 10 + u32::from(b - b'0')); + bb.append_bits(data, (chunk.len() as u8) * 3 + 1); } QrSegment::new(QrSegmentMode::Numeric, text.len(), bb.data, bb.length) } @@ -1139,21 +1130,10 @@ impl<'a> QrSegment<'a> { /// Panics if the string contains non-encodable characters. pub fn make_alphanumeric(text: &str, buf: &'a mut [u8]) -> Self { let mut bb = BitBuffer::new(buf); - let mut accumdata: u32 = 0; - let mut accumcount: u8 = 0; - for c in text.chars() { - let i: usize = ALPHANUMERIC_CHARSET.find(c) - .expect("String contains unencodable characters in alphanumeric mode"); - accumdata = accumdata * 45 + u32::try_from(i).unwrap(); - accumcount += 1; - if accumcount == 2 { - bb.append_bits(accumdata, 11); - accumdata = 0; - accumcount = 0; - } - } - if accumcount > 0 { // 1 character remaining - bb.append_bits(accumdata, 6); + for chunk in text.as_bytes().chunks(2) { + let data: u32 = chunk.iter().fold(0u32, |acc, &b| acc * 45 + u32::try_from( + ALPHANUMERIC_CHARSET.find(char::from(b)).expect("String contains unencodable characters in alphanumeric mode")).unwrap()); + bb.append_bits(data, (chunk.len() as u8) * 5 + 1); } QrSegment::new(QrSegmentMode::Alphanumeric, text.len(), bb.data, bb.length) } diff --git a/rust/src/lib.rs b/rust/src/lib.rs index a8b3e6d..eff521f 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -985,22 +985,13 @@ impl QrSegment { /// /// Panics if the string contains non-digit characters. pub fn make_numeric(text: &str) -> Self { + assert!(text.bytes().all(|b| (b'0' ..= b'9').contains(&b)), "String contains non-numeric characters"); let mut bb = BitBuffer(Vec::with_capacity( text.len().checked_mul(3).unwrap().checked_add(text.len().div_ceil(3)).unwrap())); - let mut accumdata: u32 = 0; - let mut accumcount: u8 = 0; - for b in text.bytes() { - assert!((b'0' ..= b'9').contains(&b), "String contains non-numeric characters"); - accumdata = accumdata * 10 + u32::from(b - b'0'); - accumcount += 1; - if accumcount == 3 { - bb.append_bits(accumdata, 10); - accumdata = 0; - accumcount = 0; - } - } - if accumcount > 0 { // 1 or 2 digits remaining - bb.append_bits(accumdata, accumcount * 3 + 1); + for chunk in text.as_bytes().chunks(3) { + let data: u32 = chunk.iter().fold(0u32, + |acc, &b| acc * 10 + u32::from(b - b'0')); + bb.append_bits(data, (chunk.len() as u8) * 3 + 1); } QrSegment::new(QrSegmentMode::Numeric, text.len(), bb.0) } @@ -1015,21 +1006,10 @@ impl QrSegment { pub fn make_alphanumeric(text: &str) -> Self { let mut bb = BitBuffer(Vec::with_capacity( text.len().checked_mul(5).unwrap().checked_add(text.len().div_ceil(2)).unwrap())); - let mut accumdata: u32 = 0; - let mut accumcount: u32 = 0; - for c in text.chars() { - let i: usize = ALPHANUMERIC_CHARSET.find(c) - .expect("String contains unencodable characters in alphanumeric mode"); - accumdata = accumdata * 45 + u32::try_from(i).unwrap(); - accumcount += 1; - if accumcount == 2 { - bb.append_bits(accumdata, 11); - accumdata = 0; - accumcount = 0; - } - } - if accumcount > 0 { // 1 character remaining - bb.append_bits(accumdata, 6); + for chunk in text.as_bytes().chunks(2) { + let data: u32 = chunk.iter().fold(0u32, |acc, &b| acc * 45 + u32::try_from( + ALPHANUMERIC_CHARSET.find(char::from(b)).expect("String contains unencodable characters in alphanumeric mode")).unwrap()); + bb.append_bits(data, (chunk.len() as u8) * 5 + 1); } QrSegment::new(QrSegmentMode::Alphanumeric, text.len(), bb.0) }