Skip to content

first attempt at implementing %p for fmt! #8085

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/libstd/unstable/extfmt.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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));
}