diff --git a/mono-fmt-macro/src/lib.rs b/mono-fmt-macro/src/lib.rs index a1df169..2c76142 100644 --- a/mono-fmt-macro/src/lib.rs +++ b/mono-fmt-macro/src/lib.rs @@ -5,7 +5,7 @@ use std::cell::Cell; use format::Parse as _; use proc_macro::TokenStream; -use quote::{ToTokens, quote}; +use quote::{quote, ToTokens}; use syn::{ parse::{Parse, ParseStream}, parse_macro_input, Expr, ExprAssign, ExprPath, Ident, LitStr, PathArguments, Result, Token, @@ -77,7 +77,9 @@ fn format_args_impl(input: Input) -> syn::Result { let current_position = Cell::new(0); - Ok(Scoped::new(&input, &fmt_parts, ¤t_position).to_token_stream().into()) + Ok(Scoped::new(&input, &fmt_parts, ¤t_position) + .to_token_stream() + .into()) } #[proc_macro] diff --git a/mono-fmt-macro/src/to_tokens.rs b/mono-fmt-macro/src/to_tokens.rs index a7fb6db..2a8606d 100644 --- a/mono-fmt-macro/src/to_tokens.rs +++ b/mono-fmt-macro/src/to_tokens.rs @@ -5,7 +5,8 @@ use quote::{quote, ToTokens}; use crate::{ format::{ - Align, Count, Format, FormatArg, FormatArgRef, FormatTrait, FormatterArgs, Piece, Sign, + Align, Count, DebugHex, Format, FormatArg, FormatArgRef, FormatTrait, FormatterArgs, Piece, + Sign, }, Input, }; @@ -148,6 +149,14 @@ fn opt_value_tokens(scope: Scoped<'_, FormatterArgs<'_>>) -> TokenStream { opts = quote! { #prefix::WithSignAwareZeroPad(#opts) }; } + if let Some(DebugHex::Lower) = args.debug_hex { + opts = quote! { #prefix::WithDebugLowerHex(#opts) }; + } + + if let Some(DebugHex::Upper) = args.debug_hex { + opts = quote! { #prefix::WithDebugUpperHex(#opts) }; + } + opts } @@ -197,6 +206,14 @@ fn opt_ty_tokens(scope: Scoped<'_, FormatterArgs<'_>>) -> TokenStream { opts = quote! { #prefix::WithSignAwareZeroPad<#opts> }; } + if let Some(DebugHex::Lower) = args.debug_hex { + opts = quote! { #prefix::WithDebugLowerHex<#opts> }; + } + + if let Some(DebugHex::Upper) = args.debug_hex { + opts = quote! { #prefix::WithDebugUpperHex<#opts> }; + } + opts } diff --git a/src/formatter.rs b/src/formatter.rs index 266f7c9..5713211 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -64,7 +64,9 @@ impl Formatter { } } -// BUILDERS +///////////////////////////////////////// +////////////// BUILDERS ///////////////// +///////////////////////////////////////// // adapted from `core` use crate as fmt; diff --git a/src/lib.rs b/src/lib.rs index f7cbb57..f3525e6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -121,4 +121,10 @@ mod tests { let result = format!("a: {}", 32523532u64); assert_eq!(result, "a: 32523532"); } + + #[test] + fn escape() { + let result = format!("a: {{{}}}", 6); + assert_eq!(result, "a: {6}"); + } } diff --git a/src/rust_core_impl/aggregated.rs b/src/rust_core_impl/aggregated.rs index 10024fd..01ba656 100644 --- a/src/rust_core_impl/aggregated.rs +++ b/src/rust_core_impl/aggregated.rs @@ -40,13 +40,12 @@ mod pointers { } } - impl Pointer for &T { fn fmt(&self, f: &mut Formatter) -> Result { Pointer::fmt(&(*self as *const T), f) } } - + impl Pointer for &mut T { fn fmt(&self, f: &mut Formatter) -> Result { Pointer::fmt(&(&**self as *const T), f) @@ -57,6 +56,11 @@ mod pointers { ptr_addr: usize, f: &mut Formatter, ) -> Result { + fn tail(f: &mut Formatter, ptr_addr: usize) -> Result { + let mut f = f.wrap_with(&crate::opts::WithAlternate(())); + LowerHex::fmt(&ptr_addr, &mut f) + } + // The alternate flag is already treated by LowerHex as being special- // it denotes whether to prefix with 0x. We use it to work out whether // or not to zero extend, and then unconditionally set it to get the @@ -69,22 +73,13 @@ mod pointers { let mut f = f.wrap_with(&crate::opts::WithWidth::<(), WIDTH>(())); - let mut f = f.wrap_with(&crate::opts::WithAlternate(())); - let ret = LowerHex::fmt(&ptr_addr, &mut f); - - return ret; + tail(&mut f, ptr_addr) + } else { + tail(&mut f, ptr_addr) } - - let mut f = f.wrap_with(&crate::opts::WithAlternate(())); - let ret = LowerHex::fmt(&ptr_addr, &mut f); - - return ret; + } else { + tail(f, ptr_addr) } - - let mut f = f.wrap_with(&crate::opts::WithAlternate(())); - let ret = LowerHex::fmt(&ptr_addr, &mut f); - - ret } } diff --git a/src/rust_core_impl/mod.rs b/src/rust_core_impl/mod.rs index 11833c7..bbea42f 100644 --- a/src/rust_core_impl/mod.rs +++ b/src/rust_core_impl/mod.rs @@ -247,13 +247,13 @@ impl Formatter { &mut self, padding: usize, default: Alignment, - actual_fill: char, - actual_align: Alignment, + self_fill: char, + self_align: Alignment, ) -> std::result::Result { // WARN: We might have `self` in an invalid state, don't touch `self` opts - let align = match actual_align { + let align = match self_align { Alignment::Unknown => default, - _ => actual_align, + _ => self_align, }; let (pre_pad, post_pad) = match align { @@ -263,10 +263,10 @@ impl Formatter { }; for _ in 0..pre_pad { - self.buf.write_char(actual_fill)?; + self.buf.write_char(self_fill)?; } - Ok(PostPadding::new(actual_fill, post_pad)) + Ok(PostPadding::new(self_fill, post_pad)) } fn write_formatted_parts(&mut self, formatted: &numfmt::Formatted<'_>) -> Result { @@ -355,7 +355,7 @@ impl Formatter { else { let align = Alignment::Left; let post_padding = - self.padding(width - chars_count, Alignment::Right, self.fill(), align)?; + self.padding(width - chars_count, align, self.fill(), self.align())?; self.buf.write_str(s)?; post_padding.write(self) } diff --git a/tests/num.rs b/tests/num.rs index 06f611e..4bc9a41 100644 --- a/tests/num.rs +++ b/tests/num.rs @@ -115,14 +115,23 @@ fn test_format_int_exp_limits() { assert_eq!(format!("{:e}", i32::MAX), "2.147483647e9"); assert_eq!(format!("{:e}", i64::MIN), "-9.223372036854775808e18"); assert_eq!(format!("{:e}", i64::MAX), "9.223372036854775807e18"); - assert_eq!(format!("{:e}", i128::MIN), "-1.70141183460469231731687303715884105728e38"); - assert_eq!(format!("{:e}", i128::MAX), "1.70141183460469231731687303715884105727e38"); + assert_eq!( + format!("{:e}", i128::MIN), + "-1.70141183460469231731687303715884105728e38" + ); + assert_eq!( + format!("{:e}", i128::MAX), + "1.70141183460469231731687303715884105727e38" + ); assert_eq!(format!("{:e}", u8::MAX), "2.55e2"); assert_eq!(format!("{:e}", u16::MAX), "6.5535e4"); assert_eq!(format!("{:e}", u32::MAX), "4.294967295e9"); assert_eq!(format!("{:e}", u64::MAX), "1.8446744073709551615e19"); - assert_eq!(format!("{:e}", u128::MAX), "3.40282366920938463463374607431768211455e38"); + assert_eq!( + format!("{:e}", u128::MAX), + "3.40282366920938463463374607431768211455e38" + ); } #[test] @@ -132,7 +141,10 @@ fn test_format_int_exp_precision() { assert_eq!(format!("{:.10e}", i16::MIN), "-3.2768000000e4"); assert_eq!(format!("{:.10e}", i32::MIN), "-2.1474836480e9"); assert_eq!(format!("{:.20e}", i64::MIN), "-9.22337203685477580800e18"); - assert_eq!(format!("{:.40e}", i128::MIN), "-1.7014118346046923173168730371588410572800e38"); + assert_eq!( + format!("{:.40e}", i128::MIN), + "-1.7014118346046923173168730371588410572800e38" + ); //test rounding assert_eq!(format!("{:.1e}", i8::MIN), "-1.3e2");