allow unknown eci to work as binary eci

This commit is contained in:
Henry Schimke
2023-01-29 11:11:20 -06:00
parent 00e3cc00fe
commit f38e294481

View File

@@ -37,7 +37,7 @@ use super::CharacterSetECI;
pub struct ECIStringBuilder { pub struct ECIStringBuilder {
current_bytes: Vec<u8>, current_bytes: Vec<u8>,
result: String, result: String,
current_charset: EncodingRef, //= StandardCharsets.ISO_8859_1; current_charset: Option<EncodingRef>, //= StandardCharsets.ISO_8859_1;
} }
impl ECIStringBuilder { impl ECIStringBuilder {
@@ -45,14 +45,14 @@ impl ECIStringBuilder {
Self { Self {
current_bytes: Vec::new(), current_bytes: Vec::new(),
result: String::new(), result: String::new(),
current_charset: encoding::all::ISO_8859_1, current_charset: Some(encoding::all::ISO_8859_1),
} }
} }
pub fn with_capacity(initial_capacity: usize) -> Self { pub fn with_capacity(initial_capacity: usize) -> Self {
Self { Self {
current_bytes: Vec::with_capacity(initial_capacity), current_bytes: Vec::with_capacity(initial_capacity),
result: String::new(), result: String::new(),
current_charset: encoding::all::ISO_8859_1, current_charset: Some(encoding::all::ISO_8859_1),
} }
} }
@@ -105,14 +105,20 @@ impl ECIStringBuilder {
*/ */
pub fn appendECI(&mut self, value: u32) -> Result<(), Exceptions> { pub fn appendECI(&mut self, value: u32) -> Result<(), Exceptions> {
self.encodeCurrentBytesIfAny(); self.encodeCurrentBytesIfAny();
let character_set_eci = CharacterSetECI::getCharacterSetECIByValue(value)?;
self.current_charset = CharacterSetECI::getCharset(&character_set_eci); if let Ok(character_set_eci) = CharacterSetECI::getCharacterSetECIByValue(value) {
self.current_charset = Some(CharacterSetECI::getCharset(&character_set_eci));
}else {
self.current_charset = None
}
// self.current_charset = CharacterSetECI::getCharset(&character_set_eci);
Ok(()) Ok(())
} }
pub fn encodeCurrentBytesIfAny(&mut self) { pub fn encodeCurrentBytesIfAny(&mut self) {
if self.current_charset.name() == encoding::all::UTF_8.name() { if let Some(encoder) = self.current_charset {
if encoder.name() == encoding::all::UTF_8.name() {
if !self.current_bytes.is_empty() { if !self.current_bytes.is_empty() {
// if result == null { // if result == null {
// result = currentBytes; // result = currentBytes;
@@ -129,13 +135,18 @@ impl ECIStringBuilder {
// if (result == null) { // if (result == null) {
// result = new StringBuilder(new String(bytes, currentCharset)); // result = new StringBuilder(new String(bytes, currentCharset));
// } else { // } else {
let encoded_value = self let encoded_value = encoder
.current_charset
.decode(&bytes, encoding::DecoderTrap::Replace) .decode(&bytes, encoding::DecoderTrap::Replace)
.unwrap(); .unwrap();
self.result.push_str(&encoded_value); self.result.push_str(&encoded_value);
// } // }
} }
}else {
for byte in &self.current_bytes {
self.result.push(char::from(*byte))
}
self.current_bytes.clear();
}
} }
/** /**