diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2020-03-18 17:23:15 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2020-03-18 17:29:27 +0100 |
commit | 97908d6f25f8973aee95a1850ae42af0323698c7 (patch) | |
tree | 3bc69e031e3e4dce5d3d1b60885f0bf50c04ee12 /openpgp/src/fmt.rs | |
parent | 153db43f8f4eae71a906beacaea149492d75a1ac (diff) |
openpgp: Move ASCII dumping code to the library.
Diffstat (limited to 'openpgp/src/fmt.rs')
-rw-r--r-- | openpgp/src/fmt.rs | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/openpgp/src/fmt.rs b/openpgp/src/fmt.rs index 429f2b69..3b79ceba 100644 --- a/openpgp/src/fmt.rs +++ b/openpgp/src/fmt.rs @@ -89,6 +89,30 @@ pub mod hex { }) } + /// Writes a chunk of data with ASCII-representation. + /// + /// This produces output similar to `hexdump(1)`. + pub fn write_ascii<B>(&mut self, buf: B) -> io::Result<()> + where B: AsRef<[u8]>, + { + self.write_labeled(buf, |offset, data| { + let mut l = String::new(); + for _ in 0..offset { + l.push(' '); + } + for &c in data { + l.push(if c < 32 { + '.' + } else if c < 128 { + c.into() + } else { + '.' + }) + } + Some(l) + }) + } + /// Writes a chunk of data. /// /// For each line, the given function is called to compute a |