Skip to content

Commit

Permalink
Added %p directive to fmt!, which expects *T as argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Do Nhat Minh committed Jul 29, 2013
1 parent 5c4cd30 commit 79f1052
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/libstd/unstable/extfmt.rs
Expand Up @@ -114,6 +114,7 @@ pub mod ct {
TyHex(Caseness),
TyOctal,
TyFloat,
TyPointer,
TyPoly,
}

Expand Down Expand Up @@ -325,6 +326,7 @@ pub mod ct {
't' => TyBits,
'o' => TyOctal,
'f' => TyFloat,
'p' => TyPointer,
'?' => TyPoly,
_ => err(fmt!("unknown type in conversion: %c", s.char_at(i)))
};
Expand Down Expand Up @@ -434,6 +436,7 @@ pub mod ct {
assert!(test("t", TyBits));
assert!(test("x", TyHex(CaseLower)));
assert!(test("X", TyHex(CaseUpper)));
assert!(test("p", TyPointer));
assert!(test("?", TyPoly));
}

Expand Down Expand Up @@ -573,6 +576,10 @@ pub mod rt {
} else { None };
pad(cv, s, head, PadFloat, buf);
}
pub fn conv_pointer<T>(cv: Conv, ptr: *T, buf: &mut ~str) {
let s = ~"0x" + uint_to_str_prec(ptr as uint, 16, 1u);
pad(cv, s, None, PadNozero, buf);
}
pub fn conv_poly<T>(cv: Conv, v: &T, buf: &mut ~str) {
let s = sys::log_str(v);
conv_str(cv, s, buf);
Expand Down
2 changes: 2 additions & 0 deletions src/libsyntax/ext/fmt.rs
Expand Up @@ -191,6 +191,7 @@ fn pieces_to_expr(cx: @ExtCtxt, sp: span,
TyChar => ("char", arg),
TyBits | TyOctal | TyHex(_) | TyInt(Unsigned) => ("uint", arg),
TyFloat => ("float", arg),
TyPointer => ("pointer", arg),
TyPoly => ("poly", cx.expr_addr_of(sp, arg))
};
return make_conv_call(cx, arg.span, name, cnv, actual_arg,
Expand Down Expand Up @@ -242,6 +243,7 @@ fn pieces_to_expr(cx: @ExtCtxt, sp: span,
},
TyOctal => debug!("type: octal"),
TyFloat => debug!("type: float"),
TyPointer => debug!("type: pointer"),
TyPoly => debug!("type: poly")
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/test/run-pass/syntax-extension-fmt.rs
Expand Up @@ -32,6 +32,7 @@ pub fn main() {
part6();
percent();
more_floats();
pointer();
}

fn part1() {
Expand Down Expand Up @@ -263,3 +264,13 @@ fn more_floats() {
assert_eq!(~"7.0000", fmt!("%.4f", 6.999999999));
assert_eq!(~"3.141590000", fmt!("%.9f", 3.14159));
}

fn pointer() {
for 10.times {
let x: uint = ::std::rand::random();
assert_eq!(fmt!("%p", x as *uint), fmt!("0x%x", x));
}

let i = &1;
assert_eq!(fmt!("%p", i), fmt!("0x%x", i as *uint as uint));
}

5 comments on commit 79f1052

@bors
Copy link
Contributor

@bors bors commented on 79f1052 Jul 29, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 79f1052 Jul 29, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging mrordinaire/rust/percent-p = 79f1052 into auto

@bors
Copy link
Contributor

@bors bors commented on 79f1052 Jul 29, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mrordinaire/rust/percent-p = 79f1052 merged ok, testing candidate = 8413d47

@bors
Copy link
Contributor

@bors bors commented on 79f1052 Jul 29, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 8413d47

Please sign in to comment.