Implement clippy suggestions

This commit is contained in:
Henry Schimke
2023-01-04 14:48:16 -06:00
parent c65eadf102
commit 48287631dd
196 changed files with 2465 additions and 2574 deletions

View File

@@ -101,14 +101,30 @@ fn test_error_in_parameter_locator(data: &str) {
clone(&matrix)
};
copy.flip_coords(
orientation_points.get(error1).unwrap().get_x().abs() as u32,
orientation_points.get(error1).unwrap().get_y().abs() as u32,
orientation_points
.get(error1)
.unwrap()
.get_x()
.unsigned_abs(),
orientation_points
.get(error1)
.unwrap()
.get_y()
.unsigned_abs(),
);
if error2 > error1 {
// if error2 == error1, we only test a single error
copy.flip_coords(
orientation_points.get(error2).unwrap().get_x().abs() as u32,
orientation_points.get(error2).unwrap().get_y().abs() as u32,
orientation_points
.get(error2)
.unwrap()
.get_x()
.unsigned_abs(),
orientation_points
.get(error2)
.unwrap()
.get_y()
.unsigned_abs(),
);
}
// dbg!(copy.to_string());
@@ -180,7 +196,7 @@ fn make_larger(input: &BitMatrix, factor: u32) -> BitMatrix {
}
}
}
return output;
output
}
// Returns a list of the four rotations of the BitMatrix.
@@ -209,7 +225,7 @@ fn rotate_right(input: &BitMatrix) -> BitMatrix {
}
}
}
return result;
result
}
// Returns the transpose of a bit matrix, which is equivalent to rotating the
@@ -226,7 +242,7 @@ fn transpose(input: &BitMatrix) -> BitMatrix {
}
}
}
return result;
result
}
fn clone(input: &BitMatrix) -> BitMatrix {
@@ -268,7 +284,7 @@ fn get_orientation_points(code: &AztecCode) -> Vec<Point> {
}
xSign += 2;
}
return result;
result
}
const TEST_BARCODE: &str = r" X X X X X X X X X X X X X X X X X X X X X X X X X

View File

@@ -446,16 +446,12 @@ fn testHighLevelEncodeBinary() {
let expected_length = (8 * i)
+ if i <= 31 {
10
} else if i <= 62 {
20
} else if i <= 2078 {
21
} else {
if i <= 62 {
20
} else {
if i <= 2078 {
21
} else {
31
}
}
31
};
// ( (i <= 31) ? 10 : (i <= 62) ? 20 : (i <= 2078) ? 21 : 31);
// Verify that we are correct about the length.

View File

@@ -53,8 +53,7 @@ impl Reader for AztecReader {
// let notFoundException = None;
// let formatException = None;
let mut detector = Detector::new(image.getBlackMatrix());
let points;
let decoderRXingResult: DecoderRXingResult;
// try {
let detectorRXingResult = if let Ok(det) = detector.detect(false) {
@@ -62,13 +61,11 @@ impl Reader for AztecReader {
} else if let Ok(det) = detector.detect(true) {
det
} else {
return Err(Exceptions::NotFoundException(
"barcode not found".to_owned(),
));
return Err(Exceptions::NotFoundException(None));
};
points = detectorRXingResult.getPoints();
decoderRXingResult = decoder::decode(&detectorRXingResult)?;
let points = detectorRXingResult.getPoints();
let decoderRXingResult: DecoderRXingResult = decoder::decode(&detectorRXingResult)?;
// } catch (NotFoundException e) {
// notFoundException = e;
// } catch (FormatException e) {

View File

@@ -106,10 +106,10 @@ fn encode(
layers: i32,
) -> Result<BitMatrix, Exceptions> {
if format != BarcodeFormat::AZTEC {
return Err(Exceptions::IllegalArgumentException(format!(
return Err(Exceptions::IllegalArgumentException(Some(format!(
"Can only encode AZTEC, but got {:?}",
format
)));
))));
}
let aztec = if let Some(cset) = charset {
// dbg!(cset.name(), cset.whatwg_name());

View File

@@ -35,12 +35,12 @@ use super::AztecDetectorResult::AztecDetectorRXingResult;
#[derive(PartialEq, Eq, Copy, Clone)]
enum Table {
UPPER,
LOWER,
MIXED,
DIGIT,
PUNCT,
BINARY,
Upper,
Lower,
Mixed,
Digit,
Punct,
Binary,
}
const UPPER_TABLE: [&str; 32] = [
@@ -78,8 +78,8 @@ pub fn decode(
) -> Result<DecoderRXingResult, Exceptions> {
//let mut detectorRXingResult = detectorRXingResult.clone();
let matrix = detectorRXingResult.getBits();
let rawbits = extract_bits(&detectorRXingResult, matrix);
let corrected_bits = correct_bits(&detectorRXingResult, &rawbits)?;
let rawbits = extract_bits(detectorRXingResult, matrix);
let corrected_bits = correct_bits(detectorRXingResult, &rawbits)?;
let raw_bytes = convertBoolArrayToByteArray(&corrected_bits.correct_bits);
let result = get_encoded_data(&corrected_bits.correct_bits);
let mut decoder_rxing_result = DecoderRXingResult::new(
@@ -105,8 +105,8 @@ pub fn highLevelDecode(correctedBits: &[bool]) -> Result<String, Exceptions> {
*/
fn get_encoded_data(corrected_bits: &[bool]) -> Result<String, Exceptions> {
let end_index = corrected_bits.len();
let mut latch_table = Table::UPPER; // table most recently latched to
let mut shift_table = Table::UPPER; // table to use for the next read
let mut latch_table = Table::Upper; // table most recently latched to
let mut shift_table = Table::Upper; // table to use for the next read
// Final decoded string result
// (correctedBits-5) / 4 is an upper bound on the size (all-digit result)
@@ -121,7 +121,7 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result<String, Exceptions> {
let mut index = 0;
'main: while index < end_index {
if shift_table == Table::BINARY {
if shift_table == Table::Binary {
if end_index - index < 5 {
break;
}
@@ -147,7 +147,7 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result<String, Exceptions> {
// Go back to whatever mode we had been in
shift_table = latch_table;
} else {
let size = if shift_table == Table::DIGIT { 4 } else { 5 };
let size = if shift_table == Table::Digit { 4 } else { 5 };
if end_index - index < size {
break;
}
@@ -171,9 +171,9 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result<String, Exceptions> {
match n {
0 => result.push(29 as char), // translate FNC1 as ASCII 29
7 => {
return Err(Exceptions::FormatException(
return Err(Exceptions::FormatException(Some(
"FLG(7) is reserved and illegal".to_owned(),
))
)))
} // FLG(7) is reserved and illegal
_ => {
// ECI is decimal integer encoded as 1-6 codes in DIGIT mode
@@ -185,19 +185,19 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result<String, Exceptions> {
//while (n-- > 0) {
let next_digit = read_code(corrected_bits, index, 4);
index += 4;
if next_digit < 2 || next_digit > 11 {
return Err(Exceptions::FormatException(
if !(2..=11).contains(&next_digit) {
return Err(Exceptions::FormatException(Some(
"Not a decimal digit".to_owned(),
)); // Not a decimal digit
))); // Not a decimal digit
}
eci = eci * 10 + (next_digit - 2);
n -= 1;
}
let charset_eci = CharacterSetECI::getCharacterSetECIByValue(eci);
if charset_eci.is_err() {
return Err(Exceptions::FormatException(
return Err(Exceptions::FormatException(Some(
"Charset must exist".to_owned(),
));
)));
}
encdr = CharacterSetECI::getCharset(&charset_eci?);
}
@@ -232,7 +232,9 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result<String, Exceptions> {
if let Ok(str) = encdr.decode(&decoded_bytes, encoding::DecoderTrap::Strict) {
result.push_str(&str);
} else {
return Err(Exceptions::IllegalStateException("bad encoding".to_owned()));
return Err(Exceptions::IllegalStateException(Some(
"bad encoding".to_owned(),
)));
}
// result.push_str(decodedBytes.toString(encoding.name()));
//} catch (UnsupportedEncodingException uee) {
@@ -247,12 +249,12 @@ fn get_encoded_data(corrected_bits: &[bool]) -> Result<String, Exceptions> {
*/
fn getTable(t: char) -> Table {
match t {
'L' => Table::LOWER,
'P' => Table::PUNCT,
'M' => Table::MIXED,
'D' => Table::DIGIT,
'B' => Table::BINARY,
_ => Table::UPPER,
'L' => Table::Lower,
'P' => Table::Punct,
'M' => Table::Mixed,
'D' => Table::Digit,
'B' => Table::Binary,
_ => Table::Upper,
}
// switch (t) {
// case 'L':
@@ -279,12 +281,14 @@ fn getTable(t: char) -> Table {
*/
fn get_character(table: Table, code: u32) -> Result<&'static str, Exceptions> {
match table {
Table::UPPER => Ok(UPPER_TABLE[code as usize]),
Table::LOWER => Ok(LOWER_TABLE[code as usize]),
Table::MIXED => Ok(MIXED_TABLE[code as usize]),
Table::DIGIT => Ok(DIGIT_TABLE[code as usize]),
Table::PUNCT => Ok(PUNCT_TABLE[code as usize]),
_ => Err(Exceptions::IllegalStateException("Bad table".to_owned())),
Table::Upper => Ok(UPPER_TABLE[code as usize]),
Table::Lower => Ok(LOWER_TABLE[code as usize]),
Table::Mixed => Ok(MIXED_TABLE[code as usize]),
Table::Digit => Ok(DIGIT_TABLE[code as usize]),
Table::Punct => Ok(PUNCT_TABLE[code as usize]),
_ => Err(Exceptions::IllegalStateException(Some(
"Bad table".to_owned(),
))),
}
// switch (table) {
// case UPPER:
@@ -345,17 +349,18 @@ fn correct_bits(
let num_data_codewords = ddata.getNbDatablocks();
let num_codewords = rawbits.len() / codeword_size;
if num_codewords < num_data_codewords as usize {
return Err(Exceptions::FormatException(format!(
return Err(Exceptions::FormatException(Some(format!(
"numCodewords {}< numDataCodewords{}",
num_codewords, num_data_codewords
)));
))));
}
let mut offset = rawbits.len() % codeword_size;
let mut data_words = vec![0i32; num_codewords];
for i in 0..num_codewords {
for word in data_words.iter_mut().take(num_codewords) {
// for i in 0..num_codewords {
// for (int i = 0; i < numCodewords; i++, offset += codewordSize) {
data_words[i] = read_code(rawbits, offset, codeword_size) as i32;
*word = read_code(rawbits, offset, codeword_size) as i32;
offset += codeword_size;
}
@@ -373,15 +378,14 @@ fn correct_bits(
// First, count how many bits are going to be thrown out as stuffing
let mask = (1 << codeword_size) - 1;
let mut stuffed_bits = 0;
for i in 0..num_data_codewords as usize {
for data_word in data_words.iter().take(num_data_codewords as usize) {
// for i in 0..num_data_codewords as usize {
// for (int i = 0; i < numDataCodewords; i++) {
let data_word = data_words[i];
if data_word == 0 || data_word == mask {
return Err(Exceptions::FormatException(
"dataWord == 0 || dataWord == mask".to_owned(),
));
// let data_word = data_words[i];
if data_word == &0 || data_word == &mask {
return Err(Exceptions::FormatException(None));
//throw FormatException.getFormatInstance();
} else if data_word == 1 || data_word == mask - 1 {
} else if data_word == &1 || data_word == &(mask - 1) {
stuffed_bits += 1;
}
}
@@ -389,21 +393,22 @@ fn correct_bits(
let mut corrected_bits =
vec![false; (num_data_codewords * codeword_size as u32 - stuffed_bits) as usize];
let mut index = 0;
for i in 0..num_data_codewords as usize {
for data_word in data_words.iter().take(num_data_codewords as usize) {
// for i in 0..num_data_codewords as usize {
// for (int i = 0; i < numDataCodewords; i++) {
let data_word = data_words[i];
if data_word == 1 || data_word == mask - 1 {
// let data_word = data_words[i];
if *data_word == 1 || *data_word == mask - 1 {
// next codewordSize-1 bits are all zeros or all ones
corrected_bits.splice(
index..index + codeword_size - 1,
vec![data_word > 1; codeword_size - 1],
vec![*data_word > 1; codeword_size - 1],
);
// Arrays.fill(correctedBits, index, index + codewordSize - 1, dataWord > 1);
index += codeword_size - 1;
} else {
for bit in (0..codeword_size).rev() {
// for (int bit = codewordSize - 1; bit >= 0; --bit) {
corrected_bits[index] = (data_word & (1 << bit)) != 0;
corrected_bits[index] = (*data_word & (1 << bit)) != 0;
index += 1;
}
}
@@ -428,9 +433,9 @@ fn extract_bits(ddata: &AztecDetectorRXingResult, matrix: &BitMatrix) -> Vec<boo
let mut rawbits = vec![false; total_bits_in_layer(layers as usize, compact)];
if compact {
for i in 0..alignment_map.len() {
for (i, am) in alignment_map.iter_mut().enumerate() {
// for (int i = 0; i < alignmentMap.length; i++) {
alignment_map[i] = i as u32;
*am = i as u32;
}
} else {
let matrix_size = base_matrix_size + 1 + 2 * ((base_matrix_size / 2 - 1) / 15);
@@ -481,7 +486,7 @@ fn extract_bits(ddata: &AztecDetectorRXingResult, matrix: &BitMatrix) -> Vec<boo
}
row_offset += row_size * 8;
}
return rawbits;
rawbits
}
/**
@@ -489,14 +494,15 @@ fn extract_bits(ddata: &AztecDetectorRXingResult, matrix: &BitMatrix) -> Vec<boo
*/
fn read_code(rawbits: &[bool], start_index: usize, length: usize) -> u32 {
let mut res = 0;
for i in start_index..start_index + length {
for bit in rawbits.iter().skip(start_index).take(length) {
// for i in start_index..start_index + length {
// for (int i = startIndex; i < startIndex + length; i++) {
res <<= 1;
if rawbits[i] {
if *bit {
res |= 0x01;
}
}
return res;
res
}
/**
@@ -507,7 +513,7 @@ fn read_byte(rawbits: &[bool], start_index: usize) -> u8 {
if n >= 8 {
return read_code(rawbits, start_index, 8) as u8;
}
return (read_code(rawbits, start_index, n) << (8 - n)) as u8;
(read_code(rawbits, start_index, n) << (8 - n)) as u8
}
/**
@@ -515,11 +521,12 @@ fn read_byte(rawbits: &[bool], start_index: usize) -> u8 {
*/
pub fn convertBoolArrayToByteArray(bool_arr: &[bool]) -> Vec<u8> {
let mut byte_arr = vec![0u8; (bool_arr.len() + 7) / 8];
for i in 0..byte_arr.len() {
// for i in 0..byte_arr.len() {
for (i, byte) in byte_arr.iter_mut().enumerate() {
// for (int i = 0; i < byteArr.length; i++) {
byte_arr[i] = read_byte(bool_arr, 8 * i);
*byte = read_byte(bool_arr, 8 * i);
}
return byte_arr;
byte_arr
}
fn total_bits_in_layer(layers: usize, compact: bool) -> usize {

View File

@@ -93,7 +93,7 @@ impl<'a> Detector<'_> {
// 4. Sample the grid
let bits = self.sample_grid(
&self.image,
self.image,
&bulls_eye_corners[self.shift as usize % 4],
&bulls_eye_corners[(self.shift as usize + 1) % 4],
&bulls_eye_corners[(self.shift as usize + 2) % 4],
@@ -127,7 +127,9 @@ impl<'a> Detector<'_> {
|| !self.is_valid(&bulls_eye_corners[2])
|| !self.is_valid(&bulls_eye_corners[3])
{
return Err(Exceptions::NotFoundException("no valid points".to_owned()));
return Err(Exceptions::NotFoundException(Some(
"no valid points".to_owned(),
)));
}
let length = 2 * self.nb_center_layers;
// Get the bits around the bull's eye
@@ -208,7 +210,9 @@ impl<'a> Detector<'_> {
return Ok(shift);
}
}
Err(Exceptions::NotFoundException("rotation failure".to_owned()))
Err(Exceptions::NotFoundException(Some(
"rotation failure".to_owned(),
)))
}
/**
@@ -302,8 +306,7 @@ impl<'a> Detector<'_> {
// &pind.to_rxing_result_point(),
// &pina.to_rxing_result_point(),
// ) * (nbCenterLayers + 2) as f32);
if q < 0.75
|| q > 1.25
if !(0.75..=1.25).contains(&q)
|| !self.is_white_or_black_rectangle(&pouta, &poutb, &poutc, &poutd)
{
break;
@@ -321,7 +324,7 @@ impl<'a> Detector<'_> {
}
if self.nb_center_layers != 5 && self.nb_center_layers != 7 {
return Err(Exceptions::NotFoundException("".to_owned()));
return Err(Exceptions::NotFoundException(None));
}
self.compact = self.nb_center_layers == 5;
@@ -360,7 +363,7 @@ impl<'a> Detector<'_> {
let mut fnd = false;
//Get a white rectangle that can be the border of the matrix in center bull's eye or
if let Ok(wrd) = WhiteRectangleDetector::new_from_image(&self.image) {
if let Ok(wrd) = WhiteRectangleDetector::new_from_image(self.image) {
if let Ok(cornerPoints) = wrd.detect() {
point_a = cornerPoints[0];
point_b = cornerPoints[1];
@@ -421,7 +424,7 @@ impl<'a> Detector<'_> {
// This will ensure that we end up with a white rectangle in center bull's eye
// in order to compute a more accurate center.
let mut fnd = false;
if let Ok(wrd) = WhiteRectangleDetector::new(&self.image, 15, cx, cy) {
if let Ok(wrd) = WhiteRectangleDetector::new(self.image, 15, cx, cy) {
if let Ok(cornerPoints) = wrd.detect() {
point_a = cornerPoints[0];
point_b = cornerPoints[1];
@@ -557,7 +560,7 @@ impl<'a> Detector<'_> {
result |= 1 << (size - i - 1);
}
}
return result;
result
}
/**
@@ -607,7 +610,7 @@ impl<'a> Detector<'_> {
let c = self.get_color(&p3, &p4);
return c == c_init;
c == c_init
}
/**

View File

@@ -171,25 +171,25 @@ pub fn encode_bytes_with_charset(
MAX_NB_BITS
})
{
return Err(Exceptions::IllegalArgumentException(format!(
return Err(Exceptions::IllegalArgumentException(Some(format!(
"Illegal value {} for layers",
user_specified_layers
)));
))));
}
total_bits_in_layer_var = total_bits_in_layer(layers, compact);
word_size = WORD_SIZE[layers as usize];
let usable_bits_in_layers = total_bits_in_layer_var - (total_bits_in_layer_var % word_size);
stuffed_bits = stuffBits(&bits, word_size as usize);
if stuffed_bits.getSize() as u32 + ecc_bits > usable_bits_in_layers {
return Err(Exceptions::IllegalArgumentException(
return Err(Exceptions::IllegalArgumentException(Some(
"Data to large for user specified layer".to_owned(),
));
)));
}
if compact && stuffed_bits.getSize() as u32 > word_size * 64 {
// Compact format only allows 64 data words, though C4 can hold more words than that
return Err(Exceptions::IllegalArgumentException(
return Err(Exceptions::IllegalArgumentException(Some(
"Data to large for user specified layer".to_owned(),
));
)));
}
} else {
word_size = 0;
@@ -201,14 +201,14 @@ pub fn encode_bytes_with_charset(
loop {
// for (int i = 0; ; i++) {
if i > MAX_NB_BITS {
return Err(Exceptions::IllegalArgumentException(
return Err(Exceptions::IllegalArgumentException(Some(
"Data too large for an Aztec code".to_owned(),
));
)));
}
compact = i <= 3;
layers = if compact { i + 1 } else { i };
total_bits_in_layer_var = total_bits_in_layer(layers, compact);
if total_size_bits > total_bits_in_layer_var as u32 {
if total_size_bits > total_bits_in_layer_var {
i += 1;
continue;
}
@@ -239,7 +239,7 @@ pub fn encode_bytes_with_charset(
// generate mode message
let messageSizeInWords = stuffed_bits.getSize() as u32 / word_size;
let modeMessage = generateModeMessage(compact, layers as u32, messageSizeInWords);
let modeMessage = generateModeMessage(compact, layers, messageSizeInWords);
// allocate symbol
let baseMatrixSize = (if compact { 11 } else { 14 }) + layers * 4; // not including alignment lines
@@ -248,10 +248,8 @@ pub fn encode_bytes_with_charset(
if compact {
// no alignment marks in compact mode, alignmentMap is a no-op
matrixSize = baseMatrixSize;
for i in 0..alignmentMap.len() {
// for (int i = 0; i < alignmentMap.length; i++) {
alignmentMap[i] = i as u32;
}
// for i in 0..alignmentMap.len() {
alignmentMap[..].copy_from_slice(&(0..baseMatrixSize).collect::<Vec<u32>>()[..]);
} else {
matrixSize = baseMatrixSize + 1 + 2 * ((baseMatrixSize / 2 - 1) / 15);
let origCenter = (baseMatrixSize / 2) as usize;
@@ -259,11 +257,11 @@ pub fn encode_bytes_with_charset(
for i in 0..origCenter {
// for (int i = 0; i < origCenter; i++) {
let newOffset = (i + i / 15) as u32;
alignmentMap[origCenter - i - 1] = center as u32 - newOffset - 1;
alignmentMap[origCenter + i] = center as u32 + newOffset + 1;
alignmentMap[origCenter - i - 1] = center - newOffset - 1;
alignmentMap[origCenter + i] = center + newOffset + 1;
}
}
let mut matrix = BitMatrix::with_single_dimension(matrixSize as u32);
let mut matrix = BitMatrix::with_single_dimension(matrixSize);
// dbg!(matrix.to_string());
@@ -306,25 +304,25 @@ pub fn encode_bytes_with_charset(
// dbg!(matrix.to_string());
// draw mode message
drawModeMessage(&mut matrix, compact, matrixSize as u32, modeMessage);
drawModeMessage(&mut matrix, compact, matrixSize, modeMessage);
// dbg!(matrix.to_string());
// draw alignment marks
if compact {
drawBullsEye(&mut matrix, matrixSize as u32 / 2, 5);
drawBullsEye(&mut matrix, matrixSize / 2, 5);
} else {
drawBullsEye(&mut matrix, matrixSize as u32 / 2, 7);
drawBullsEye(&mut matrix, matrixSize / 2, 7);
let mut i = 0;
let mut j = 0;
while i < baseMatrixSize / 2 - 1 {
let mut k = (matrixSize / 2) & 1;
while k < matrixSize {
// for (int k = (matrixSize / 2) & 1; k < matrixSize; k += 2) {
matrix.set(matrixSize as u32 / 2 - j, k as u32);
matrix.set(matrixSize as u32 / 2 + j, k as u32);
matrix.set(k as u32, matrixSize as u32 / 2 - j);
matrix.set(k as u32, matrixSize as u32 / 2 + j);
matrix.set(matrixSize / 2 - j, k);
matrix.set(matrixSize / 2 + j, k);
matrix.set(k, matrixSize / 2 - j);
matrix.set(k, matrixSize / 2 + j);
k += 2;
}
@@ -344,13 +342,7 @@ pub fn encode_bytes_with_charset(
// dbg!(matrix.to_string());
let aztec = AztecCode::new(
compact,
matrixSize as u32,
layers,
messageSizeInWords as u32,
matrix,
);
let aztec = AztecCode::new(compact, matrixSize, layers, messageSizeInWords, matrix);
// aztec.setCompact(compact);
// aztec.setSize(matrixSize);
// aztec.setLayers(layers);
@@ -399,7 +391,7 @@ pub fn generateModeMessage(compact: bool, layers: u32, messageSizeInWords: u32)
.expect("should append");
mode_message = generateCheckWords(&mode_message, 40, 4);
}
return mode_message;
mode_message
}
fn drawModeMessage(matrix: &mut BitMatrix, compact: bool, matrixSize: u32, modeMessage: BitArray) {
@@ -459,7 +451,7 @@ fn generateCheckWords(bitArray: &BitArray, totalBits: usize, wordSize: usize) ->
.expect("must append");
}
// dbg!(message_bits.to_string());
return message_bits;
message_bits
}
fn bitsToWords(stuffedBits: &BitArray, wordSize: usize, totalWords: usize) -> Vec<i32> {
@@ -472,7 +464,7 @@ fn bitsToWords(stuffedBits: &BitArray, wordSize: usize, totalWords: usize) -> Ve
for j in 0..wordSize {
// for (int j = 0; j < wordSize; j++) {
value |= if stuffedBits.get(i * wordSize + j) {
1 << wordSize - j - 1
1 << (wordSize - j - 1)
} else {
0
};
@@ -481,7 +473,7 @@ fn bitsToWords(stuffedBits: &BitArray, wordSize: usize, totalWords: usize) -> Ve
i += 1;
}
return message;
message
}
fn getGF(wordSize: usize) -> Result<GenericGFRef, Exceptions> {
@@ -491,10 +483,10 @@ fn getGF(wordSize: usize) -> Result<GenericGFRef, Exceptions> {
8 => Ok(get_predefined_genericgf(PredefinedGenericGF::AztecData8)),
10 => Ok(get_predefined_genericgf(PredefinedGenericGF::AztecData10)),
12 => Ok(get_predefined_genericgf(PredefinedGenericGF::AztecData12)),
_ => Err(Exceptions::IllegalArgumentException(format!(
_ => Err(Exceptions::IllegalArgumentException(Some(format!(
"Unsupported word size {}",
wordSize
))),
)))),
}
// switch (wordSize) {
// case 4:
@@ -539,7 +531,7 @@ pub fn stuffBits(bits: &BitArray, word_size: usize) -> BitArray {
i += word_size as isize;
}
return out;
out
}
fn total_bits_in_layer(layers: u32, compact: bool) -> u32 {

View File

@@ -14,8 +14,6 @@
* limitations under the License.
*/
use std::cmp::Ordering;
use crate::{
common::{BitArray, CharacterSetECI},
exceptions::Exceptions,
@@ -250,9 +248,9 @@ impl HighLevelEncoder {
initial_state = initial_state.appendFLGn(CharacterSetECI::getValue(&eci))?;
}
} else {
return Err(Exceptions::IllegalArgumentException(
return Err(Exceptions::IllegalArgumentException(Some(
"No ECI code for character set".to_owned(),
));
)));
}
// if self.charset != null {
// CharacterSetECI eci = CharacterSetECI.getCharacterSetECI(charset);
@@ -266,13 +264,13 @@ impl HighLevelEncoder {
while index < self.text.len() {
// for index in 0..self.text.len() {
// for (int index = 0; index < text.length; index++) {
let pair_code;
let next_char = if index + 1 < self.text.len() {
self.text[index + 1]
} else {
0
};
pair_code = match self.text[index] {
let pair_code = match self.text[index] {
b'\r' if next_char == b'\n' => 2,
b'.' if next_char == b' ' => 3,
b',' if next_char == b' ' => 4,
@@ -301,13 +299,19 @@ impl HighLevelEncoder {
.into_iter()
.min_by(|a, b| {
let diff: i64 = a.getBitCount() as i64 - b.getBitCount() as i64;
if diff < 0 {
Ordering::Less
} else if diff == 0 {
Ordering::Equal
} else {
Ordering::Greater
}
diff.cmp(&0)
// match diff {
// ..0 => Ordering::Less,
// 0 => Ordering::Equal,
// 0.. => Ordering::Greater,
// }
// if diff < 0 {
// Ordering::Less
// } else if diff == 0 {
// Ordering::Equal
// } else {
// Ordering::Greater
// }
// a.getBitCount() - b.getBitCount()
})
.unwrap();
@@ -344,7 +348,7 @@ impl HighLevelEncoder {
let mut state_no_binary = None;
for mode in 0..=Self::MODE_PUNCT {
// for (int mode = 0; mode <= MODE_PUNCT; mode++) {
let char_in_mode = Self::CHAR_MAP[mode as usize][ch as usize];
let char_in_mode = Self::CHAR_MAP[mode][ch as usize];
if char_in_mode > 0 {
if state_no_binary.is_none() {
// Only create stateNoBinary the first time it's required.
@@ -446,7 +450,7 @@ impl HighLevelEncoder {
add = false;
break;
}
if newState.isBetterThanOrEqualTo(&oldState) {
if newState.isBetterThanOrEqualTo(oldState) {
result.remove(i);
}
}
@@ -455,6 +459,6 @@ impl HighLevelEncoder {
result.push(newState);
}
}
return result;
result
}
}

View File

@@ -80,9 +80,9 @@ impl State {
token.add(0, 3); // 0: FNC1
} else */
if eci > 999999 {
return Err(Exceptions::IllegalArgumentException(
return Err(Exceptions::IllegalArgumentException(Some(
"ECI code must be between 0 and 999999".to_owned(),
));
)));
// throw new IllegalArgumentException("ECI code must be between 0 and 999999");
} else {
let eci_digits = encoding::all::ISO_8859_1
@@ -155,12 +155,10 @@ impl State {
let deltaBitCount =
if self.binary_shift_byte_count == 0 || self.binary_shift_byte_count == 31 {
18
} else if self.binary_shift_byte_count == 62 {
9
} else {
if self.binary_shift_byte_count == 62 {
9
} else {
8
}
8
};
let mut result = State::new(
token,
@@ -240,7 +238,7 @@ impl State {
if binary_shift_byte_count > 0 {
return 10; // one B/S
}
return 0;
0
}
}

View File

@@ -115,3 +115,9 @@ impl IntoIterator for Token {
// fn appendTo(&self, bitArray: BitArray, text: &[u8]);
// }
impl Default for Token {
fn default() -> Self {
Self::new()
}
}

View File

@@ -24,9 +24,10 @@ pub fn toBitArray(bits: &str) -> BitArray {
#[allow(dead_code)]
pub fn toBooleanArray(bitArray: &BitArray) -> Vec<bool> {
let mut result = vec![false; bitArray.getSize()];
for i in 0..result.len() {
// for i in 0..result.len() {
for (i, res) in result.iter_mut().enumerate() {
// for (int i = 0; i < result.length; i++) {
result[i] = bitArray.get(i);
*res = bitArray.get(i);
}
result
}