This commit is contained in:
nora 2022-09-24 22:05:13 +02:00
parent 010126ce0b
commit 8862186a1f
7 changed files with 65 additions and 31 deletions

View file

@ -64,7 +64,9 @@ impl<W, O: FmtOpts> Formatter<W, O> {
}
}
// BUILDERS
/////////////////////////////////////////
////////////// BUILDERS /////////////////
/////////////////////////////////////////
// adapted from `core`
use crate as fmt;

View file

@ -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}");
}
}

View file

@ -40,13 +40,12 @@ mod pointers {
}
}
impl<T: ?Sized> Pointer for &T {
fn fmt<W: Write, O: FmtOpts>(&self, f: &mut Formatter<W, O>) -> Result {
Pointer::fmt(&(*self as *const T), f)
}
}
impl<T: ?Sized> Pointer for &mut T {
fn fmt<W: Write, O: FmtOpts>(&self, f: &mut Formatter<W, O>) -> Result {
Pointer::fmt(&(&**self as *const T), f)
@ -57,6 +56,11 @@ mod pointers {
ptr_addr: usize,
f: &mut Formatter<W, O>,
) -> Result {
fn tail<W: Write, O: FmtOpts>(f: &mut Formatter<W, O>, 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
}
}

View file

@ -247,13 +247,13 @@ impl<W: Write, O: FmtOpts> Formatter<W, O> {
&mut self,
padding: usize,
default: Alignment,
actual_fill: char,
actual_align: Alignment,
self_fill: char,
self_align: Alignment,
) -> std::result::Result<PostPadding, Error> {
// 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<W: Write, O: FmtOpts> Formatter<W, O> {
};
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<W: Write, O: FmtOpts> Formatter<W, O> {
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)
}