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/codes.rs | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) (limited to 'src/util/codes.rs') diff --git a/src/util/codes.rs b/src/util/codes.rs index 9b6ad39..d35d7d9 100644 --- a/src/util/codes.rs +++ b/src/util/codes.rs @@ -5,19 +5,21 @@ use crate::tokenizer::Code; /// Turn a string into codes. pub fn parse(value: &str) -> Vec { - let mut codes: Vec = vec![]; + // Note: It’ll grow a bit bigger with each `Code::VirtualSpace`, smaller + // with `Code::CarriageReturnLineFeed`. + let mut codes: Vec = Vec::with_capacity(value.len()); let mut at_start = true; let mut at_carriage_return = false; let mut column = 1; for char in value.chars() { if at_start { + at_start = false; + if char == '\u{feff}' { // Ignore. continue; } - - at_start = false; } // Send a CRLF. @@ -83,34 +85,33 @@ pub fn parse(value: &str) -> Vec { /// Serialize codes, optionally expanding tabs. pub fn serialize(codes: &[Code], expand_tabs: bool) -> String { let mut at_tab = false; - let mut index = 0; - let mut value: Vec = vec![]; + // Note: It’ll grow a bit smaller with each + // `Code::Char('\t') | Code::VirtualSpace` if `expand_tabs` is false, + // and bigger with `Code::CarriageReturnLineFeed`, + let mut value = String::with_capacity(codes.len()); - while index < codes.len() { - let code = codes[index]; + for code in codes { let mut at_tab_next = false; match code { Code::CarriageReturnLineFeed => { - value.push('\r'); - value.push('\n'); + value.push_str("\r\n"); } - Code::Char(char) if char == '\n' || char == '\r' => { - value.push(char); + Code::Char(char) if *char == '\n' || *char == '\r' => { + value.push(*char); } - Code::Char(char) if char == '\t' => { + Code::Char(char) if *char == '\t' => { at_tab_next = true; - value.push(if expand_tabs { ' ' } else { char }); + value.push(if expand_tabs { ' ' } else { *char }); } Code::VirtualSpace => { if !expand_tabs && at_tab { - index += 1; continue; } value.push(' '); } Code::Char(char) => { - value.push(char); + value.push(*char); } Code::None => { unreachable!("unexpected EOF code in codes"); @@ -118,9 +119,7 @@ pub fn serialize(codes: &[Code], expand_tabs: bool) -> String { } at_tab = at_tab_next; - - index += 1; } - value.into_iter().collect() + value } -- cgit