Cleanup of many components of the code.

Many unwraps, expects, and panics removed or handled.

More rustification of the codebase.

Squashed commit of the following:

commit 80c6483cf8c819496a7b781f9e3a149d1f18d5fe
Author: Henry <henry@azule.info>
Date:   Fri Feb 10 20:36:57 2023 -0600

    add other parsed result option

commit 412d92ae585e7bf194697f5282dbc045e448bace
Author: Henry <henry@azule.info>
Date:   Fri Feb 10 19:06:34 2023 -0600

    fix some panics

commit 9108d3e931aadec00fc0bf346ebc52d24e5d8146
Author: Henry <henry@azule.info>
Date:   Fri Feb 10 18:56:30 2023 -0600

    clippy --fix

commit 8cd89d314323f54f342f15fc73297b62d418ac79
Author: Henry <henry@azule.info>
Date:   Fri Feb 10 18:55:45 2023 -0600

    some more cleanup in aztec

commit fb7f9242e273b85a4bb212f22a185a0dcbfafc36
Author: Henry <henry@azule.info>
Date:   Fri Feb 10 18:19:54 2023 -0600

    cleanup root

commit 4fb7c0639830869149332b29be789e891eade3e7
Author: Henry Schimke <henry@azule.info>
Date:   Fri Feb 10 16:50:15 2023 -0600

    format

commit 3ac271af657b25fbe5ee39cbdf8859e5aa38ea41
Author: Henry Schimke <henry@azule.info>
Date:   Fri Feb 10 16:43:14 2023 -0600

    clippy --fix

commit 0c5b4c1477159db93a9da9645039f51cd1a7c574
Author: Henry Schimke <henry@azule.info>
Date:   Fri Feb 10 16:04:31 2023 -0600

    qrcode cleanup complete

commit 70c1d107cc672e45d46567c650c46fece7e7da9c
Author: Henry Schimke <henry@azule.info>
Date:   Fri Feb 10 13:28:25 2023 -0600

    clippy --fix

commit e13263d0f1f2a13dbb59b6e60d8b871a32babc92
Author: Henry Schimke <henry@azule.info>
Date:   Fri Feb 10 13:27:44 2023 -0600

    qrcode detector cleanup

commit f7f2855099e469fe1091868c6099196705d0ae83
Author: Henry Schimke <henry@azule.info>
Date:   Fri Feb 10 11:48:13 2023 -0600

    qrcode decoder

commit bdbaaba597b3479639c2ed3726465a69e3004471
Author: Henry Schimke <henry@azule.info>
Date:   Fri Feb 10 11:13:18 2023 -0600

    clippy --fix

commit fd6dbcaeb1f45dc91ae98f0e7c58aad48d67df78
Author: Henry Schimke <henry@azule.info>
Date:   Fri Feb 10 11:12:28 2023 -0600

    pdf417 cleanup complete

commit 7bbd47604c2639a5eb61bdb5533e71be3bce95a1
Author: Henry Schimke <henry@azule.info>
Date:   Fri Feb 10 10:39:28 2023 -0600

    clippy --fix

commit 8f4962b7bb5f133524fe4d3d4948e5672a88c545
Author: Henry Schimke <henry@azule.info>
Date:   Fri Feb 10 10:39:00 2023 -0600

    cleanup on pdf417 hie

commit 254c8243953776d3c8704dd81529aadd05d5a94e
Author: Henry <henry@azule.info>
Date:   Fri Feb 10 02:51:21 2023 +0000

    partway through pdf417 hle

commit d4a5b00fde8c0282b0c6e4175a21c1e111b11533
Author: Henry <henry@azule.info>
Date:   Wed Feb 8 21:35:25 2023 +0000

    clippy --fix

commit 2bd4a8e218aa963a88997b50bc5a198f9a88205e
Author: Henry <henry@azule.info>
Date:   Wed Feb 8 21:31:55 2023 +0000

    pdf417 decode and detect cleanup

commit 05130ac54a72c2303045d64507bf6185aa74e531
Author: Henry Schimke <henry@azule.info>
Date:   Tue Feb 7 08:06:25 2023 -0600

    at decodeTextCompaction in decoded_bit_straem_parser

commit 924c023a7d2339dfffa6ef0c4a805fb192576796
Author: Henry Schimke <henry@azule.info>
Date:   Mon Feb 6 18:22:02 2023 -0600

    clippy --fix

commit 5cfb81a885c22b4b5ec937a79be31d0815def211
Author: Henry Schimke <henry@azule.info>
Date:   Mon Feb 6 18:20:22 2023 -0600

    oned cleanup

commit 0649b64854baf56a8e3314728d4f6cf4063db8b2
Author: Henry Schimke <henry@azule.info>
Date:   Mon Feb 6 11:47:28 2023 -0600

    rss cleanup

commit 2d3127898fc11f7e65d590e983cda799b798b646
Author: Henry Schimke <henry@azule.info>
Date:   Mon Feb 6 10:19:56 2023 -0600

    multi cleanup

commit 7e8b43a159083a2096123e6204d4d1d359c59981
Author: Henry <henry@azule.info>
Date:   Sun Feb 5 21:41:59 2023 -0600

    cargo fmt

commit df8285e8da07b4bffd48ca6e596de3f010eb8d9e
Author: Henry <henry@azule.info>
Date:   Sun Feb 5 21:41:29 2023 -0600

    maxicode cleanup

commit a51f923a14016eb416ca202b2194258808dde3e7
Author: Henry <henry@azule.info>
Date:   Sun Feb 5 21:27:57 2023 -0600

    cargo fmt

commit 332d7eef4a25318611025a3fcb0b70c604d917be
Author: Henry <henry@azule.info>
Date:   Sun Feb 5 21:27:48 2023 -0600

    cleanup datamatrix

commit eb2311c476a9c9993f271ab6cf9fd359aedca26f
Author: Henry <henry@azule.info>
Date:   Sun Feb 5 21:27:26 2023 -0600

    cargo clippy --fix

commit 2abaa4c51d287f619d4c7a438f85ba2a30f159cf
Author: Henry <henry@azule.info>
Date:   Sun Feb 5 19:13:02 2023 -0600

    finish cleanup in parsers

commit 716aa2fbf728716cff36657a5fd80e7bca5276b7
Author: Henry Schimke <henry@azule.info>
Date:   Sun Feb 5 17:44:56 2023 -0600

    cleanup continues

commit 7d843788a49fa9cc82c4616d226c0f882e73612b
Author: Henry Schimke <henry@azule.info>
Date:   Sun Feb 5 15:15:09 2023 -0600

    common cleanup

commit fd251866c68f22d450dd428c93d7a3620bf1be27
Author: Henry Schimke <henry@azule.info>
Date:   Sun Feb 5 10:24:54 2023 -0600

    cleanup reedsolomon

commit 132e27ed1468f5c1ce998516ff5a00687217c3e6
Author: Henry Schimke <henry@azule.info>
Date:   Sun Feb 5 09:52:04 2023 -0600

    make MathUtils more generic

commit d8352b7fff4cb4f9ecc8e8a969710d26ef410e8b
Author: Henry Schimke <henry@azule.info>
Date:   Sun Feb 5 09:29:33 2023 -0600

    cleanup some string handling in ResultParser

    There may still be some edge cases where multi-byte characters break the parser
This commit is contained in:
Henry
2023-02-10 20:41:25 -06:00
parent 5222bb25b7
commit 28723be2eb
180 changed files with 3248 additions and 4117 deletions

View File

@@ -73,13 +73,17 @@ impl OneDimensionalCodeWriter for Code128Writer {
) -> Result<Vec<bool>, Exceptions> {
let forcedCodeSet = check(contents, hints)?;
let hasCompactionHint = if let Some(EncodeHintValue::Code128Compact(compat)) =
hints.get(&EncodeHintType::CODE128_COMPACT)
{
*compat
} else {
false
};
let hasCompactionHint = matches!(
hints.get(&EncodeHintType::CODE128_COMPACT),
Some(EncodeHintValue::Code128Compact(true))
);
// let hasCompactionHint = if let Some(EncodeHintValue::Code128Compact(compat)) =
// hints.get(&EncodeHintType::CODE128_COMPACT)
// {
// *compat
// } else {
// false
// };
// let hasCompactionHint = hints != null && hints.containsKey(EncodeHintType::CODE128_COMPACT) &&
// Boolean.parseBoolean(hints.get(EncodeHintType::CODE128_COMPACT).toString());
@@ -103,7 +107,7 @@ fn check(contents: &str, hints: &crate::EncodingHintDictionary) -> Result<i32, E
// Check for forced code set hint.
let mut forcedCodeSet = -1_i32;
if hints.contains_key(&EncodeHintType::FORCE_CODE_SET) {
let Some(EncodeHintValue::ForceCodeSet(codeSetHint)) = hints.get(&EncodeHintType::FORCE_CODE_SET) else { panic!("This must exist by checks previous")};
let Some(EncodeHintValue::ForceCodeSet(codeSetHint)) = hints.get(&EncodeHintType::FORCE_CODE_SET) else { return Err(Exceptions::IllegalStateException(None)) };
match codeSetHint.as_str() {
"A" => forcedCodeSet = CODE_CODE_A as i32,
"B" => forcedCodeSet = CODE_CODE_B as i32,
@@ -192,6 +196,7 @@ fn encodeFast(contents: &str, forcedCodeSet: i32) -> Result<Vec<bool>, Exception
//Select code to use
let newCodeSet = if forcedCodeSet == -1 {
chooseCode(contents, position, codeSet)
.ok_or(Exceptions::IllegalStateException(None))?
} else {
forcedCodeSet as usize // THIS IS RISKY
};
@@ -201,7 +206,11 @@ fn encodeFast(contents: &str, forcedCodeSet: i32) -> Result<Vec<bool>, Exception
if newCodeSet == codeSet {
// Encode the current character
// First handle escapes
match contents.chars().nth(position).unwrap() {
match contents
.chars()
.nth(position)
.ok_or(Exceptions::IndexOutOfBoundsException(None))?
{
ESCAPE_FNC_1 => patternIndex = CODE_FNC_1 as isize,
ESCAPE_FNC_2 => patternIndex = CODE_FNC_2 as isize,
ESCAPE_FNC_3 => patternIndex = CODE_FNC_3 as isize,
@@ -217,16 +226,24 @@ fn encodeFast(contents: &str, forcedCodeSet: i32) -> Result<Vec<bool>, Exception
{
match codeSet {
CODE_CODE_A => {
patternIndex =
contents.chars().nth(position).unwrap() as isize - ' ' as isize;
patternIndex = contents
.chars()
.nth(position)
.ok_or(Exceptions::IndexOutOfBoundsException(None))?
as isize
- ' ' as isize;
if patternIndex < 0 {
// everything below a space character comes behind the underscore in the code patterns table
patternIndex += '`' as isize;
}
}
CODE_CODE_B => {
patternIndex =
contents.chars().nth(position).unwrap() as isize - ' ' as isize
patternIndex = contents
.chars()
.nth(position)
.ok_or(Exceptions::IndexOutOfBoundsException(None))?
as isize
- ' ' as isize
}
_ => {
// CODE_CODE_C
@@ -242,9 +259,9 @@ fn encodeFast(contents: &str, forcedCodeSet: i32) -> Result<Vec<bool>, Exception
.take(2)
.map(|(_u, c)| c)
.collect();
patternIndex = s.parse::<isize>().unwrap();
// contents[position..position + 2].parse::<isize>().unwrap();
// patternIndex = Integer.parseInt(contents.substring(position, position + 2));
patternIndex = s.parse::<isize>().map_err(|e| {
Exceptions::ParseException(Some(format!("issue parsing {s}: {e}")))
})?;
position += 1;
} // Also incremented below
}
@@ -308,11 +325,7 @@ fn produceRXingResult(patterns: &mut Vec<Vec<usize>>, checkSum: usize) -> Vec<bo
// Compute code width
let mut codeWidth = 0_usize;
for pattern in &mut *patterns {
// for (int[] pattern : patterns) {
for width in pattern {
// for (int width : pattern) {
codeWidth += *width;
}
codeWidth += pattern.iter().sum::<usize>();
}
// Compute result
@@ -326,99 +339,99 @@ fn produceRXingResult(patterns: &mut Vec<Vec<usize>>, checkSum: usize) -> Vec<bo
result
}
fn findCType(value: &str, start: usize) -> CType {
fn findCType(value: &str, start: usize) -> Option<CType> {
let last = value.chars().count();
if start >= last {
return CType::Uncodable;
return Some(CType::Uncodable);
}
let c = value.chars().nth(start).unwrap();
let c = value.chars().nth(start)?;
if c == ESCAPE_FNC_1 {
return CType::Fnc1;
return Some(CType::Fnc1);
}
if !('0'..='9').contains(&c) {
return CType::Uncodable;
return Some(CType::Uncodable);
}
if start + 1 >= last {
return CType::OneDigit;
return Some(CType::OneDigit);
}
let c = value.chars().nth(start + 1).unwrap();
let c = value.chars().nth(start + 1)?;
if !('0'..='9').contains(&c) {
return CType::OneDigit;
return Some(CType::OneDigit);
}
CType::TwoDigits
Some(CType::TwoDigits)
}
fn chooseCode(value: &str, start: usize, oldCode: usize) -> usize {
let mut lookahead = findCType(value, start);
fn chooseCode(value: &str, start: usize, oldCode: usize) -> Option<usize> {
let mut lookahead = findCType(value, start)?;
if lookahead == CType::OneDigit {
if oldCode == CODE_CODE_A {
return CODE_CODE_A;
return Some(CODE_CODE_A);
}
return CODE_CODE_B;
return Some(CODE_CODE_B);
}
if lookahead == CType::Uncodable {
if start < value.chars().count() {
let c = value.chars().nth(start).unwrap();
let c = value.chars().nth(start)?;
if c < ' '
|| (oldCode == CODE_CODE_A && (c < '`' || (c >= ESCAPE_FNC_1 && c <= ESCAPE_FNC_4)))
{
// can continue in code A, encodes ASCII 0 to 95 or FNC1 to FNC4
return CODE_CODE_A;
return Some(CODE_CODE_A);
}
}
return CODE_CODE_B; // no choice
return Some(CODE_CODE_B); // no choice
}
if oldCode == CODE_CODE_A && lookahead == CType::Fnc1 {
return CODE_CODE_A;
return Some(CODE_CODE_A);
}
if oldCode == CODE_CODE_C {
// can continue in code C
return CODE_CODE_C;
return Some(CODE_CODE_C);
}
if oldCode == CODE_CODE_B {
if lookahead == CType::Fnc1 {
return CODE_CODE_B; // can continue in code B
return Some(CODE_CODE_B); // can continue in code B
}
// Seen two consecutive digits, see what follows
lookahead = findCType(value, start + 2);
lookahead = findCType(value, start + 2)?;
if lookahead == CType::Uncodable || lookahead == CType::OneDigit {
return CODE_CODE_B; // not worth switching now
return Some(CODE_CODE_B); // not worth switching now
}
if lookahead == CType::Fnc1 {
// two digits, then FNC_1...
lookahead = findCType(value, start + 3);
lookahead = findCType(value, start + 3)?;
if lookahead == CType::TwoDigits {
// then two more digits, switch
return CODE_CODE_C;
return Some(CODE_CODE_C);
} else {
return CODE_CODE_B; // otherwise not worth switching
return Some(CODE_CODE_B); // otherwise not worth switching
}
}
// At this point, there are at least 4 consecutive digits.
// Look ahead to choose whether to switch now or on the next round.
let mut index = start + 4;
let mut lookahead = findCType(value, index);
let mut lookahead = findCType(value, index)?;
while lookahead == CType::TwoDigits {
// while (lookahead = findCType(value, index)) == CType::TWO_DIGITS {
index += 2;
lookahead = findCType(value, index);
lookahead = findCType(value, index)?;
}
if lookahead == CType::OneDigit {
// odd number of digits, switch later
return CODE_CODE_B;
return Some(CODE_CODE_B);
}
return CODE_CODE_C; // even number of digits, switch now
return Some(CODE_CODE_C); // even number of digits, switch now
}
// Here oldCode == 0, which means we are choosing the initial code
if lookahead == CType::Fnc1 {
// ignore FNC_1
lookahead = findCType(value, start + 1);
lookahead = findCType(value, start + 1)?;
}
if lookahead == CType::TwoDigits {
// at least two digits, start in code C
return CODE_CODE_C;
return Some(CODE_CODE_C);
}
CODE_CODE_B
Some(CODE_CODE_B)
}
/**
@@ -520,7 +533,12 @@ stuvwxyz{|}~\u{007F}\u{00FF}";
Latch::None => { /* skip */ }
}
if charset == Charset::C {
if contents.chars().nth(i).unwrap() == ESCAPE_FNC_1 {
if contents
.chars()
.nth(i)
.ok_or(Exceptions::IndexOutOfBoundsException(None))?
== ESCAPE_FNC_1
{
addPattern(
&mut patterns,
CODE_FNC_1,
@@ -537,7 +555,9 @@ stuvwxyz{|}~\u{007F}\u{00FF}";
.collect();
addPattern(
&mut patterns,
s.parse::<usize>().unwrap(),
s.parse::<usize>().map_err(|e| {
Exceptions::ParseException(Some(format!("unable to parse {s} {e}")))
})?,
&mut checkSum,
&mut checkWeight,
i,
@@ -549,7 +569,11 @@ stuvwxyz{|}~\u{007F}\u{00FF}";
}
} else {
// charset A or B
let mut patternIndex = match contents.chars().nth(i).unwrap() {
let mut patternIndex = match contents
.chars()
.nth(i)
.ok_or(Exceptions::IndexOutOfBoundsException(None))?
{
ESCAPE_FNC_1 => CODE_FNC_1 as isize,
ESCAPE_FNC_2 => CODE_FNC_2 as isize,
ESCAPE_FNC_3 => CODE_FNC_3 as isize,
@@ -562,7 +586,14 @@ stuvwxyz{|}~\u{007F}\u{00FF}";
CODE_FNC_4_B as isize
}
}
_ => contents.chars().nth(i).unwrap() as isize - ' ' as isize,
_ => {
contents
.chars()
.nth(i)
.ok_or(Exceptions::IndexOutOfBoundsException(None))?
as isize
- ' ' as isize
}
};
if ((charset == Charset::A && latch != Latch::Shift)
|| (charset == Charset::B && latch == Latch::Shift))
@@ -611,7 +642,9 @@ stuvwxyz{|}~\u{007F}\u{00FF}";
}
fn canEncode(contents: &str, charset: Charset, position: usize) -> bool {
let c = contents.chars().nth(position).unwrap();
let Some(c) = contents.chars().nth(position) else {
return false;
};
match charset {
Charset::A => {
c == ESCAPE_FNC_1
@@ -628,10 +661,11 @@ stuvwxyz{|}~\u{007F}\u{00FF}";
|| B.find(c).is_some()
}
Charset::C => {
let Some(c_p_1) = contents.chars().nth(position + 1) else {
return false;
};
c == ESCAPE_FNC_1
|| (position + 1 < contents.chars().count()
&& isDigit(c)
&& isDigit(contents.chars().nth(position + 1).unwrap()))
|| (position + 1 < contents.chars().count() && isDigit(c) && isDigit(c_p_1))
}
_ => false,
}
@@ -647,7 +681,9 @@ stuvwxyz{|}~\u{007F}\u{00FF}";
memoizedCost: &mut Vec<Vec<u32>>,
minPath: &mut Vec<Vec<Latch>>,
) -> Result<u32, Exceptions> {
assert!(position < contents.chars().count());
if position >= contents.chars().count() {
return Err(Exceptions::IllegalStateException(None));
}
let mCost = memoizedCost[charset.ordinal()][position];
if mCost > 0 {
return Ok(mCost);
@@ -665,7 +701,7 @@ stuvwxyz{|}~\u{007F}\u{00FF}";
let mut latch = Latch::None;
if charset != sets[i] {
cost += 1;
latch = sets[i].into(); //Latch::valueOf(sets[i].toString());
latch = sets[i].into();
}
if !atEnd {
cost += encode_with_start_position(
@@ -707,7 +743,7 @@ stuvwxyz{|}~\u{007F}\u{00FF}";
cost += 1;
latch = Latch::C;
}
let advance = if contents.chars().nth(position).unwrap() == ESCAPE_FNC_1 {
let advance = if contents.chars().nth(position).unwrap_or_default() == ESCAPE_FNC_1 {
1
} else {
2