Fixes after PR review

This commit is contained in:
Chris Wood
2024-01-02 22:42:03 +00:00
parent f315df8456
commit d43c525d3b
5 changed files with 161 additions and 162 deletions

View File

@@ -93,4 +93,3 @@ mod upc_e_writer;
pub use upc_e_writer::*; pub use upc_e_writer::*;
mod telepen_common; mod telepen_common;
pub use telepen_common::*;

View File

@@ -1,8 +1,7 @@
pub mod TelepenCommon { use crate::Exceptions;
use crate::Exceptions; use crate::common::Result;
use crate::common::Result;
pub fn calculate_checksum(contents: &str) -> char { pub fn calculate_checksum(contents: &str) -> char {
let mut sum = 0; let mut sum = 0;
for c in contents.chars() { for c in contents.chars() {
@@ -17,26 +16,24 @@ pub mod TelepenCommon {
else { else {
0 as char 0 as char
}; };
} }
pub fn ascii_to_numeric(contents: &str) -> String { pub fn ascii_to_numeric(contents: &str) -> String {
let mut number = String::new(); let mut number = String::new();
for i in 0 .. contents.len() { for c in contents.chars().map(|x| x as u32) {
let temp = contents.chars().nth(i).unwrap() as u32; if c >= 27 {
number.push_str(&format!("{:0>2}", (c - 27)));
if temp >= 27 {
number.push_str(&format!("{:0>2}", (temp - 27)));
} }
else { else {
number.push_str(&format!("{:0>2}", (temp - 17))); number.push_str(&format!("{:0>2}", (c - 17)));
} }
} }
return number; return number;
} }
pub fn numeric_to_ascii(contents: &str) -> Result<String> { pub fn numeric_to_ascii(contents: &str) -> Result<String> {
if contents.len() % 2 != 0 { if contents.len() % 2 != 0 {
return Err(Exceptions::illegal_argument_with("Input must contain an even number of characters.")); return Err(Exceptions::illegal_argument_with("Input must contain an even number of characters."));
} }
@@ -62,5 +59,40 @@ pub mod TelepenCommon {
} }
return Ok(ascii); return Ok(ascii);
} }
#[test]
fn telepen_checksum_test1() {
let contents = "Hello world!";
let checksum = calculate_checksum(contents);
assert_eq!('\u{1a}', checksum);
}
#[test]
fn telepen_checksum_test2() {
let contents = "ABC123456";
let checksum = calculate_checksum(contents);
assert_eq!('\u{1}', checksum);
}
#[test]
fn telepen_alpha_to_numeric_test() {
let mut ascii = "'=Siu";
let mut result = ascii_to_numeric(ascii);
assert_eq!("1234567890", result);
ascii = "& oe";
result = ascii_to_numeric(ascii);
assert_eq!("11058474", result);
}
#[test]
fn telepen_numeric_to_ascii_test() {
let mut numeric = "1234567890";
let mut result = numeric_to_ascii(numeric).unwrap();
assert_eq!("'=Siu", result);
numeric = "11058474";
result = numeric_to_ascii(numeric).unwrap();
assert_eq!("& oe", result);
} }

View File

@@ -20,8 +20,8 @@ use crate::common::{BitArray, Result};
use crate::Exceptions; use crate::Exceptions;
use crate::RXingResult; use crate::RXingResult;
use crate::DecodeHintValue; use crate::DecodeHintValue;
use crate::oned::telepen_common;
use crate::{point_f, BarcodeFormat}; use crate::{point_f, BarcodeFormat};
use crate::oned::TelepenCommon;
use bit_reverse::ParallelReverse; use bit_reverse::ParallelReverse;
use super::OneDReader; use super::OneDReader;
@@ -180,6 +180,12 @@ impl OneDReader for TelepenReader {
} }
let byteLength = bits.getSizeInBytes(); let byteLength = bits.getSizeInBytes();
// Any Telepen barcode will be longer than two bytes.
if byteLength < 3 {
return Err(Exceptions::NOT_FOUND);
}
let mut bytes: Vec<u8> = vec![0; byteLength]; let mut bytes: Vec<u8> = vec![0; byteLength];
bits.toBytes(0, bytes.as_mut_slice(), 0, byteLength); bits.toBytes(0, bytes.as_mut_slice(), 0, byteLength);
@@ -220,7 +226,7 @@ impl OneDReader for TelepenReader {
// Penultimate byte is a block check character. // Penultimate byte is a block check character.
let check = bytes[byteLength - 2]; let check = bytes[byteLength - 2];
let checksum = TelepenCommon::calculate_checksum(&contentString); let checksum = telepen_common::calculate_checksum(&contentString);
// Validate checksum // Validate checksum
if check != checksum as u8 { if check != checksum as u8 {
@@ -231,7 +237,7 @@ impl OneDReader for TelepenReader {
hints.get(&DecodeHintType::TELEPEN_AS_NUMERIC), hints.get(&DecodeHintType::TELEPEN_AS_NUMERIC),
Some(DecodeHintValue::TelepenAsNumeric(true)) Some(DecodeHintValue::TelepenAsNumeric(true))
) { ) {
contentString = TelepenCommon::ascii_to_numeric(&contentString); contentString = telepen_common::ascii_to_numeric(&contentString);
} }
let mut runningCount = 0; let mut runningCount = 0;
@@ -427,8 +433,4 @@ impl TelepenReader {
} }
return Ok((self.counterLength - 1) as u32); return Ok((self.counterLength - 1) as u32);
} }
pub fn arrayContains(array: &[char], key: char) -> bool {
array.contains(&key)
}
} }

View File

@@ -18,7 +18,7 @@ use rxing_one_d_proc_derive::OneDWriter;
use regex::Regex; use regex::Regex;
use crate::common::Result; use crate::common::Result;
use crate::BarcodeFormat; use crate::BarcodeFormat;
use crate::oned::TelepenCommon; use crate::oned::telepen_common;
use super::OneDimensionalCodeWriter; use super::OneDimensionalCodeWriter;
@@ -44,11 +44,11 @@ impl OneDimensionalCodeWriter for TelepenWriter {
hints.get(&EncodeHintType::TELEPEN_AS_NUMERIC), hints.get(&EncodeHintType::TELEPEN_AS_NUMERIC),
Some(EncodeHintValue::TelepenAsNumeric(true)) Some(EncodeHintValue::TelepenAsNumeric(true))
) { ) {
decodedContents = TelepenCommon::numeric_to_ascii(&contents).unwrap(); decodedContents = telepen_common::numeric_to_ascii(&contents)?;
} }
// Calculate the checksum character // Calculate the checksum character
let checksum = TelepenCommon::calculate_checksum(&decodedContents); let checksum = telepen_common::calculate_checksum(&decodedContents);
// Build binary string // Build binary string
let mut binary = String::new(); let mut binary = String::new();
@@ -80,7 +80,8 @@ impl OneDimensionalCodeWriter for TelepenWriter {
let mut resultPosition = 0; let mut resultPosition = 0;
for b in matches { for b in matches {
if b == "010" { match b {
"010" => {
// BBB... // BBB...
result[resultPosition] = true; result[resultPosition] = true;
result[resultPosition + 1] = true; result[resultPosition + 1] = true;
@@ -90,8 +91,8 @@ impl OneDimensionalCodeWriter for TelepenWriter {
result[resultPosition + 5] = false; result[resultPosition + 5] = false;
resultPosition += 6; resultPosition += 6;
} },
else if b == "00" { "00" => {
// BBB. // BBB.
result[resultPosition] = true; result[resultPosition] = true;
result[resultPosition + 1] = true; result[resultPosition + 1] = true;
@@ -99,15 +100,15 @@ impl OneDimensionalCodeWriter for TelepenWriter {
result[resultPosition + 3] = false; result[resultPosition + 3] = false;
resultPosition += 4; resultPosition += 4;
} },
else if b == "1" { "1" => {
// B. // B.
result[resultPosition] = true; result[resultPosition] = true;
result[resultPosition + 1] = false; result[resultPosition + 1] = false;
resultPosition += 2; resultPosition += 2;
} },
else if b == "01" { "01" => {
// B... // B...
result[resultPosition] = true; result[resultPosition] = true;
result[resultPosition + 1] = false; result[resultPosition + 1] = false;
@@ -115,8 +116,8 @@ impl OneDimensionalCodeWriter for TelepenWriter {
result[resultPosition + 3] = false; result[resultPosition + 3] = false;
resultPosition += 4; resultPosition += 4;
} },
else if b == "10" { "10" => {
// B... // B...
result[resultPosition] = true; result[resultPosition] = true;
result[resultPosition + 1] = false; result[resultPosition + 1] = false;
@@ -124,13 +125,13 @@ impl OneDimensionalCodeWriter for TelepenWriter {
result[resultPosition + 3] = false; result[resultPosition + 3] = false;
resultPosition += 4; resultPosition += 4;
} },
else if b == "0" { "0" => {
return Err(Exceptions::illegal_argument_with(format!( return Err(Exceptions::illegal_argument_with(format!(
"Invalid bit combination!" "Invalid bit combination!"
))); )));
} },
else { _ => {
// B... (B.)* B... // B... (B.)* B...
result[resultPosition] = true; result[resultPosition] = true;
result[resultPosition + 1] = false; result[resultPosition + 1] = false;
@@ -154,6 +155,7 @@ impl OneDimensionalCodeWriter for TelepenWriter {
resultPosition += 4; resultPosition += 4;
} }
} }
}
Ok(result[0 .. resultPosition - 1].to_vec()) Ok(result[0 .. resultPosition - 1].to_vec())
} }

View File

@@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
use rxing::{oned::TelepenReader, BarcodeFormat, oned::TelepenCommon}; use rxing::{oned::TelepenReader, BarcodeFormat};
mod common; mod common;
@@ -51,39 +51,3 @@ fn telepen_numeric_test_case() {
tester.test_black_box(); tester.test_black_box();
} }
#[test]
fn telepen_checksum_test1() {
let contents = "Hello world!";
let checksum = TelepenCommon::calculate_checksum(contents);
assert_eq!('\u{1a}', checksum);
}
#[test]
fn telepen_checksum_test2() {
let contents = "ABC123456";
let checksum = TelepenCommon::calculate_checksum(contents);
assert_eq!('\u{1}', checksum);
}
#[test]
fn telepen_alpha_to_numeric_test() {
let mut ascii = "'=Siu";
let mut result = TelepenCommon::ascii_to_numeric(ascii);
assert_eq!("1234567890", result);
ascii = "& oe";
result = TelepenCommon::ascii_to_numeric(ascii);
assert_eq!("11058474", result);
}
#[test]
fn telepen_numeric_to_ascii_test() {
let mut numeric = "1234567890";
let mut result = TelepenCommon::numeric_to_ascii(numeric).unwrap();
assert_eq!("'=Siu", result);
numeric = "11058474";
result = TelepenCommon::numeric_to_ascii(numeric).unwrap();
assert_eq!("& oe", result);
}