Skip to content

File tree

5 files changed

+42
-4
lines changed

5 files changed

+42
-4
lines changed
 

‎src/liballoc/fmt.rs

+2
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@
113113
//!
114114
//! * *nothing* ⇒ [`Display`]
115115
//! * `?` ⇒ [`Debug`]
116+
//! * `x?` ⇒ [`Debug`] with lower-case hexadecimal integers
117+
//! * `X?` ⇒ [`Debug`] with lower-case hexadecimal integers
116118
//! * `o` ⇒ [`Octal`](trait.Octal.html)
117119
//! * `x` ⇒ [`LowerHex`](trait.LowerHex.html)
118120
//! * `X` ⇒ [`UpperHex`](trait.UpperHex.html)

‎src/libcore/fmt/mod.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ impl<'a> ArgumentV1<'a> {
333333

334334
// flags available in the v1 format of format_args
335335
#[derive(Copy, Clone)]
336-
enum FlagV1 { SignPlus, SignMinus, Alternate, SignAwareZeroPad, }
336+
enum FlagV1 { SignPlus, SignMinus, Alternate, SignAwareZeroPad, DebugLowerHex, DebugUpperHex }
337337

338338
impl<'a> Arguments<'a> {
339339
/// When using the format_args!() macro, this function is used to generate the
@@ -1401,6 +1401,12 @@ impl<'a> Formatter<'a> {
14011401
self.flags & (1 << FlagV1::SignAwareZeroPad as u32) != 0
14021402
}
14031403

1404+
// FIXME: Decide what public API we want for these two flags.
1405+
// https://github.com/rust-lang/rust/issues/48584
1406+
fn debug_lower_hex(&self) -> bool { self.flags & (1 << FlagV1::DebugLowerHex as u32) != 0 }
1407+
1408+
fn debug_upper_hex(&self) -> bool { self.flags & (1 << FlagV1::DebugUpperHex as u32) != 0 }
1409+
14041410
/// Creates a [`DebugStruct`] builder designed to assist with creation of
14051411
/// [`fmt::Debug`] implementations for structs.
14061412
///

‎src/libcore/fmt/num.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,13 @@ macro_rules! debug {
159159
impl fmt::Debug for $T {
160160
#[inline]
161161
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
162-
fmt::Display::fmt(self, f)
162+
if f.debug_lower_hex() {
163+
fmt::LowerHex::fmt(self, f)
164+
} else if f.debug_upper_hex() {
165+
fmt::UpperHex::fmt(self, f)
166+
} else {
167+
fmt::Display::fmt(self, f)
168+
}
163169
}
164170
}
165171
}

‎src/libcore/tests/fmt/num.rs

+6
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,9 @@ fn test_format_int_twos_complement() {
150150
assert!(format!("{}", i32::MIN) == "-2147483648");
151151
assert!(format!("{}", i64::MIN) == "-9223372036854775808");
152152
}
153+
154+
#[test]
155+
fn test_format_debug_hex() {
156+
assert!(format!("{:02x?}", b"Foo\0") == "[46, 6f, 6f, 00]");
157+
assert!(format!("{:02X?}", b"Foo\0") == "[46, 6F, 6F, 00]");
158+
}

‎src/libfmt_macros/lib.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ pub enum Flag {
108108
/// For numbers, this means that the number will be padded with zeroes,
109109
/// and the sign (`+` or `-`) will precede them.
110110
FlagSignAwareZeroPad,
111+
/// For Debug / `?`, format integers in lower-case hexadecimal.
112+
FlagDebugLowerHex,
113+
/// For Debug / `?`, format integers in upper-case hexadecimal.
114+
FlagDebugUpperHex,
111115
}
112116

113117
/// A count is used for the precision and width parameters of an integer, and
@@ -377,8 +381,22 @@ impl<'a> Parser<'a> {
377381
spec.precision = self.count();
378382
}
379383
}
380-
// Finally the actual format specifier
381-
if self.consume('?') {
384+
// Optional radix followed by the actual format specifier
385+
if self.consume('x') {
386+
if self.consume('?') {
387+
spec.flags |= 1 << (FlagDebugLowerHex as u32);
388+
spec.ty = "?";
389+
} else {
390+
spec.ty = "x";
391+
}
392+
} else if self.consume('X') {
393+
if self.consume('?') {
394+
spec.flags |= 1 << (FlagDebugUpperHex as u32);
395+
spec.ty = "?";
396+
} else {
397+
spec.ty = "X";
398+
}
399+
} else if self.consume('?') {
382400
spec.ty = "?";
383401
} else {
384402
spec.ty = self.word();

0 commit comments

Comments
 (0)
Please sign in to comment.