/rust/registry/src/index.crates.io-1949cf8c6b5b557f/unicode-width-0.2.3/src/width_info.rs
Line | Count | Source |
1 | | // Copyright 2012-2026 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 | | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
12 | | pub(crate) struct WidthInfo(u16); |
13 | | |
14 | | #[path = "gen/width_info.rs"] |
15 | | mod gen; |
16 | | |
17 | | const LIGATURE_TRANSPARENT_MASK: u16 = 0b0010_0000_0000_0000; |
18 | | |
19 | | impl WidthInfo { |
20 | | // Additional constants are in the `gen` module. |
21 | | |
22 | | /// Whether this width mode is ligature_transparent |
23 | | /// (has 5th MSB set.) |
24 | 0 | pub(crate) fn is_ligature_transparent(self) -> bool { |
25 | 0 | (self.0 & 0b0000_1000_0000_0000) == 0b0000_1000_0000_0000 |
26 | 0 | } |
27 | | |
28 | | /// Sets 6th MSB. |
29 | 0 | pub(crate) fn set_zwj_bit(self) -> Self { |
30 | 0 | Self(self.0 | 0b0000_0100_0000_0000) |
31 | 0 | } |
32 | | |
33 | | /// Has top bit set |
34 | 0 | pub(crate) fn is_emoji_presentation(self) -> bool { |
35 | 0 | (self.0 & WidthInfo::VARIATION_SELECTOR_16.0) == WidthInfo::VARIATION_SELECTOR_16.0 |
36 | 0 | } |
37 | | |
38 | 0 | pub(crate) fn is_zwj_emoji_presentation(self) -> bool { |
39 | 0 | (self.0 & 0b1011_0000_0000_0000) == 0b1001_0000_0000_0000 |
40 | 0 | } |
41 | | |
42 | | /// Set top bit |
43 | 0 | pub(crate) fn set_emoji_presentation(self) -> Self { |
44 | 0 | if (self.0 & LIGATURE_TRANSPARENT_MASK) == LIGATURE_TRANSPARENT_MASK |
45 | 0 | || (self.0 & 0b1001_0000_0000_0000) == 0b0001_0000_0000_0000 |
46 | | { |
47 | 0 | Self( |
48 | 0 | self.0 |
49 | 0 | | WidthInfo::VARIATION_SELECTOR_16.0 |
50 | 0 | & !WidthInfo::VARIATION_SELECTOR_15.0 |
51 | 0 | & !WidthInfo::VARIATION_SELECTOR_1_2_OR_3.0, |
52 | 0 | ) |
53 | | } else { |
54 | 0 | Self::VARIATION_SELECTOR_16 |
55 | | } |
56 | 0 | } |
57 | | |
58 | | /// Clear top bit |
59 | 0 | pub(crate) fn unset_emoji_presentation(self) -> Self { |
60 | 0 | if (self.0 & LIGATURE_TRANSPARENT_MASK) == LIGATURE_TRANSPARENT_MASK { |
61 | 0 | Self(self.0 & !WidthInfo::VARIATION_SELECTOR_16.0) |
62 | | } else { |
63 | 0 | Self::DEFAULT |
64 | | } |
65 | 0 | } |
66 | | |
67 | | /// Has 2nd bit set |
68 | 0 | pub(crate) fn is_text_presentation(self) -> bool { |
69 | 0 | (self.0 & WidthInfo::VARIATION_SELECTOR_15.0) == WidthInfo::VARIATION_SELECTOR_15.0 |
70 | 0 | } |
71 | | |
72 | | /// Set 2nd bit |
73 | 0 | pub(crate) fn set_text_presentation(self) -> Self { |
74 | 0 | if (self.0 & LIGATURE_TRANSPARENT_MASK) == LIGATURE_TRANSPARENT_MASK { |
75 | 0 | Self( |
76 | 0 | self.0 |
77 | 0 | | WidthInfo::VARIATION_SELECTOR_15.0 |
78 | 0 | & !WidthInfo::VARIATION_SELECTOR_16.0 |
79 | 0 | & !WidthInfo::VARIATION_SELECTOR_1_2_OR_3.0, |
80 | 0 | ) |
81 | | } else { |
82 | 0 | Self(WidthInfo::VARIATION_SELECTOR_15.0) |
83 | | } |
84 | 0 | } |
85 | | |
86 | | /// Clear 2nd bit |
87 | 0 | pub(crate) fn unset_text_presentation(self) -> Self { |
88 | 0 | Self(self.0 & !WidthInfo::VARIATION_SELECTOR_15.0) |
89 | 0 | } |
90 | | |
91 | | /// Has 7th bit set |
92 | 0 | pub(crate) fn is_vs1_2_3(self) -> bool { |
93 | 0 | (self.0 & WidthInfo::VARIATION_SELECTOR_1_2_OR_3.0) |
94 | 0 | == WidthInfo::VARIATION_SELECTOR_1_2_OR_3.0 |
95 | 0 | } |
96 | | |
97 | | /// Set 7th bit |
98 | 0 | pub(crate) fn set_vs1_2_3(self) -> Self { |
99 | 0 | if (self.0 & LIGATURE_TRANSPARENT_MASK) == LIGATURE_TRANSPARENT_MASK { |
100 | 0 | Self( |
101 | 0 | self.0 |
102 | 0 | | WidthInfo::VARIATION_SELECTOR_1_2_OR_3.0 |
103 | 0 | & !WidthInfo::VARIATION_SELECTOR_15.0 |
104 | 0 | & !WidthInfo::VARIATION_SELECTOR_16.0, |
105 | 0 | ) |
106 | | } else { |
107 | 0 | Self(WidthInfo::VARIATION_SELECTOR_1_2_OR_3.0) |
108 | | } |
109 | 0 | } |
110 | | |
111 | | /// Clear 7th bit |
112 | 0 | pub(crate) fn unset_vs1_2_3(self) -> Self { |
113 | 0 | Self(self.0 & !WidthInfo::VARIATION_SELECTOR_1_2_OR_3.0) |
114 | 0 | } |
115 | | } |