From c1aaede6f72d49417834672db4c8bd0031cb2fa0 Mon Sep 17 00:00:00 2001 From: sergeych Date: Tue, 10 Oct 2023 12:59:50 +0100 Subject: [PATCH] refinements for publication --- Cargo.toml | 9 ++++----- src/lib.rs | 10 +++++++++- src/tools.rs | 38 ++++++++++++++++++++++++++++++++------ 3 files changed, 45 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4794b1e..253bbe1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,14 +4,13 @@ version = "0.1.0" edition = "2021" license = "Apache-2.0" description = "binary size-effective format used in Divan smart contracts, wasm bindings, network protocols, etc." +homepage = "https://gitea.sergeych.net/Divan/bipack_ru" +repository = "https://gitea.sergeych.net/Divan/bipack_ru" +readme = "README.md" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -#anyhow = "1.0" -lazy_static = "1.4.0" -string-builder = "0.2.0" [dev-dependencies] base64 = "0.21.4" -hex = "0.4.3" -derive_more = "0.99.17" \ No newline at end of file +hex = "0.4.3" \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 308917b..d95c51b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -34,6 +34,8 @@ //! - [tools::to_dump] utility function converts binary data into human-readable dump as in old goot //! times (address, bytes, ASCII characters). //! +//! - [tools::StringBuilder] minimalistic growing strings builder. +//! //! ## About Bipack format //! //! This is a binary format created wround the idea of bit-effectiveness and not disclosing @@ -190,7 +192,13 @@ mod tests { for u in 0..l { d2.push(u as u8); } - println!("size {}\n{}", d2.len(), to_dump(&d2)); + // println!("size {}\n{}", d2.len(), to_dump(&d2)); + if d2.len() == 41 { + let x = to_dump(&d2); + assert_eq!(x, "0000 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f |................| +0010 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f |................| +0020 20 21 22 23 24 25 26 27 28 | !\"#$%&'( |\n"); + } } } } \ No newline at end of file diff --git a/src/tools.rs b/src/tools.rs index 661728e..0bedb65 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -12,7 +12,33 @@ // See the License for the specific language governing permissions and // limitations under the License. -use string_builder::Builder; +// use string_builder::Builder; + +use std::string::FromUtf8Error; + +/// Absolutely minimalistic string builder (growing string implemented minimal and +/// more or less effective). Just to avoid dependencies for better .wasm usage. +pub struct StringBuilder(Vec); + +impl StringBuilder { + /// Append something string-like (you can use &str and String for example) to the buffer. + fn append>(self: &mut Self, text: T) { + for b in text.as_ref().bytes() { self.0.push(b) } + } + + /// Append char as far as it is a valid char in rust limited sense: + fn append_char(self: &mut Self, c: char) { + self.append(String::from(c)) + } + + /// Finalize the builder and return the result string. + fn string(self: &mut Self) -> Result { + String::from_utf8(self.0.clone()) + } + + fn new() -> StringBuilder { StringBuilder(Vec::new()) } +} + /// Convert binary data into text dump, human readable, like: /// ```text @@ -24,20 +50,20 @@ use string_builder::Builder; pub fn to_dump(data: &[u8]) -> String { let mut offset = 0usize; let mut counter = 0; - let mut result = Builder::default(); + let mut result = StringBuilder::new(); - fn ascii_dump(result: &mut Builder, counter: usize, data: &[u8], offset: usize) { + fn ascii_dump(result: &mut StringBuilder, counter: usize, data: &[u8], offset: usize) { for i in counter..16 { result.append(" "); } result.append("|"); for i in 0..counter { let b = data[offset - counter + i]; if b >= 32 && b <= 127 { - result.append(b as char) + result.append_char(b as char) } else { - result.append('.'); + result.append_char('.'); } } - for i in counter..16 { result.append(' '); } + for i in counter..16 { result.append_char(' '); } result.append("|\n"); }