diff options
author | Titus Wormer <tituswormer@gmail.com> | 2022-07-20 17:19:17 +0200 |
---|---|---|
committer | Titus Wormer <tituswormer@gmail.com> | 2022-07-20 17:19:17 +0200 |
commit | a820d849c3e20a1d72137072d70a7c8e00306f98 (patch) | |
tree | c2916ab31d6d481e0b53a06aa9b95dfcddd4163f /src/util/normalize_identifier.rs | |
parent | 7894ec75a7070591c3499fce1f409563c4edc7d7 (diff) | |
download | markdown-rs-a820d849c3e20a1d72137072d70a7c8e00306f98.tar.gz markdown-rs-a820d849c3e20a1d72137072d70a7c8e00306f98.tar.bz2 markdown-rs-a820d849c3e20a1d72137072d70a7c8e00306f98.zip |
Refactor to improve allocation around strings
Diffstat (limited to '')
-rw-r--r-- | src/util/normalize_identifier.rs | 13 |
1 files changed, 5 insertions, 8 deletions
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::<String>() - .to_lowercase() - .to_uppercase() + result.to_lowercase().to_uppercase() } |