mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-26 04:12:34 +00:00
cargo clippy && fmt
This commit is contained in:
@@ -40,19 +40,14 @@ fn test_random() {
|
||||
// }
|
||||
assert_eq!(
|
||||
CharacterSetECI::UTF8,
|
||||
StringUtils::guessCharset(&bytes, &HashMap::new())
|
||||
.unwrap()
|
||||
StringUtils::guessCharset(&bytes, &HashMap::new()).unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_short_shift_jis1() {
|
||||
// 金魚
|
||||
do_test(
|
||||
&[0x8b, 0xe0, 0x8b, 0x9b],
|
||||
CharacterSetECI::SJIS,
|
||||
"SJIS",
|
||||
);
|
||||
do_test(&[0x8b, 0xe0, 0x8b, 0x9b], CharacterSetECI::SJIS, "SJIS");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -87,7 +82,7 @@ fn test_utf16_be() {
|
||||
do_test(
|
||||
&[0xFE, 0xFF, 0x8c, 0x03, 0x53, 0x8b, 0x67, 0xdc],
|
||||
CharacterSetECI::UnicodeBigUnmarked,
|
||||
&CharacterSetECI::UnicodeBigUnmarked.getCharsetName(),
|
||||
CharacterSetECI::UnicodeBigUnmarked.getCharsetName(),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -97,7 +92,7 @@ fn test_utf16_le() {
|
||||
do_test(
|
||||
&[0xFF, 0xFE, 0x03, 0x8c, 0x8b, 0x53, 0xdc, 0x67],
|
||||
CharacterSetECI::UTF16LE,
|
||||
&CharacterSetECI::UTF16LE.getCharsetName(),
|
||||
CharacterSetECI::UTF16LE.getCharsetName(),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
use encoding::EncodingRef;
|
||||
|
||||
use crate::common::Result;
|
||||
use crate::{Exceptions};
|
||||
use crate::Exceptions;
|
||||
|
||||
/**
|
||||
* Encapsulates a Character Set ECI, according to "Extended Channel Interpretations" 5.3.1.1
|
||||
@@ -51,11 +51,11 @@ pub enum CharacterSetECI {
|
||||
Cp1256, //(24, "windows-1256"),
|
||||
UnicodeBigUnmarked, //(25, "UTF-16BE", "UnicodeBig"),
|
||||
UTF16LE,
|
||||
UTF8, //(26, "UTF-8"),
|
||||
ASCII, //(new int[] {27, 170}, "US-ASCII"),
|
||||
Big5, //(28),
|
||||
GB18030, //(29, "GB2312", "EUC_CN", "GBK"),
|
||||
EUC_KR, //(30, "EUC-KR");
|
||||
UTF8, //(26, "UTF-8"),
|
||||
ASCII, //(new int[] {27, 170}, "US-ASCII"),
|
||||
Big5, //(28),
|
||||
GB18030, //(29, "GB2312", "EUC_CN", "GBK"),
|
||||
EUC_KR, //(30, "EUC-KR");
|
||||
}
|
||||
impl CharacterSetECI {
|
||||
// private static final Map<Integer,CharacterSetECI> VALUE_TO_ECI = new HashMap<>();
|
||||
@@ -126,7 +126,7 @@ impl CharacterSetECI {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn getCharset(&self, ) -> EncodingRef {
|
||||
pub fn getCharset(&self) -> EncodingRef {
|
||||
let name = match self {
|
||||
// CharacterSetECI::Cp437 => "CP437",
|
||||
CharacterSetECI::Cp437 => "cp437",
|
||||
@@ -161,8 +161,8 @@ impl CharacterSetECI {
|
||||
encoding::label::encoding_from_whatwg_label(name).unwrap()
|
||||
}
|
||||
|
||||
pub fn getCharsetName(&self, ) -> &'static str {
|
||||
match self {
|
||||
pub fn getCharsetName(&self) -> &'static str {
|
||||
match self {
|
||||
// CharacterSetECI::Cp437 => "CP437",
|
||||
CharacterSetECI::Cp437 => "cp437",
|
||||
CharacterSetECI::ISO8859_1 => "iso-8859-1",
|
||||
@@ -193,7 +193,6 @@ impl CharacterSetECI {
|
||||
CharacterSetECI::GB18030 => "gb2312",
|
||||
CharacterSetECI::EUC_KR => "euc-kr",
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -317,25 +316,33 @@ impl CharacterSetECI {
|
||||
}
|
||||
|
||||
pub fn encode(&self, input: &str) -> Result<Vec<u8>> {
|
||||
self.getCharset().encode(input, encoding::EncoderTrap::Strict).map_err(|e| Exceptions::format_with(e.to_string()))
|
||||
self.getCharset()
|
||||
.encode(input, encoding::EncoderTrap::Strict)
|
||||
.map_err(|e| Exceptions::format_with(e.to_string()))
|
||||
}
|
||||
|
||||
pub fn encode_replace(&self, input: &str) -> Result<Vec<u8>> {
|
||||
self.getCharset().encode(input, encoding::EncoderTrap::Replace).map_err(|e| Exceptions::format_with(e.to_string()))
|
||||
self.getCharset()
|
||||
.encode(input, encoding::EncoderTrap::Replace)
|
||||
.map_err(|e| Exceptions::format_with(e.to_string()))
|
||||
}
|
||||
|
||||
pub fn decode(&self, input:&[u8]) -> Result<String> {
|
||||
pub fn decode(&self, input: &[u8]) -> Result<String> {
|
||||
if self == &CharacterSetECI::Cp437 {
|
||||
use codepage_437::BorrowFromCp437;
|
||||
use codepage_437::CP437_CONTROL;
|
||||
|
||||
Ok(String::borrow_from_cp437(&input, &CP437_CONTROL))
|
||||
}else {
|
||||
self.getCharset().decode(input, encoding::DecoderTrap::Strict).map_err(|e| Exceptions::format_with(e.to_string()))
|
||||
} else {
|
||||
self.getCharset()
|
||||
.decode(input, encoding::DecoderTrap::Strict)
|
||||
.map_err(|e| Exceptions::format_with(e.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decode_replace(&self, input:&[u8]) -> Result<String> {
|
||||
self.getCharset().decode(input, encoding::DecoderTrap::Replace).map_err(|e| Exceptions::format_with(e.to_string()))
|
||||
pub fn decode_replace(&self, input: &[u8]) -> Result<String> {
|
||||
self.getCharset()
|
||||
.decode(input, encoding::DecoderTrap::Replace)
|
||||
.map_err(|e| Exceptions::format_with(e.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,9 +112,7 @@ impl ECIEncoderSet {
|
||||
for encoder in &neededEncoders {
|
||||
// for (CharsetEncoder encoder : neededEncoders) {
|
||||
let c = stringToEncode.get(i).unwrap();
|
||||
if (fnc1.is_some() && c == fnc1.as_ref().unwrap())
|
||||
|| encoder.encode(c).is_ok()
|
||||
{
|
||||
if (fnc1.is_some() && c == fnc1.as_ref().unwrap()) || encoder.encode(c).is_ok() {
|
||||
canEncode = true;
|
||||
break;
|
||||
}
|
||||
@@ -125,12 +123,7 @@ impl ECIEncoderSet {
|
||||
// for encoder in ENCODERS {
|
||||
let encoder = ENCODERS.get(i_encoder).unwrap();
|
||||
// for (CharsetEncoder encoder : ENCODERS) {
|
||||
if encoder
|
||||
.encode(
|
||||
stringToEncode.get(i).unwrap()
|
||||
)
|
||||
.is_ok()
|
||||
{
|
||||
if encoder.encode(stringToEncode.get(i).unwrap()).is_ok() {
|
||||
//Good, we found an encoder that can encode the character. We add him to the list and continue scanning
|
||||
//the input
|
||||
neededEncoders.push(*encoder);
|
||||
|
||||
@@ -136,9 +136,7 @@ impl ECIStringBuilder {
|
||||
} else if !self.current_bytes.is_empty() {
|
||||
let bytes = std::mem::take(&mut self.current_bytes);
|
||||
self.current_bytes.clear();
|
||||
let encoded_value = encoder
|
||||
.decode(&bytes)
|
||||
.unwrap();
|
||||
let encoded_value = encoder.decode(&bytes).unwrap();
|
||||
self.result.push_str(&encoded_value);
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -21,7 +21,7 @@ use unicode_segmentation::UnicodeSegmentation;
|
||||
use crate::common::Result;
|
||||
use crate::Exceptions;
|
||||
|
||||
use super::{ECIEncoderSet, ECIInput, CharacterSetECI};
|
||||
use super::{CharacterSetECI, ECIEncoderSet, ECIInput};
|
||||
|
||||
//* approximated (latch + 2 codewords)
|
||||
pub const COST_PER_ECI: usize = 3;
|
||||
|
||||
@@ -16,8 +16,6 @@
|
||||
|
||||
use crate::{DecodeHintType, DecodeHintValue, DecodingHintDictionary};
|
||||
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
use super::CharacterSetECI;
|
||||
|
||||
/**
|
||||
@@ -50,8 +48,7 @@ const ASSUME_SHIFT_JIS: bool = false;
|
||||
// static SHIFT_JIS: &'static str = "SJIS";
|
||||
// static GB2312: &'static str = "GB2312";
|
||||
|
||||
pub static SHIFT_JIS_CHARSET: CharacterSetECI =
|
||||
CharacterSetECI::SJIS;
|
||||
pub static SHIFT_JIS_CHARSET: CharacterSetECI = CharacterSetECI::SJIS;
|
||||
|
||||
// private static final boolean ASSUME_SHIFT_JIS =
|
||||
// SHIFT_JIS_CHARSET.equals(PLATFORM_DEFAULT_ENCODING) ||
|
||||
@@ -74,7 +71,7 @@ impl StringUtils {
|
||||
} else if c == CharacterSetECI::ISO8859_1 {
|
||||
Some("ISO8859_1")
|
||||
} else {
|
||||
Some(&c.getCharsetName())
|
||||
Some(c.getCharsetName())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,7 +89,7 @@ impl StringUtils {
|
||||
hints.get(&DecodeHintType::CHARACTER_SET)
|
||||
{
|
||||
// if let DecodeHintValue::CharacterSet(cs_name) = hint {
|
||||
return CharacterSetECI::getCharacterSetECIByName(cs_name)
|
||||
return CharacterSetECI::getCharacterSetECIByName(cs_name);
|
||||
// }
|
||||
}
|
||||
// if hints.contains_key(&DecodeHintType::CHARACTER_SET) {
|
||||
@@ -260,6 +257,6 @@ impl StringUtils {
|
||||
return Some(CharacterSetECI::UTF8);
|
||||
}
|
||||
// Otherwise, we take a wild guess with platform encoding
|
||||
Some(CharacterSetECI::UTF8)
|
||||
Some(CharacterSetECI::UTF8)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user