File tree 3 files changed +39
-1
lines changed
3 files changed +39
-1
lines changed Original file line number Diff line number Diff line change @@ -847,9 +847,33 @@ impl Display for char {
847
847
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
848
848
impl < T > Pointer for * const T {
849
849
fn fmt ( & self , f : & mut Formatter ) -> Result {
850
+ let old_width = f. width ;
851
+ let old_flags = f. flags ;
852
+
853
+ // The alternate flag is already treated by LowerHex as being special-
854
+ // it denotes whether to prefix with 0x. We use it to work out whether
855
+ // or not to zero extend, and then unconditionally set it to get the
856
+ // prefix.
857
+ if f. flags & 1 << ( FlagV1 :: Alternate as u32 ) > 0 {
858
+ f. flags |= 1 << ( FlagV1 :: SignAwareZeroPad as u32 ) ;
859
+
860
+ if let None = f. width {
861
+ // The formats need two extra bytes, for the 0x
862
+ if cfg ! ( target_pointer_width = "32" ) {
863
+ f. width = Some ( 10 ) ;
864
+ }
865
+ if cfg ! ( target_pointer_width = "64" ) {
866
+ f. width = Some ( 18 ) ;
867
+ }
868
+ }
869
+ }
850
870
f. flags |= 1 << ( FlagV1 :: Alternate as u32 ) ;
871
+
851
872
let ret = LowerHex :: fmt ( & ( * self as usize ) , f) ;
852
- f. flags &= !( 1 << ( FlagV1 :: Alternate as u32 ) ) ;
873
+
874
+ f. width = old_width;
875
+ f. flags = old_flags;
876
+
853
877
ret
854
878
}
855
879
}
Original file line number Diff line number Diff line change @@ -23,6 +23,14 @@ fn main() {
23
23
let _ = format ! ( "{:p}{:p}{:p}" ,
24
24
rc, arc, b) ;
25
25
26
+ if cfg ! ( target_pointer_width = "32" ) {
27
+ assert_eq ! ( format!( "{:#p}" , p) ,
28
+ "0x00000000" ) ;
29
+ }
30
+ if cfg ! ( target_pointer_width = "64" ) {
31
+ assert_eq ! ( format!( "{:#p}" , p) ,
32
+ "0x0000000000000000" ) ;
33
+ }
26
34
assert_eq ! ( format!( "{:p}" , p) ,
27
35
"0x0" ) ;
28
36
}
Original file line number Diff line number Diff line change @@ -42,6 +42,10 @@ impl fmt::Display for C {
42
42
macro_rules! t {
43
43
( $a: expr, $b: expr) => { assert_eq!( $a, $b) }
44
44
}
45
+ #[ cfg( target_pointer_width = "32" ) ]
46
+ const PTR : & ' static str = "0x00001234" ;
47
+ #[ cfg( target_pointer_width = "64" ) ]
48
+ const PTR : & ' static str = "0x0000000000001234" ;
45
49
46
50
pub fn main ( ) {
47
51
// Various edge cases without formats
@@ -72,6 +76,8 @@ pub fn main() {
72
76
t ! ( format!( "{:X}" , 10_usize ) , "A" ) ;
73
77
t ! ( format!( "{}" , "foo" ) , "foo" ) ;
74
78
t ! ( format!( "{}" , "foo" . to_string( ) ) , "foo" ) ;
79
+ t ! ( format!( "{:#p}" , 0x1234 as * const isize ) , PTR ) ;
80
+ t ! ( format!( "{:#p}" , 0x1234 as * mut isize ) , PTR ) ;
75
81
t ! ( format!( "{:p}" , 0x1234 as * const isize ) , "0x1234" ) ;
76
82
t ! ( format!( "{:p}" , 0x1234 as * mut isize ) , "0x1234" ) ;
77
83
t ! ( format!( "{:x}" , A ) , "aloha" ) ;
You can’t perform that action at this time.
0 commit comments