television_utils::strings

Function proportion_of_printable_ascii_characters

Source
pub fn proportion_of_printable_ascii_characters(buffer: &[u8]) -> f32
Expand description

Returns the proportion of printable ASCII characters in the given buffer.

This really is a cheap way to determine if a buffer is likely to be a text file.

ยงExamples

use television_utils::strings::proportion_of_printable_ascii_characters;

let buffer = b"Hello, World!";
let proportion = proportion_of_printable_ascii_characters(buffer);
assert_eq!(proportion, 1.0);

let buffer = b"Hello, World!\x00";
let proportion = proportion_of_printable_ascii_characters(buffer);
assert_eq!(proportion, 0.9285714);

let buffer = b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F";
let proportion = proportion_of_printable_ascii_characters(buffer);
assert_eq!(proportion, 0.0);