Skip to content

Commit 60605a2

Browse files
committed
Reuse results from split_into_parts()
1 parent 8b03b41 commit 60605a2

File tree

2 files changed

+9
-21
lines changed

2 files changed

+9
-21
lines changed

crates/ide_assists/src/handlers/number_representation.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,15 @@ pub(crate) fn reformat_number_literal(acc: &mut Assists, ctx: &AssistContext) ->
2727
return remove_separators(acc, literal);
2828
}
2929

30-
let value = literal.str_value();
30+
let (prefix, value, suffix) = literal.split_into_parts();
3131
if value.len() < MIN_NUMBER_OF_DIGITS_TO_FORMAT {
3232
return None;
3333
}
3434

3535
let radix = literal.radix();
36-
let mut converted = literal.prefix().to_string();
37-
converted.push_str(&add_group_separators(literal.str_value(), group_size(radix)));
38-
if let Some(suffix) = literal.suffix() {
39-
converted.push_str(suffix);
40-
}
36+
let mut converted = prefix.to_string();
37+
converted.push_str(&add_group_separators(value, group_size(radix)));
38+
converted.push_str(suffix);
4139

4240
let group_id = GroupLabel("Reformat number literal".into());
4341
let label = format!("Convert {} to {}", literal, converted);

crates/syntax/src/ast/token_ext.rs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -613,8 +613,6 @@ impl HasFormatSpecifier for ast::String {
613613
}
614614
}
615615

616-
struct IntNumberParts<'a>(&'a str, &'a str, &'a str);
617-
618616
impl ast::IntNumber {
619617
pub fn radix(&self) -> Radix {
620618
match self.text().get(..2).unwrap_or_default() {
@@ -625,7 +623,7 @@ impl ast::IntNumber {
625623
}
626624
}
627625

628-
fn split_into_parts(&self) -> IntNumberParts {
626+
pub fn split_into_parts(&self) -> (&str, &str, &str) {
629627
let radix = self.radix();
630628
let (prefix, mut text) = self.text().split_at(radix.prefix_len());
631629

@@ -641,25 +639,17 @@ impl ast::IntNumber {
641639
suffix = suffix2;
642640
};
643641

644-
IntNumberParts(prefix, text, suffix)
645-
}
646-
647-
pub fn prefix(&self) -> &str {
648-
self.split_into_parts().0
649-
}
650-
651-
pub fn str_value(&self) -> &str {
652-
self.split_into_parts().1
642+
(prefix, text, suffix)
653643
}
654644

655645
pub fn value(&self) -> Option<u128> {
656-
let text = self.str_value().replace("_", "");
657-
let value = u128::from_str_radix(&text, self.radix() as u32).ok()?;
646+
let (_, text, _) = self.split_into_parts();
647+
let value = u128::from_str_radix(&text.replace("_", ""), self.radix() as u32).ok()?;
658648
Some(value)
659649
}
660650

661651
pub fn suffix(&self) -> Option<&str> {
662-
let suffix = self.split_into_parts().2;
652+
let (_, _, suffix) = self.split_into_parts();
663653
if suffix.is_empty() {
664654
None
665655
} else {

0 commit comments

Comments
 (0)