utf8proc 0.1.2

Rust bindings to the utf8proc library
Documentation
use indexmap::IndexMap;
use std::ops::RangeInclusive;
use unicode_width::UnicodeWidthChar;
use utf8proc::properties::CharProperties;

#[test]
fn ascii() {
    for c in (0 as char)..(128 as char) {
        assert_eq!(c.width(), CharProperties::for_char(c).char_width(), "{c:?}");
    }
}

/// The percentage from which the output of [`CharProperties::char_width`],
/// is allowed to differ from `unicode-width` crate.
///
/// Update docs for [`CharProperties::char_width`] if this value changes.
const ALLOWED_DIFFERENT_PERCENTAGE: f64 = 7.0;

#[test]
fn mostly_matches_unicode_width() {
    const ALL_CHARS: RangeInclusive<char> = char::MIN..=char::MAX;
    let total_chars = ALL_CHARS.count();
    let mut differing_chars = 0;
    let mut most_common_differences = IndexMap::<_, usize>::new();
    for c in ALL_CHARS {
        let their_width = c.width();
        let our_width = CharProperties::for_char(c).char_width();
        if c.width() != our_width {
            *most_common_differences.entry((their_width, our_width)).or_default() += 1;
            differing_chars += 1;
        }
    }
    most_common_differences.sort_by_cached_key(|&(_, _), &count| std::cmp::Reverse(count));
    println!(
        "common differences: {:?}",
        most_common_differences.iter().take(5).collect::<IndexMap<_, _>>()
    );
    assert!(
        ALLOWED_DIFFERENT_PERCENTAGE >= 0.0 && ALLOWED_DIFFERENT_PERCENTAGE <= 100.0,
        "invalid percentage: {ALLOWED_DIFFERENT_PERCENTAGE}"
    );
    let actual_percentage = (differing_chars as f64 / total_chars as f64) * 100.0;
    assert!(
        actual_percentage <= ALLOWED_DIFFERENT_PERCENTAGE,
        "{actual_percentage}% percent difference exceeds {ALLOWED_DIFFERENT_PERCENTAGE}% limit"
    );
}