termwiz: make emoji presentation very slightly more efficient
When resolving the variation, we can pre-compute the base presentation so let's combine that with the data in the perfect hash map.
This commit is contained in:
1 parent
f64a91783e
commit
27df31e0d7
4 files changed
+727
-712
No files matched your search
@@ -5,3 +5,4 @@ edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
phf_codegen = "0.10"
|
||||
ucd-trie = "0.1"
|
||||
@@ -1,5 +1,7 @@
|
||||
const VARIATION_SEQUENCES: &str = include_str!("../../data/emoji-variation-sequences.txt");
|
||||
|
||||
include!("../../src/emoji_presentation.rs");
|
||||
|
||||
/// Parses emoji-variation-sequences.txt, which is part of the UCD download
|
||||
/// for a given version of the Unicode spec.
|
||||
/// It defines which sequences can have explicit presentation selectors.
|
||||
@@ -25,13 +27,19 @@ fn main() {
|
||||
}
|
||||
|
||||
if let Some(last) = last {
|
||||
let first = if EMOJI_PRESENTATION.contains_u32(s.chars().next().unwrap() as u32) {
|
||||
"Presentation::Emoji"
|
||||
} else {
|
||||
"Presentation::Text"
|
||||
};
|
||||
map.entry(
|
||||
s,
|
||||
&format!("({}, {})", first,
|
||||
match last {
|
||||
'\u{FE0F}' => "Presentation::Emoji",
|
||||
'\u{FE0E}' => "Presentation::Text",
|
||||
_ => unreachable!(),
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -44,7 +52,7 @@ fn main() {
|
||||
println!("use crate::emoji::Presentation;");
|
||||
println!();
|
||||
println!(
|
||||
"pub static VARIATION_MAP: phf::Map<&'static str, Presentation> = \n{};\n",
|
||||
"pub static VARIATION_MAP: phf::Map<&'static str, (Presentation, Presentation)> = \n{};\n",
|
||||
map.build(),
|
||||
);
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
use crate::emoji_variation::VARIATION_MAP;
|
||||
#[cfg(feature = "use_serde")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
|
||||
pub enum Presentation {
|
||||
Text,
|
||||
Emoji,
|
||||
@@ -11,6 +14,9 @@ impl Presentation {
|
||||
/// by the explicit presentation if specified
|
||||
/// by a variation selector
|
||||
pub fn for_grapheme(s: &str) -> (Self, Option<Self>) {
|
||||
if let Some((a, b)) = VARIATION_MAP.get(s) {
|
||||
return (*a, Some(*b));
|
||||
}
|
||||
let mut presentation = Self::Text;
|
||||
for c in s.chars() {
|
||||
if Self::for_char(c) == Self::Emoji {
|
||||
@@ -23,7 +29,7 @@ impl Presentation {
|
||||
// change presentation when we identify an
|
||||
// emoji char.
|
||||
}
|
||||
(presentation, VARIATION_MAP.get(s).copied())
|
||||
(presentation, None)
|
||||
}
|
||||
|
||||
pub fn for_char(c: char) -> Self {
|
||||
|
||||
+709
-709
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user