#unicode #category #properties #no-std #general

no-std unicode-general-category

Fast lookup of the Unicode General Category property for char

8 releases (1 stable)

1.0.0 Oct 30, 2024
1.0.0-pre Oct 8, 2024
0.6.0 Sep 19, 2022
0.5.1 Jan 25, 2022
0.1.0 Dec 13, 2019

#37 in Text processing

Download history 8083/week @ 2024-08-17 8332/week @ 2024-08-24 8365/week @ 2024-08-31 8340/week @ 2024-09-07 7190/week @ 2024-09-14 7161/week @ 2024-09-21 8560/week @ 2024-09-28 6836/week @ 2024-10-05 6499/week @ 2024-10-12 10441/week @ 2024-10-19 14849/week @ 2024-10-26 15420/week @ 2024-11-02 10143/week @ 2024-11-09 14948/week @ 2024-11-16 17231/week @ 2024-11-23 14298/week @ 2024-11-30

59,660 downloads per month
Used in 133 crates (11 directly)

Apache-2.0

225KB
4.5K SLoC

unicode-general-category


Fast lookup of the Unicode General Category property for char in Rust using Unicode 16.0 data. This crate is no-std compatible.

Usage

use unicode_general_category::{get_general_category, GeneralCategory};

fn main() {
    assert_eq!(get_general_category('A'), GeneralCategory::UppercaseLetter);
}

Performance & Implementation Notes

ucd-generate is used to generate tables.rs. A build script (build.rs) compiles this into a two level look up table. The look up time is constant as it is just indexing into two arrays.

The two level approach maps a code point to a block, then to a position within a block. The allows the second level of block to be deduplicated, saving space. The code is parameterised over the block size, which must be a power of 2. The value in the build script is optimal for the data set.

This approach trades off some space for faster lookups. The tables take up about 45KiB. Benchmarks showed this approach to be ~5–10× faster than the typical binary search approach.

It's possible there are further optimisations that could be made to eliminate some runs of repeated values in the first level array.

No runtime deps