@@ -52,6 +52,8 @@ fn generate_entities_rs(entities: &[(String, String)]) {
5252 /// Entity | Codepoints | Glyph\n \
5353 /// -------------------------------|--------------------|------") . unwrap ( ) ;
5454
55+ let mut hashify = String :: new ( ) ;
56+
5557 let mut map_builder = phf_codegen:: Map :: < & [ u8 ] > :: new ( ) ;
5658 let mut max_len: usize = 0 ;
5759 let mut min_len: usize = usize:: MAX ;
@@ -60,6 +62,15 @@ fn generate_entities_rs(entities: &[(String, String)]) {
6062 max_len = max ( max_len, name. len ( ) ) ;
6163 min_len = min ( min_len, name. len ( ) ) ;
6264
65+ {
66+ use std:: fmt:: Write ;
67+ write ! ( & mut hashify, "\n b\" {name}\" => &[" ) . unwrap ( ) ;
68+ for & byte in glyph. as_bytes ( ) {
69+ write ! ( & mut hashify, "{byte}," ) . unwrap ( ) ;
70+ }
71+ write ! ( & mut hashify, "]," ) . unwrap ( ) ;
72+ }
73+
6374 // `{:28}` would pad the output inside the backticks.
6475 let name = format ! ( "`{name}`" ) ;
6576
@@ -84,15 +95,23 @@ fn generate_entities_rs(entities: &[(String, String)]) {
8495 let map = map_builder. build ( ) ;
8596 writeln ! (
8697 out,
87- "\
88- #[allow(clippy::unreadable_literal)]\n \
89- pub static ENTITIES: phf::Map<&[u8], &[u8]> = {map};\n \
90- \n \
91- /// Length of longest entity including ‘&’ and possibly ‘;’.\n \
92- pub const ENTITY_MAX_LENGTH: usize = {max_len};\n \
93- \n \
94- /// Length of shortest entity including ‘&’ and possibly ‘;’.\n \
95- pub const ENTITY_MIN_LENGTH: usize = {min_len};"
98+ r#"#[allow(clippy::unreadable_literal)]
99+ pub static ENTITIES: phf::Map<&[u8], &[u8]> = {map};
100+
101+ /// Length of longest entity including ‘&’ and possibly ‘;’.
102+ pub const ENTITY_MAX_LENGTH: usize = {max_len};
103+
104+ /// Length of shortest entity including ‘&’ and possibly ‘;’.
105+ pub const ENTITY_MIN_LENGTH: usize = {min_len};
106+
107+ /// Get an unescaped character by its HTML entity
108+ pub(crate) fn get_entity(candidate: &[u8]) -> Option<&[u8]> {{
109+ hashify::map! {{
110+ candidate,
111+ &[u8],{hashify}
112+ }}
113+ }}
114+ "#
96115 )
97116 . unwrap ( ) ;
98117}
0 commit comments