mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 06:11:35 +00:00
Reuse results from split_into_parts()
This commit is contained in:
parent
8b03b41b7a
commit
60605a24d4
2 changed files with 9 additions and 21 deletions
|
@ -27,17 +27,15 @@ pub(crate) fn reformat_number_literal(acc: &mut Assists, ctx: &AssistContext) ->
|
||||||
return remove_separators(acc, literal);
|
return remove_separators(acc, literal);
|
||||||
}
|
}
|
||||||
|
|
||||||
let value = literal.str_value();
|
let (prefix, value, suffix) = literal.split_into_parts();
|
||||||
if value.len() < MIN_NUMBER_OF_DIGITS_TO_FORMAT {
|
if value.len() < MIN_NUMBER_OF_DIGITS_TO_FORMAT {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let radix = literal.radix();
|
let radix = literal.radix();
|
||||||
let mut converted = literal.prefix().to_string();
|
let mut converted = prefix.to_string();
|
||||||
converted.push_str(&add_group_separators(literal.str_value(), group_size(radix)));
|
converted.push_str(&add_group_separators(value, group_size(radix)));
|
||||||
if let Some(suffix) = literal.suffix() {
|
|
||||||
converted.push_str(suffix);
|
converted.push_str(suffix);
|
||||||
}
|
|
||||||
|
|
||||||
let group_id = GroupLabel("Reformat number literal".into());
|
let group_id = GroupLabel("Reformat number literal".into());
|
||||||
let label = format!("Convert {} to {}", literal, converted);
|
let label = format!("Convert {} to {}", literal, converted);
|
||||||
|
|
|
@ -613,8 +613,6 @@ impl HasFormatSpecifier for ast::String {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct IntNumberParts<'a>(&'a str, &'a str, &'a str);
|
|
||||||
|
|
||||||
impl ast::IntNumber {
|
impl ast::IntNumber {
|
||||||
pub fn radix(&self) -> Radix {
|
pub fn radix(&self) -> Radix {
|
||||||
match self.text().get(..2).unwrap_or_default() {
|
match self.text().get(..2).unwrap_or_default() {
|
||||||
|
@ -625,7 +623,7 @@ impl ast::IntNumber {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn split_into_parts(&self) -> IntNumberParts {
|
pub fn split_into_parts(&self) -> (&str, &str, &str) {
|
||||||
let radix = self.radix();
|
let radix = self.radix();
|
||||||
let (prefix, mut text) = self.text().split_at(radix.prefix_len());
|
let (prefix, mut text) = self.text().split_at(radix.prefix_len());
|
||||||
|
|
||||||
|
@ -641,25 +639,17 @@ impl ast::IntNumber {
|
||||||
suffix = suffix2;
|
suffix = suffix2;
|
||||||
};
|
};
|
||||||
|
|
||||||
IntNumberParts(prefix, text, suffix)
|
(prefix, text, suffix)
|
||||||
}
|
|
||||||
|
|
||||||
pub fn prefix(&self) -> &str {
|
|
||||||
self.split_into_parts().0
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn str_value(&self) -> &str {
|
|
||||||
self.split_into_parts().1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn value(&self) -> Option<u128> {
|
pub fn value(&self) -> Option<u128> {
|
||||||
let text = self.str_value().replace("_", "");
|
let (_, text, _) = self.split_into_parts();
|
||||||
let value = u128::from_str_radix(&text, self.radix() as u32).ok()?;
|
let value = u128::from_str_radix(&text.replace("_", ""), self.radix() as u32).ok()?;
|
||||||
Some(value)
|
Some(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn suffix(&self) -> Option<&str> {
|
pub fn suffix(&self) -> Option<&str> {
|
||||||
let suffix = self.split_into_parts().2;
|
let (_, _, suffix) = self.split_into_parts();
|
||||||
if suffix.is_empty() {
|
if suffix.is_empty() {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue