Coverage Report

Created: 2025-08-03 06:56

/src/unicode-normalization/src/lookups.rs
Line
Count
Source (jump to first uncovered line)
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
13
use crate::perfect_hash::mph_lookup;
14
use crate::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.
19
865M
pub fn canonical_combining_class(c: char) -> u8 {
20
865M
    mph_lookup(
21
865M
        c.into(),
22
865M
        CANONICAL_COMBINING_CLASS_SALT,
23
865M
        CANONICAL_COMBINING_CLASS_KV,
24
865M
        u8_lookup_fk,
25
865M
        u8_lookup_fv,
26
865M
        0,
27
865M
    )
28
865M
}
29
30
70.7M
pub(crate) fn composition_table(c1: char, c2: char) -> Option<char> {
31
70.7M
    if c1 < '\u{10000}' && c2 < '\u{10000}' {
32
70.5M
        mph_lookup(
33
70.5M
            (c1 as u32) << 16 | (c2 as u32),
34
70.5M
            COMPOSITION_TABLE_SALT,
35
70.5M
            COMPOSITION_TABLE_KV,
36
70.5M
            pair_lookup_fk,
37
70.5M
            pair_lookup_fv_opt,
38
70.5M
            None,
39
70.5M
        )
40
    } else {
41
269k
        composition_table_astral(c1, c2)
42
    }
43
70.7M
}
44
45
275M
pub(crate) fn canonical_fully_decomposed(c: char) -> Option<&'static [char]> {
46
275M
    mph_lookup(
47
275M
        c.into(),
48
275M
        CANONICAL_DECOMPOSED_SALT,
49
275M
        CANONICAL_DECOMPOSED_KV,
50
275M
        pair_lookup_fk,
51
275M
        pair_lookup_fv_opt,
52
275M
        None,
53
275M
    )
54
275M
    .map(|(start, len)| &CANONICAL_DECOMPOSED_CHARS[start as usize..][..len as usize])
55
275M
}
56
57
205M
pub(crate) fn compatibility_fully_decomposed(c: char) -> Option<&'static [char]> {
58
205M
    mph_lookup(
59
205M
        c.into(),
60
205M
        COMPATIBILITY_DECOMPOSED_SALT,
61
205M
        COMPATIBILITY_DECOMPOSED_KV,
62
205M
        pair_lookup_fk,
63
205M
        pair_lookup_fv_opt,
64
205M
        None,
65
205M
    )
66
205M
    .map(|(start, len)| &COMPATIBILITY_DECOMPOSED_CHARS[start as usize..][..len as usize])
67
205M
}
68
69
0
pub(crate) fn cjk_compat_variants_fully_decomposed(c: char) -> Option<&'static [char]> {
70
0
    mph_lookup(
71
0
        c.into(),
72
0
        CJK_COMPAT_VARIANTS_DECOMPOSED_SALT,
73
0
        CJK_COMPAT_VARIANTS_DECOMPOSED_KV,
74
0
        pair_lookup_fk,
75
0
        pair_lookup_fv_opt,
76
0
        None,
77
0
    )
78
0
    .map(|(start, len)| &CJK_COMPAT_VARIANTS_DECOMPOSED_CHARS[start as usize..][..len as usize])
79
0
}
80
81
/// Return whether the given character is a combining mark (`General_Category=Mark`)
82
49
pub fn is_combining_mark(c: char) -> bool {
83
49
    mph_lookup(
84
49
        c.into(),
85
49
        COMBINING_MARK_SALT,
86
49
        COMBINING_MARK_KV,
87
49
        bool_lookup_fk,
88
49
        bool_lookup_fv,
89
49
        false,
90
49
    )
91
49
}
92
93
28.9M
pub fn stream_safe_trailing_nonstarters(c: char) -> usize {
94
28.9M
    mph_lookup(
95
28.9M
        c.into(),
96
28.9M
        TRAILING_NONSTARTERS_SALT,
97
28.9M
        TRAILING_NONSTARTERS_KV,
98
28.9M
        u8_lookup_fk,
99
28.9M
        u8_lookup_fv,
100
28.9M
        0,
101
28.9M
    ) as usize
102
28.9M
}
103
104
/// Extract the key in a 24 bit key and 8 bit value packed in a u32.
105
#[inline]
106
894M
fn u8_lookup_fk(kv: u32) -> u32 {
107
894M
    kv >> 8
108
894M
}
109
110
/// Extract the value in a 24 bit key and 8 bit value packed in a u32.
111
#[inline]
112
634M
fn u8_lookup_fv(kv: u32) -> u8 {
113
634M
    (kv & 0xff) as u8
114
634M
}
115
116
/// Extract the key for a boolean lookup.
117
#[inline]
118
49
fn bool_lookup_fk(kv: u32) -> u32 {
119
49
    kv
120
49
}
121
122
/// Extract the value for a boolean lookup.
123
#[inline]
124
1
fn bool_lookup_fv(_kv: u32) -> bool {
125
1
    true
126
1
}
127
128
/// Extract the key in a pair.
129
#[inline]
130
551M
fn pair_lookup_fk<T>(kv: (u32, T)) -> u32 {
131
551M
    kv.0
132
551M
}
unicode_normalization::lookups::pair_lookup_fk::<(u16, u16)>
Line
Count
Source
130
480M
fn pair_lookup_fk<T>(kv: (u32, T)) -> u32 {
131
480M
    kv.0
132
480M
}
unicode_normalization::lookups::pair_lookup_fk::<char>
Line
Count
Source
130
70.5M
fn pair_lookup_fk<T>(kv: (u32, T)) -> u32 {
131
70.5M
    kv.0
132
70.5M
}
133
134
/// Extract the value in a pair, returning an option.
135
#[inline]
136
102M
fn pair_lookup_fv_opt<T>(kv: (u32, T)) -> Option<T> {
137
102M
    Some(kv.1)
138
102M
}
unicode_normalization::lookups::pair_lookup_fv_opt::<(u16, u16)>
Line
Count
Source
136
98.6M
fn pair_lookup_fv_opt<T>(kv: (u32, T)) -> Option<T> {
137
98.6M
    Some(kv.1)
138
98.6M
}
unicode_normalization::lookups::pair_lookup_fv_opt::<char>
Line
Count
Source
136
3.81M
fn pair_lookup_fv_opt<T>(kv: (u32, T)) -> Option<T> {
137
3.81M
    Some(kv.1)
138
3.81M
}