Skip to content

Commit

Permalink
xml: Cache regex in custom_unescape
Browse files Browse the repository at this point in the history
  • Loading branch information
adrian17 authored and torokati44 committed Sep 10, 2024
1 parent fd61be2 commit aded790
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion core/src/xml/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -555,6 +556,8 @@ impl<'gc> fmt::Debug for XmlNode<'gc> {
}
}

static ENTITY_REGEX: OnceLock<Regex> = OnceLock::new();

/// Handles flash-specific XML unescaping behavior.
/// We accept all XML entities, and also accept standalone '&' without
/// a corresponding ';'
Expand All @@ -564,7 +567,7 @@ pub fn custom_unescape(
) -> Result<String, quick_xml::Error> {
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;

Expand Down

0 comments on commit aded790

Please sign in to comment.