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)]
pub struct ECIEncoderSet {
encoders: Vec<EncodingRef>,
priorityEncoderIndex: usize,
priorityEncoderIndex: Option<usize>,
}
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
* 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.
let mut encoders: Vec<EncodingRef>;
let mut priorityEncoderIndexValue = 0;
let mut priorityEncoderIndexValue = None;
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
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
for i in 0..stringToEncode.len() {
@@ -3005,13 +3009,14 @@ impl ECIEncoderSet {
//Compute priorityEncoderIndex by looking up priorityCharset in encoders
// if priorityCharset != null {
if priorityCharset.is_some() {
for i in 0..encoders.len() {
// for (int i = 0; i < encoders.length; i++) {
if priorityCharset.name() == encoders[i].name() {
priorityEncoderIndexValue = i;
if priorityCharset.as_ref().unwrap().name() == encoders[i].name() {
priorityEncoderIndexValue = Some(i);
break;
}
}
}}
// }
//invariants
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
*/
pub fn getPriorityEncoderIndex(&self) -> usize {
return self.priorityEncoderIndex;
pub fn getPriorityEncoderIndex(&self) -> Option<usize> {
self.priorityEncoderIndex
}
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
* 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 encoderSet = ECIEncoderSet::new(stringToEncodeInput, priorityCharset, fnc1);
let bytes = if encoderSet.len() == 1 {
@@ -3333,10 +3338,10 @@ impl MinimalECIInput {
let mut start = 0;
let mut end = encoderSet.len();
if encoderSet.getPriorityEncoderIndex() >= 0
&& (ch.chars().nth(0).unwrap() as u16 == fnc1 || encoderSet.canEncode(ch, encoderSet.getPriorityEncoderIndex()))
if encoderSet.getPriorityEncoderIndex().is_some()
&& (ch.chars().nth(0).unwrap() as u16 == fnc1 || encoderSet.canEncode(ch, encoderSet.getPriorityEncoderIndex().unwrap()))
{
start = encoderSet.getPriorityEncoderIndex();
start = encoderSet.getPriorityEncoderIndex().unwrap();
end = start + 1;
}

View File

@@ -1100,9 +1100,10 @@ fn testMinimalEncoder41() {
#[test]
fn testMinimalEncoder42() {
// 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(
"Katakana:\u{FF66}\u{FF66}\u{FF66}\u{FF66}\u{FF66}\u{FF66}",
"ECI(Shift_JIS),BYTE(Katakana:......)",
"ECI(windows-31j),BYTE(Katakana:......)",
None,
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
// 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
// NOTE: Changed to windows-31j because that is what is supported in encoding crate
verifyMinimalEncoding(
"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,
false,
);
@@ -1141,7 +1143,7 @@ fn verifyMinimalEncoding(
let result = MinimalEncoder::encode_with_details(
input,
None,
priorityCharset.unwrap_or(encoding::all::ISO_8859_1),
priorityCharset,
isGS1,
ErrorCorrectionLevel::L,
)

View File

@@ -136,11 +136,12 @@ pub fn encode_with_hints(
if hasCompactionHint {
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 rn = MinimalEncoder::encode_with_details(
content,
None,
priorityEncoding,
Some(priorityEncoding),
hasGS1FormatHint,
ecLevel,
)?;

View File

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