File tree Expand file tree Collapse file tree 3 files changed +41
-1
lines changed Expand file tree Collapse file tree 3 files changed +41
-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 @@ -72,6 +72,14 @@ pub fn main() {
72
72
t ! ( format!( "{:X}" , 10_usize ) , "A" ) ;
73
73
t ! ( format!( "{}" , "foo" ) , "foo" ) ;
74
74
t ! ( format!( "{}" , "foo" . to_string( ) ) , "foo" ) ;
75
+ if cfg ! ( target_pointer_width = "32" ) {
76
+ t ! ( format!( "{:#p}" , 0x1234 as * const isize ) , "0x00001234" ) ;
77
+ t ! ( format!( "{:#p}" , 0x1234 as * mut isize ) , "0x00001234" ) ;
78
+ }
79
+ if cfg ! ( target_pointer_width = "64" ) {
80
+ t ! ( format!( "{:#p}" , 0x1234 as * const isize ) , "0x0000000000001234" ) ;
81
+ t ! ( format!( "{:#p}" , 0x1234 as * mut isize ) , "0x0000000000001234" ) ;
82
+ }
75
83
t ! ( format!( "{:p}" , 0x1234 as * const isize ) , "0x1234" ) ;
76
84
t ! ( format!( "{:p}" , 0x1234 as * mut isize ) , "0x1234" ) ;
77
85
t ! ( format!( "{:x}" , A ) , "aloha" ) ;
You can’t perform that action at this time.
0 commit comments