refinements for publication

This commit is contained in:
Sergey Chernov 2023-10-10 12:59:50 +01:00
parent 8e56f10e2c
commit c1aaede6f7
3 changed files with 45 additions and 12 deletions

View File

@ -4,14 +4,13 @@ version = "0.1.0"
edition = "2021" edition = "2021"
license = "Apache-2.0" license = "Apache-2.0"
description = "binary size-effective format used in Divan smart contracts, wasm bindings, network protocols, etc." 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 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
#anyhow = "1.0"
lazy_static = "1.4.0"
string-builder = "0.2.0"
[dev-dependencies] [dev-dependencies]
base64 = "0.21.4" base64 = "0.21.4"
hex = "0.4.3" hex = "0.4.3"
derive_more = "0.99.17"

View File

@ -34,6 +34,8 @@
//! - [tools::to_dump] utility function converts binary data into human-readable dump as in old goot //! - [tools::to_dump] utility function converts binary data into human-readable dump as in old goot
//! times (address, bytes, ASCII characters). //! times (address, bytes, ASCII characters).
//! //!
//! - [tools::StringBuilder] minimalistic growing strings builder.
//!
//! ## About Bipack format //! ## About Bipack format
//! //!
//! This is a binary format created wround the idea of bit-effectiveness and not disclosing //! 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 { for u in 0..l {
d2.push(u as u8); 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");
}
} }
} }
} }

View File

@ -12,7 +12,33 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // 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<u8>);
impl StringBuilder {
/// Append something string-like (you can use &str and String for example) to the buffer.
fn append<T: AsRef<str>>(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, FromUtf8Error> {
String::from_utf8(self.0.clone())
}
fn new() -> StringBuilder { StringBuilder(Vec::new()) }
}
/// Convert binary data into text dump, human readable, like: /// Convert binary data into text dump, human readable, like:
/// ```text /// ```text
@ -24,20 +50,20 @@ use string_builder::Builder;
pub fn to_dump(data: &[u8]) -> String { pub fn to_dump(data: &[u8]) -> String {
let mut offset = 0usize; let mut offset = 0usize;
let mut counter = 0; 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(" "); } for i in counter..16 { result.append(" "); }
result.append("|"); result.append("|");
for i in 0..counter { for i in 0..counter {
let b = data[offset - counter + i]; let b = data[offset - counter + i];
if b >= 32 && b <= 127 { if b >= 32 && b <= 127 {
result.append(b as char) result.append_char(b as char)
} else { } else {
result.append('.'); result.append_char('.');
} }
} }
for i in counter..16 { result.append(' '); } for i in counter..16 { result.append_char(' '); }
result.append("|\n"); result.append("|\n");
} }