Skip to content

Commit 37b0cf2

Browse files
authored
[prism] Remove more string conversions (#747)
1 parent a037b58 commit 37b0cf2

File tree

1 file changed

+20
-30
lines changed

1 file changed

+20
-30
lines changed

librubyfmt/src/format_prism.rs

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -659,12 +659,8 @@ fn format_string_node<'src>(ps: &mut ParserState<'src>, string_node: prism::Stri
659659

660660
// `opening_loc()` is only `None` in the case of the inner parts of multiline strings
661661
// (e.g. the inner contents of a heredoc)
662-
let opener = string_node
663-
.opening_loc()
664-
.map(|s| loc_to_str(s).trim().to_string());
665-
let closer = string_node
666-
.closing_loc()
667-
.map(|s| loc_to_str(s).trim().to_string());
662+
let opener = string_node.opening_loc().map(|s| loc_to_str(s).trim());
663+
let closer = string_node.closing_loc().map(|s| loc_to_str(s).trim());
668664
let is_heredoc = opener.as_ref().map(|s| s.starts_with("<")).unwrap_or(false);
669665

670666
if is_heredoc {
@@ -695,7 +691,7 @@ fn format_string_node<'src>(ps: &mut ParserState<'src>, string_node: prism::Stri
695691
// a closing loc. In that case, fall back to a double quote, since
696692
// we render character literals as double-quoted string literals
697693
let end_delim = if let Some(ref closer) = closer {
698-
closer.as_str()
694+
closer
699695
} else {
700696
"\""
701697
};
@@ -724,10 +720,10 @@ fn format_interpolated_string_node<'src>(
724720
) {
725721
let opener = interpolated_string_node
726722
.opening_loc()
727-
.map(|s| loc_to_str(s).trim().to_string());
723+
.map(|s| loc_to_str(s).trim());
728724
let closer = interpolated_string_node
729725
.closing_loc()
730-
.map(|s| loc_to_str(s).trim().to_string());
726+
.map(|s| loc_to_str(s).trim());
731727
let is_heredoc = opener.as_ref().map(|s| s.starts_with("<")).unwrap_or(false);
732728
let needs_escape = opener
733729
.as_ref()
@@ -777,7 +773,7 @@ fn format_interpolated_string_node<'src>(
777773
if needs_escape {
778774
ps.emit_double_quote();
779775
} else {
780-
ps.emit_string_content(s.clone());
776+
ps.emit_string_content(s.to_string());
781777
}
782778
}
783779

@@ -801,7 +797,7 @@ fn format_interpolated_string_node<'src>(
801797
let escaped = crate::string_escape::single_to_double_quoted(
802798
content,
803799
opener.as_ref().unwrap(),
804-
closer.as_deref().unwrap_or("\""),
800+
closer.unwrap_or("\""),
805801
);
806802
ps.emit_string_content(escaped);
807803
} else {
@@ -815,7 +811,7 @@ fn format_interpolated_string_node<'src>(
815811
if needs_escape {
816812
ps.emit_double_quote();
817813
} else {
818-
ps.emit_string_content(s.clone());
814+
ps.emit_string_content(s.to_string());
819815
}
820816
}
821817
ps.emit_space();
@@ -833,7 +829,7 @@ fn format_interpolated_string_node<'src>(
833829
if needs_escape {
834830
ps.emit_double_quote();
835831
} else {
836-
ps.emit_string_content(closer.clone());
832+
ps.emit_string_content(closer.to_string());
837833
}
838834
}
839835
}
@@ -2023,7 +2019,7 @@ fn format_unary_operator<'src>(
20232019
fn format_infix_operator<'src>(
20242020
ps: &mut ParserState<'src>,
20252021
left: prism::Node<'src>,
2026-
operator: &str,
2022+
operator: &'src str,
20272023
right: prism::Node<'src>,
20282024
) {
20292025
ps.with_formatting_context(FormattingContext::Binary, |ps| {
@@ -2045,7 +2041,7 @@ fn format_infix_operator<'src>(
20452041
let is_comparison = comparison_operators.iter().any(|o| o == &operator);
20462042

20472043
ps.emit_space();
2048-
ps.emit_ident(operator.to_string());
2044+
ps.emit_ident(operator);
20492045

20502046
if is_comparison {
20512047
// For comparison operators, we always put the right-hand side
@@ -2231,7 +2227,7 @@ fn format_call_body<'src>(
22312227
"." => ps.emit_dot(),
22322228
"&." => ps.emit_lonely_operator(),
22332229
"::" => ps.emit_colon_colon(),
2234-
_ => ps.emit_ident(call_operator.to_string()),
2230+
_ => ps.emit_ident(call_operator),
22352231
}
22362232
}
22372233

@@ -2269,7 +2265,7 @@ fn format_call_body<'src>(
22692265
"." => ps.emit_dot(),
22702266
"&." => ps.emit_lonely_operator(),
22712267
"::" => ps.emit_colon_colon(),
2272-
_ => ps.emit_ident(call_operator.to_string()),
2268+
_ => ps.emit_ident(call_operator),
22732269
}
22742270
}
22752271

@@ -3217,7 +3213,7 @@ fn format_splat_node<'src>(ps: &mut ParserState<'src>, splat_node: prism::SplatN
32173213
}
32183214
}
32193215

3220-
fn format_ident<'src>(ps: &mut ParserState<'src>, ident: impl Into<Cow<'src, str>>, offset: usize) {
3216+
fn format_ident<'src>(ps: &mut ParserState<'src>, ident: &'src str, offset: usize) {
32213217
handle_string_at_offset(ps, ident, offset);
32223218
}
32233219

@@ -3989,7 +3985,7 @@ fn format_constant_path_and_write_node<'src>(
39893985
format_constant_path_write(
39903986
ps,
39913987
constant_path_and_write_node.target(),
3992-
Cow::Borrowed("&&="),
3988+
"&&=",
39933989
constant_path_and_write_node.value(),
39943990
);
39953991
}
@@ -4001,9 +3997,7 @@ fn format_constant_path_operator_write_node<'src>(
40013997
format_constant_path_write(
40023998
ps,
40033999
constant_path_operator_write_node.target(),
4004-
Cow::Borrowed(loc_to_str(
4005-
constant_path_operator_write_node.binary_operator_loc(),
4006-
)),
4000+
loc_to_str(constant_path_operator_write_node.binary_operator_loc()),
40074001
constant_path_operator_write_node.value(),
40084002
);
40094003
}
@@ -4015,7 +4009,7 @@ fn format_constant_path_or_write_node<'src>(
40154009
format_constant_path_write(
40164010
ps,
40174011
constant_path_or_write_node.target(),
4018-
Cow::Borrowed("||="),
4012+
"||=",
40194013
constant_path_or_write_node.value(),
40204014
);
40214015
}
@@ -4047,7 +4041,7 @@ fn format_constant_path_write_node<'src>(
40474041
format_constant_path_write(
40484042
ps,
40494043
constant_path_write_node.target(),
4050-
Cow::Borrowed("="),
4044+
"=",
40514045
constant_path_write_node.value(),
40524046
);
40534047
}
@@ -4066,7 +4060,7 @@ fn format_constant_target_node<'src>(
40664060
fn format_constant_path_write<'src>(
40674061
ps: &mut ParserState<'src>,
40684062
target: prism::ConstantPathNode<'src>,
4069-
op: Cow<'src, str>,
4063+
op: &'src str,
40704064
value: prism::Node<'src>,
40714065
) {
40724066
format_constant_path_node(ps, target);
@@ -4729,11 +4723,7 @@ fn format_yield_node<'src>(ps: &mut ParserState<'src>, yield_node: prism::YieldN
47294723
}
47304724
}
47314725

4732-
fn handle_string_at_offset<'src>(
4733-
ps: &mut ParserState<'src>,
4734-
ident: impl Into<Cow<'src, str>>,
4735-
offset: usize,
4736-
) {
4726+
fn handle_string_at_offset<'src>(ps: &mut ParserState<'src>, ident: &'src str, offset: usize) {
47374727
ps.at_offset(offset);
47384728
ps.emit_ident(ident);
47394729
}

0 commit comments

Comments
 (0)