Simplified some Rust code by using slice.chunks().

master
Project Nayuki 5 days ago
parent 2b28bc2671
commit f40366c40d

@ -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)
}

@ -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)
}

Loading…
Cancel
Save