high level encoder builds

This commit is contained in:
Henry Schimke
2022-10-27 17:45:32 -05:00
parent fb6dfac670
commit 7ac08007e7
6 changed files with 45 additions and 44 deletions

View File

@@ -28,7 +28,7 @@ impl Encoder for Base256Encoder {
} }
fn encode(&self, context: &mut super::EncoderContext) -> Result<(), crate::Exceptions> { fn encode(&self, context: &mut super::EncoderContext) -> Result<(), crate::Exceptions> {
let buffer = String::new(); let mut buffer = String::new();
buffer.push('\0'); //Initialize length field buffer.push('\0'); //Initialize length field
while context.hasMoreCharacters() { while context.hasMoreCharacters() {
let c = context.getCurrentChar(); let c = context.getCurrentChar();
@@ -55,7 +55,7 @@ impl Encoder for Base256Encoder {
if context.hasMoreCharacters() || mustPad { if context.hasMoreCharacters() || mustPad {
if dataCount <= 249 { 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).unwrap().to_string());
} else if dataCount as u8 <= 1555 { } else if dataCount <= 1555 {
buffer.replace_range( buffer.replace_range(
0..1, 0..1,
&char::from_u32(((dataCount as u32 / 250) + 249)) &char::from_u32(((dataCount as u32 / 250) + 249))

View File

@@ -43,7 +43,7 @@ impl C40Encoder {
&self, &self,
context: &mut super::EncoderContext, context: &mut super::EncoderContext,
encodeChar: &dyn Fn(char, &mut String) -> u32, encodeChar: &dyn Fn(char, &mut String) -> u32,
handleEOD : &dyn Fn( &EncoderContext, &mut String) -> Result<(), Exceptions>, handleEOD : &dyn Fn( &mut EncoderContext, &mut String) -> Result<(), Exceptions>,
) -> Result<(), Exceptions> { ) -> Result<(), Exceptions> {
//step C //step C
let mut buffer = String::new(); let mut buffer = String::new();
@@ -51,7 +51,7 @@ impl C40Encoder {
let c = context.getCurrentChar(); let c = context.getCurrentChar();
context.pos += 1; context.pos += 1;
let lastCharSize = encodeChar(c, &mut buffer); let mut lastCharSize = encodeChar(c, &mut buffer);
let unwritten = (buffer.len() / 3) * 2; let unwritten = (buffer.len() / 3) * 2;
@@ -76,7 +76,7 @@ impl C40Encoder {
lastCharSize = self.backtrackOneCharacter( lastCharSize = self.backtrackOneCharacter(
context, context,
&mut buffer, &mut buffer,
&removed, &mut removed,
lastCharSize, lastCharSize,
encodeChar, encodeChar,
); );
@@ -102,20 +102,20 @@ impl C40Encoder {
Ok(()) Ok(())
} }
pub fn encodeMaximalC40(&self, context: &EncoderContext,) { pub fn encodeMaximalC40(&self, context: &mut EncoderContext,) {
self.encodeMaximal(context, &Self::encodeChar_c40, &Self::handleEOD_c40) self.encodeMaximal(context, &Self::encodeChar_c40, &Self::handleEOD_c40)
} }
fn encodeMaximal( fn encodeMaximal(
&self, &self,
context: &EncoderContext, context: &mut EncoderContext,
encodeChar: &dyn Fn(char, &mut String) -> u32, encodeChar: &dyn Fn(char, &mut String) -> u32,
handleEOD : &dyn Fn( &EncoderContext, &mut String) -> Result<(), Exceptions>, handleEOD : &dyn Fn( &mut EncoderContext, &mut String) -> Result<(), Exceptions>,
) { ) {
let buffer = String::new(); let mut buffer = String::new();
let lastCharSize = 0; let mut lastCharSize = 0;
let backtrackStartPosition = context.pos; let mut backtrackStartPosition = context.pos;
let backtrackBufferLength = 0; let mut backtrackBufferLength = 0;
while context.hasMoreCharacters() { while context.hasMoreCharacters() {
let c = context.getCurrentChar(); let c = context.getCurrentChar();
context.pos += 1; context.pos += 1;
@@ -144,14 +144,14 @@ impl C40Encoder {
context.writeCodeword(LATCH_TO_C40); context.writeCodeword(LATCH_TO_C40);
} }
handleEOD(context, &mut buffer); handleEOD( context, &mut buffer);
} }
fn backtrackOneCharacter( fn backtrackOneCharacter(
&self, &self,
context: &EncoderContext, context: &mut EncoderContext,
buffer: &mut String, buffer: &mut String,
removed: &String, removed: &mut String,
lastCharSize: u32, lastCharSize: u32,
encodeChar: &dyn Fn(char, &mut String) -> u32, encodeChar: &dyn Fn(char, &mut String) -> u32,
) -> u32 { ) -> u32 {
@@ -160,12 +160,12 @@ impl C40Encoder {
buffer.replace_range((count - lastCharSize as usize)..count, ""); buffer.replace_range((count - lastCharSize as usize)..count, "");
context.pos -= 1; context.pos -= 1;
let c = context.getCurrentChar(); let c = context.getCurrentChar();
lastCharSize = encodeChar(c, &mut removed); let lastCharSize = encodeChar(c, removed);
context.resetSymbolInfo(); //Deal with possible reduction in symbol size context.resetSymbolInfo(); //Deal with possible reduction in symbol size
return lastCharSize; return lastCharSize;
} }
pub(super) fn writeNextTriplet(context: &EncoderContext, buffer: &mut String) { pub(super) fn writeNextTriplet(context: &mut EncoderContext, buffer: &mut String) {
context.writeCodewords(&Self::encodeToCodewords(buffer)); context.writeCodewords(&Self::encodeToCodewords(buffer));
buffer.replace_range(0..3, ""); buffer.replace_range(0..3, "");
// buffer.delete(0, 3); // buffer.delete(0, 3);
@@ -177,7 +177,7 @@ impl C40Encoder {
* @param context the encoder context * @param context the encoder context
* @param buffer the buffer with the remaining encoded characters * @param buffer the buffer with the remaining encoded characters
*/ */
pub fn handleEOD_c40( context: &EncoderContext, buffer: &mut String) -> Result<(), Exceptions> { pub fn handleEOD_c40( context: &mut EncoderContext, buffer: &mut String) -> Result<(), Exceptions> {
let unwritten = (buffer.len() / 3) * 2; let unwritten = (buffer.len() / 3) * 2;
let rest = buffer.len() % 3; let rest = buffer.len() % 3;
@@ -259,7 +259,7 @@ impl C40Encoder {
return 2; return 2;
} }
sb.push_str("\u{1}\u{001e}"); //Shift 2, Upper Shift sb.push_str("\u{1}\u{001e}"); //Shift 2, Upper Shift
let len = 2; let mut len = 2;
len += Self::encodeChar_c40((c as u8 - 128) as char, sb); len += Self::encodeChar_c40((c as u8 - 128) as char, sb);
len len

View File

@@ -26,7 +26,7 @@ impl Encoder for EdifactEncoder {
fn encode(&self, context: &mut super::EncoderContext) -> Result<(), crate::Exceptions> { fn encode(&self, context: &mut super::EncoderContext) -> Result<(), crate::Exceptions> {
//step F //step F
let buffer = String::new(); let mut buffer = String::new();
while context.hasMoreCharacters() { while context.hasMoreCharacters() {
let c = context.getCurrentChar(); let c = context.getCurrentChar();
Self::encodeChar(c, &mut buffer); Self::encodeChar(c, &mut buffer);
@@ -65,8 +65,8 @@ impl EdifactEncoder {
* @param context the encoder context * @param context the encoder context
* @param buffer the buffer with the remaining encoded characters * @param buffer the buffer with the remaining encoded characters
*/ */
fn handleEOD(context: &EncoderContext, buffer: &mut String) -> Result<(), Exceptions> { fn handleEOD(context: &mut EncoderContext, buffer: &mut String) -> Result<(), Exceptions> {
let runner = || -> Result<(), Exceptions> { let mut runner = || -> Result<(), Exceptions> {
let count = buffer.len(); let count = buffer.len();
if count == 0 { if count == 0 {
return Ok(()); //Already finished return Ok(()); //Already finished
@@ -74,7 +74,7 @@ impl EdifactEncoder {
if count == 1 { if count == 1 {
//Only an unlatch at the end //Only an unlatch at the end
context.updateSymbolInfo(); context.updateSymbolInfo();
let available = context.getSymbolInfo().unwrap().getDataCapacity() let mut available = context.getSymbolInfo().unwrap().getDataCapacity()
- context.getCodewordCount() as u32; - context.getCodewordCount() as u32;
let remaining = context.getRemainingCharacters(); let remaining = context.getRemainingCharacters();
// The following two lines are a hack inspired by the 'fix' from https://sourceforge.net/p/barcode4j/svn/221/ // The following two lines are a hack inspired by the 'fix' from https://sourceforge.net/p/barcode4j/svn/221/
@@ -96,7 +96,7 @@ impl EdifactEncoder {
let restChars = count - 1; let restChars = count - 1;
let encoded = Self::encodeToCodewords(buffer)?; let encoded = Self::encodeToCodewords(buffer)?;
let endOfSymbolReached = !context.hasMoreCharacters(); let endOfSymbolReached = !context.hasMoreCharacters();
let restInAscii = endOfSymbolReached && restChars <= 2; let mut restInAscii = endOfSymbolReached && restChars <= 2;
if restChars <= 2 { if restChars <= 2 {
context.updateSymbolInfoWithLength(context.getCodewordCount() + restChars); context.updateSymbolInfoWithLength(context.getCodewordCount() + restChars);
@@ -162,7 +162,7 @@ impl EdifactEncoder {
let cw1 = (v as u32 >> 16) & 255; let cw1 = (v as u32 >> 16) & 255;
let cw2 = (v as u32 >> 8) & 255; let cw2 = (v as u32 >> 8) & 255;
let cw3 = v as u32 & 255; let cw3 = v as u32 & 255;
let res = String::with_capacity(3); let mut res = String::with_capacity(3);
res.push(char::from_u32(cw1).unwrap()); res.push(char::from_u32(cw1).unwrap());
if len >= 2 { if len >= 2 {
res.push(char::from_u32(cw2).unwrap()); res.push(char::from_u32(cw2).unwrap());

View File

@@ -201,7 +201,7 @@ pub fn encodeHighLevelWithDimensionForceC40(
context.pos += MACRO_06_HEADER.len() as u32; context.pos += MACRO_06_HEADER.len() as u32;
} }
let encodingMode = ASCII_ENCODATION; //Default mode let mut encodingMode = ASCII_ENCODATION; //Default mode
if forceC40 { if forceC40 {
c40Encoder.encodeMaximalC40(&mut context); c40Encoder.encodeMaximalC40(&mut context);
@@ -228,14 +228,14 @@ pub fn encodeHighLevelWithDimensionForceC40(
// context.writeCodeword("\u{00fe}"); //Unlatch (254) // context.writeCodeword("\u{00fe}"); //Unlatch (254)
} }
//Padding //Padding
let codewords = context.getCodewords(); // let codewords = context.getCodewords();
if codewords.len() < capacity as usize{ if context.getCodewords().len() < capacity as usize{
// codewords.push(PAD as char); // codewords.push(PAD as char);
context.writeCodeword(PAD) context.writeCodeword(PAD)
} }
while codewords.len() < capacity as usize { while context.getCodewords().len() < capacity as usize {
// codewords.append(randomize253State(codewords.len() + 1)); // codewords.append(randomize253State(codewords.len() + 1));
context.writeCodewords(&randomize253State(codewords.len() as u32 + 1)) context.writeCodewords(&randomize253State(context.getCodewords().len() as u32 + 1))
} }
Ok(context.getCodewords().to_owned()) Ok(context.getCodewords().to_owned())
@@ -269,7 +269,7 @@ fn lookAheadTestIntern(msg: &str, startpos: u32, currentMode: u32) -> usize {
if startpos as usize >= msg.len() { if startpos as usize >= msg.len() {
return currentMode as usize; return currentMode as usize;
} }
let charCounts: [f32; 6]; let mut charCounts: [f32; 6];
//step J //step J
if currentMode == ASCII_ENCODATION as u32 { if currentMode == ASCII_ENCODATION as u32 {
charCounts = [0.0, 1.0, 1.0, 1.0, 1.0, 1.25]; charCounts = [0.0, 1.0, 1.0, 1.0, 1.0, 1.25];
@@ -278,9 +278,9 @@ fn lookAheadTestIntern(msg: &str, startpos: u32, currentMode: u32) -> usize {
charCounts[currentMode as usize] = 0.0; charCounts[currentMode as usize] = 0.0;
} }
let charsProcessed = 0; let mut charsProcessed = 0;
let mins = [0u8; 6]; let mut mins = [0u8; 6];
let intCharCounts = [0u32; 6]; let mut intCharCounts = [0u32; 6];
loop { loop {
//step K //step K
if (startpos + charsProcessed) == msg.len() as u32 { if (startpos + charsProcessed) == msg.len() as u32 {
@@ -288,7 +288,7 @@ fn lookAheadTestIntern(msg: &str, startpos: u32, currentMode: u32) -> usize {
intCharCounts.fill(0); intCharCounts.fill(0);
// Arrays.fill(mins, (byte) 0); // Arrays.fill(mins, (byte) 0);
// Arrays.fill(intCharCounts, 0); // Arrays.fill(intCharCounts, 0);
let min = findMinimums(&charCounts, &intCharCounts, u32::MAX, &mins); let min = findMinimums(&charCounts, &mut intCharCounts, u32::MAX, &mut mins);
let minCount = getMinimumCount(&mins); let minCount = getMinimumCount(&mins);
if intCharCounts[ASCII_ENCODATION] == min { if intCharCounts[ASCII_ENCODATION] == min {
@@ -379,7 +379,7 @@ fn lookAheadTestIntern(msg: &str, startpos: u32, currentMode: u32) -> usize {
intCharCounts.fill(0); intCharCounts.fill(0);
// Arrays.fill(mins, (byte) 0); // Arrays.fill(mins, (byte) 0);
// Arrays.fill(intCharCounts, 0); // Arrays.fill(intCharCounts, 0);
findMinimums(&charCounts, &intCharCounts, u32::MAX, &mins); findMinimums(&charCounts, &mut intCharCounts, u32::MAX, &mut mins);
if intCharCounts[ASCII_ENCODATION] if intCharCounts[ASCII_ENCODATION]
< min5( < min5(
@@ -448,7 +448,7 @@ fn lookAheadTestIntern(msg: &str, startpos: u32, currentMode: u32) -> usize {
return C40_ENCODATION; return C40_ENCODATION;
} }
if intCharCounts[C40_ENCODATION] == intCharCounts[X12_ENCODATION] { if intCharCounts[C40_ENCODATION] == intCharCounts[X12_ENCODATION] {
let p = startpos + charsProcessed + 1; let mut _p = startpos + charsProcessed + 1;
for tc in msg.chars() { for tc in msg.chars() {
// while (p as usize) < msg.len() { // while (p as usize) < msg.len() {
// let tc = msg.charAt(p); // let tc = msg.charAt(p);
@@ -458,7 +458,7 @@ fn lookAheadTestIntern(msg: &str, startpos: u32, currentMode: u32) -> usize {
if !isNativeX12(tc) { if !isNativeX12(tc) {
break; break;
} }
p += 1; _p += 1;
} }
return C40_ENCODATION; return C40_ENCODATION;
} }
@@ -476,7 +476,8 @@ fn min4(f1: u32, f2: u32, f3: u32, f4: u32) -> u32 {
// Math.min(f1, Math.min(f2, Math.min(f3, f4))) // Math.min(f1, Math.min(f2, Math.min(f3, f4)))
} }
fn findMinimums(charCounts: &[f32; 6], intCharCounts: &[u32; 6], min: u32, mins: &[u8]) -> u32 { fn findMinimums(charCounts: &[f32; 6], intCharCounts: &mut [u32; 6], min: u32, mins: &mut [u8]) -> u32 {
let mut min = min;
for i in 0..6 { for i in 0..6 {
// for (int i = 0; i < 6; i++) { // for (int i = 0; i < 6; i++) {
intCharCounts[i] = charCounts[i].ceil() as u32; intCharCounts[i] = charCounts[i].ceil() as u32;
@@ -494,7 +495,7 @@ fn findMinimums(charCounts: &[f32; 6], intCharCounts: &[u32; 6], min: u32, mins:
} }
fn getMinimumCount(mins: &[u8]) -> u32 { fn getMinimumCount(mins: &[u8]) -> u32 {
let minCount = 0; let mut minCount = 0;
for i in 0..6 { for i in 0..6 {
// for (int i = 0; i < 6; i++) { // for (int i = 0; i < 6; i++) {
minCount += mins[i] as u32; minCount += mins[i] as u32;
@@ -549,7 +550,7 @@ fn isSpecialB256(ch: char) -> bool {
*/ */
pub fn determineConsecutiveDigitCount(msg: &str, startpos: u32) -> u32 { pub fn determineConsecutiveDigitCount(msg: &str, startpos: u32) -> u32 {
let len = msg.chars().count();//len(); let len = msg.chars().count();//len();
let idx = startpos; let mut idx = startpos;
// let graphemes = msg.graphemes(true); // 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()) {
idx += 1; idx += 1;

View File

@@ -80,7 +80,7 @@ impl TextEncoder {
return 2; return 2;
} }
sb.push_str("\u{1}\u{001e}"); //Shift 2, Upper Shift sb.push_str("\u{1}\u{001e}"); //Shift 2, Upper Shift
let len = 2; let mut len = 2;
len += Self::encodeChar((c as u8 - 128) as char, sb); len += Self::encodeChar((c as u8 - 128) as char, sb);
return len; return len;
} }

View File

@@ -26,7 +26,7 @@ impl Encoder for X12Encoder {
fn encode(&self, context: &mut super::EncoderContext) -> Result<(), crate::Exceptions> { fn encode(&self, context: &mut super::EncoderContext) -> Result<(), crate::Exceptions> {
//step C //step C
let buffer = String::new(); let mut buffer = String::new();
while context.hasMoreCharacters() { while context.hasMoreCharacters() {
let c = context.getCurrentChar(); let c = context.getCurrentChar();
context.pos += 1; context.pos += 1;
@@ -77,7 +77,7 @@ impl X12Encoder {
return 1; return 1;
} }
fn handleEOD(context: &EncoderContext, buffer: &mut String) -> Result<(), Exceptions> { fn handleEOD(context: &mut EncoderContext, buffer: &mut String) -> Result<(), Exceptions> {
context.updateSymbolInfo(); context.updateSymbolInfo();
let available = let available =
context.getSymbolInfo().unwrap().getDataCapacity() - context.getCodewordCount() as u32; context.getSymbolInfo().unwrap().getDataCapacity() - context.getCodewordCount() as u32;