commit 8e913b62fe182585a959e45496e0d71707bf3157
parent fd51b273d06b40ead9be24d15b52c88323d813e0
Author: Jared Tobin <jared@jtobin.ca>
Date: Sun, 18 Dec 2016 20:05:43 +1300
Char frequency tool.
Diffstat:
3 files changed, 62 insertions(+), 0 deletions(-)
diff --git a/lib/charfreq/Cargo.lock b/lib/charfreq/Cargo.lock
@@ -0,0 +1,14 @@
+[root]
+name = "charfreq"
+version = "0.1.0"
+dependencies = [
+ "rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "rustc-serialize"
+version = "0.3.22"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[metadata]
+"checksum rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)" = "237546c689f20bb44980270c73c3b9edd0891c1be49cc1274406134a66d3957b"
diff --git a/lib/charfreq/Cargo.toml b/lib/charfreq/Cargo.toml
@@ -0,0 +1,8 @@
+[package]
+name = "charfreq"
+version = "0.1.0"
+authors = ["Jared Tobin <jared@jtobin.ca>"]
+
+[dependencies]
+
+rustc-serialize = "0.3.0"
diff --git a/lib/charfreq/src/main.rs b/lib/charfreq/src/main.rs
@@ -0,0 +1,40 @@
+extern crate rustc_serialize as serialize;
+
+use serialize::hex::FromHex;
+use std::collections::HashMap;
+use std::io::{self, Read};
+use std::string::String;
+use std::vec::Vec;
+
+fn tally(vec: Vec<u8>) -> HashMap<u8, u8> {
+ let mut hashmap = HashMap::new();
+
+ for byte in vec {
+ let count = hashmap.entry(byte).or_insert(0);
+ *count += 1;
+ }
+
+ hashmap
+}
+
+fn main() {
+ let mut buffer = String::new();
+
+ io::stdin().read_to_string(&mut buffer)
+ .expect("charfreq: bad input");
+
+ let decoded = match buffer.from_hex() {
+ Err(err) => panic!("charfreq: {}", err),
+ Ok(val) => val,
+ };
+
+ let mut results: Vec<(u8, u8)> = tally(decoded).into_iter().collect();
+ results.sort_by(|a, b| b.1.cmp(&a.1));
+
+ let best: Vec<(u8, u8)> = results.into_iter().take(5).collect();
+
+ println!("byte (frequency)");
+ println!("----------------");
+ for (val, count) in best { println!("{} ({})", val, count); }
+}
+