Skip to content

Commit 8413d47

Browse files
committed
auto merge of #8085 : mrordinaire/rust/percent-p, r=huonw
pull request for #8011
2 parents 27812ea + 79f1052 commit 8413d47

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

Diff for: src/libstd/unstable/extfmt.rs

+7
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ pub mod ct {
114114
TyHex(Caseness),
115115
TyOctal,
116116
TyFloat,
117+
TyPointer,
117118
TyPoly,
118119
}
119120

@@ -325,6 +326,7 @@ pub mod ct {
325326
't' => TyBits,
326327
'o' => TyOctal,
327328
'f' => TyFloat,
329+
'p' => TyPointer,
328330
'?' => TyPoly,
329331
_ => err(fmt!("unknown type in conversion: %c", s.char_at(i)))
330332
};
@@ -434,6 +436,7 @@ pub mod ct {
434436
assert!(test("t", TyBits));
435437
assert!(test("x", TyHex(CaseLower)));
436438
assert!(test("X", TyHex(CaseUpper)));
439+
assert!(test("p", TyPointer));
437440
assert!(test("?", TyPoly));
438441
}
439442
@@ -573,6 +576,10 @@ pub mod rt {
573576
} else { None };
574577
pad(cv, s, head, PadFloat, buf);
575578
}
579+
pub fn conv_pointer<T>(cv: Conv, ptr: *T, buf: &mut ~str) {
580+
let s = ~"0x" + uint_to_str_prec(ptr as uint, 16, 1u);
581+
pad(cv, s, None, PadNozero, buf);
582+
}
576583
pub fn conv_poly<T>(cv: Conv, v: &T, buf: &mut ~str) {
577584
let s = sys::log_str(v);
578585
conv_str(cv, s, buf);

Diff for: src/libsyntax/ext/fmt.rs

+2
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ fn pieces_to_expr(cx: @ExtCtxt, sp: span,
191191
TyChar => ("char", arg),
192192
TyBits | TyOctal | TyHex(_) | TyInt(Unsigned) => ("uint", arg),
193193
TyFloat => ("float", arg),
194+
TyPointer => ("pointer", arg),
194195
TyPoly => ("poly", cx.expr_addr_of(sp, arg))
195196
};
196197
return make_conv_call(cx, arg.span, name, cnv, actual_arg,
@@ -242,6 +243,7 @@ fn pieces_to_expr(cx: @ExtCtxt, sp: span,
242243
},
243244
TyOctal => debug!("type: octal"),
244245
TyFloat => debug!("type: float"),
246+
TyPointer => debug!("type: pointer"),
245247
TyPoly => debug!("type: poly")
246248
}
247249
}

Diff for: src/test/run-pass/syntax-extension-fmt.rs

+11
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub fn main() {
3232
part6();
3333
percent();
3434
more_floats();
35+
pointer();
3536
}
3637
3738
fn part1() {
@@ -263,3 +264,13 @@ fn more_floats() {
263264
assert_eq!(~"7.0000", fmt!("%.4f", 6.999999999));
264265
assert_eq!(~"3.141590000", fmt!("%.9f", 3.14159));
265266
}
267+
268+
fn pointer() {
269+
for 10.times {
270+
let x: uint = ::std::rand::random();
271+
assert_eq!(fmt!("%p", x as *uint), fmt!("0x%x", x));
272+
}
273+
274+
let i = &1;
275+
assert_eq!(fmt!("%p", i), fmt!("0x%x", i as *uint as uint));
276+
}

0 commit comments

Comments
 (0)