mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-25 20:02:34 +00:00
Fixes after PR review
This commit is contained in:
@@ -93,4 +93,3 @@ mod upc_e_writer;
|
||||
pub use upc_e_writer::*;
|
||||
|
||||
mod telepen_common;
|
||||
pub use telepen_common::*;
|
||||
@@ -1,8 +1,7 @@
|
||||
pub mod TelepenCommon {
|
||||
use crate::Exceptions;
|
||||
use crate::common::Result;
|
||||
use crate::Exceptions;
|
||||
use crate::common::Result;
|
||||
|
||||
pub fn calculate_checksum(contents: &str) -> char {
|
||||
pub fn calculate_checksum(contents: &str) -> char {
|
||||
let mut sum = 0;
|
||||
|
||||
for c in contents.chars() {
|
||||
@@ -17,26 +16,24 @@ pub mod TelepenCommon {
|
||||
else {
|
||||
0 as char
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ascii_to_numeric(contents: &str) -> String {
|
||||
pub fn ascii_to_numeric(contents: &str) -> String {
|
||||
let mut number = String::new();
|
||||
|
||||
for i in 0 .. contents.len() {
|
||||
let temp = contents.chars().nth(i).unwrap() as u32;
|
||||
|
||||
if temp >= 27 {
|
||||
number.push_str(&format!("{:0>2}", (temp - 27)));
|
||||
for c in contents.chars().map(|x| x as u32) {
|
||||
if c >= 27 {
|
||||
number.push_str(&format!("{:0>2}", (c - 27)));
|
||||
}
|
||||
else {
|
||||
number.push_str(&format!("{:0>2}", (temp - 17)));
|
||||
number.push_str(&format!("{:0>2}", (c - 17)));
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
return Err(Exceptions::illegal_argument_with("Input must contain an even number of characters."));
|
||||
}
|
||||
@@ -62,5 +59,40 @@ pub mod TelepenCommon {
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -20,8 +20,8 @@ use crate::common::{BitArray, Result};
|
||||
use crate::Exceptions;
|
||||
use crate::RXingResult;
|
||||
use crate::DecodeHintValue;
|
||||
use crate::oned::telepen_common;
|
||||
use crate::{point_f, BarcodeFormat};
|
||||
use crate::oned::TelepenCommon;
|
||||
use bit_reverse::ParallelReverse;
|
||||
|
||||
use super::OneDReader;
|
||||
@@ -180,6 +180,12 @@ impl OneDReader for TelepenReader {
|
||||
}
|
||||
|
||||
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];
|
||||
bits.toBytes(0, bytes.as_mut_slice(), 0, byteLength);
|
||||
|
||||
@@ -220,7 +226,7 @@ impl OneDReader for TelepenReader {
|
||||
// Penultimate byte is a block check character.
|
||||
let check = bytes[byteLength - 2];
|
||||
|
||||
let checksum = TelepenCommon::calculate_checksum(&contentString);
|
||||
let checksum = telepen_common::calculate_checksum(&contentString);
|
||||
|
||||
// Validate checksum
|
||||
if check != checksum as u8 {
|
||||
@@ -231,7 +237,7 @@ impl OneDReader for TelepenReader {
|
||||
hints.get(&DecodeHintType::TELEPEN_AS_NUMERIC),
|
||||
Some(DecodeHintValue::TelepenAsNumeric(true))
|
||||
) {
|
||||
contentString = TelepenCommon::ascii_to_numeric(&contentString);
|
||||
contentString = telepen_common::ascii_to_numeric(&contentString);
|
||||
}
|
||||
|
||||
let mut runningCount = 0;
|
||||
@@ -427,8 +433,4 @@ impl TelepenReader {
|
||||
}
|
||||
return Ok((self.counterLength - 1) as u32);
|
||||
}
|
||||
|
||||
pub fn arrayContains(array: &[char], key: char) -> bool {
|
||||
array.contains(&key)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ use rxing_one_d_proc_derive::OneDWriter;
|
||||
use regex::Regex;
|
||||
use crate::common::Result;
|
||||
use crate::BarcodeFormat;
|
||||
use crate::oned::TelepenCommon;
|
||||
use crate::oned::telepen_common;
|
||||
|
||||
use super::OneDimensionalCodeWriter;
|
||||
|
||||
@@ -44,11 +44,11 @@ impl OneDimensionalCodeWriter for TelepenWriter {
|
||||
hints.get(&EncodeHintType::TELEPEN_AS_NUMERIC),
|
||||
Some(EncodeHintValue::TelepenAsNumeric(true))
|
||||
) {
|
||||
decodedContents = TelepenCommon::numeric_to_ascii(&contents).unwrap();
|
||||
decodedContents = telepen_common::numeric_to_ascii(&contents)?;
|
||||
}
|
||||
|
||||
// Calculate the checksum character
|
||||
let checksum = TelepenCommon::calculate_checksum(&decodedContents);
|
||||
let checksum = telepen_common::calculate_checksum(&decodedContents);
|
||||
|
||||
// Build binary string
|
||||
let mut binary = String::new();
|
||||
@@ -80,7 +80,8 @@ impl OneDimensionalCodeWriter for TelepenWriter {
|
||||
let mut resultPosition = 0;
|
||||
|
||||
for b in matches {
|
||||
if b == "010" {
|
||||
match b {
|
||||
"010" => {
|
||||
// BBB...
|
||||
result[resultPosition] = true;
|
||||
result[resultPosition + 1] = true;
|
||||
@@ -90,8 +91,8 @@ impl OneDimensionalCodeWriter for TelepenWriter {
|
||||
result[resultPosition + 5] = false;
|
||||
|
||||
resultPosition += 6;
|
||||
}
|
||||
else if b == "00" {
|
||||
},
|
||||
"00" => {
|
||||
// BBB.
|
||||
result[resultPosition] = true;
|
||||
result[resultPosition + 1] = true;
|
||||
@@ -99,15 +100,15 @@ impl OneDimensionalCodeWriter for TelepenWriter {
|
||||
result[resultPosition + 3] = false;
|
||||
|
||||
resultPosition += 4;
|
||||
}
|
||||
else if b == "1" {
|
||||
},
|
||||
"1" => {
|
||||
// B.
|
||||
result[resultPosition] = true;
|
||||
result[resultPosition + 1] = false;
|
||||
|
||||
resultPosition += 2;
|
||||
}
|
||||
else if b == "01" {
|
||||
},
|
||||
"01" => {
|
||||
// B...
|
||||
result[resultPosition] = true;
|
||||
result[resultPosition + 1] = false;
|
||||
@@ -115,8 +116,8 @@ impl OneDimensionalCodeWriter for TelepenWriter {
|
||||
result[resultPosition + 3] = false;
|
||||
|
||||
resultPosition += 4;
|
||||
}
|
||||
else if b == "10" {
|
||||
},
|
||||
"10" => {
|
||||
// B...
|
||||
result[resultPosition] = true;
|
||||
result[resultPosition + 1] = false;
|
||||
@@ -124,13 +125,13 @@ impl OneDimensionalCodeWriter for TelepenWriter {
|
||||
result[resultPosition + 3] = false;
|
||||
|
||||
resultPosition += 4;
|
||||
}
|
||||
else if b == "0" {
|
||||
},
|
||||
"0" => {
|
||||
return Err(Exceptions::illegal_argument_with(format!(
|
||||
"Invalid bit combination!"
|
||||
)));
|
||||
}
|
||||
else {
|
||||
},
|
||||
_ => {
|
||||
// B... (B.)* B...
|
||||
result[resultPosition] = true;
|
||||
result[resultPosition + 1] = false;
|
||||
@@ -154,6 +155,7 @@ impl OneDimensionalCodeWriter for TelepenWriter {
|
||||
resultPosition += 4;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(result[0 .. resultPosition - 1].to_vec())
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
use rxing::{oned::TelepenReader, BarcodeFormat, oned::TelepenCommon};
|
||||
use rxing::{oned::TelepenReader, BarcodeFormat};
|
||||
|
||||
mod common;
|
||||
|
||||
@@ -51,39 +51,3 @@ fn telepen_numeric_test_case() {
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user