Add hexadecimal formatting of integers with fmt::Debug
This can be used for integers within a larger types which implements Debug
(possibly through derive) but not fmt::UpperHex or fmt::LowerHex.
```rust
assert!(format!("{:02x?}", b"Foo\0") == "[46, 6f, 6f, 00]");
assert!(format!("{:02X?}", b"Foo\0") == "[46, 6F, 6F, 00]");
```
RFC: https://github.com/rust-lang/rfcs/pull/2226
This commit is contained in:
@@ -108,6 +108,10 @@ pub enum Flag {
|
||||
/// For numbers, this means that the number will be padded with zeroes,
|
||||
/// and the sign (`+` or `-`) will precede them.
|
||||
FlagSignAwareZeroPad,
|
||||
/// For Debug / `?`, format integers in lower-case hexadecimal.
|
||||
FlagDebugLowerHex,
|
||||
/// For Debug / `?`, format integers in upper-case hexadecimal.
|
||||
FlagDebugUpperHex,
|
||||
}
|
||||
|
||||
/// A count is used for the precision and width parameters of an integer, and
|
||||
@@ -377,8 +381,22 @@ impl<'a> Parser<'a> {
|
||||
spec.precision = self.count();
|
||||
}
|
||||
}
|
||||
// Finally the actual format specifier
|
||||
if self.consume('?') {
|
||||
// Optional radix followed by the actual format specifier
|
||||
if self.consume('x') {
|
||||
if self.consume('?') {
|
||||
spec.flags |= 1 << (FlagDebugLowerHex as u32);
|
||||
spec.ty = "?";
|
||||
} else {
|
||||
spec.ty = "x";
|
||||
}
|
||||
} else if self.consume('X') {
|
||||
if self.consume('?') {
|
||||
spec.flags |= 1 << (FlagDebugUpperHex as u32);
|
||||
spec.ty = "?";
|
||||
} else {
|
||||
spec.ty = "X";
|
||||
}
|
||||
} else if self.consume('?') {
|
||||
spec.ty = "?";
|
||||
} else {
|
||||
spec.ty = self.word();
|
||||
|
||||
Reference in New Issue
Block a user