Skip to content

Instantly share code, notes, and snippets.

@LPGhatguy
Created April 7, 2019 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LPGhatguy/78f432e605cb0bd6f7f1bc43e360ec86 to your computer and use it in GitHub Desktop.
Save LPGhatguy/78f432e605cb0bd6f7f1bc43e360ec86 to your computer and use it in GitHub Desktop.
What percentage of v4 UUIDs are valid UTF-8?
// uuid: 0.7.4 with "v4" feature enabled
// Valid UTF-8: 6868 / 100000000
// Valid %: 0.006868
//
// Sample strings:
//
// "2U6!>:N۸\u{1e}cUV3m%"
// "ɷ1e\r}AـΕ+3Wĭ"
// "aTA ?\u{17}KǺ\u{1f}\u{7}\u{19}\u{2}\u{c}\u{7}?"
// "|7zp`^EΑ%b/\u{17}uX "
// "sˬ\u{5}x[MضmƯ?Nd-"
// "N5z\n\u{15}-A\u{64ee5}!x\u{16}\u{1c}\u{5}"
// "O\napKsB\u{1ade}l܇]Ԗ"
// "8YjFhDNẃqpĈ7W"
// "\u{12}/<KC\u{19}Oɕ6!=\tq|@"
// "ž\u{1a}!{gD˔7&\u{8}o1l-"
// ")ǗNlwAЬ(cG\u{13}\u{18}Ѵ"
use std::{
str,
fs::File,
io::{Write, BufWriter},
};
use uuid::Uuid;
const SAMPLE_COUNT: usize = 100_000_000;
fn main() {
let mut output = BufWriter::new(File::create("unicode-uuids.txt").unwrap());
let mut valid_count = 0;
for _ in 0..SAMPLE_COUNT {
let uuid = Uuid::new_v4();
let bytes = uuid.as_bytes();
if let Ok(unicode) = str::from_utf8(bytes) {
writeln!(output, "{:?}", unicode).unwrap();
valid_count += 1;
}
}
output.flush().unwrap();
println!("Valid UTF-8: {} / {}", valid_count, SAMPLE_COUNT);
println!(" Valid %: {}", 100.0 * (valid_count as f64) / (SAMPLE_COUNT as f64));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment