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