Initial implementation
This commit is contained in:
commit
4557fe7a79
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
/target
|
||||
.vscode
|
||||
134
Cargo.lock
generated
Normal file
134
Cargo.lock
generated
Normal file
@ -0,0 +1,134 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "bytecount"
|
||||
version = "0.6.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2c676a478f63e9fa2dd5368a42f28bba0d6c560b775f38583c8bbaa7fcd67c9c"
|
||||
|
||||
[[package]]
|
||||
name = "enc"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"tabled",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fnv"
|
||||
version = "1.0.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
|
||||
|
||||
[[package]]
|
||||
name = "papergrid"
|
||||
version = "0.10.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a2ccbe15f2b6db62f9a9871642746427e297b0ceb85f9a7f1ee5ff47d184d0c8"
|
||||
dependencies = [
|
||||
"bytecount",
|
||||
"fnv",
|
||||
"unicode-width",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-error"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
|
||||
dependencies = [
|
||||
"proc-macro-error-attr",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-error-attr"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.67"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3d433d9f1a3e8c1263d9456598b16fec66f4acc9a74dacffd35c7bb09b3a1328"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.33"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.109"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tabled"
|
||||
version = "0.14.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dfe9c3632da101aba5131ed63f9eed38665f8b3c68703a6bb18124835c1a5d22"
|
||||
dependencies = [
|
||||
"papergrid",
|
||||
"tabled_derive",
|
||||
"unicode-width",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tabled_derive"
|
||||
version = "0.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "99f688a08b54f4f02f0a3c382aefdb7884d3d69609f785bd253dc033243e3fe4"
|
||||
dependencies = [
|
||||
"heck",
|
||||
"proc-macro-error",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-width"
|
||||
version = "0.1.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85"
|
||||
|
||||
[[package]]
|
||||
name = "version_check"
|
||||
version = "0.9.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
|
||||
9
Cargo.toml
Normal file
9
Cargo.toml
Normal file
@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "enc"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
tabled = "0.14.0"
|
||||
104
src/main.rs
Normal file
104
src/main.rs
Normal file
@ -0,0 +1,104 @@
|
||||
use std::env;
|
||||
|
||||
use tabled::{
|
||||
builder::Builder,
|
||||
settings::{Modify, object::Rows, Alignment, Style}
|
||||
};
|
||||
|
||||
struct StringDetail {
|
||||
characters: Vec<CharacterDetail>,
|
||||
len: usize,
|
||||
}
|
||||
|
||||
struct CharacterDetail {
|
||||
byte_index: usize,
|
||||
character: Option<char>,
|
||||
byte: u8,
|
||||
}
|
||||
|
||||
impl StringDetail{
|
||||
fn parse_utf8(query: &String) -> Self {
|
||||
let mut details:StringDetail = StringDetail::default();
|
||||
for i in query.chars() {
|
||||
let mut bytes = [0; 4];
|
||||
i.encode_utf8(&mut bytes);
|
||||
|
||||
details.push(Some(i), bytes[0]);
|
||||
for b in 1..i.len_utf8() {
|
||||
details.push(None, bytes[b]);
|
||||
}
|
||||
}
|
||||
|
||||
details
|
||||
}
|
||||
|
||||
fn default() -> Self {
|
||||
Self { characters: Vec::new(), len: 0 }
|
||||
}
|
||||
|
||||
fn push(&mut self, character:Option<char>, byte:u8){
|
||||
self.characters
|
||||
.push(CharacterDetail {
|
||||
byte_index: self.len,
|
||||
character,
|
||||
byte,
|
||||
});
|
||||
self.len += 1;
|
||||
}
|
||||
|
||||
fn print_table(&self) {
|
||||
let mut table_builder = Builder::default();
|
||||
table_builder.set_header(StringDetail::table_header());
|
||||
for i in self.table_rows() {
|
||||
table_builder.push_record(i);
|
||||
}
|
||||
|
||||
let table = table_builder.build()
|
||||
.with(Style::sharp())
|
||||
.with(Modify::new(Rows::new(1..)).with(Alignment::left()))
|
||||
.to_string();
|
||||
print!("{}", table);
|
||||
}
|
||||
|
||||
fn table_rows(&self) -> Vec<Vec<String>> {
|
||||
self.characters.iter()
|
||||
.map(StringDetail::to_table_row)
|
||||
.collect::<Vec<Vec<_>>>()
|
||||
}
|
||||
|
||||
fn to_table_row(char_detail: &CharacterDetail) -> Vec<String> {
|
||||
let character: String = match char_detail.character {
|
||||
Some(x) => String::from(format!("{}", x)),
|
||||
None => String::from("<..>"),
|
||||
};
|
||||
|
||||
vec![
|
||||
character,
|
||||
format!("{}", char_detail.byte_index),
|
||||
format!("{:02x}", char_detail.byte),
|
||||
format!("{}", char_detail.byte),
|
||||
format!("{:08b}", char_detail.byte)]
|
||||
}
|
||||
|
||||
fn table_header() -> Vec<String> {
|
||||
vec![
|
||||
String::from("character"),
|
||||
String::from("byte"),
|
||||
String::from("hex"),
|
||||
String::from("dec"),
|
||||
String::from("bin"),
|
||||
]
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
if args.len() < 2 {
|
||||
print!("Usage: enc <string>");
|
||||
return;
|
||||
}
|
||||
|
||||
let utf8 = StringDetail::parse_utf8(&args[1]);
|
||||
utf8.print_table();
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user