Coverage Report

Created: 2025-11-11 06:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/unicode-normalization/src/lookups.rs
Line
Count
Source
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
1.31G
pub fn canonical_combining_class(c: char) -> u8 {
20
1.31G
    mph_lookup(
21
1.31G
        c.into(),
22
1.31G
        CANONICAL_COMBINING_CLASS_SALT,
23
1.31G
        CANONICAL_COMBINING_CLASS_KV,
24
        u8_lookup_fk,
25
        u8_lookup_fv,
26
        0,
27
    )
28
1.31G
}
29
30
119M
pub(crate) fn composition_table(c1: char, c2: char) -> Option<char> {
31
119M
    if c1 < '\u{10000}' && c2 < '\u{10000}' {
32
119M
        mph_lookup(
33
119M
            (c1 as u32) << 16 | (c2 as u32),
34
119M
            COMPOSITION_TABLE_SALT,
35
119M
            COMPOSITION_TABLE_KV,
36
            pair_lookup_fk,
37
            pair_lookup_fv_opt,
38
119M
            None,
39
        )
40
    } else {
41
120k
        composition_table_astral(c1, c2)
42
    }
43
119M
}
44
45
438M
pub(crate) fn canonical_fully_decomposed(c: char) -> Option<&'static [char]> {
46
438M
    mph_lookup(
47
438M
        c.into(),
48
438M
        CANONICAL_DECOMPOSED_SALT,
49
438M
        CANONICAL_DECOMPOSED_KV,
50
        pair_lookup_fk,
51
        pair_lookup_fv_opt,
52
438M
        None,
53
    )
54
438M
    .map(|(start, len)| &CANONICAL_DECOMPOSED_CHARS[start as usize..][..len as usize])
55
438M
}
56
57
338M
pub(crate) fn compatibility_fully_decomposed(c: char) -> Option<&'static [char]> {
58
338M
    mph_lookup(
59
338M
        c.into(),
60
338M
        COMPATIBILITY_DECOMPOSED_SALT,
61
338M
        COMPATIBILITY_DECOMPOSED_KV,
62
        pair_lookup_fk,
63
        pair_lookup_fv_opt,
64
338M
        None,
65
    )
66
338M
    .map(|(start, len)| &COMPATIBILITY_DECOMPOSED_CHARS[start as usize..][..len as usize])
67
338M
}
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
        pair_lookup_fk,
75
        pair_lookup_fv_opt,
76
0
        None,
77
    )
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
50
pub fn is_combining_mark(c: char) -> bool {
83
50
    mph_lookup(
84
50
        c.into(),
85
50
        COMBINING_MARK_SALT,
86
50
        COMBINING_MARK_KV,
87
        bool_lookup_fk,
88
        bool_lookup_fv,
89
        false,
90
    )
91
50
}
92
93
46.4M
pub fn stream_safe_trailing_nonstarters(c: char) -> usize {
94
46.4M
    mph_lookup(
95
46.4M
        c.into(),
96
46.4M
        TRAILING_NONSTARTERS_SALT,
97
46.4M
        TRAILING_NONSTARTERS_KV,
98
46.4M
        u8_lookup_fk,
99
46.4M
        u8_lookup_fv,
100
46.4M
        0,
101
46.4M
    ) as usize
102
46.4M
}
103
104
/// Extract the key in a 24 bit key and 8 bit value packed in a u32.
105
#[inline]
106
1.36G
fn u8_lookup_fk(kv: u32) -> u32 {
107
1.36G
    kv >> 8
108
1.36G
}
109
110
/// Extract the value in a 24 bit key and 8 bit value packed in a u32.
111
#[inline]
112
963M
fn u8_lookup_fv(kv: u32) -> u8 {
113
963M
    (kv & 0xff) as u8
114
963M
}
115
116
/// Extract the key for a boolean lookup.
117
#[inline]
118
50
fn bool_lookup_fk(kv: u32) -> u32 {
119
50
    kv
120
50
}
121
122
/// Extract the value for a boolean lookup.
123
#[inline]
124
0
fn bool_lookup_fv(_kv: u32) -> bool {
125
0
    true
126
0
}
127
128
/// Extract the key in a pair.
129
#[inline]
130
896M
fn pair_lookup_fk<T>(kv: (u32, T)) -> u32 {
131
896M
    kv.0
132
896M
}
unicode_normalization::lookups::pair_lookup_fk::<(u16, u16)>
Line
Count
Source
130
776M
fn pair_lookup_fk<T>(kv: (u32, T)) -> u32 {
131
776M
    kv.0
132
776M
}
unicode_normalization::lookups::pair_lookup_fk::<char>
Line
Count
Source
130
119M
fn pair_lookup_fk<T>(kv: (u32, T)) -> u32 {
131
119M
    kv.0
132
119M
}
133
134
/// Extract the value in a pair, returning an option.
135
#[inline]
136
142M
fn pair_lookup_fv_opt<T>(kv: (u32, T)) -> Option<T> {
137
142M
    Some(kv.1)
138
142M
}
unicode_normalization::lookups::pair_lookup_fv_opt::<(u16, u16)>
Line
Count
Source
136
132M
fn pair_lookup_fv_opt<T>(kv: (u32, T)) -> Option<T> {
137
132M
    Some(kv.1)
138
132M
}
unicode_normalization::lookups::pair_lookup_fv_opt::<char>
Line
Count
Source
136
9.91M
fn pair_lookup_fv_opt<T>(kv: (u32, T)) -> Option<T> {
137
9.91M
    Some(kv.1)
138
9.91M
}