Skip to content

Commit d63a2af

Browse files
Optimize nested find in XML parsing
Replaces multiple redundant evaluations of `.find('>')` with a single evaluation, storing the result in a variable and reusing it. This provides a measurable performance improvement for large strings. Co-authored-by: Tcode-Motion <188012755+Tcode-Motion@users.noreply.github.com>
1 parent 43c970d commit d63a2af

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

‎stdlib/src/xml.rs‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ impl StdlibRegistry {
1919
callback: |_ctx, args| {
2020
let xml = args[0].try_into_string()?;
2121
let mut map = IndexMap::new();
22-
if xml.starts_with('<') && xml.contains('>') {
23-
let tag_name = xml[1..xml.find('>').unwrap_or(1)].to_string();
24-
let close_tag = format!("</{}>", tag_name);
25-
if let Some(close_pos) = xml.find(&close_tag) {
26-
let content = xml[xml.find('>').unwrap() + 1..close_pos].to_string();
27-
map.insert(tag_name, RuntimeValue::Str(content));
22+
if xml.starts_with('<') {
23+
if let Some(gt_pos) = xml.find('>') {
24+
let tag_name = xml[1..gt_pos].to_string();
25+
let close_tag = format!("</{}>", tag_name);
26+
if let Some(close_pos) = xml.find(&close_tag) {
27+
let content = xml[gt_pos + 1..close_pos].to_string();
28+
map.insert(tag_name, RuntimeValue::Str(content));
29+
}
2830
}
2931
}
3032
Ok(RuntimeValue::Map {

0 commit comments

Comments
 (0)