unicode_normalization_alignments/
lookups.rs

1// Copyright 2019 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
11//! Lookups of unicode properties using minimal perfect hashing.
12
13use perfect_hash::mph_lookup;
14use tables::*;
15
16/// Look up the canonical combining class for a codepoint.
17/// 
18/// The value returned is as defined in the Unicode Character Database.
19pub fn canonical_combining_class(c: char) -> u8 {
20    mph_lookup(c.into(), CANONICAL_COMBINING_CLASS_SALT, CANONICAL_COMBINING_CLASS_KV,
21        u8_lookup_fk, u8_lookup_fv, 0)
22}
23
24pub(crate) fn composition_table(c1: char, c2: char) -> Option<char> {
25    if c1 < '\u{10000}' && c2 < '\u{10000}' {
26        mph_lookup((c1 as u32) << 16 | (c2 as u32),
27        COMPOSITION_TABLE_SALT, COMPOSITION_TABLE_KV,
28        pair_lookup_fk, pair_lookup_fv_opt, None)
29    } else {
30        composition_table_astral(c1, c2)
31    }
32}
33
34pub(crate) fn canonical_fully_decomposed(c: char) -> Option<&'static [char]> {
35    mph_lookup(c.into(), CANONICAL_DECOMPOSED_SALT, CANONICAL_DECOMPOSED_KV,
36        pair_lookup_fk, pair_lookup_fv_opt, None)
37}
38
39pub(crate) fn compatibility_fully_decomposed(c: char) -> Option<&'static [char]> {
40    mph_lookup(c.into(), COMPATIBILITY_DECOMPOSED_SALT, COMPATIBILITY_DECOMPOSED_KV,
41        pair_lookup_fk, pair_lookup_fv_opt, None)
42}
43
44/// Return whether the given character is a combining mark (`General_Category=Mark`)
45pub fn is_combining_mark(c: char) -> bool {
46    mph_lookup(c.into(), COMBINING_MARK_SALT, COMBINING_MARK_KV,
47        bool_lookup_fk, bool_lookup_fv, false)
48}
49
50pub fn stream_safe_trailing_nonstarters(c: char) -> usize {
51    mph_lookup(c.into(), TRAILING_NONSTARTERS_SALT, TRAILING_NONSTARTERS_KV,
52        u8_lookup_fk, u8_lookup_fv, 0) as usize
53}
54
55/// Extract the key in a 24 bit key and 8 bit value packed in a u32.
56#[inline]
57fn u8_lookup_fk(kv: u32) -> u32 {
58    kv >> 8
59}
60
61/// Extract the value in a 24 bit key and 8 bit value packed in a u32.
62#[inline]
63fn u8_lookup_fv(kv: u32) -> u8 {
64    (kv & 0xff) as u8
65}
66
67/// Extract the key for a boolean lookup.
68#[inline]
69fn bool_lookup_fk(kv: u32) -> u32 {
70    kv
71}
72
73/// Extract the value for a boolean lookup.
74#[inline]
75fn bool_lookup_fv(_kv: u32) -> bool {
76    true
77}
78
79/// Extract the key in a pair.
80#[inline]
81fn pair_lookup_fk<T>(kv: (u32, T)) -> u32 {
82    kv.0
83}
84
85/// Extract the value in a pair, returning an option.
86#[inline]
87fn pair_lookup_fv_opt<T>(kv: (u32, T)) -> Option<T> {
88    Some(kv.1)
89}