Code tidy-up

This commit is contained in:
Chris Wood
2023-12-31 11:43:23 +00:00
parent 75d56b7282
commit 5fac0353e6
4 changed files with 69 additions and 77 deletions

View File

@@ -91,3 +91,6 @@ pub use ean_8_writer::*;
mod upc_e_writer;
pub use upc_e_writer::*;
mod telepen_common;
pub use telepen_common::*;

View File

@@ -0,0 +1,20 @@
pub struct TelepenCommon;
impl TelepenCommon {
pub fn calculate_checksum(contents: &str) -> char {
let mut sum = 0;
for c in contents.chars() {
sum += c as u32;
}
let remainder = sum % 127;
let diff = 127 - remainder;
return if diff != 127 {
diff as u8 as char
}
else {
0 as char
};
}
}

View File

@@ -20,6 +20,7 @@ use crate::common::{BitArray, Result};
use crate::Exceptions;
use crate::RXingResult;
use crate::{point_f, BarcodeFormat};
use crate::oned::telepen_common::TelepenCommon;
use bit_reverse::ParallelReverse;
use super::OneDReader;
@@ -48,7 +49,7 @@ impl Default for TelepenReader {
impl OneDReader for TelepenReader {
fn decode_row(
&mut self,
_rowNumber: u32,
rowNumber: u32,
row: &crate::common::BitArray,
_hints: &crate::DecodingHintDictionary,
) -> Result<crate::RXingResult> {
@@ -218,34 +219,32 @@ impl OneDReader for TelepenReader {
// Penultimate byte is a block check character.
let check = bytes[byteLength - 2];
// Calculate the checksum character
let mut sum = 0;
for c in contentString.chars() {
sum += c as u32;
}
let remainder = sum % 127;
let diff = 127 - remainder;
let checksum = if diff != 127 {
diff as u8 as char
}
else {
0 as char
};
let checksum = TelepenCommon::calculate_checksum(&contentString);
// Validate checksum
if check != checksum as u8 {
return Err(Exceptions::NOT_FOUND);
}
let mut runningCount = 0;
runningCount += self.counters.iter().take(startOffset).sum::<u32>();
let left: f32 = runningCount as f32;
runningCount += self
.counters
.iter()
.skip(startOffset)
.take(self.counterLength - startOffset - end)
.sum::<u32>();
let right: f32 = runningCount as f32;
let mut result = RXingResult::new(
&contentString,
bytes,
vec![
// TODO: populate with real numbers
point_f(0 as f32, 0 as f32),
point_f(0 as f32, 0 as f32),
point_f(left, rowNumber as f32),
point_f(right, rowNumber as f32),
],
BarcodeFormat::TELEPEN,
);

View File

@@ -18,6 +18,7 @@ use rxing_one_d_proc_derive::OneDWriter;
use regex::Regex;
use crate::common::Result;
use crate::BarcodeFormat;
use crate::oned::telepen_common::TelepenCommon;
use super::OneDimensionalCodeWriter;
@@ -32,77 +33,30 @@ pub struct TelepenWriter;
impl OneDimensionalCodeWriter for TelepenWriter {
fn encode_oned(&self, contents: &str) -> Result<Vec<bool>> {
// Calculate the checksum character
let mut sum = 0;
for c in contents.chars() {
sum += c as u32;
}
let remainder = sum % 127;
let diff = 127 - remainder;
let checksum = if diff != 127 {
diff as u8 as char
}
else {
0 as char
};
let checksum = TelepenCommon::calculate_checksum(contents);
// Build binary string
let mut binary = String::new();
// Opening character is always _
let mut byte = "_".as_bytes()[0];
let mut bits = self.get_bits(byte);
for i in 0..8 {
binary.push(if bits[i] {
'1'
}
else {
'0'
});
}
binary = self.add_to_binary('_', binary);
// Content
for index in 0..contents.chars().count() {
byte = contents.chars().nth(index).unwrap() as u8;
bits = self.get_bits(byte);
for c in contents.chars() {
if c as u8 > 127 {
return Err(Exceptions::illegal_argument_with(format!(
"Telepen only supports ASCII characters."
)));
}
for i in 0..8 {
binary.push(if bits[i] {
'1'
}
else {
'0'
});
}
binary = self.add_to_binary(c, binary)
}
// Checksum
byte = checksum as u8;
bits = self.get_bits(byte);
for i in 0..8 {
binary.push(if bits[i] {
'1'
}
else {
'0'
});
}
binary = self.add_to_binary(checksum, binary);
// Closing character is always z.
byte = "z".as_bytes()[0];
bits = self.get_bits(byte);
for i in 0..8 {
binary.push(if bits[i] {
'1'
}
else {
'0'
});
}
binary = self.add_to_binary('z', binary);
let re = Regex::new(r"^01|10$|01*0|00|1").unwrap();
let matches: Vec<&str> = re.find_iter(&binary).map(|m| m.as_str()).collect();
@@ -214,6 +168,22 @@ impl TelepenWriter {
return bits;
}
fn add_to_binary(&self, c: char, mut binary: String) -> String {
let byte = c as u8;
let bits = self.get_bits(byte);
for i in 0..8 {
binary.push(if bits[i] {
'1'
}
else {
'0'
});
}
return binary;
}
}
/**