mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 04:45:04 +02:00
Remove old nushell/merge engine-q
This commit is contained in:
201
crates/nu-pretty-hex/Cargo.lock
generated
Normal file
201
crates/nu-pretty-hex/Cargo.lock
generated
Normal file
@ -0,0 +1,201 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "as-slice"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "45403b49e3954a4b8428a0ac21a4b7afadccf92bfd96273f1a58cd4812496ae0"
|
||||
dependencies = [
|
||||
"generic-array 0.12.4",
|
||||
"generic-array 0.13.3",
|
||||
"generic-array 0.14.4",
|
||||
"stable_deref_trait",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "byteorder"
|
||||
version = "1.4.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "generic-array"
|
||||
version = "0.12.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ffdf9f34f1447443d37393cc6c2b8313aebddcd96906caf34e54c68d8e57d7bd"
|
||||
dependencies = [
|
||||
"typenum",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "generic-array"
|
||||
version = "0.13.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f797e67af32588215eaaab8327027ee8e71b9dd0b2b26996aedf20c030fce309"
|
||||
dependencies = [
|
||||
"typenum",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "generic-array"
|
||||
version = "0.14.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "501466ecc8a30d1d3b7fc9229b122b2ce8ed6e9d9223f1138d4babb253e51817"
|
||||
dependencies = [
|
||||
"typenum",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"wasi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hash32"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d4041af86e63ac4298ce40e5cca669066e75b6f1aa3390fe2561ffa5e1d9f4cc"
|
||||
dependencies = [
|
||||
"byteorder",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "heapless"
|
||||
version = "0.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "634bd4d29cbf24424d0a4bfcbf80c6960129dc24424752a7d1d1390607023422"
|
||||
dependencies = [
|
||||
"as-slice",
|
||||
"generic-array 0.14.4",
|
||||
"hash32",
|
||||
"stable_deref_trait",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.93"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9385f66bf6105b241aa65a61cb923ef20efc665cb9f9bb50ac2f0c4b7f378d41"
|
||||
|
||||
[[package]]
|
||||
name = "nu-ansi-term"
|
||||
version = "0.29.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6bd69a141e8fdfa5ac882d8b816db2b9ad138ef7e3baa7cb753a9b3789aa8c7e"
|
||||
dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nu-pretty-hex"
|
||||
version = "0.2.1"
|
||||
dependencies = [
|
||||
"heapless",
|
||||
"nu-ansi-term",
|
||||
"rand",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ppv-lite86"
|
||||
version = "0.2.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.8.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"rand_chacha",
|
||||
"rand_core",
|
||||
"rand_hc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_chacha"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d"
|
||||
dependencies = [
|
||||
"ppv-lite86",
|
||||
"rand_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.6.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_hc"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73"
|
||||
dependencies = [
|
||||
"rand_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "stable_deref_trait"
|
||||
version = "1.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3"
|
||||
|
||||
[[package]]
|
||||
name = "typenum"
|
||||
version = "1.13.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "879f6906492a7cd215bfa4cf595b600146ccfac0c79bcbd1f3000162af5e8b06"
|
||||
|
||||
[[package]]
|
||||
name = "version_check"
|
||||
version = "0.9.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe"
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.10.2+wasi-snapshot-preview1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.3.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
||||
dependencies = [
|
||||
"winapi-i686-pc-windows-gnu",
|
||||
"winapi-x86_64-pc-windows-gnu",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-i686-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-x86_64-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
@ -1,17 +1,10 @@
|
||||
[package]
|
||||
authors = ["Andrei Volnin <wolandr@gmail.com>", "The Nu Project Contributors"]
|
||||
description = "Pretty hex dump of bytes slice in the common style."
|
||||
<<<<<<< HEAD
|
||||
edition = "2018"
|
||||
license = "MIT"
|
||||
name = "nu-pretty-hex"
|
||||
version = "0.43.0"
|
||||
=======
|
||||
edition = "2021"
|
||||
license = "MIT"
|
||||
name = "nu-pretty-hex"
|
||||
version = "0.41.0"
|
||||
>>>>>>> 9259a56a28f1dd3a4b720ad815aa19c6eaf6adce
|
||||
version = "0.59.0"
|
||||
|
||||
[lib]
|
||||
doctest = false
|
||||
@ -23,11 +16,9 @@ name = "nu_pretty_hex"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
<<<<<<< HEAD
|
||||
nu-ansi-term = { path="../nu-ansi-term", version = "0.43.0" }
|
||||
=======
|
||||
#nu-ansi-term = {path = "../nu-ansi-term", version = "0.59.0" }
|
||||
nu-ansi-term = "0.42.0"
|
||||
>>>>>>> 9259a56a28f1dd3a4b720ad815aa19c6eaf6adce
|
||||
|
||||
rand = "0.8.3"
|
||||
|
||||
[dev-dependencies]
|
||||
|
21
crates/nu-pretty-hex/LICENSE
Normal file
21
crates/nu-pretty-hex/LICENSE
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2018 Andrei Volnin
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
81
crates/nu-pretty-hex/README.md
Normal file
81
crates/nu-pretty-hex/README.md
Normal file
@ -0,0 +1,81 @@
|
||||
# nu-pretty-hex
|
||||
|
||||
An update of prett-hex to make it prettier
|
||||
|
||||
[](https://crates.io/crates/pretty-hex)
|
||||
[](https://docs.rs/pretty-hex)
|
||||
|
||||
A Rust library providing pretty hex dump.
|
||||
|
||||
A `simple_hex()` way renders one-line hex dump, a `pretty_hex()` way renders
|
||||
columned multi-line hex dump with addressing and ASCII representation.
|
||||
A `config_hex()` way renders hex dump in specified format.
|
||||
|
||||
## Inspiration
|
||||
|
||||
[Hexed](https://github.com/adolfohw/hexed) \
|
||||
[Hexyl](https://github.com/sharkdp/hexyl) \
|
||||
[Pretty-hex](https://github.com/wolandr/pretty-hex)
|
||||
|
||||
## Example of `simple_hex()`
|
||||
|
||||
```rust
|
||||
use pretty_hex::*;
|
||||
|
||||
let v = vec![222, 173, 190, 239, 202, 254, 32, 24];
|
||||
assert_eq!(simple_hex(&v), format!("{}", v.hex_dump()));
|
||||
|
||||
println!("{}", v.hex_dump());
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```text
|
||||
de ad be ef ca fe 20 18
|
||||
```
|
||||
|
||||
## Example of `pretty_hex()`
|
||||
|
||||
```rust
|
||||
use pretty_hex::*;
|
||||
|
||||
let v: &[u8] = &random::<[u8;30]>();
|
||||
assert_eq!(pretty_hex(&v), format!("{:?}", v.hex_dump()));
|
||||
|
||||
println!("{:?}", v.hex_dump());
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```text
|
||||
Length: 30 (0x1e) bytes
|
||||
0000: 6b 4e 1a c3 af 03 d2 1e 7e 73 ba c8 bd 84 0f 83 kN......~s......
|
||||
0010: 89 d5 cf 90 23 67 4b 48 db b1 bc 35 bf ee ....#gKH...5..
|
||||
```
|
||||
|
||||
## Example of `config_hex()`
|
||||
|
||||
```rust
|
||||
use pretty_hex::*;
|
||||
|
||||
let cfg = HexConfig {title: false, width: 8, group: 0, ..HexConfig::default() };
|
||||
|
||||
let v = &include_bytes!("data");
|
||||
assert_eq!(config_hex(&v, cfg), format!("{:?}", v.hex_conf(cfg)));
|
||||
|
||||
println!("{:?}", v.hex_conf(cfg));
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```text
|
||||
0000: 6b 4e 1a c3 af 03 d2 1e kN......
|
||||
0008: 7e 73 ba c8 bd 84 0f 83 ~s......
|
||||
0010: 89 d5 cf 90 23 67 4b 48 ....#gKH
|
||||
0018: db b1 bc 35 bf ee ...5..
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
Inspired by [haskell's pretty-hex](https://hackage.haskell.org/package/pretty-hex-1.0).
|
66
crates/nu-pretty-hex/src/lib.rs
Normal file
66
crates/nu-pretty-hex/src/lib.rs
Normal file
@ -0,0 +1,66 @@
|
||||
// #![no_std]
|
||||
|
||||
//! A Rust library providing pretty hex dump.
|
||||
//!
|
||||
//! A `simple_hex()` way renders one-line hex dump, and a `pretty_hex()` way renders
|
||||
//! columned multi-line hex dump with addressing and ASCII representation.
|
||||
//! A `config_hex()` way renders hex dump in specified format.
|
||||
//!
|
||||
//! ## Example of `simple_hex()`
|
||||
//! ```
|
||||
//! use pretty_hex::*;
|
||||
//!
|
||||
//! let v = vec![222, 173, 190, 239, 202, 254, 32, 24];
|
||||
//! # #[cfg(feature = "alloc")]
|
||||
//! assert_eq!(simple_hex(&v), format!("{}", v.hex_dump()));
|
||||
//!
|
||||
//! println!("{}", v.hex_dump());
|
||||
//! ```
|
||||
//! Output:
|
||||
//!
|
||||
//! ```text
|
||||
//! de ad be ef ca fe 20 18
|
||||
//! ```
|
||||
//! ## Example of `pretty_hex()`
|
||||
//! ```
|
||||
//! use pretty_hex::*;
|
||||
//!
|
||||
//! let v = &include_bytes!("../tests/data");
|
||||
//! # #[cfg(feature = "alloc")]
|
||||
//! assert_eq!(pretty_hex(&v), format!("{:?}", v.hex_dump()));
|
||||
//!
|
||||
//! println!("{:?}", v.hex_dump());
|
||||
//! ```
|
||||
//! Output:
|
||||
//!
|
||||
//! ```text
|
||||
//! Length: 30 (0x1e) bytes
|
||||
//! 0000: 6b 4e 1a c3 af 03 d2 1e 7e 73 ba c8 bd 84 0f 83 kN......~s......
|
||||
//! 0010: 89 d5 cf 90 23 67 4b 48 db b1 bc 35 bf ee ....#gKH...5..
|
||||
//! ```
|
||||
//! ## Example of `config_hex()`
|
||||
//! ```
|
||||
//! use pretty_hex::*;
|
||||
//!
|
||||
//! let cfg = HexConfig {title: false, width: 8, group: 0, ..HexConfig::default() };
|
||||
//!
|
||||
//! let v = &include_bytes!("../tests/data");
|
||||
//! # #[cfg(feature = "alloc")]
|
||||
//! assert_eq!(config_hex(&v, cfg), format!("{:?}", v.hex_conf(cfg)));
|
||||
//!
|
||||
//! println!("{:?}", v.hex_conf(cfg));
|
||||
//! ```
|
||||
//! Output:
|
||||
//!
|
||||
//! ```text
|
||||
//! 0000: 6b 4e 1a c3 af 03 d2 1e kN......
|
||||
//! 0008: 7e 73 ba c8 bd 84 0f 83 ~s......
|
||||
//! 0010: 89 d5 cf 90 23 67 4b 48 ....#gKH
|
||||
//! 0018: db b1 bc 35 bf ee ...5..
|
||||
//! ```
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
extern crate alloc;
|
||||
|
||||
mod pretty_hex;
|
||||
pub use pretty_hex::*;
|
@ -7,10 +7,7 @@ fn main() {
|
||||
width: 16,
|
||||
group: 4,
|
||||
chunk: 1,
|
||||
<<<<<<< HEAD
|
||||
=======
|
||||
address_offset: 0,
|
||||
>>>>>>> 9259a56a28f1dd3a4b720ad815aa19c6eaf6adce
|
||||
skip: Some(10),
|
||||
// length: Some(5),
|
||||
// length: None,
|
||||
|
@ -57,11 +57,8 @@ pub struct HexConfig {
|
||||
pub group: usize,
|
||||
/// Source bytes per chunk (word). 0 for single word.
|
||||
pub chunk: usize,
|
||||
<<<<<<< HEAD
|
||||
=======
|
||||
/// Offset to start counting addresses from
|
||||
pub address_offset: usize,
|
||||
>>>>>>> 9259a56a28f1dd3a4b720ad815aa19c6eaf6adce
|
||||
/// Bytes from 0 to skip
|
||||
pub skip: Option<usize>,
|
||||
/// Length to return
|
||||
@ -78,10 +75,7 @@ impl Default for HexConfig {
|
||||
width: 16,
|
||||
group: 4,
|
||||
chunk: 1,
|
||||
<<<<<<< HEAD
|
||||
=======
|
||||
address_offset: 0,
|
||||
>>>>>>> 9259a56a28f1dd3a4b720ad815aa19c6eaf6adce
|
||||
skip: None,
|
||||
length: None,
|
||||
}
|
||||
@ -173,11 +167,8 @@ where
|
||||
|
||||
let skip = cfg.skip.unwrap_or(0);
|
||||
|
||||
<<<<<<< HEAD
|
||||
=======
|
||||
let address_offset = cfg.address_offset;
|
||||
|
||||
>>>>>>> 9259a56a28f1dd3a4b720ad815aa19c6eaf6adce
|
||||
let source_part_vec: Vec<u8> = source
|
||||
.as_ref()
|
||||
.iter()
|
||||
@ -219,19 +210,11 @@ where
|
||||
writer,
|
||||
"{}{:08x}{}: ",
|
||||
style.prefix(),
|
||||
<<<<<<< HEAD
|
||||
i * cfg.width + skip,
|
||||
style.suffix()
|
||||
)?;
|
||||
} else {
|
||||
write!(writer, "{:08x}: ", i * cfg.width + skip,)?;
|
||||
=======
|
||||
i * cfg.width + skip + address_offset,
|
||||
style.suffix()
|
||||
)?;
|
||||
} else {
|
||||
write!(writer, "{:08x}: ", i * cfg.width + skip + address_offset,)?;
|
||||
>>>>>>> 9259a56a28f1dd3a4b720ad815aa19c6eaf6adce
|
||||
}
|
||||
}
|
||||
for (i, x) in row.as_ref().iter().enumerate() {
|
||||
|
17
crates/nu-pretty-hex/tests/256.txt
Normal file
17
crates/nu-pretty-hex/tests/256.txt
Normal file
@ -0,0 +1,17 @@
|
||||
Length: 256 (0x100) bytes
|
||||
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 29 2a 2b 2c 2d 2e 2f !"#$%&'()*+,-./
|
||||
0030: 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?
|
||||
0040: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO
|
||||
0050: 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f PQRSTUVWXYZ[\]^_
|
||||
0060: 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f `abcdefghijklmno
|
||||
0070: 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f pqrstuvwxyz{|}~.
|
||||
0080: 80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f ................
|
||||
0090: 90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f ................
|
||||
00a0: a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af ................
|
||||
00b0: b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf ................
|
||||
00c0: c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf ................
|
||||
00d0: d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df ................
|
||||
00e0: e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef ................
|
||||
00f0: f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff ................
|
1
crates/nu-pretty-hex/tests/data
Normal file
1
crates/nu-pretty-hex/tests/data
Normal file
@ -0,0 +1 @@
|
||||
kNï<03>~s<>Ƚ<EFBFBD><0F><><EFBFBD>ϐ#gKH۱<48>5<EFBFBD><35>
|
175
crates/nu-pretty-hex/tests/tests.rs
Normal file
175
crates/nu-pretty-hex/tests/tests.rs
Normal file
@ -0,0 +1,175 @@
|
||||
// #![no_std]
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
extern crate alloc;
|
||||
extern crate nu_pretty_hex;
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
use alloc::{format, string::String, vec, vec::Vec};
|
||||
use nu_pretty_hex::*;
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
#[test]
|
||||
fn test_simple() {
|
||||
let bytes: Vec<u8> = (0..16).collect();
|
||||
let expected = "00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f";
|
||||
assert_eq!(expected, simple_hex(&bytes));
|
||||
assert_eq!(expected, bytes.hex_dump().to_string());
|
||||
assert_eq!(simple_hex(&bytes), config_hex(&bytes, HexConfig::simple()));
|
||||
|
||||
let mut have = String::new();
|
||||
simple_hex_write(&mut have, &bytes).unwrap();
|
||||
assert_eq!(expected, have);
|
||||
|
||||
let str = "string";
|
||||
let string: String = String::from("string");
|
||||
let slice: &[u8] = &[0x73, 0x74, 0x72, 0x69, 0x6e, 0x67];
|
||||
assert_eq!(simple_hex(&str), "73 74 72 69 6e 67");
|
||||
assert_eq!(simple_hex(&str), simple_hex(&string));
|
||||
assert_eq!(simple_hex(&str), simple_hex(&slice));
|
||||
|
||||
assert!(simple_hex(&vec![]).is_empty());
|
||||
}
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
#[test]
|
||||
fn test_pretty() {
|
||||
let bytes: Vec<u8> = (0..256).map(|x| x as u8).collect();
|
||||
let want = include_str!("256.txt");
|
||||
|
||||
let mut hex = String::new();
|
||||
pretty_hex_write(&mut hex, &bytes).unwrap();
|
||||
assert_eq!(want, hex);
|
||||
assert_eq!(want, format!("{:?}", bytes.hex_dump()));
|
||||
assert_eq!(want, pretty_hex(&bytes));
|
||||
assert_eq!(want, config_hex(&bytes, HexConfig::default()));
|
||||
|
||||
assert_eq!("Length: 0 (0x0) bytes\n", pretty_hex(&vec![]));
|
||||
}
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
#[test]
|
||||
fn test_config() {
|
||||
let cfg = HexConfig {
|
||||
title: false,
|
||||
ascii: false,
|
||||
width: 0,
|
||||
group: 0,
|
||||
chunk: 0,
|
||||
};
|
||||
assert!(config_hex(&vec![], cfg).is_empty());
|
||||
assert_eq!("2425262728", config_hex(&"$%&'(", cfg));
|
||||
|
||||
let v = include_bytes!("data");
|
||||
let cfg = HexConfig {
|
||||
title: false,
|
||||
group: 8,
|
||||
..HexConfig::default()
|
||||
};
|
||||
let hex = "0000: 6b 4e 1a c3 af 03 d2 1e 7e 73 ba c8 bd 84 0f 83 kN......~s......\n\
|
||||
0010: 89 d5 cf 90 23 67 4b 48 db b1 bc 35 bf ee ....#gKH...5..";
|
||||
assert_eq!(hex, config_hex(&v, cfg));
|
||||
assert_eq!(hex, format!("{:?}", v.hex_conf(cfg)));
|
||||
let mut str = String::new();
|
||||
hex_write(&mut str, v, cfg).unwrap();
|
||||
assert_eq!(hex, str);
|
||||
|
||||
assert_eq!(
|
||||
config_hex(
|
||||
&v,
|
||||
HexConfig {
|
||||
ascii: false,
|
||||
..cfg
|
||||
}
|
||||
),
|
||||
"0000: 6b 4e 1a c3 af 03 d2 1e 7e 73 ba c8 bd 84 0f 83\n\
|
||||
0010: 89 d5 cf 90 23 67 4b 48 db b1 bc 35 bf ee"
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
config_hex(
|
||||
&v,
|
||||
HexConfig {
|
||||
ascii: false,
|
||||
group: 4,
|
||||
chunk: 2,
|
||||
..cfg
|
||||
}
|
||||
),
|
||||
"0000: 6b4e 1ac3 af03 d21e 7e73 bac8 bd84 0f83\n\
|
||||
0010: 89d5 cf90 2367 4b48 dbb1 bc35 bfee"
|
||||
);
|
||||
|
||||
let v: Vec<u8> = (0..21).collect();
|
||||
let want = r##"Length: 21 (0x15) bytes
|
||||
0000: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f ................
|
||||
0010: 10 11 12 13 14 ....."##;
|
||||
assert_eq!(want, pretty_hex(&v));
|
||||
|
||||
let v: Vec<u8> = (0..13).collect();
|
||||
assert_eq!(
|
||||
config_hex(
|
||||
&v,
|
||||
HexConfig {
|
||||
title: false,
|
||||
ascii: true,
|
||||
width: 11,
|
||||
group: 2,
|
||||
chunk: 3
|
||||
}
|
||||
),
|
||||
"0000: 000102 030405 060708 090a ...........\n\
|
||||
000b: 0b0c .."
|
||||
);
|
||||
|
||||
let v: Vec<u8> = (0..19).collect();
|
||||
assert_eq!(
|
||||
config_hex(
|
||||
&v,
|
||||
HexConfig {
|
||||
title: false,
|
||||
ascii: true,
|
||||
width: 16,
|
||||
group: 3,
|
||||
chunk: 3
|
||||
}
|
||||
),
|
||||
"0000: 000102 030405 060708 090a0b 0c0d0e 0f ................\n\
|
||||
0010: 101112 ..."
|
||||
);
|
||||
|
||||
let cfg = HexConfig {
|
||||
title: false,
|
||||
group: 0,
|
||||
..HexConfig::default()
|
||||
};
|
||||
assert_eq!(
|
||||
format!("{:?}", v.hex_conf(cfg)),
|
||||
"0000: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f ................\n\
|
||||
0010: 10 11 12 ..."
|
||||
);
|
||||
assert_eq!(
|
||||
v.hex_conf(cfg).to_string(),
|
||||
"00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12"
|
||||
);
|
||||
}
|
||||
|
||||
// This test case checks that hex_write works even without the alloc crate.
|
||||
// Decorators to this function like simple_hex_write or PrettyHex::hex_dump()
|
||||
// will be tested when the alloc feature is selected because it feels quite
|
||||
// cumbersome to set up these tests without the comodity from `alloc`.
|
||||
#[test]
|
||||
fn test_hex_write_with_simple_config() {
|
||||
let config = HexConfig::simple();
|
||||
let bytes = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
|
||||
let expected =
|
||||
core::str::from_utf8(b"00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f").unwrap();
|
||||
// let expected =
|
||||
// "\u{1b}[38;5;242m00\u{1b}[0m \u{1b}[1;35m01\u{1b}[0m \u{1b}[1;35m02\u{1b}[0m \u{1b}[1;";
|
||||
let mut buffer = heapless::Vec::<u8, 50>::new();
|
||||
|
||||
hex_write(&mut buffer, &bytes, config, None).unwrap();
|
||||
|
||||
let have = core::str::from_utf8(&buffer).unwrap();
|
||||
assert_eq!(expected, have);
|
||||
}
|
Reference in New Issue
Block a user