refinements for publication

This commit is contained in:
sergeych committed 2023-10-10 12:59:50 +01:00
1 parent 8e56f10e2c
commit c1aaede6f7
3 files changed
+45 -12

No files matched your search

+9 -1
View File
@@ -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");
}
}
}
}
+32 -6
View File
@@ -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<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:
/// ```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");
}