From a820d849c3e20a1d72137072d70a7c8e00306f98 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Wed, 20 Jul 2022 17:19:17 +0200 Subject: Refactor to improve allocation around strings --- src/util/normalize_identifier.rs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) (limited to 'src/util/normalize_identifier.rs') diff --git a/src/util/normalize_identifier.rs b/src/util/normalize_identifier.rs index feb7239..42a2bb0 100644 --- a/src/util/normalize_identifier.rs +++ b/src/util/normalize_identifier.rs @@ -32,7 +32,8 @@ /// [definition]: crate::construct::definition /// [label_end]: crate::construct::label_end pub fn normalize_identifier(value: &str) -> String { - let mut codes = vec![]; + // Note: it’ll grow a bit smaller for consecutive whitespace. + let mut result = String::with_capacity(value.len()); let mut at_start = true; let mut at_whitespace = true; @@ -44,10 +45,10 @@ pub fn normalize_identifier(value: &str) -> String { } _ => { if at_whitespace && !at_start { - codes.push(' '); + result.push(' '); } - codes.push(char); + result.push(char); at_start = false; at_whitespace = false; } @@ -66,9 +67,5 @@ pub fn normalize_identifier(value: &str) -> String { // to `SS` (U+0053 U+0053). // If we’d inverse the steps, for `ẞ`, we’d first uppercase without a // change, and then lowercase to `ß`, which would not match `ss`. - codes - .iter() - .collect::() - .to_lowercase() - .to_uppercase() + result.to_lowercase().to_uppercase() } -- cgit