about summary refs log tree commit diff
diff options
context:
space:
mode:
authorOleg Matrokhin <matrokhin@gmail.com>2021-12-13 21:16:04 +0300
committerOleg Matrokhin <matrokhin@gmail.com>2021-12-13 21:16:04 +0300
commit60605a24d404ada4fdfd040ac924926cd699415b (patch)
treead45ac042749e82a853436d2991a879ebbda1006
parent8b03b41b7a4a4b90b5d1306794fd964a44c90fdc (diff)
downloadrust-60605a24d404ada4fdfd040ac924926cd699415b.tar.gz
rust-60605a24d404ada4fdfd040ac924926cd699415b.zip
Reuse results from split_into_parts()
-rw-r--r--crates/ide_assists/src/handlers/number_representation.rs10
-rw-r--r--crates/syntax/src/ast/token_ext.rs20
2 files changed, 9 insertions, 21 deletions
diff --git a/crates/ide_assists/src/handlers/number_representation.rs b/crates/ide_assists/src/handlers/number_representation.rs
index d1ca1ad1c80..df017636631 100644
--- a/crates/ide_assists/src/handlers/number_representation.rs
+++ b/crates/ide_assists/src/handlers/number_representation.rs
@@ -27,17 +27,15 @@ pub(crate) fn reformat_number_literal(acc: &mut Assists, ctx: &AssistContext) ->
         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 {
         return None;
     }
 
     let radix = literal.radix();
-    let mut converted = literal.prefix().to_string();
-    converted.push_str(&add_group_separators(literal.str_value(), group_size(radix)));
-    if let Some(suffix) = literal.suffix() {
-        converted.push_str(suffix);
-    }
+    let mut converted = prefix.to_string();
+    converted.push_str(&add_group_separators(value, group_size(radix)));
+    converted.push_str(suffix);
 
     let group_id = GroupLabel("Reformat number literal".into());
     let label = format!("Convert {} to {}", literal, converted);
diff --git a/crates/syntax/src/ast/token_ext.rs b/crates/syntax/src/ast/token_ext.rs
index 2465e4a3a31..c32ab686cca 100644
--- a/crates/syntax/src/ast/token_ext.rs
+++ b/crates/syntax/src/ast/token_ext.rs
@@ -613,8 +613,6 @@ impl HasFormatSpecifier for ast::String {
     }
 }
 
-struct IntNumberParts<'a>(&'a str, &'a str, &'a str);
-
 impl ast::IntNumber {
     pub fn radix(&self) -> Radix {
         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 (prefix, mut text) = self.text().split_at(radix.prefix_len());
 
@@ -641,25 +639,17 @@ impl ast::IntNumber {
             suffix = suffix2;
         };
 
-        IntNumberParts(prefix, text, suffix)
-    }
-
-    pub fn prefix(&self) -> &str {
-        self.split_into_parts().0
-    }
-
-    pub fn str_value(&self) -> &str {
-        self.split_into_parts().1
+        (prefix, text, suffix)
     }
 
     pub fn value(&self) -> Option<u128> {
-        let text = self.str_value().replace("_", "");
-        let value = u128::from_str_radix(&text, self.radix() as u32).ok()?;
+        let (_, text, _) = self.split_into_parts();
+        let value = u128::from_str_radix(&text.replace("_", ""), self.radix() as u32).ok()?;
         Some(value)
     }
 
     pub fn suffix(&self) -> Option<&str> {
-        let suffix = self.split_into_parts().2;
+        let (_, _, suffix) = self.split_into_parts();
         if suffix.is_empty() {
             None
         } else {