From 2f0de79c38060c83c1b36e2c8ebf4397da518f7e Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Thu, 1 Sep 2022 15:52:08 -0500 Subject: [PATCH] ECIStringBuilder --- src/common/ECIStringBuilder.java | 145 ----------------------------- src/common/mod.rs | 154 +++++++++++++++++++++++++++++++ 2 files changed, 154 insertions(+), 145 deletions(-) delete mode 100644 src/common/ECIStringBuilder.java diff --git a/src/common/ECIStringBuilder.java b/src/common/ECIStringBuilder.java deleted file mode 100644 index a4a2968..0000000 --- a/src/common/ECIStringBuilder.java +++ /dev/null @@ -1,145 +0,0 @@ -/* - * Copyright 2022 ZXing authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.zxing.common; - -import com.google.zxing.FormatException; - -import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; - -/** - * Class that converts a sequence of ECIs and bytes into a string - * - * @author Alex Geller - */ -public final class ECIStringBuilder { - private StringBuilder currentBytes; - private StringBuilder result; - private Charset currentCharset = StandardCharsets.ISO_8859_1; - - public ECIStringBuilder() { - currentBytes = new StringBuilder(); - } - public ECIStringBuilder(int initialCapacity) { - currentBytes = new StringBuilder(initialCapacity); - } - - /** - * Appends {@code value} as a byte value - * - * @param value character whose lowest byte is to be appended - */ - public void append(char value) { - currentBytes.append((char) (value & 0xff)); - } - - /** - * Appends {@code value} as a byte value - * - * @param value byte to append - */ - public void append(byte value) { - currentBytes.append((char) (value & 0xff)); - } - - /** - * Appends the characters in {@code value} as bytes values - * - * @param value string to append - */ - public void append(String value) { - currentBytes.append(value); - } - - /** - * Append the string repesentation of {@code value} (short for {@code append(String.valueOf(value))}) - * - * @param value int to append as a string - */ - public void append(int value) { - append(String.valueOf(value)); - } - - /** - * Appends ECI value to output. - * - * @param value ECI value to append, as an int - * @throws FormatException on invalid ECI value - */ - public void appendECI(int value) throws FormatException { - encodeCurrentBytesIfAny(); - CharacterSetECI characterSetECI = CharacterSetECI.getCharacterSetECIByValue(value); - if (characterSetECI == null) { - throw FormatException.getFormatInstance(); - } - currentCharset = characterSetECI.getCharset(); - } - - private void encodeCurrentBytesIfAny() { - if (currentCharset.equals(StandardCharsets.ISO_8859_1)) { - if (currentBytes.length() > 0) { - if (result == null) { - result = currentBytes; - currentBytes = new StringBuilder(); - } else { - result.append(currentBytes); - currentBytes = new StringBuilder(); - } - } - } else if (currentBytes.length() > 0) { - byte[] bytes = currentBytes.toString().getBytes(StandardCharsets.ISO_8859_1); - currentBytes = new StringBuilder(); - if (result == null) { - result = new StringBuilder(new String(bytes, currentCharset)); - } else { - result.append(new String(bytes, currentCharset)); - } - } - } - - /** - * Appends the characters from {@code value} (unlike all other append methods of this class who append bytes) - * - * @param value characters to append - */ - public void appendCharacters(StringBuilder value) { - encodeCurrentBytesIfAny(); - result.append(value); - } - - /** - * Short for {@code toString().length()} (if possible, use {@link #isEmpty()} instead) - * - * @return length of string representation in characters - */ - public int length() { - return toString().length(); - } - - /** - * @return true iff nothing has been appended - */ - public boolean isEmpty() { - return currentBytes.length() == 0 && (result == null || result.length() == 0); - } - - @Override - public String toString() { - encodeCurrentBytesIfAny(); - return result == null ? "" : result.toString(); - } -} diff --git a/src/common/mod.rs b/src/common/mod.rs index b5dcc20..cc8ad4b 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -2654,3 +2654,157 @@ impl CharacterSetECI { } +/* + * Copyright 2022 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// package com.google.zxing.common; + +// import com.google.zxing.FormatException; + +// import java.nio.charset.Charset; +// import java.nio.charset.StandardCharsets; + +/** + * Class that converts a sequence of ECIs and bytes into a string + * + * @author Alex Geller + */ +pub struct ECIStringBuilder { + current_bytes: Vec, + result: String, + current_charset : &'static dyn Encoding, //= StandardCharsets.ISO_8859_1; +} + +impl ECIStringBuilder { + pub fn new() -> Self{ + Self { current_bytes: Vec::new(), result: String::new(), current_charset: encoding::all::UTF_8 } + } + pub fn with_capacity( initial_capacity : usize) ->Self{ + Self { current_bytes: Vec::with_capacity(initial_capacity), result: String::new(), current_charset: encoding::all::ISO_8859_1 } + + } + + /** + * Appends {@code value} as a byte value + * + * @param value character whose lowest byte is to be appended + */ + pub fn append_char(&mut self, value:char) { + self.current_bytes.push( value as u8 ); + } + + /** + * Appends {@code value} as a byte value + * + * @param value byte to append + */ + pub fn append_byte(&mut self, value:u8) { + self.current_bytes.push( value ); + } + + /** + * Appends the characters in {@code value} as bytes values + * + * @param value string to append + */ + pub fn append_string(&mut self, value :&str) { + value.as_bytes().iter().map(|b| self.current_bytes.push(*b)); + // self.current_bytes.push(value.as_bytes()); + } + + /** + * Append the string repesentation of {@code value} (short for {@code append(String.valueOf(value))}) + * + * @param value int to append as a string + */ + pub fn append(&mut self, value:i32) { + self.append_string(&format!("{}", value)); + } + + /** + * Appends ECI value to output. + * + * @param value ECI value to append, as an int + * @throws FormatException on invalid ECI value + */ + pub fn appendECI(&mut self, value:u32) -> Result<(),Exceptions> { + self.encodeCurrentBytesIfAny(); + let character_set_eci = CharacterSetECI::getCharacterSetECIByValue(value)?; + // if (character_set_eci == null) { + // throw FormatException.getFormatInstance(); + // } + self.current_charset = CharacterSetECI::getCharset(&character_set_eci); + Ok(()) + } + + pub fn encodeCurrentBytesIfAny(&mut self) { + if self.current_charset.name() == encoding::all::UTF_8.name() { + if !self.current_bytes.is_empty() { + // if result == null { + // result = currentBytes; + // currentBytes = new StringBuilder(); + // } else { + self.result.push_str(&String::from_utf8(self.current_bytes.clone()).unwrap()); + self.current_bytes.clear(); + // } + } + } else if !self.current_bytes.is_empty() { + let bytes = self.current_bytes.clone(); + self.current_bytes.clear(); + // if (result == null) { + // result = new StringBuilder(new String(bytes, currentCharset)); + // } else { + let encoded_value = self.current_charset.decode(&bytes, encoding::DecoderTrap::Replace).unwrap(); + self.result.push_str(&encoded_value); + // } + } + } + + /** + * Appends the characters from {@code value} (unlike all other append methods of this class who append bytes) + * + * @param value characters to append + */ + pub fn appendCharacters(&mut self, value:&str) { + self.encodeCurrentBytesIfAny(); + self.result.push_str(value); + } + + /** + * Short for {@code toString().length()} (if possible, use {@link #isEmpty()} instead) + * + * @return length of string representation in characters + */ + pub fn len(&mut self) -> usize{ + self.encodeCurrentBytesIfAny();//return toString().length(); + self.result.len() + } + + /** + * @return true iff nothing has been appended + */ + pub fn is_empty(&self) -> bool{ + return self.current_bytes.is_empty() && self.result.is_empty(); + } + +} + +impl fmt::Display for ECIStringBuilder{ + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + //self.encodeCurrentBytesIfAny(); + write!(f,"{}", self.result) + } +} \ No newline at end of file