mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-26 04:12:34 +00:00
Fixes after PR review
This commit is contained in:
@@ -92,5 +92,4 @@ pub use ean_8_writer::*;
|
|||||||
mod upc_e_writer;
|
mod upc_e_writer;
|
||||||
pub use upc_e_writer::*;
|
pub use upc_e_writer::*;
|
||||||
|
|
||||||
mod telepen_common;
|
mod telepen_common;
|
||||||
pub use telepen_common::*;
|
|
||||||
@@ -1,66 +1,98 @@
|
|||||||
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() {
|
||||||
sum += c as u32;
|
sum += c as u32;
|
||||||
}
|
}
|
||||||
|
|
||||||
let remainder = sum % 127;
|
let remainder = sum % 127;
|
||||||
let diff = 127 - remainder;
|
let diff = 127 - remainder;
|
||||||
return if diff != 127 {
|
return if diff != 127 {
|
||||||
diff as u8 as char
|
diff as u8 as char
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
0 as char
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn ascii_to_numeric(contents: &str) -> String {
|
||||||
|
let mut number = String::new();
|
||||||
|
|
||||||
|
for c in contents.chars().map(|x| x as u32) {
|
||||||
|
if c >= 27 {
|
||||||
|
number.push_str(&format!("{:0>2}", (c - 27)));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
0 as char
|
number.push_str(&format!("{:0>2}", (c - 17)));
|
||||||
};
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return number;
|
||||||
|
}
|
||||||
|
|
||||||
|
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."));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ascii_to_numeric(contents: &str) -> String {
|
let mut ascii = String::new();
|
||||||
let mut number = String::new();
|
let mut i = 0;
|
||||||
|
|
||||||
for i in 0 .. contents.len() {
|
while i < contents.len() {
|
||||||
let temp = contents.chars().nth(i).unwrap() as u32;
|
let first = contents.chars().nth(i).unwrap() as u8;
|
||||||
|
let second = contents.chars().nth(i + 1).unwrap() as u8;
|
||||||
|
|
||||||
if temp >= 27 {
|
if second == 88 && first >= 48 && first <= 57 {
|
||||||
number.push_str(&format!("{:0>2}", (temp - 27)));
|
ascii.push((17 + first - 48) as char);
|
||||||
}
|
|
||||||
else {
|
|
||||||
number.push_str(&format!("{:0>2}", (temp - 17)));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
else if second >= 48 && second <= 57 && first >= 48 && first <= 57 {
|
||||||
return number;
|
ascii.push((27 + (first - 48) * 10 + (second - 48)) as char);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return Err(Exceptions::illegal_argument_with(format!("Input contains an invalid character around position {}.", i.to_string())));
|
||||||
|
}
|
||||||
|
|
||||||
|
i += 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn numeric_to_ascii(contents: &str) -> Result<String> {
|
return Ok(ascii);
|
||||||
if contents.len() % 2 != 0 {
|
}
|
||||||
return Err(Exceptions::illegal_argument_with("Input must contain an even number of characters."));
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut ascii = String::new();
|
#[test]
|
||||||
let mut i = 0;
|
fn telepen_checksum_test1() {
|
||||||
|
let contents = "Hello world!";
|
||||||
|
let checksum = calculate_checksum(contents);
|
||||||
|
assert_eq!('\u{1a}', checksum);
|
||||||
|
}
|
||||||
|
|
||||||
while i < contents.len() {
|
#[test]
|
||||||
let first = contents.chars().nth(i).unwrap() as u8;
|
fn telepen_checksum_test2() {
|
||||||
let second = contents.chars().nth(i + 1).unwrap() as u8;
|
let contents = "ABC123456";
|
||||||
|
let checksum = calculate_checksum(contents);
|
||||||
|
assert_eq!('\u{1}', checksum);
|
||||||
|
}
|
||||||
|
|
||||||
if second == 88 && first >= 48 && first <= 57 {
|
#[test]
|
||||||
ascii.push((17 + first - 48) as char);
|
fn telepen_alpha_to_numeric_test() {
|
||||||
}
|
let mut ascii = "'=Siu";
|
||||||
else if second >= 48 && second <= 57 && first >= 48 && first <= 57 {
|
let mut result = ascii_to_numeric(ascii);
|
||||||
ascii.push((27 + (first - 48) * 10 + (second - 48)) as char);
|
assert_eq!("1234567890", result);
|
||||||
}
|
|
||||||
else {
|
|
||||||
return Err(Exceptions::illegal_argument_with(format!("Input contains an invalid character around position {}.", i.to_string())));
|
|
||||||
}
|
|
||||||
|
|
||||||
i += 2;
|
ascii = "& oe";
|
||||||
}
|
result = ascii_to_numeric(ascii);
|
||||||
|
assert_eq!("11058474", result);
|
||||||
|
}
|
||||||
|
|
||||||
return Ok(ascii);
|
#[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::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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,78 +80,80 @@ 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 {
|
||||||
// BBB...
|
"010" => {
|
||||||
result[resultPosition] = true;
|
// BBB...
|
||||||
result[resultPosition + 1] = true;
|
result[resultPosition] = true;
|
||||||
result[resultPosition + 2] = true;
|
result[resultPosition + 1] = true;
|
||||||
result[resultPosition + 3] = false;
|
result[resultPosition + 2] = true;
|
||||||
result[resultPosition + 4] = false;
|
result[resultPosition + 3] = false;
|
||||||
result[resultPosition + 5] = false;
|
result[resultPosition + 4] = 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;
|
||||||
result[resultPosition + 2] = true;
|
result[resultPosition + 2] = true;
|
||||||
result[resultPosition + 3] = false;
|
result[resultPosition + 3] = false;
|
||||||
|
|
||||||
resultPosition += 4;
|
resultPosition += 4;
|
||||||
}
|
},
|
||||||
else if b == "1" {
|
"1" => {
|
||||||
// B.
|
// B.
|
||||||
result[resultPosition] = true;
|
|
||||||
result[resultPosition + 1] = false;
|
|
||||||
|
|
||||||
resultPosition += 2;
|
|
||||||
}
|
|
||||||
else if b == "01" {
|
|
||||||
// B...
|
|
||||||
result[resultPosition] = true;
|
|
||||||
result[resultPosition + 1] = false;
|
|
||||||
result[resultPosition + 2] = false;
|
|
||||||
result[resultPosition + 3] = false;
|
|
||||||
|
|
||||||
resultPosition += 4;
|
|
||||||
}
|
|
||||||
else if b == "10" {
|
|
||||||
// B...
|
|
||||||
result[resultPosition] = true;
|
|
||||||
result[resultPosition + 1] = false;
|
|
||||||
result[resultPosition + 2] = false;
|
|
||||||
result[resultPosition + 3] = false;
|
|
||||||
|
|
||||||
resultPosition += 4;
|
|
||||||
}
|
|
||||||
else if b == "0" {
|
|
||||||
return Err(Exceptions::illegal_argument_with(format!(
|
|
||||||
"Invalid bit combination!"
|
|
||||||
)));
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// B... (B.)* B...
|
|
||||||
result[resultPosition] = true;
|
|
||||||
result[resultPosition + 1] = false;
|
|
||||||
result[resultPosition + 2] = false;
|
|
||||||
result[resultPosition + 3] = false;
|
|
||||||
|
|
||||||
resultPosition += 4;
|
|
||||||
|
|
||||||
for _j in 2 .. b.len() - 2 {
|
|
||||||
result[resultPosition] = true;
|
result[resultPosition] = true;
|
||||||
result[resultPosition + 1] = false;
|
result[resultPosition + 1] = false;
|
||||||
|
|
||||||
resultPosition += 2;
|
resultPosition += 2;
|
||||||
|
},
|
||||||
|
"01" => {
|
||||||
|
// B...
|
||||||
|
result[resultPosition] = true;
|
||||||
|
result[resultPosition + 1] = false;
|
||||||
|
result[resultPosition + 2] = false;
|
||||||
|
result[resultPosition + 3] = false;
|
||||||
|
|
||||||
|
resultPosition += 4;
|
||||||
|
},
|
||||||
|
"10" => {
|
||||||
|
// B...
|
||||||
|
result[resultPosition] = true;
|
||||||
|
result[resultPosition + 1] = false;
|
||||||
|
result[resultPosition + 2] = false;
|
||||||
|
result[resultPosition + 3] = false;
|
||||||
|
|
||||||
|
resultPosition += 4;
|
||||||
|
},
|
||||||
|
"0" => {
|
||||||
|
return Err(Exceptions::illegal_argument_with(format!(
|
||||||
|
"Invalid bit combination!"
|
||||||
|
)));
|
||||||
|
},
|
||||||
|
_ => {
|
||||||
|
// B... (B.)* B...
|
||||||
|
result[resultPosition] = true;
|
||||||
|
result[resultPosition + 1] = false;
|
||||||
|
result[resultPosition + 2] = false;
|
||||||
|
result[resultPosition + 3] = false;
|
||||||
|
|
||||||
|
resultPosition += 4;
|
||||||
|
|
||||||
|
for _j in 2 .. b.len() - 2 {
|
||||||
|
result[resultPosition] = true;
|
||||||
|
result[resultPosition + 1] = false;
|
||||||
|
|
||||||
|
resultPosition += 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
result[resultPosition] = true;
|
||||||
|
result[resultPosition + 1] = false;
|
||||||
|
result[resultPosition + 2] = false;
|
||||||
|
result[resultPosition + 3] = false;
|
||||||
|
|
||||||
|
resultPosition += 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
result[resultPosition] = true;
|
|
||||||
result[resultPosition + 1] = false;
|
|
||||||
result[resultPosition + 2] = false;
|
|
||||||
result[resultPosition + 3] = false;
|
|
||||||
|
|
||||||
resultPosition += 4;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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);
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user