mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-26 04:12:34 +00:00
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:
@@ -31,12 +31,12 @@ impl Encoder for ASCIIEncoder {
|
||||
.getMessage()
|
||||
.chars()
|
||||
.nth(context.pos as usize)
|
||||
.unwrap(),
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?,
|
||||
context
|
||||
.getMessage()
|
||||
.chars()
|
||||
.nth(context.pos as usize + 1)
|
||||
.unwrap(),
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?,
|
||||
)? as u8);
|
||||
context.pos += 2;
|
||||
} else {
|
||||
|
||||
@@ -51,19 +51,36 @@ impl Encoder for Base256Encoder {
|
||||
let lengthFieldSize = 1;
|
||||
let currentSize = context.getCodewordCount() + dataCount + lengthFieldSize;
|
||||
context.updateSymbolInfoWithLength(currentSize);
|
||||
let mustPad = (context.getSymbolInfo().unwrap().getDataCapacity() - currentSize as u32) > 0;
|
||||
let mustPad = (context
|
||||
.getSymbolInfo()
|
||||
.ok_or(Exceptions::IllegalStateException(None))?
|
||||
.getDataCapacity()
|
||||
- currentSize as u32)
|
||||
> 0;
|
||||
if context.hasMoreCharacters() || mustPad {
|
||||
if dataCount <= 249 {
|
||||
buffer.replace_range(0..1, &char::from_u32(dataCount as u32).unwrap().to_string());
|
||||
buffer.replace_range(
|
||||
0..1,
|
||||
&char::from_u32(dataCount as u32)
|
||||
.ok_or(Exceptions::ParseException(None))?
|
||||
.to_string(),
|
||||
);
|
||||
} else if dataCount <= 1555 {
|
||||
buffer.replace_range(
|
||||
0..1,
|
||||
&char::from_u32((dataCount as u32 / 250) + 249)
|
||||
.unwrap()
|
||||
.ok_or(Exceptions::ParseException(None))?
|
||||
.to_string(),
|
||||
);
|
||||
let (ci_pos, _) = buffer.char_indices().nth(1).unwrap();
|
||||
buffer.insert(ci_pos, char::from_u32(dataCount as u32 % 250).unwrap());
|
||||
let (ci_pos, _) = buffer
|
||||
.char_indices()
|
||||
.nth(1)
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?;
|
||||
buffer.insert(
|
||||
ci_pos,
|
||||
char::from_u32(dataCount as u32 % 250)
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?,
|
||||
);
|
||||
} else {
|
||||
return Err(Exceptions::IllegalStateException(Some(format!(
|
||||
"Message length not in valid ranges: {dataCount}"
|
||||
@@ -73,10 +90,16 @@ impl Encoder for Base256Encoder {
|
||||
let c = buffer.chars().count();
|
||||
for i in 0..c {
|
||||
// for (int i = 0, c = buffer.length(); i < c; i++) {
|
||||
context.writeCodeword(Self::randomize255State(
|
||||
buffer.chars().nth(i).unwrap(),
|
||||
context.getCodewordCount() as u32 + 1,
|
||||
) as u8);
|
||||
context.writeCodeword(
|
||||
Self::randomize255State(
|
||||
buffer
|
||||
.chars()
|
||||
.nth(i)
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?,
|
||||
context.getCodewordCount() as u32 + 1,
|
||||
)
|
||||
.ok_or(Exceptions::ParseException(None))? as u8,
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -85,13 +108,13 @@ impl Base256Encoder {
|
||||
pub fn new() -> Self {
|
||||
Self
|
||||
}
|
||||
fn randomize255State(ch: char, codewordPosition: u32) -> char {
|
||||
fn randomize255State(ch: char, codewordPosition: u32) -> Option<char> {
|
||||
let pseudoRandom = ((149 * codewordPosition) % 255) + 1;
|
||||
let tempVariable = ch as u32 + pseudoRandom;
|
||||
if tempVariable <= 255 {
|
||||
char::from_u32(tempVariable).unwrap()
|
||||
char::from_u32(tempVariable)
|
||||
} else {
|
||||
char::from_u32(tempVariable - 256).unwrap()
|
||||
char::from_u32(tempVariable - 256)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,8 +63,11 @@ impl C40Encoder {
|
||||
|
||||
let curCodewordCount = context.getCodewordCount() + unwritten;
|
||||
context.updateSymbolInfoWithLength(curCodewordCount);
|
||||
let available =
|
||||
context.getSymbolInfo().unwrap().getDataCapacity() as usize - curCodewordCount;
|
||||
let available = context
|
||||
.getSymbolInfo()
|
||||
.ok_or(Exceptions::IllegalStateException(None))?
|
||||
.getDataCapacity() as usize
|
||||
- curCodewordCount;
|
||||
|
||||
if !context.hasMoreCharacters() {
|
||||
//Avoid having a single C40 value in the last triplet
|
||||
@@ -107,7 +110,7 @@ impl C40Encoder {
|
||||
handleEOD(context, &mut buffer)
|
||||
}
|
||||
|
||||
pub fn encodeMaximalC40(&self, context: &mut EncoderContext) {
|
||||
pub fn encodeMaximalC40(&self, context: &mut EncoderContext) -> Result<(), Exceptions> {
|
||||
self.encodeMaximal(context, &Self::encodeChar_c40, &Self::handleEOD_c40)
|
||||
}
|
||||
|
||||
@@ -116,7 +119,7 @@ impl C40Encoder {
|
||||
context: &mut EncoderContext,
|
||||
encodeChar: &dyn Fn(char, &mut String) -> u32,
|
||||
handleEOD: &dyn Fn(&mut EncoderContext, &mut String) -> Result<(), Exceptions>,
|
||||
) {
|
||||
) -> Result<(), Exceptions> {
|
||||
let mut buffer = String::new();
|
||||
let mut lastCharSize = 0;
|
||||
let mut backtrackStartPosition = context.pos;
|
||||
@@ -135,8 +138,11 @@ impl C40Encoder {
|
||||
|
||||
let curCodewordCount = context.getCodewordCount() + unwritten + 1; // +1 for the latch to C40
|
||||
context.updateSymbolInfoWithLength(curCodewordCount);
|
||||
let available =
|
||||
context.getSymbolInfo().unwrap().getDataCapacity() as usize - curCodewordCount;
|
||||
let available = context
|
||||
.getSymbolInfo()
|
||||
.ok_or(Exceptions::IllegalStateException(None))?
|
||||
.getDataCapacity() as usize
|
||||
- curCodewordCount;
|
||||
let rest = buffer.chars().count() % 3;
|
||||
if (rest == 2 && available != 2) || (rest == 1 && (lastCharSize > 3 || available != 1))
|
||||
{
|
||||
@@ -149,7 +155,9 @@ impl C40Encoder {
|
||||
context.writeCodeword(LATCH_TO_C40);
|
||||
}
|
||||
|
||||
handleEOD(context, &mut buffer).expect("eod");
|
||||
handleEOD(context, &mut buffer)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn backtrackOneCharacter(
|
||||
@@ -170,10 +178,16 @@ impl C40Encoder {
|
||||
lastCharSize
|
||||
}
|
||||
|
||||
pub(super) fn writeNextTriplet(context: &mut EncoderContext, buffer: &mut String) {
|
||||
context.writeCodewords(&Self::encodeToCodewords(buffer));
|
||||
pub(super) fn writeNextTriplet(
|
||||
context: &mut EncoderContext,
|
||||
buffer: &mut String,
|
||||
) -> Result<(), Exceptions> {
|
||||
context.writeCodewords(
|
||||
&Self::encodeToCodewords(buffer).ok_or(Exceptions::FormatException(None))?,
|
||||
);
|
||||
buffer.replace_range(0..3, "");
|
||||
// buffer.delete(0, 3);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -191,20 +205,23 @@ impl C40Encoder {
|
||||
|
||||
let curCodewordCount = context.getCodewordCount() + unwritten;
|
||||
context.updateSymbolInfoWithLength(curCodewordCount);
|
||||
let available =
|
||||
context.getSymbolInfo().unwrap().getDataCapacity() as usize - curCodewordCount;
|
||||
let available = context
|
||||
.getSymbolInfo()
|
||||
.ok_or(Exceptions::IllegalStateException(None))?
|
||||
.getDataCapacity() as usize
|
||||
- curCodewordCount;
|
||||
|
||||
if rest == 2 {
|
||||
buffer.push('\0'); //Shift 1
|
||||
while buffer.chars().count() >= 3 {
|
||||
C40Encoder::writeNextTriplet(context, buffer);
|
||||
C40Encoder::writeNextTriplet(context, buffer)?;
|
||||
}
|
||||
if context.hasMoreCharacters() {
|
||||
context.writeCodeword(C40_UNLATCH);
|
||||
}
|
||||
} else if available == 1 && rest == 1 {
|
||||
while buffer.chars().count() >= 3 {
|
||||
C40Encoder::writeNextTriplet(context, buffer);
|
||||
C40Encoder::writeNextTriplet(context, buffer)?;
|
||||
}
|
||||
if context.hasMoreCharacters() {
|
||||
context.writeCodeword(C40_UNLATCH);
|
||||
@@ -213,7 +230,7 @@ impl C40Encoder {
|
||||
context.pos -= 1;
|
||||
} else if rest == 0 {
|
||||
while buffer.chars().count() >= 3 {
|
||||
C40Encoder::writeNextTriplet(context, buffer);
|
||||
C40Encoder::writeNextTriplet(context, buffer)?;
|
||||
}
|
||||
if available > 0 || context.hasMoreCharacters() {
|
||||
context.writeCodeword(C40_UNLATCH);
|
||||
@@ -273,16 +290,18 @@ impl C40Encoder {
|
||||
len
|
||||
}
|
||||
|
||||
fn encodeToCodewords(sb: &str) -> String {
|
||||
let v = (1600 * sb.chars().next().unwrap() as u32)
|
||||
+ (40 * sb.chars().nth(1).unwrap() as u32)
|
||||
+ sb.chars().nth(2).unwrap() as u32
|
||||
fn encodeToCodewords(sb: &str) -> Option<String> {
|
||||
let v = (1600 * sb.chars().next()? as u32)
|
||||
+ (40 * sb.chars().nth(1)? as u32)
|
||||
+ sb.chars().nth(2)? as u32
|
||||
+ 1;
|
||||
let cw1 = v / 256;
|
||||
let cw2 = v % 256;
|
||||
[char::from_u32(cw1).unwrap(), char::from_u32(cw2).unwrap()]
|
||||
.into_iter()
|
||||
.collect()
|
||||
Some(
|
||||
[char::from_u32(cw1)?, char::from_u32(cw2)?]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
)
|
||||
// return new String(new char[] {cw1, cw2});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,8 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
use crate::Exceptions;
|
||||
|
||||
const EMPTY_BIT_VAL: u8 = 13;
|
||||
|
||||
/**
|
||||
@@ -71,7 +73,7 @@ impl DefaultPlacement {
|
||||
self.bits[row * self.numcols + col] == EMPTY_BIT_VAL
|
||||
}
|
||||
|
||||
pub fn place(&mut self) {
|
||||
pub fn place(&mut self) -> Result<(), Exceptions> {
|
||||
let mut pos = 0;
|
||||
let mut row = 4_isize;
|
||||
let mut col = 0_isize;
|
||||
@@ -79,19 +81,19 @@ impl DefaultPlacement {
|
||||
loop {
|
||||
// repeatedly first check for one of the special corner cases, then...
|
||||
if (row == self.numrows as isize) && (col == 0) {
|
||||
self.corner1(pos);
|
||||
self.corner1(pos)?;
|
||||
pos += 1;
|
||||
}
|
||||
if (row == self.numrows as isize - 2) && (col == 0) && ((self.numcols % 4) != 0) {
|
||||
self.corner2(pos);
|
||||
self.corner2(pos)?;
|
||||
pos += 1;
|
||||
}
|
||||
if (row == self.numrows as isize - 2) && (col == 0) && (self.numcols % 8 == 4) {
|
||||
self.corner3(pos);
|
||||
self.corner3(pos)?;
|
||||
pos += 1;
|
||||
}
|
||||
if (row == self.numrows as isize + 4) && (col == 2) && ((self.numcols % 8) == 0) {
|
||||
self.corner4(pos);
|
||||
self.corner4(pos)?;
|
||||
pos += 1;
|
||||
}
|
||||
// sweep upward diagonally, inserting successive characters...
|
||||
@@ -100,7 +102,7 @@ impl DefaultPlacement {
|
||||
&& (col >= 0)
|
||||
&& self.noBit(col as usize, row as usize)
|
||||
{
|
||||
self.utah(row, col, pos);
|
||||
self.utah(row, col, pos)?;
|
||||
pos += 1;
|
||||
}
|
||||
row -= 2;
|
||||
@@ -118,7 +120,7 @@ impl DefaultPlacement {
|
||||
&& (col < self.numcols as isize)
|
||||
&& self.noBit(col as usize, row as usize)
|
||||
{
|
||||
self.utah(row, col, pos);
|
||||
self.utah(row, col, pos)?;
|
||||
pos += 1;
|
||||
}
|
||||
row += 2;
|
||||
@@ -142,9 +144,10 @@ impl DefaultPlacement {
|
||||
self.setBit(self.numcols - 1, self.numrows - 1, true);
|
||||
self.setBit(self.numcols - 2, self.numrows - 2, true);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn module(&mut self, row: isize, col: isize, pos: usize, bit: u32) {
|
||||
fn module(&mut self, row: isize, col: isize, pos: usize, bit: u32) -> Result<(), Exceptions> {
|
||||
let mut row = row;
|
||||
let mut col = col;
|
||||
|
||||
@@ -157,9 +160,15 @@ impl DefaultPlacement {
|
||||
row += 4 - ((self.numcols + 4) % 8) as isize;
|
||||
}
|
||||
// Note the conversion:
|
||||
let mut v = self.codewords.chars().nth(pos).unwrap() as u32;
|
||||
let mut v = self
|
||||
.codewords
|
||||
.chars()
|
||||
.nth(pos)
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))? as u32;
|
||||
v &= 1 << (8 - bit);
|
||||
self.setBit(col as usize, row as usize, v != 0);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -169,59 +178,64 @@ impl DefaultPlacement {
|
||||
* @param col the column
|
||||
* @param pos character position
|
||||
*/
|
||||
fn utah(&mut self, row: isize, col: isize, pos: usize) {
|
||||
self.module(row - 2, col - 2, pos, 1);
|
||||
self.module(row - 2, col - 1, pos, 2);
|
||||
self.module(row - 1, col - 2, pos, 3);
|
||||
self.module(row - 1, col - 1, pos, 4);
|
||||
self.module(row - 1, col, pos, 5);
|
||||
self.module(row, col - 2, pos, 6);
|
||||
self.module(row, col - 1, pos, 7);
|
||||
self.module(row, col, pos, 8);
|
||||
fn utah(&mut self, row: isize, col: isize, pos: usize) -> Result<(), Exceptions> {
|
||||
self.module(row - 2, col - 2, pos, 1)?;
|
||||
self.module(row - 2, col - 1, pos, 2)?;
|
||||
self.module(row - 1, col - 2, pos, 3)?;
|
||||
self.module(row - 1, col - 1, pos, 4)?;
|
||||
self.module(row - 1, col, pos, 5)?;
|
||||
self.module(row, col - 2, pos, 6)?;
|
||||
self.module(row, col - 1, pos, 7)?;
|
||||
self.module(row, col, pos, 8)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn corner1(&mut self, pos: usize) {
|
||||
self.module(self.numrows as isize - 1, 0, pos, 1);
|
||||
self.module(self.numrows as isize - 1, 1, pos, 2);
|
||||
self.module(self.numrows as isize - 1, 2, pos, 3);
|
||||
self.module(0, self.numcols as isize - 2, pos, 4);
|
||||
self.module(0, self.numcols as isize - 1, pos, 5);
|
||||
self.module(1, self.numcols as isize - 1, pos, 6);
|
||||
self.module(2, self.numcols as isize - 1, pos, 7);
|
||||
self.module(3, self.numcols as isize - 1, pos, 8);
|
||||
fn corner1(&mut self, pos: usize) -> Result<(), Exceptions> {
|
||||
self.module(self.numrows as isize - 1, 0, pos, 1)?;
|
||||
self.module(self.numrows as isize - 1, 1, pos, 2)?;
|
||||
self.module(self.numrows as isize - 1, 2, pos, 3)?;
|
||||
self.module(0, self.numcols as isize - 2, pos, 4)?;
|
||||
self.module(0, self.numcols as isize - 1, pos, 5)?;
|
||||
self.module(1, self.numcols as isize - 1, pos, 6)?;
|
||||
self.module(2, self.numcols as isize - 1, pos, 7)?;
|
||||
self.module(3, self.numcols as isize - 1, pos, 8)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn corner2(&mut self, pos: usize) {
|
||||
self.module(self.numrows as isize - 3, 0, pos, 1);
|
||||
self.module(self.numrows as isize - 2, 0, pos, 2);
|
||||
self.module(self.numrows as isize - 1, 0, pos, 3);
|
||||
self.module(0, self.numcols as isize - 4, pos, 4);
|
||||
self.module(0, self.numcols as isize - 3, pos, 5);
|
||||
self.module(0, self.numcols as isize - 2, pos, 6);
|
||||
self.module(0, self.numcols as isize - 1, pos, 7);
|
||||
self.module(1, self.numcols as isize - 1, pos, 8);
|
||||
fn corner2(&mut self, pos: usize) -> Result<(), Exceptions> {
|
||||
self.module(self.numrows as isize - 3, 0, pos, 1)?;
|
||||
self.module(self.numrows as isize - 2, 0, pos, 2)?;
|
||||
self.module(self.numrows as isize - 1, 0, pos, 3)?;
|
||||
self.module(0, self.numcols as isize - 4, pos, 4)?;
|
||||
self.module(0, self.numcols as isize - 3, pos, 5)?;
|
||||
self.module(0, self.numcols as isize - 2, pos, 6)?;
|
||||
self.module(0, self.numcols as isize - 1, pos, 7)?;
|
||||
self.module(1, self.numcols as isize - 1, pos, 8)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn corner3(&mut self, pos: usize) {
|
||||
self.module(self.numrows as isize - 3, 0, pos, 1);
|
||||
self.module(self.numrows as isize - 2, 0, pos, 2);
|
||||
self.module(self.numrows as isize - 1, 0, pos, 3);
|
||||
self.module(0, self.numcols as isize - 2, pos, 4);
|
||||
self.module(0, self.numcols as isize - 1, pos, 5);
|
||||
self.module(1, self.numcols as isize - 1, pos, 6);
|
||||
self.module(2, self.numcols as isize - 1, pos, 7);
|
||||
self.module(3, self.numcols as isize - 1, pos, 8);
|
||||
fn corner3(&mut self, pos: usize) -> Result<(), Exceptions> {
|
||||
self.module(self.numrows as isize - 3, 0, pos, 1)?;
|
||||
self.module(self.numrows as isize - 2, 0, pos, 2)?;
|
||||
self.module(self.numrows as isize - 1, 0, pos, 3)?;
|
||||
self.module(0, self.numcols as isize - 2, pos, 4)?;
|
||||
self.module(0, self.numcols as isize - 1, pos, 5)?;
|
||||
self.module(1, self.numcols as isize - 1, pos, 6)?;
|
||||
self.module(2, self.numcols as isize - 1, pos, 7)?;
|
||||
self.module(3, self.numcols as isize - 1, pos, 8)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn corner4(&mut self, pos: usize) {
|
||||
self.module(self.numrows as isize - 1, 0, pos, 1);
|
||||
self.module(self.numrows as isize - 1, self.numcols as isize - 1, pos, 2);
|
||||
self.module(0, self.numcols as isize - 3, pos, 3);
|
||||
self.module(0, self.numcols as isize - 2, pos, 4);
|
||||
self.module(0, self.numcols as isize - 1, pos, 5);
|
||||
self.module(1, self.numcols as isize - 3, pos, 6);
|
||||
self.module(1, self.numcols as isize - 2, pos, 7);
|
||||
self.module(1, self.numcols as isize - 1, pos, 8);
|
||||
fn corner4(&mut self, pos: usize) -> Result<(), Exceptions> {
|
||||
self.module(self.numrows as isize - 1, 0, pos, 1)?;
|
||||
self.module(self.numrows as isize - 1, self.numcols as isize - 1, pos, 2)?;
|
||||
self.module(0, self.numcols as isize - 3, pos, 3)?;
|
||||
self.module(0, self.numcols as isize - 2, pos, 4)?;
|
||||
self.module(0, self.numcols as isize - 1, pos, 5)?;
|
||||
self.module(1, self.numcols as isize - 3, pos, 6)?;
|
||||
self.module(1, self.numcols as isize - 2, pos, 7)?;
|
||||
self.module(1, self.numcols as isize - 1, pos, 8)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
@@ -257,7 +271,7 @@ mod test_placement {
|
||||
fn testPlacement() {
|
||||
let codewords = unvisualize("66 74 78 66 74 78 129 56 35 102 192 96 226 100 156 1 107 221"); //"AIMAIM" encoded
|
||||
let mut placement = DefaultPlacement::new(codewords, 12, 12);
|
||||
placement.place();
|
||||
placement.place().expect("msg");
|
||||
let expected = [
|
||||
"011100001111",
|
||||
"001010101000",
|
||||
|
||||
@@ -29,7 +29,7 @@ impl Encoder for EdifactEncoder {
|
||||
let mut buffer = String::new();
|
||||
while context.hasMoreCharacters() {
|
||||
let c = context.getCurrentChar();
|
||||
Self::encodeChar(c, &mut buffer);
|
||||
Self::encodeChar(c, &mut buffer)?;
|
||||
context.pos += 1;
|
||||
|
||||
let count = buffer.chars().count();
|
||||
@@ -74,13 +74,19 @@ impl EdifactEncoder {
|
||||
if count == 1 {
|
||||
//Only an unlatch at the end
|
||||
context.updateSymbolInfo();
|
||||
let mut available = context.getSymbolInfo().unwrap().getDataCapacity()
|
||||
let mut available = context
|
||||
.getSymbolInfo()
|
||||
.ok_or(Exceptions::IllegalStateException(None))?
|
||||
.getDataCapacity()
|
||||
- context.getCodewordCount() as u32;
|
||||
let remaining = context.getRemainingCharacters();
|
||||
// The following two lines are a hack inspired by the 'fix' from https://sourceforge.net/p/barcode4j/svn/221/
|
||||
if remaining > available {
|
||||
context.updateSymbolInfoWithLength(context.getCodewordCount() + 1);
|
||||
available = context.getSymbolInfo().unwrap().getDataCapacity()
|
||||
available = context
|
||||
.getSymbolInfo()
|
||||
.ok_or(Exceptions::IllegalStateException(None))?
|
||||
.getDataCapacity()
|
||||
- context.getCodewordCount() as u32;
|
||||
}
|
||||
if remaining <= available && available <= 2 {
|
||||
@@ -100,7 +106,10 @@ impl EdifactEncoder {
|
||||
|
||||
if restChars <= 2 {
|
||||
context.updateSymbolInfoWithLength(context.getCodewordCount() + restChars);
|
||||
let available = context.getSymbolInfo().unwrap().getDataCapacity()
|
||||
let available = context
|
||||
.getSymbolInfo()
|
||||
.ok_or(Exceptions::IllegalStateException(None))?
|
||||
.getDataCapacity()
|
||||
- context.getCodewordCount() as u32;
|
||||
if available >= 3 {
|
||||
restInAscii = false;
|
||||
@@ -126,14 +135,15 @@ impl EdifactEncoder {
|
||||
res
|
||||
}
|
||||
|
||||
fn encodeChar(c: char, sb: &mut String) {
|
||||
fn encodeChar(c: char, sb: &mut String) -> Result<(), Exceptions> {
|
||||
if (' '..='?').contains(&c) {
|
||||
sb.push(c);
|
||||
} else if ('@'..='^').contains(&c) {
|
||||
sb.push((c as u8 - 64) as char);
|
||||
} else {
|
||||
high_level_encoder::illegalCharacter(c).expect("");
|
||||
high_level_encoder::illegalCharacter(c)?
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn encodeToCodewords(sb: &str) -> Result<String, Exceptions> {
|
||||
@@ -143,19 +153,28 @@ impl EdifactEncoder {
|
||||
"StringBuilder must not be empty".to_owned(),
|
||||
)));
|
||||
}
|
||||
let c1 = sb.chars().next().unwrap();
|
||||
let c1 = sb
|
||||
.chars()
|
||||
.next()
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?;
|
||||
let c2 = if len >= 2 {
|
||||
sb.chars().nth(1).unwrap()
|
||||
sb.chars()
|
||||
.nth(1)
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?
|
||||
} else {
|
||||
0 as char
|
||||
};
|
||||
let c3 = if len >= 3 {
|
||||
sb.chars().nth(2).unwrap()
|
||||
sb.chars()
|
||||
.nth(2)
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?
|
||||
} else {
|
||||
0 as char
|
||||
};
|
||||
let c4 = if len >= 4 {
|
||||
sb.chars().nth(3).unwrap()
|
||||
sb.chars()
|
||||
.nth(3)
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?
|
||||
} else {
|
||||
0 as char
|
||||
};
|
||||
@@ -165,12 +184,12 @@ impl EdifactEncoder {
|
||||
let cw2 = (v >> 8) & 255;
|
||||
let cw3 = v & 255;
|
||||
let mut res = String::with_capacity(3);
|
||||
res.push(char::from_u32(cw1).unwrap());
|
||||
res.push(char::from_u32(cw1).ok_or(Exceptions::IndexOutOfBoundsException(None))?);
|
||||
if len >= 2 {
|
||||
res.push(char::from_u32(cw2).unwrap());
|
||||
res.push(char::from_u32(cw2).ok_or(Exceptions::IndexOutOfBoundsException(None))?);
|
||||
}
|
||||
if len >= 3 {
|
||||
res.push(char::from_u32(cw3).unwrap());
|
||||
res.push(char::from_u32(cw3).ok_or(Exceptions::IndexOutOfBoundsException(None))?);
|
||||
}
|
||||
|
||||
Ok(res)
|
||||
|
||||
@@ -62,7 +62,11 @@ impl<'a> EncoderContext<'_> {
|
||||
{
|
||||
ISO_8859_1_ENCODER
|
||||
.decode(&encoded_bytes, encoding::DecoderTrap::Strict)
|
||||
.expect("round trip decode should always work")
|
||||
.map_err(|e| {
|
||||
Exceptions::ParseException(Some(format!(
|
||||
"round trip decode should always work: {e}"
|
||||
)))
|
||||
})?
|
||||
} else {
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"Message contains characters outside ISO-8859-1 encoding.".to_owned(),
|
||||
@@ -99,13 +103,13 @@ impl<'a> EncoderContext<'_> {
|
||||
self.skipAtEnd = count;
|
||||
}
|
||||
|
||||
/// panics if current position is out of bounds
|
||||
pub fn getCurrentChar(&self) -> char {
|
||||
// self.msg.graphemes(true).nth(self.pos as usize).unwrap()
|
||||
self.msg.chars().nth(self.pos as usize).unwrap()
|
||||
}
|
||||
|
||||
/// panics if current position is out of bounds
|
||||
pub fn getCurrent(&self) -> char {
|
||||
// self.msg.graphemes(true).nth(self.pos as usize).unwrap()
|
||||
self.msg.chars().nth(self.pos as usize).unwrap()
|
||||
}
|
||||
|
||||
@@ -157,6 +161,9 @@ impl<'a> EncoderContext<'_> {
|
||||
self.updateSymbolInfoWithLength(self.getCodewordCount());
|
||||
}
|
||||
|
||||
/// Update symbol info with the length
|
||||
///
|
||||
/// This function can panic
|
||||
pub fn updateSymbolInfoWithLength(&mut self, len: usize) {
|
||||
if self.symbolInfo.is_none()
|
||||
|| len > self.symbolInfo.as_ref().unwrap().getDataCapacity() as usize
|
||||
|
||||
@@ -35,61 +35,61 @@ const FACTOR_SETS: [u32; 16] = [5, 7, 10, 11, 12, 14, 18, 20, 24, 28, 36, 42, 48
|
||||
*/
|
||||
static FACTORS: Lazy<[Vec<u32>; 16]> = Lazy::new(|| {
|
||||
[
|
||||
Vec::from([228, 48, 15, 111, 62]),
|
||||
Vec::from([23, 68, 144, 134, 240, 92, 254]),
|
||||
Vec::from([28, 24, 185, 166, 223, 248, 116, 255, 110, 61]),
|
||||
Vec::from([175, 138, 205, 12, 194, 168, 39, 245, 60, 97, 120]),
|
||||
Vec::from([41, 153, 158, 91, 61, 42, 142, 213, 97, 178, 100, 242]),
|
||||
Vec::from([
|
||||
vec![228, 48, 15, 111, 62],
|
||||
vec![23, 68, 144, 134, 240, 92, 254],
|
||||
vec![28, 24, 185, 166, 223, 248, 116, 255, 110, 61],
|
||||
vec![175, 138, 205, 12, 194, 168, 39, 245, 60, 97, 120],
|
||||
vec![41, 153, 158, 91, 61, 42, 142, 213, 97, 178, 100, 242],
|
||||
vec![
|
||||
156, 97, 192, 252, 95, 9, 157, 119, 138, 45, 18, 186, 83, 185,
|
||||
]),
|
||||
Vec::from([
|
||||
],
|
||||
vec![
|
||||
83, 195, 100, 39, 188, 75, 66, 61, 241, 213, 109, 129, 94, 254, 225, 48, 90, 188,
|
||||
]),
|
||||
Vec::from([
|
||||
],
|
||||
vec![
|
||||
15, 195, 244, 9, 233, 71, 168, 2, 188, 160, 153, 145, 253, 79, 108, 82, 27, 174, 186,
|
||||
172,
|
||||
]),
|
||||
Vec::from([
|
||||
],
|
||||
vec![
|
||||
52, 190, 88, 205, 109, 39, 176, 21, 155, 197, 251, 223, 155, 21, 5, 172, 254, 124, 12,
|
||||
181, 184, 96, 50, 193,
|
||||
]),
|
||||
Vec::from([
|
||||
],
|
||||
vec![
|
||||
211, 231, 43, 97, 71, 96, 103, 174, 37, 151, 170, 53, 75, 34, 249, 121, 17, 138, 110,
|
||||
213, 141, 136, 120, 151, 233, 168, 93, 255,
|
||||
]),
|
||||
Vec::from([
|
||||
],
|
||||
vec![
|
||||
245, 127, 242, 218, 130, 250, 162, 181, 102, 120, 84, 179, 220, 251, 80, 182, 229, 18,
|
||||
2, 4, 68, 33, 101, 137, 95, 119, 115, 44, 175, 184, 59, 25, 225, 98, 81, 112,
|
||||
]),
|
||||
Vec::from([
|
||||
],
|
||||
vec![
|
||||
77, 193, 137, 31, 19, 38, 22, 153, 247, 105, 122, 2, 245, 133, 242, 8, 175, 95, 100, 9,
|
||||
167, 105, 214, 111, 57, 121, 21, 1, 253, 57, 54, 101, 248, 202, 69, 50, 150, 177, 226,
|
||||
5, 9, 5,
|
||||
]),
|
||||
Vec::from([
|
||||
],
|
||||
vec![
|
||||
245, 132, 172, 223, 96, 32, 117, 22, 238, 133, 238, 231, 205, 188, 237, 87, 191, 106,
|
||||
16, 147, 118, 23, 37, 90, 170, 205, 131, 88, 120, 100, 66, 138, 186, 240, 82, 44, 176,
|
||||
87, 187, 147, 160, 175, 69, 213, 92, 253, 225, 19,
|
||||
]),
|
||||
Vec::from([
|
||||
],
|
||||
vec![
|
||||
175, 9, 223, 238, 12, 17, 220, 208, 100, 29, 175, 170, 230, 192, 215, 235, 150, 159,
|
||||
36, 223, 38, 200, 132, 54, 228, 146, 218, 234, 117, 203, 29, 232, 144, 238, 22, 150,
|
||||
201, 117, 62, 207, 164, 13, 137, 245, 127, 67, 247, 28, 155, 43, 203, 107, 233, 53,
|
||||
143, 46,
|
||||
]),
|
||||
Vec::from([
|
||||
],
|
||||
vec![
|
||||
242, 93, 169, 50, 144, 210, 39, 118, 202, 188, 201, 189, 143, 108, 196, 37, 185, 112,
|
||||
134, 230, 245, 63, 197, 190, 250, 106, 185, 221, 175, 64, 114, 71, 161, 44, 147, 6, 27,
|
||||
218, 51, 63, 87, 10, 40, 130, 188, 17, 163, 31, 176, 170, 4, 107, 232, 7, 94, 166, 224,
|
||||
124, 86, 47, 11, 204,
|
||||
]),
|
||||
Vec::from([
|
||||
],
|
||||
vec![
|
||||
220, 228, 173, 89, 251, 149, 159, 56, 89, 33, 147, 244, 154, 36, 73, 127, 213, 136,
|
||||
248, 180, 234, 197, 158, 177, 68, 122, 93, 213, 15, 160, 227, 236, 66, 139, 153, 185,
|
||||
202, 167, 179, 25, 220, 232, 96, 210, 231, 136, 223, 239, 181, 241, 59, 52, 172, 25,
|
||||
49, 232, 211, 189, 64, 54, 108, 153, 132, 63, 96, 103, 82, 186,
|
||||
]),
|
||||
],
|
||||
]
|
||||
});
|
||||
|
||||
@@ -181,7 +181,12 @@ pub fn encodeECC200(codewords: &str, symbolInfo: &SymbolInfo) -> Result<String,
|
||||
let mut d = block;
|
||||
while d < symbolInfo.getDataCapacity() as usize {
|
||||
// for (int d = block; d < symbolInfo.getDataCapacity(); d += blockCount) {
|
||||
temp.push(codewords.chars().nth(d).unwrap());
|
||||
temp.push(
|
||||
codewords
|
||||
.chars()
|
||||
.nth(d)
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?,
|
||||
);
|
||||
|
||||
d += blockCount;
|
||||
}
|
||||
@@ -193,10 +198,13 @@ pub fn encodeECC200(codewords: &str, symbolInfo: &SymbolInfo) -> Result<String,
|
||||
let (char_index, replace_char) = sb
|
||||
.char_indices()
|
||||
.nth(symbolInfo.getDataCapacity() as usize + e)
|
||||
.unwrap();
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?;
|
||||
sb.replace_range(
|
||||
char_index..(replace_char.len_utf8()),
|
||||
&ecc.chars().nth(pos).unwrap().to_string(),
|
||||
&ecc.chars()
|
||||
.nth(pos)
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?
|
||||
.to_string(),
|
||||
);
|
||||
// sb.setCharAt(symbolInfo.getDataCapacity() + e, ecc.charAt(pos));
|
||||
pos += 1;
|
||||
@@ -232,20 +240,25 @@ fn createECCBlock(codewords: &str, numECWords: usize) -> Result<String, Exceptio
|
||||
// }
|
||||
for i in 0..codewords.chars().count() {
|
||||
// for (int i = 0; i < codewords.length(); i++) {
|
||||
let m = ecc[numECWords - 1] as usize ^ codewords.chars().nth(i).unwrap() as usize;
|
||||
let m = ecc[numECWords - 1] as usize
|
||||
^ codewords
|
||||
.chars()
|
||||
.nth(i)
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))? as usize;
|
||||
for k in (1..numECWords).rev() {
|
||||
// for (int k = numECWords - 1; k > 0; k--) {
|
||||
if m != 0 && poly[k] != 0 {
|
||||
ecc[k] = char::from_u32(
|
||||
ecc[k - 1] as u32 ^ ALOG[(LOG[m] + LOG[poly[k] as usize]) as usize % 255],
|
||||
)
|
||||
.unwrap();
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?;
|
||||
} else {
|
||||
ecc[k] = ecc[k - 1];
|
||||
}
|
||||
}
|
||||
if m != 0 && poly[0] != 0 {
|
||||
ecc[0] = char::from_u32(ALOG[(LOG[m] + LOG[poly[0] as usize]) as usize % 255]).unwrap();
|
||||
ecc[0] = char::from_u32(ALOG[(LOG[m] + LOG[poly[0] as usize]) as usize % 255])
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?;
|
||||
} else {
|
||||
ecc[0] = 0 as char;
|
||||
}
|
||||
|
||||
@@ -121,7 +121,8 @@ fn randomize253State(codewordPosition: u32) -> String {
|
||||
} else {
|
||||
char::from_u32(tempVariable - 254)
|
||||
}
|
||||
.expect("must become a char")
|
||||
// .expect("must become a char")
|
||||
.unwrap_or(char::default())
|
||||
.to_string()
|
||||
// return (char) (tempVariable <= 254 ? tempVariable : tempVariable - 254);
|
||||
}
|
||||
@@ -219,21 +220,28 @@ pub fn encodeHighLevelWithDimensionForceC40WithSymbolInfoLookup(
|
||||
let mut encodingMode = ASCII_ENCODATION; //Default mode
|
||||
|
||||
if forceC40 {
|
||||
c40Encoder.encodeMaximalC40(&mut context);
|
||||
encodingMode = context.getNewEncoding().unwrap();
|
||||
c40Encoder.encodeMaximalC40(&mut context)?;
|
||||
encodingMode = context
|
||||
.getNewEncoding()
|
||||
.ok_or(Exceptions::IllegalStateException(None))?;
|
||||
context.resetEncoderSignal();
|
||||
}
|
||||
|
||||
while context.hasMoreCharacters() {
|
||||
encoders[encodingMode].encode(&mut context)?;
|
||||
if context.getNewEncoding().is_some() {
|
||||
encodingMode = context.getNewEncoding().unwrap();
|
||||
encodingMode = context
|
||||
.getNewEncoding()
|
||||
.ok_or(Exceptions::IllegalStateException(None))?;
|
||||
context.resetEncoderSignal();
|
||||
}
|
||||
}
|
||||
let len = context.getCodewordCount();
|
||||
context.updateSymbolInfo();
|
||||
let capacity = context.getSymbolInfo().unwrap().getDataCapacity();
|
||||
let capacity = context
|
||||
.getSymbolInfo()
|
||||
.ok_or(Exceptions::IllegalStateException(None))?
|
||||
.getDataCapacity();
|
||||
if len < capacity as usize
|
||||
&& encodingMode != ASCII_ENCODATION
|
||||
&& encodingMode != BASE256_ENCODATION
|
||||
@@ -289,8 +297,10 @@ pub fn lookAheadTest(msg: &str, startpos: u32, currentMode: u32) -> usize {
|
||||
let endpos = (startpos + 3).min(msg.chars().count() as u32);
|
||||
for i in startpos..endpos {
|
||||
// for (int i = startpos; i < endpos; i++) {
|
||||
if !isNativeX12(msg.chars().nth(i as usize).unwrap()) {
|
||||
return ASCII_ENCODATION;
|
||||
if let Some(c) = msg.chars().nth(i as usize) {
|
||||
if !isNativeX12(c) {
|
||||
return ASCII_ENCODATION;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if currentMode as usize == EDIFACT_ENCODATION && newMode == EDIFACT_ENCODATION {
|
||||
@@ -298,8 +308,10 @@ pub fn lookAheadTest(msg: &str, startpos: u32, currentMode: u32) -> usize {
|
||||
let endpos = (startpos + 4).min(msg.chars().count() as u32);
|
||||
for i in startpos..endpos {
|
||||
// for (int i = startpos; i < endpos; i++) {
|
||||
if !isNativeEDIFACT(msg.chars().nth(i as usize).unwrap()) {
|
||||
return ASCII_ENCODATION;
|
||||
if let Some(c) = msg.chars().nth(i as usize) {
|
||||
if !isNativeEDIFACT(c) {
|
||||
return ASCII_ENCODATION;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -352,15 +364,11 @@ fn lookAheadTestIntern(msg: &str, startpos: u32, currentMode: u32) -> usize {
|
||||
return C40_ENCODATION;
|
||||
}
|
||||
|
||||
// let c = msg
|
||||
// .graphemes(true)
|
||||
// .nth((startpos + charsProcessed) as usize)
|
||||
// .unwrap();
|
||||
// let c = msg.charAt(startpos + charsProcessed);
|
||||
let c = msg
|
||||
.chars()
|
||||
.nth((startpos + charsProcessed) as usize)
|
||||
.unwrap();
|
||||
let Some(c) = msg
|
||||
.chars()
|
||||
.nth((startpos + charsProcessed) as usize) else {
|
||||
break 0;
|
||||
};
|
||||
charsProcessed += 1;
|
||||
|
||||
//step L
|
||||
@@ -593,7 +601,8 @@ pub fn determineConsecutiveDigitCount(msg: &str, startpos: u32) -> u32 {
|
||||
let len = msg.chars().count(); //len();
|
||||
let mut idx = startpos;
|
||||
// let graphemes = msg.graphemes(true);
|
||||
while (idx as usize) < len && isDigit(msg.chars().nth(idx as usize).unwrap()) {
|
||||
while (idx as usize) < len && isDigit(msg.chars().nth(idx as usize).unwrap_or(char::default()))
|
||||
{
|
||||
idx += 1;
|
||||
}
|
||||
idx - startpos
|
||||
|
||||
@@ -218,7 +218,7 @@ fn addEdge(edges: &mut [Vec<Option<Rc<Edge>>>], edge: Rc<Edge>) -> Result<(), Ex
|
||||
if edges[vertexIndex][edge.getEndMode()?.ordinal()].is_none()
|
||||
|| edges[vertexIndex][edge.getEndMode()?.ordinal()]
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.ok_or(Exceptions::IllegalStateException(None))?
|
||||
.cachedTotalSize
|
||||
> edge.cachedTotalSize
|
||||
{
|
||||
@@ -296,7 +296,7 @@ fn addEdges(
|
||||
//not possible to unlatch a full EDF edge to something
|
||||
//else
|
||||
if high_level_encoder::isDigit(ch)
|
||||
&& input.haveNCharacters(from as usize, 2)
|
||||
&& input.haveNCharacters(from as usize, 2)?
|
||||
&& high_level_encoder::isDigit(input.charAt(from as usize + 1)?)
|
||||
{
|
||||
// two digits ASCII encoded
|
||||
@@ -344,7 +344,7 @@ fn addEdges(
|
||||
}
|
||||
}
|
||||
|
||||
if input.haveNCharacters(from as usize, 3)
|
||||
if input.haveNCharacters(from as usize, 3)?
|
||||
&& high_level_encoder::isNativeX12(input.charAt(from as usize)?)
|
||||
&& high_level_encoder::isNativeX12(input.charAt(from as usize + 1)?)
|
||||
&& high_level_encoder::isNativeX12(input.charAt(from as usize + 2)?)
|
||||
@@ -379,7 +379,7 @@ fn addEdges(
|
||||
while i < 3 {
|
||||
// for (i = 0; i < 3; i++) {
|
||||
let pos = from + i;
|
||||
if input.haveNCharacters(pos as usize, 1)
|
||||
if input.haveNCharacters(pos as usize, 1)?
|
||||
&& high_level_encoder::isNativeEDIFACT(input.charAt(pos as usize)?)
|
||||
{
|
||||
addEdge(
|
||||
@@ -398,7 +398,7 @@ fn addEdges(
|
||||
i += 1;
|
||||
}
|
||||
if i == 3
|
||||
&& input.haveNCharacters(from as usize, 4)
|
||||
&& input.haveNCharacters(from as usize, 4)?
|
||||
&& high_level_encoder::isNativeEDIFACT(input.charAt(from as usize + 3)?)
|
||||
{
|
||||
addEdge(
|
||||
@@ -604,9 +604,7 @@ fn encodeMinimally(input: Rc<Input>) -> Result<RXingResult, Exceptions> {
|
||||
addEdges(input.clone(), &mut edges, 0, None)?;
|
||||
|
||||
for i in 1..=inputLength {
|
||||
// for (int i = 1; i <= inputLength; i++) {
|
||||
for j in 0..6 {
|
||||
// for (int j = 0; j < 6; j++) {
|
||||
if edges[i][j].is_some() && i < inputLength {
|
||||
let edge = edges[i][j].clone();
|
||||
addEdges(input.clone(), &mut edges, i as u32, edge)?;
|
||||
@@ -614,7 +612,6 @@ fn encodeMinimally(input: Rc<Input>) -> Result<RXingResult, Exceptions> {
|
||||
}
|
||||
//optimize memory by removing edges that have been passed.
|
||||
for j in 0..6 {
|
||||
// for (int j = 0; j < 6; j++) {
|
||||
edges[i - 1][j] = None;
|
||||
}
|
||||
}
|
||||
@@ -622,7 +619,6 @@ fn encodeMinimally(input: Rc<Input>) -> Result<RXingResult, Exceptions> {
|
||||
let mut minimalJ: i32 = -1;
|
||||
let mut minimalSize = i32::MAX;
|
||||
for j in 0..6 {
|
||||
// for (int j = 0; j < 6; j++) {
|
||||
if edges[inputLength][j].is_some() {
|
||||
let edge = edges[inputLength][j].as_ref().unwrap();
|
||||
let size = if (1..=3).contains(&j) {
|
||||
@@ -672,12 +668,9 @@ impl Edge {
|
||||
characterLength: u32,
|
||||
previous: Option<Rc<Edge>>,
|
||||
) -> Result<Self, Exceptions> {
|
||||
// this.input = input;
|
||||
// this.mode = mode;
|
||||
// this.fromPosition = fromPosition;
|
||||
// this.characterLength = characterLength;
|
||||
// this.previous = previous;
|
||||
assert!(fromPosition + characterLength <= input.length() as u32);
|
||||
if fromPosition + characterLength > input.length() as u32 {
|
||||
return Err(Exceptions::FormatException(None));
|
||||
}
|
||||
|
||||
let mut size = if let Some(previous) = previous.clone() {
|
||||
previous.cachedTotalSize
|
||||
@@ -704,11 +697,9 @@ impl Edge {
|
||||
match mode {
|
||||
Mode::Ascii => {
|
||||
size += 1;
|
||||
if input.isECI(fromPosition).expect("bool")
|
||||
if input.isECI(fromPosition)?
|
||||
|| isExtendedASCII(
|
||||
input
|
||||
.charAt(fromPosition as usize)
|
||||
.expect("char must exist)"),
|
||||
input.charAt(fromPosition as usize)?,
|
||||
input.getFNC1Character(),
|
||||
)
|
||||
{
|
||||
@@ -748,9 +739,7 @@ impl Edge {
|
||||
fromPosition,
|
||||
mode == Mode::C40,
|
||||
&mut charLen,
|
||||
)
|
||||
.expect("works")
|
||||
* 2;
|
||||
)? * 2;
|
||||
}
|
||||
|
||||
if previousMode == Mode::Ascii || previousMode == Mode::B256 {
|
||||
@@ -1382,10 +1371,9 @@ impl RXingResult {
|
||||
|
||||
pub fn applyRandomPattern(bytesAL: &mut [u8], startPosition: u32, length: u32) {
|
||||
for i in 0..length as usize {
|
||||
// for (int i = 0; i < length; i++) {
|
||||
//See "B.1 253-state algorithm
|
||||
let Pad_codeword_position = startPosition as usize + i;
|
||||
let Pad_codeword_value = bytesAL.get(Pad_codeword_position).expect("known to exist");
|
||||
let Pad_codeword_value = bytesAL.get(Pad_codeword_position).unwrap_or(&0);
|
||||
let pseudo_random_number = ((149 * (Pad_codeword_position + 1)) % 255) + 1;
|
||||
let temp_variable: u16 = *Pad_codeword_value as u16 + pseudo_random_number as u16;
|
||||
bytesAL[Pad_codeword_position] = if temp_variable <= 255 {
|
||||
@@ -1452,7 +1440,7 @@ impl Input {
|
||||
Some(self.internal.getFNC1Character() as u8 as char)
|
||||
}
|
||||
}
|
||||
fn haveNCharacters(&self, index: usize, n: usize) -> bool {
|
||||
fn haveNCharacters(&self, index: usize, n: usize) -> Result<bool, Exceptions> {
|
||||
self.internal.haveNCharacters(index, n)
|
||||
}
|
||||
fn isFNC1(&self, index: usize) -> Result<bool, Exceptions> {
|
||||
|
||||
@@ -132,19 +132,6 @@ impl SymbolInfo {
|
||||
"Cannot handle this number of data regions".to_owned(),
|
||||
))),
|
||||
}
|
||||
// switch (dataRegions) {
|
||||
// case 1:
|
||||
// return 1;
|
||||
// case 2:
|
||||
// case 4:
|
||||
// return 2;
|
||||
// case 16:
|
||||
// return 4;
|
||||
// case 36:
|
||||
// return 6;
|
||||
// default:
|
||||
// throw new IllegalStateException("Cannot handle this number of data regions");
|
||||
// }
|
||||
}
|
||||
|
||||
fn getVerticalDataRegions(&self) -> Result<u32, Exceptions> {
|
||||
@@ -157,19 +144,6 @@ impl SymbolInfo {
|
||||
"Cannot handle this number of data regions".to_owned(),
|
||||
))),
|
||||
}
|
||||
// switch (dataRegions) {
|
||||
// case 1:
|
||||
// case 2:
|
||||
// return 1;
|
||||
// case 4:
|
||||
// return 2;
|
||||
// case 16:
|
||||
// return 4;
|
||||
// case 36:
|
||||
// return 6;
|
||||
// default:
|
||||
// throw new IllegalStateException("Cannot handle this number of data regions");
|
||||
// }
|
||||
}
|
||||
|
||||
pub fn getSymbolDataWidth(&self) -> Result<u32, Exceptions> {
|
||||
@@ -223,15 +197,6 @@ impl SymbolInfo {
|
||||
pub fn getErrorLengthForInterleavedBlock(&self, _index: u32) -> u32 {
|
||||
self.rsBlockError
|
||||
}
|
||||
|
||||
// @Override
|
||||
// public final String toString() {
|
||||
// return (rectangular ? "Rectangular Symbol:" : "Square Symbol:") +
|
||||
// " data region " + matrixWidth + 'x' + matrixHeight +
|
||||
// ", symbol size " + getSymbolWidth() + 'x' + getSymbolHeight() +
|
||||
// ", symbol data size " + getSymbolDataWidth() + 'x' + getSymbolDataHeight() +
|
||||
// ", codewords " + dataCapacity + '+' + errorCodewords;
|
||||
// }
|
||||
}
|
||||
|
||||
impl fmt::Display for SymbolInfo {
|
||||
@@ -424,7 +389,7 @@ mod tests {
|
||||
assert_eq!(24, info.getSymbolWidth().expect("return"));
|
||||
assert_eq!(24, info.getSymbolHeight().expect("return"));
|
||||
|
||||
let fixedSize = Dimension::new(26, 26).expect("new dimension");
|
||||
let fixedSize = Dimension::new(26, 26);
|
||||
let info = LOOKUP
|
||||
.lookup_with_codewords_shape_size_fail(
|
||||
35,
|
||||
@@ -451,7 +416,7 @@ mod tests {
|
||||
assert!(info.is_none());
|
||||
|
||||
let minSize = fixedSize;
|
||||
let maxSize = Dimension::new(32, 32).expect("new dimension");
|
||||
let maxSize = Dimension::new(32, 32);
|
||||
|
||||
let info = LOOKUP
|
||||
.lookup_with_codewords_shape_size_fail(
|
||||
|
||||
@@ -31,11 +31,11 @@ impl Encoder for X12Encoder {
|
||||
let c = context.getCurrentChar();
|
||||
context.pos += 1;
|
||||
|
||||
Self::encodeChar(c, &mut buffer);
|
||||
Self::encodeChar(c, &mut buffer)?;
|
||||
|
||||
let count = buffer.chars().count();
|
||||
if (count % 3) == 0 {
|
||||
C40Encoder::writeNextTriplet(context, &mut buffer);
|
||||
C40Encoder::writeNextTriplet(context, &mut buffer)?;
|
||||
|
||||
let newMode = high_level_encoder::lookAheadTest(
|
||||
context.getMessage(),
|
||||
@@ -58,7 +58,7 @@ impl X12Encoder {
|
||||
Self(C40Encoder::new())
|
||||
}
|
||||
|
||||
fn encodeChar(c: char, sb: &mut String) -> u32 {
|
||||
fn encodeChar(c: char, sb: &mut String) -> Result<u32, Exceptions> {
|
||||
match c {
|
||||
'\r' => sb.push('\0'),
|
||||
'*' => sb.push('\u{1}'),
|
||||
@@ -70,17 +70,20 @@ impl X12Encoder {
|
||||
} else if ('A'..='Z').contains(&c) {
|
||||
sb.push((c as u8 - 65 + 14) as char);
|
||||
} else {
|
||||
high_level_encoder::illegalCharacter(c).expect("detect_illegal_character");
|
||||
high_level_encoder::illegalCharacter(c)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
1
|
||||
Ok(1)
|
||||
}
|
||||
|
||||
fn handleEOD(context: &mut EncoderContext, buffer: &mut str) -> Result<(), Exceptions> {
|
||||
context.updateSymbolInfo();
|
||||
let available =
|
||||
context.getSymbolInfo().unwrap().getDataCapacity() - context.getCodewordCount() as u32;
|
||||
let available = context
|
||||
.getSymbolInfo()
|
||||
.ok_or(Exceptions::IllegalStateException(None))?
|
||||
.getDataCapacity()
|
||||
- context.getCodewordCount() as u32;
|
||||
let count = buffer.chars().count();
|
||||
context.pos -= count as u32;
|
||||
if context.getRemainingCharacters() > 1
|
||||
|
||||
Reference in New Issue
Block a user