minimal encoder tests pass

This commit is contained in:
Henry Schimke
2022-10-04 19:29:03 -05:00
parent 86f6bd0c69
commit 0435e0fb7c
4 changed files with 31 additions and 23 deletions

View File

@@ -2910,7 +2910,7 @@ const NAMES: [&str; 20] = [
#[derive(Clone)] #[derive(Clone)]
pub struct ECIEncoderSet { pub struct ECIEncoderSet {
encoders: Vec<EncodingRef>, encoders: Vec<EncodingRef>,
priorityEncoderIndex: usize, priorityEncoderIndex: Option<usize>,
} }
impl ECIEncoderSet { impl ECIEncoderSet {
@@ -2922,11 +2922,11 @@ impl ECIEncoderSet {
* @param fnc1 fnc1 denotes the character in the input that represents the FNC1 character or -1 for a non-GS1 bar * @param fnc1 fnc1 denotes the character in the input that represents the FNC1 character or -1 for a non-GS1 bar
* code. When specified, it is considered an error to pass it as argument to the methods canEncode() or encode(). * code. When specified, it is considered an error to pass it as argument to the methods canEncode() or encode().
*/ */
pub fn new(stringToEncodeMain: &str, priorityCharset: EncodingRef, fnc1: Option<&str>) -> Self { pub fn new(stringToEncodeMain: &str, priorityCharset: Option<EncodingRef>, fnc1: Option<&str>) -> Self {
// List of encoders that potentially encode characters not in ISO-8859-1 in one byte. // List of encoders that potentially encode characters not in ISO-8859-1 in one byte.
let mut encoders: Vec<EncodingRef>; let mut encoders: Vec<EncodingRef>;
let mut priorityEncoderIndexValue = 0; let mut priorityEncoderIndexValue = None;
let mut neededEncoders: Vec<EncodingRef> = Vec::new(); let mut neededEncoders: Vec<EncodingRef> = Vec::new();
@@ -2934,7 +2934,11 @@ impl ECIEncoderSet {
//we always need the ISO-8859-1 encoder. It is the default encoding //we always need the ISO-8859-1 encoder. It is the default encoding
neededEncoders.push(encoding::all::ISO_8859_1); neededEncoders.push(encoding::all::ISO_8859_1);
let mut needUnicodeEncoder = priorityCharset.name().starts_with("UTF"); let mut needUnicodeEncoder = if let Some(pc) = priorityCharset {
pc.name().starts_with("UTF")
}else{
false
};
//Walk over the input string and see if all characters can be encoded with the list of encoders //Walk over the input string and see if all characters can be encoded with the list of encoders
for i in 0..stringToEncode.len() { for i in 0..stringToEncode.len() {
@@ -3005,13 +3009,14 @@ impl ECIEncoderSet {
//Compute priorityEncoderIndex by looking up priorityCharset in encoders //Compute priorityEncoderIndex by looking up priorityCharset in encoders
// if priorityCharset != null { // if priorityCharset != null {
if priorityCharset.is_some() {
for i in 0..encoders.len() { for i in 0..encoders.len() {
// for (int i = 0; i < encoders.length; i++) { // for (int i = 0; i < encoders.length; i++) {
if priorityCharset.name() == encoders[i].name() { if priorityCharset.as_ref().unwrap().name() == encoders[i].name() {
priorityEncoderIndexValue = i; priorityEncoderIndexValue = Some(i);
break; break;
} }
} }}
// } // }
//invariants //invariants
assert_eq!(encoders[0].name(), encoding::all::ISO_8859_1.name()); assert_eq!(encoders[0].name(), encoding::all::ISO_8859_1.name());
@@ -3044,8 +3049,8 @@ impl ECIEncoderSet {
/* /*
* returns -1 if no priority charset was defined * returns -1 if no priority charset was defined
*/ */
pub fn getPriorityEncoderIndex(&self) -> usize { pub fn getPriorityEncoderIndex(&self) -> Option<usize> {
return self.priorityEncoderIndex; self.priorityEncoderIndex
} }
pub fn canEncode(&self, c: &str, encoderIndex: usize) -> bool { pub fn canEncode(&self, c: &str, encoderIndex: usize) -> bool {
@@ -3264,7 +3269,7 @@ impl MinimalECIInput {
* @param fnc1 denotes the character in the input that represents the FNC1 character or -1 if this is not GS1 * @param fnc1 denotes the character in the input that represents the FNC1 character or -1 if this is not GS1
* input. * input.
*/ */
pub fn new(stringToEncodeInput: &str, priorityCharset: EncodingRef, fnc1: Option<&str>) -> Self { pub fn new(stringToEncodeInput: &str, priorityCharset: Option<EncodingRef>, fnc1: Option<&str>) -> Self {
let stringToEncode = stringToEncodeInput.graphemes(true).collect::<Vec<&str>>(); let stringToEncode = stringToEncodeInput.graphemes(true).collect::<Vec<&str>>();
let encoderSet = ECIEncoderSet::new(stringToEncodeInput, priorityCharset, fnc1); let encoderSet = ECIEncoderSet::new(stringToEncodeInput, priorityCharset, fnc1);
let bytes = if encoderSet.len() == 1 { let bytes = if encoderSet.len() == 1 {
@@ -3333,10 +3338,10 @@ impl MinimalECIInput {
let mut start = 0; let mut start = 0;
let mut end = encoderSet.len(); let mut end = encoderSet.len();
if encoderSet.getPriorityEncoderIndex() >= 0 if encoderSet.getPriorityEncoderIndex().is_some()
&& (ch.chars().nth(0).unwrap() as u16 == fnc1 || encoderSet.canEncode(ch, encoderSet.getPriorityEncoderIndex())) && (ch.chars().nth(0).unwrap() as u16 == fnc1 || encoderSet.canEncode(ch, encoderSet.getPriorityEncoderIndex().unwrap()))
{ {
start = encoderSet.getPriorityEncoderIndex(); start = encoderSet.getPriorityEncoderIndex().unwrap();
end = start + 1; end = start + 1;
} }

View File

@@ -1100,9 +1100,10 @@ fn testMinimalEncoder41() {
#[test] #[test]
fn testMinimalEncoder42() { fn testMinimalEncoder42() {
// test halfwidth Katakana character (they are single byte encoded in Shift_JIS) // test halfwidth Katakana character (they are single byte encoded in Shift_JIS)
// NOTE: Changed to windows-31j because that is what is supported in encoding crate
verifyMinimalEncoding( verifyMinimalEncoding(
"Katakana:\u{FF66}\u{FF66}\u{FF66}\u{FF66}\u{FF66}\u{FF66}", "Katakana:\u{FF66}\u{FF66}\u{FF66}\u{FF66}\u{FF66}\u{FF66}",
"ECI(Shift_JIS),BYTE(Katakana:......)", "ECI(windows-31j),BYTE(Katakana:......)",
None, None,
false, false,
); );
@@ -1124,9 +1125,10 @@ fn testMinimalEncoder44() {
// The character \u30A2 encodes as double byte in Shift_JIS but KANJI is not more compact in this case because // The character \u30A2 encodes as double byte in Shift_JIS but KANJI is not more compact in this case because
// KANJI is only more compact when it encodes pairs of characters. In the case of mixed text it can however be // KANJI is only more compact when it encodes pairs of characters. In the case of mixed text it can however be
// that Shift_JIS encoding is more compact as in this example // that Shift_JIS encoding is more compact as in this example
// NOTE: Changed to windows-31j because that is what is supported in encoding crate
verifyMinimalEncoding( verifyMinimalEncoding(
"Katakana:\u{30A2}a\u{30A2}a\u{30A2}a\u{30A2}a\u{30A2}a\u{30A2}", "Katakana:\u{30A2}a\u{30A2}a\u{30A2}a\u{30A2}a\u{30A2}a\u{30A2}",
"ECI(Shift_JIS),BYTE(Katakana:.a.a.a.a.a.)", "ECI(windows-31j),BYTE(Katakana:.a.a.a.a.a.)",
None, None,
false, false,
); );
@@ -1141,7 +1143,7 @@ fn verifyMinimalEncoding(
let result = MinimalEncoder::encode_with_details( let result = MinimalEncoder::encode_with_details(
input, input,
None, None,
priorityCharset.unwrap_or(encoding::all::ISO_8859_1), priorityCharset,
isGS1, isGS1,
ErrorCorrectionLevel::L, ErrorCorrectionLevel::L,
) )

View File

@@ -136,11 +136,12 @@ pub fn encode_with_hints(
if hasCompactionHint { if hasCompactionHint {
mode = Mode::BYTE; mode = Mode::BYTE;
dbg!("consider this a huge risk, not sure if it should be defaulting to default");
let priorityEncoding = encoding; //if encoding.name() == DEFAULT_BYTE_MODE_ENCODING.name() {None} else {Some(encoding)}; let priorityEncoding = encoding; //if encoding.name() == DEFAULT_BYTE_MODE_ENCODING.name() {None} else {Some(encoding)};
let rn = MinimalEncoder::encode_with_details( let rn = MinimalEncoder::encode_with_details(
content, content,
None, None,
priorityEncoding, Some(priorityEncoding),
hasGS1FormatHint, hasGS1FormatHint,
ecLevel, ecLevel,
)?; )?;

View File

@@ -107,7 +107,7 @@ impl MinimalEncoder {
*/ */
pub fn new( pub fn new(
stringToEncode: &str, stringToEncode: &str,
priorityCharset: EncodingRef, priorityCharset: Option<EncodingRef>,
isGS1: bool, isGS1: bool,
ecLevel: ErrorCorrectionLevel, ecLevel: ErrorCorrectionLevel,
) -> Self { ) -> Self {
@@ -144,7 +144,7 @@ impl MinimalEncoder {
pub fn encode_with_details( pub fn encode_with_details(
stringToEncode: &str, stringToEncode: &str,
version: Option<VersionRef>, version: Option<VersionRef>,
priorityCharset: EncodingRef, priorityCharset: Option<EncodingRef>,
isGS1: bool, isGS1: bool,
ecLevel: ErrorCorrectionLevel, ecLevel: ErrorCorrectionLevel,
) -> Result<RXingResultList, Exceptions> { ) -> Result<RXingResultList, Exceptions> {
@@ -319,15 +319,15 @@ impl MinimalEncoder {
let mut start = 0; let mut start = 0;
let mut end = self.encoders.len(); let mut end = self.encoders.len();
let priorityEncoderIndex = self.encoders.getPriorityEncoderIndex(); let priorityEncoderIndex = self.encoders.getPriorityEncoderIndex();
if priorityEncoderIndex >= 0 if priorityEncoderIndex.is_some()
&& self.encoders.canEncode( && self.encoders.canEncode(
// self.stringToEncode.chars().nth(from as usize).unwrap() as i16, // self.stringToEncode.chars().nth(from as usize).unwrap() as i16,
&self.stringToEncode[from as usize], &self.stringToEncode[from as usize],
priorityEncoderIndex, priorityEncoderIndex.unwrap(),
) )
{ {
start = priorityEncoderIndex; start = priorityEncoderIndex.unwrap();
end = priorityEncoderIndex + 1; end = priorityEncoderIndex.unwrap() + 1;
} }
for i in start..end { for i in start..end {