From 3be1d8ca7d9d9df60a38106d6c8f5d12597cbafc Mon Sep 17 00:00:00 2001 From: Michael Woerister Date: Mon, 23 Nov 2015 15:59:36 +0100 Subject: Avoid some code duplication around getting names of numeric types. --- src/libsyntax/ast.rs | 4 ++-- src/libsyntax/ast_util.rs | 42 ++++++++++++++++++------------------------ src/libsyntax/print/pprust.rs | 6 +++--- 3 files changed, 23 insertions(+), 29 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index e4697a7fd91..ab62c8d9421 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -1258,7 +1258,7 @@ impl fmt::Debug for IntTy { impl fmt::Display for IntTy { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", ast_util::int_ty_to_string(*self, None)) + write!(f, "{}", ast_util::int_ty_to_string(*self)) } } @@ -1303,7 +1303,7 @@ impl fmt::Debug for UintTy { impl fmt::Display for UintTy { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", ast_util::uint_ty_to_string(*self, None)) + write!(f, "{}", ast_util::uint_ty_to_string(*self)) } } diff --git a/src/libsyntax/ast_util.rs b/src/libsyntax/ast_util.rs index 44334762d90..489c61b83da 100644 --- a/src/libsyntax/ast_util.rs +++ b/src/libsyntax/ast_util.rs @@ -111,26 +111,23 @@ pub fn is_path(e: P) -> bool { match e.node { ExprPath(..) => true, _ => false } } -/// Get a string representation of a signed int type, with its value. -/// We want to avoid "45int" and "-3int" in favor of "45" and "-3" -pub fn int_ty_to_string(t: IntTy, val: Option) -> String { - let s = match t { +pub fn int_ty_to_string(t: IntTy) -> &'static str { + match t { TyIs => "isize", TyI8 => "i8", TyI16 => "i16", TyI32 => "i32", TyI64 => "i64" - }; - - match val { - // cast to a u64 so we can correctly print INT64_MIN. All integral types - // are parsed as u64, so we wouldn't want to print an extra negative - // sign. - Some(n) => format!("{}{}", n as u64, s), - None => s.to_string() } } +pub fn int_val_to_string(t: IntTy, val: i64) -> String { + // cast to a u64 so we can correctly print INT64_MIN. All integral types + // are parsed as u64, so we wouldn't want to print an extra negative + // sign. + format!("{}{}", val as u64, int_ty_to_string(t)) +} + pub fn int_ty_max(t: IntTy) -> u64 { match t { TyI8 => 0x80, @@ -140,23 +137,20 @@ pub fn int_ty_max(t: IntTy) -> u64 { } } -/// Get a string representation of an unsigned int type, with its value. -/// We want to avoid "42u" in favor of "42us". "42uint" is right out. -pub fn uint_ty_to_string(t: UintTy, val: Option) -> String { - let s = match t { +pub fn uint_ty_to_string(t: UintTy) -> &'static str { + match t { TyUs => "usize", TyU8 => "u8", TyU16 => "u16", TyU32 => "u32", TyU64 => "u64" - }; - - match val { - Some(n) => format!("{}{}", n, s), - None => s.to_string() } } +pub fn uint_val_to_string(t: UintTy, val: u64) -> String { + format!("{}{}", val, uint_ty_to_string(t)) +} + pub fn uint_ty_max(t: UintTy) -> u64 { match t { TyU8 => 0xff, @@ -166,10 +160,10 @@ pub fn uint_ty_max(t: UintTy) -> u64 { } } -pub fn float_ty_to_string(t: FloatTy) -> String { +pub fn float_ty_to_string(t: FloatTy) -> &'static str { match t { - TyF32 => "f32".to_string(), - TyF64 => "f64".to_string(), + TyF32 => "f32", + TyF64 => "f64", } } diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index e9c716017c0..6de86de9c54 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -651,15 +651,15 @@ pub trait PrintState<'a> { match t { ast::SignedIntLit(st, ast::Plus) => { word(self.writer(), - &ast_util::int_ty_to_string(st, Some(i as i64))) + &ast_util::int_val_to_string(st, i as i64)) } ast::SignedIntLit(st, ast::Minus) => { - let istr = ast_util::int_ty_to_string(st, Some(-(i as i64))); + let istr = ast_util::int_val_to_string(st, -(i as i64)); word(self.writer(), &format!("-{}", istr)) } ast::UnsignedIntLit(ut) => { - word(self.writer(), &ast_util::uint_ty_to_string(ut, Some(i))) + word(self.writer(), &ast_util::uint_val_to_string(ut, i)) } ast::UnsuffixedIntLit(ast::Plus) => { word(self.writer(), &format!("{}", i)) -- cgit 1.4.1-3-g733a5