Skip to content

Commit efa0826

Browse files
committed
feat: add base
0 parents  commit efa0826

File tree

6 files changed

+163
-0
lines changed

6 files changed

+163
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/target
2+
**/*.rs.bk

Cargo.lock

+65
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "binyl"
3+
version = "0.1.0"
4+
authors = ["Yasuaki Uechi <[email protected]>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
colored = "1.9.0"

LICENSE

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2019 Yasuaki Uechi <[email protected]>
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
15+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
19+
OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# binyl

src/main.rs

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
use colored::*;
2+
use std::env;
3+
use std::process;
4+
5+
fn main() {
6+
let args: Vec<String> = env::args().collect();
7+
let filename = match args.get(1) {
8+
Some(f) => f,
9+
None => {
10+
println!("No arg provided");
11+
process::exit(1);
12+
}
13+
};
14+
let content = std::fs::read_to_string(filename).unwrap();
15+
let mut bytes = content.bytes();
16+
while let Some(b) = bytes.next() {
17+
if b & 0x80 == 0 {
18+
// ASCII
19+
println!(
20+
"{} [{:08b}] \"{}\" (1 byte)",
21+
"├".bright_black(),
22+
b,
23+
std::str::from_utf8(&[b])
24+
.unwrap()
25+
.replace("\x0a", "↵")
26+
.replace("\x20", "<space>")
27+
);
28+
} else {
29+
let nb = (!b).leading_zeros();
30+
println!(
31+
"{} {:08b} ({:06b})",
32+
"├┬".bright_black(),
33+
b,
34+
b & (u8::pow(2, 7 - nb) - 1)
35+
);
36+
let mut v: Vec<u8> = Vec::new();
37+
let mut f: u32 = 0;
38+
let base = 6 * (nb - 1);
39+
v.push(b);
40+
f |= (b as u32 & (u32::pow(2, 7 - nb) - 1)) << base;
41+
for i in 0..(nb - 1) {
42+
let next_byte = bytes.next().unwrap();
43+
if next_byte & 0xC0 != 0x80 {
44+
println!("Illegal byte");
45+
process::exit(1);
46+
}
47+
println!(
48+
"{} {:08b} ({:06b})",
49+
"│├".bright_black(),
50+
next_byte,
51+
next_byte & 0x3f
52+
);
53+
f |= (next_byte as u32 & 0x3f) << base - (i + 1) * 6;
54+
v.push(next_byte);
55+
}
56+
println!(
57+
"{} [U+{:x}] \"{}\" ({} bytes) - {}",
58+
"│└".bright_black(),
59+
f,
60+
std::str::from_utf8(&v[..]).unwrap().bold(),
61+
nb,
62+
format!("https://www.compart.com/en/unicode/U+{:x}", f).bright_black()
63+
);
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)