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

no-std unicode-general-category

Fast lookup of the Unicode General Category property for char

6 releases (breaking)

0.6.0 Sep 19, 2022
0.5.1 Jan 25, 2022
0.4.0 Mar 26, 2021
0.3.0 Nov 18, 2020
0.1.0 Dec 13, 2019

#151 in Text processing

Download history 10889/week @ 2023-12-02 11662/week @ 2023-12-09 10516/week @ 2023-12-16 6210/week @ 2023-12-23 8028/week @ 2023-12-30 9509/week @ 2024-01-06 13020/week @ 2024-01-13 14161/week @ 2024-01-20 12627/week @ 2024-01-27 10866/week @ 2024-02-03 12328/week @ 2024-02-10 13749/week @ 2024-02-17 12533/week @ 2024-02-24 12455/week @ 2024-03-02 10844/week @ 2024-03-09 8768/week @ 2024-03-16

47,026 downloads per month
Used in 148 crates (8 directly)

Apache-2.0

220KB
4K SLoC

unicode-general-category


Fast lookup of the Unicode General Category property for char in Rust using Unicode 15.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