mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-26 20:32:34 +00:00
repurpose CharacterSetECI as encoding abstraction
This commit is contained in:
@@ -14,12 +14,12 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
use encoding::{Encoding, EncodingRef};
|
||||
|
||||
use crate::{DecodeHintType, DecodeHintValue, DecodingHintDictionary};
|
||||
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
use super::CharacterSetECI;
|
||||
|
||||
/**
|
||||
* Common string-related functions.
|
||||
*
|
||||
@@ -50,8 +50,8 @@ const ASSUME_SHIFT_JIS: bool = false;
|
||||
// static SHIFT_JIS: &'static str = "SJIS";
|
||||
// static GB2312: &'static str = "GB2312";
|
||||
|
||||
pub static SHIFT_JIS_CHARSET: Lazy<EncodingRef> =
|
||||
Lazy::new(|| encoding::label::encoding_from_whatwg_label("SJIS").unwrap());
|
||||
pub static SHIFT_JIS_CHARSET: CharacterSetECI =
|
||||
CharacterSetECI::SJIS;
|
||||
|
||||
// private static final boolean ASSUME_SHIFT_JIS =
|
||||
// SHIFT_JIS_CHARSET.equals(PLATFORM_DEFAULT_ENCODING) ||
|
||||
@@ -67,14 +67,14 @@ impl StringUtils {
|
||||
*/
|
||||
pub fn guessEncoding(bytes: &[u8], hints: &DecodingHintDictionary) -> Option<&'static str> {
|
||||
let c = StringUtils::guessCharset(bytes, hints)?;
|
||||
if c.name() == encoding::label::encoding_from_whatwg_label("SJIS")?.name() {
|
||||
if c == CharacterSetECI::SJIS {
|
||||
Some("SJIS")
|
||||
} else if c.name() == encoding::all::UTF_8.name() {
|
||||
} else if c == CharacterSetECI::UTF8 {
|
||||
Some("UTF8")
|
||||
} else if c.name() == encoding::all::ISO_8859_1.name() {
|
||||
} else if c == CharacterSetECI::ISO8859_1 {
|
||||
Some("ISO8859_1")
|
||||
} else {
|
||||
Some(c.name())
|
||||
Some(&c.getCharsetName())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,12 +87,12 @@ impl StringUtils {
|
||||
* or the platform default encoding if
|
||||
* none of these can possibly be correct
|
||||
*/
|
||||
pub fn guessCharset(bytes: &[u8], hints: &DecodingHintDictionary) -> Option<EncodingRef> {
|
||||
pub fn guessCharset(bytes: &[u8], hints: &DecodingHintDictionary) -> Option<CharacterSetECI> {
|
||||
if let Some(DecodeHintValue::CharacterSet(cs_name)) =
|
||||
hints.get(&DecodeHintType::CHARACTER_SET)
|
||||
{
|
||||
// if let DecodeHintValue::CharacterSet(cs_name) = hint {
|
||||
return encoding::label::encoding_from_whatwg_label(cs_name);
|
||||
return CharacterSetECI::getCharacterSetECIByName(cs_name)
|
||||
// }
|
||||
}
|
||||
// if hints.contains_key(&DecodeHintType::CHARACTER_SET) {
|
||||
@@ -104,9 +104,9 @@ impl StringUtils {
|
||||
&& ((bytes[0] == 0xFE && bytes[1] == 0xFF) || (bytes[0] == 0xFF && bytes[1] == 0xFE))
|
||||
{
|
||||
if bytes[0] == 0xFE && bytes[1] == 0xFF {
|
||||
return Some(encoding::all::UTF_16BE);
|
||||
return Some(CharacterSetECI::UnicodeBigUnmarked);
|
||||
} else {
|
||||
return Some(encoding::all::UTF_16LE);
|
||||
return Some(CharacterSetECI::UTF16LE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -224,7 +224,7 @@ impl StringUtils {
|
||||
|
||||
// Easy -- if there is BOM or at least 1 valid not-single byte character (and no evidence it can't be UTF-8), done
|
||||
if can_be_utf8 && (utf8bom || utf2_bytes_chars + utf3_bytes_chars + utf4_bytes_chars > 0) {
|
||||
return Some(encoding::all::UTF_8);
|
||||
return Some(CharacterSetECI::UTF8);
|
||||
}
|
||||
// Easy -- if assuming Shift_JIS or >= 3 valid consecutive not-ascii characters (and no evidence it can't be), done
|
||||
if can_be_shift_jis
|
||||
@@ -232,7 +232,7 @@ impl StringUtils {
|
||||
|| sjis_max_katakana_word_length >= 3
|
||||
|| sjis_max_double_bytes_word_length >= 3)
|
||||
{
|
||||
return encoding::label::encoding_from_whatwg_label("SJIS");
|
||||
return Some(CharacterSetECI::SJIS); //encoding::label::encoding_from_whatwg_label("SJIS");
|
||||
}
|
||||
// Distinguishing Shift_JIS and ISO-8859-1 can be a little tough for short words. The crude heuristic is:
|
||||
// - If we saw
|
||||
@@ -243,23 +243,23 @@ impl StringUtils {
|
||||
return if (sjis_max_katakana_word_length == 2 && sjis_katakana_chars == 2)
|
||||
|| iso_high_other * 10 >= length
|
||||
{
|
||||
encoding::label::encoding_from_whatwg_label("SJIS")
|
||||
Some(CharacterSetECI::SJIS)
|
||||
} else {
|
||||
Some(encoding::all::ISO_8859_1)
|
||||
Some(CharacterSetECI::ISO8859_1)
|
||||
};
|
||||
}
|
||||
|
||||
// Otherwise, try in order ISO-8859-1, Shift JIS, UTF-8 and fall back to default platform encoding
|
||||
if can_be_iso88591 {
|
||||
return Some(encoding::all::ISO_8859_1);
|
||||
return Some(CharacterSetECI::ISO8859_1);
|
||||
}
|
||||
if can_be_shift_jis {
|
||||
return Some(encoding::label::encoding_from_whatwg_label("SJIS").unwrap());
|
||||
return Some(CharacterSetECI::SJIS);
|
||||
}
|
||||
if can_be_utf8 {
|
||||
return Some(encoding::all::UTF_8);
|
||||
return Some(CharacterSetECI::UTF8);
|
||||
}
|
||||
// Otherwise, we take a wild guess with platform encoding
|
||||
Some(encoding::all::UTF_8)
|
||||
Some(CharacterSetECI::UTF8)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user