From d09f3f2cb9a7c338c2b3e2535d40d1b806ab67c4 Mon Sep 17 00:00:00 2001 From: Reese Williams Date: Mon, 5 Jan 2026 18:54:30 +0100 Subject: [PATCH] Remove yet more string allocations --- librubyfmt/src/format.rs | 16 ++++++++-------- librubyfmt/src/format_prism.rs | 4 ++-- librubyfmt/src/string_escape.rs | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/librubyfmt/src/format.rs b/librubyfmt/src/format.rs index 402d55cf8..215089a7b 100644 --- a/librubyfmt/src/format.rs +++ b/librubyfmt/src/format.rs @@ -1172,16 +1172,16 @@ pub fn format_paren(ps: &mut ParserState, paren: ParenExpr) { } pub fn format_dot2(ps: &mut ParserState, dot2: Dot2) { - format_dot2_or_3(ps, "..".to_string(), dot2.1, dot2.2); + format_dot2_or_3(ps, "..", dot2.1, dot2.2); } pub fn format_dot3(ps: &mut ParserState, dot3: Dot3) { - format_dot2_or_3(ps, "...".to_string(), dot3.1, dot3.2); + format_dot2_or_3(ps, "...", dot3.1, dot3.2); } pub fn format_dot2_or_3( ps: &mut ParserState, - dots: String, + dots: &'static str, left: Option>, right: Option>, ) { @@ -1206,12 +1206,12 @@ pub fn format_dot2_or_3( } } -pub fn percent_symbol_for(tag: String) -> String { +pub fn percent_symbol_for(tag: String) -> &'static str { match tag.as_ref() { - "qsymbols" => "%i".to_string(), - "qwords" => "%w".to_string(), - "symbols" => "%I".to_string(), - "words" => "%W".to_string(), + "qsymbols" => "%i", + "qwords" => "%w", + "symbols" => "%I", + "words" => "%W", _ => panic!("got invalid percent symbol"), } } diff --git a/librubyfmt/src/format_prism.rs b/librubyfmt/src/format_prism.rs index 934564074..4db5ce4cb 100644 --- a/librubyfmt/src/format_prism.rs +++ b/librubyfmt/src/format_prism.rs @@ -2164,7 +2164,7 @@ fn format_call_chain_segments<'src>( // Attr_write values are formatted after the breakable so they break independently if let Some(value) = trailing_attr_write_value { ps.emit_space(); - ps.emit_op("=".to_string()); + ps.emit_op("="); ps.emit_space(); ps.with_start_of_line(false, |ps| format_node(ps, value)); } @@ -2728,7 +2728,7 @@ fn format_array_node<'src>(ps: &mut ParserState<'src>, array_node: prism::ArrayN let orig_delim = opening.and_then(|s| s.chars().nth(2)).unwrap_or('['); if is_word_array { - ps.emit_ident(opening.unwrap().split_at(2).0.to_string()); + ps.emit_ident(opening.unwrap().split_at(2).0); } if array_node.elements().is_empty() { diff --git a/librubyfmt/src/string_escape.rs b/librubyfmt/src/string_escape.rs index 10f0e1d68..5e9f49842 100644 --- a/librubyfmt/src/string_escape.rs +++ b/librubyfmt/src/string_escape.rs @@ -27,7 +27,7 @@ pub fn single_to_double_quoted(content: &str, start_delim: &str, end_delim: &str .replace_all(content, |captures: &fancy_regex::Captures| { // first capture is the entire match let val = captures.get(0).unwrap(); - let val_str = val.as_str().to_string(); + let val_str = val.as_str(); if val_str.ends_with("\"") { // Ends with a quote, which we transform to `\"` format!("{}\\\"", &val_str[0..(val_str.len() - 1)])