From aded7906728a83f54d29f9d4c0957b6d2e111158 Mon Sep 17 00:00:00 2001 From: Adrian Wielgosik Date: Tue, 10 Sep 2024 17:49:38 +0200 Subject: [PATCH] xml: Cache regex in custom_unescape --- core/src/xml/tree.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/core/src/xml/tree.rs b/core/src/xml/tree.rs index 8c03295e6..74fb8f477 100644 --- a/core/src/xml/tree.rs +++ b/core/src/xml/tree.rs @@ -10,6 +10,7 @@ use quick_xml::escape::escape; use quick_xml::events::BytesStart; use regress::Regex; use std::fmt; +use std::sync::OnceLock; pub const ELEMENT_NODE: u8 = 1; pub const TEXT_NODE: u8 = 3; @@ -555,6 +556,8 @@ impl<'gc> fmt::Debug for XmlNode<'gc> { } } +static ENTITY_REGEX: OnceLock = OnceLock::new(); + /// Handles flash-specific XML unescaping behavior. /// We accept all XML entities, and also accept standalone '&' without /// a corresponding ';' @@ -564,7 +567,7 @@ pub fn custom_unescape( ) -> Result { let input = decoder.decode(data)?; - let re = Regex::new(r"&[^;]*;").unwrap(); + let re = ENTITY_REGEX.get_or_init(|| Regex::new(r"&[^;]*;").unwrap()); let mut result = String::new(); let mut last_end = 0;