Skip to content

Commit eb836dd

Browse files
committed
Settle on the format/write/print family of names
1 parent 67512f7 commit eb836dd

File tree

10 files changed

+295
-255
lines changed

10 files changed

+295
-255
lines changed

src/libstd/fmt/mod.rs

+26-26
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,33 @@
1212
1313
# The Formatting Module
1414
15-
This module contains the runtime support for the `ifmt!` syntax extension. This
15+
This module contains the runtime support for the `format!` syntax extension. This
1616
macro is implemented in the compiler to emit calls to this module in order to
1717
format arguments at runtime into strings and streams.
1818
1919
The functions contained in this module should not normally be used in everyday
20-
use cases of `ifmt!`. The assumptions made by these functions are unsafe for all
20+
use cases of `format!`. The assumptions made by these functions are unsafe for all
2121
inputs, and the compiler performs a large amount of validation on the arguments
22-
to `ifmt!` in order to ensure safety at runtime. While it is possible to call
22+
to `format!` in order to ensure safety at runtime. While it is possible to call
2323
these functions directly, it is not recommended to do so in the general case.
2424
2525
## Usage
2626
27-
The `ifmt!` macro is intended to be familiar to those coming from C's
28-
printf/sprintf functions or Python's `str.format` function. In its current
29-
revision, the `ifmt!` macro returns a `~str` type which is the result of the
27+
The `format!` macro is intended to be familiar to those coming from C's
28+
printf/fprintf functions or Python's `str.format` function. In its current
29+
revision, the `format!` macro returns a `~str` type which is the result of the
3030
formatting. In the future it will also be able to pass in a stream to format
3131
arguments directly while performing minimal allocations.
3232
33-
Some examples of the `ifmt!` extension are:
33+
Some examples of the `format!` extension are:
3434
3535
~~~{.rust}
36-
ifmt!("Hello") // => ~"Hello"
37-
ifmt!("Hello, {:s}!", "world") // => ~"Hello, world!"
38-
ifmt!("The number is {:d}", 1) // => ~"The number is 1"
39-
ifmt!("{}", ~[3, 4]) // => ~"~[3, 4]"
40-
ifmt!("{value}", value=4) // => ~"4"
41-
ifmt!("{} {}", 1, 2) // => ~"1 2"
36+
format!("Hello") // => ~"Hello"
37+
format!("Hello, {:s}!", "world") // => ~"Hello, world!"
38+
format!("The number is {:d}", 1) // => ~"The number is 1"
39+
format!("{}", ~[3, 4]) // => ~"~[3, 4]"
40+
format!("{value}", value=4) // => ~"4"
41+
format!("{} {}", 1, 2) // => ~"1 2"
4242
~~~
4343
4444
From these, you can see that the first argument is a format string. It is
@@ -62,7 +62,7 @@ format string, although it must always be referred to with the same type.
6262
### Named parameters
6363
6464
Rust itself does not have a Python-like equivalent of named parameters to a
65-
function, but the `ifmt!` macro is a syntax extension which allows it to
65+
function, but the `format!` macro is a syntax extension which allows it to
6666
leverage named parameters. Named parameters are listed at the end of the
6767
argument list and have the syntax:
6868
@@ -146,7 +146,7 @@ helper methods.
146146
147147
## Internationalization
148148
149-
The formatting syntax supported by the `ifmt!` extension supports
149+
The formatting syntax supported by the `format!` extension supports
150150
internationalization by providing "methods" which execute various different
151151
outputs depending on the input. The syntax and methods provided are similar to
152152
other internationalization systems, so again nothing should seem alien.
@@ -164,7 +164,7 @@ to reference the string value of the argument which was selected upon. As an
164164
example:
165165
166166
~~~
167-
ifmt!("{0, select, other{#}}", "hello") // => ~"hello"
167+
format!("{0, select, other{#}}", "hello") // => ~"hello"
168168
~~~
169169
170170
This example is the equivalent of `{0:s}` essentially.
@@ -399,11 +399,11 @@ pub trait Pointer { fn fmt(&Self, &mut Formatter); }
399399
#[allow(missing_doc)]
400400
pub trait Float { fn fmt(&Self, &mut Formatter); }
401401

402-
/// The fprintf function takes an output stream, a precompiled format string,
402+
/// The `write` function takes an output stream, a precompiled format string,
403403
/// and a list of arguments. The arguments will be formatted according to the
404404
/// specified format string into the output stream provided.
405405
///
406-
/// See the documentation for `sprintf` for why this function is unsafe and care
406+
/// See the documentation for `format` for why this function is unsafe and care
407407
/// should be taken if calling it manually.
408408
///
409409
/// Thankfully the rust compiler provides the macro `fmtf!` which will perform
@@ -419,8 +419,8 @@ pub trait Float { fn fmt(&Self, &mut Formatter); }
419419
///
420420
/// Note that this function assumes that there are enough arguments for the
421421
/// format string.
422-
pub unsafe fn fprintf(output: &mut io::Writer,
423-
fmt: &[rt::Piece], args: &[Argument]) {
422+
pub unsafe fn write(output: &mut io::Writer,
423+
fmt: &[rt::Piece], args: &[Argument]) {
424424
let mut formatter = Formatter {
425425
flags: 0,
426426
width: None,
@@ -436,7 +436,7 @@ pub unsafe fn fprintf(output: &mut io::Writer,
436436
}
437437
}
438438

439-
/// The sprintf function takes a precompiled format string and a list of
439+
/// The format function takes a precompiled format string and a list of
440440
/// arguments, to return the resulting formatted string.
441441
///
442442
/// This is currently an unsafe function because the types of all arguments
@@ -446,7 +446,7 @@ pub unsafe fn fprintf(output: &mut io::Writer,
446446
/// for formatting the right type value. Because of this, the function is marked
447447
/// as `unsafe` if this is being called manually.
448448
///
449-
/// Thankfully the rust compiler provides the macro `ifmt!` which will perform
449+
/// Thankfully the rust compiler provides the macro `format!` which will perform
450450
/// all of this validation at compile-time and provides a safe interface for
451451
/// invoking this function.
452452
///
@@ -458,17 +458,17 @@ pub unsafe fn fprintf(output: &mut io::Writer,
458458
///
459459
/// Note that this function assumes that there are enough arguments for the
460460
/// format string.
461-
pub unsafe fn sprintf(fmt: &[rt::Piece], args: &[Argument]) -> ~str {
461+
pub unsafe fn format(fmt: &[rt::Piece], args: &[Argument]) -> ~str {
462462
let mut output = MemWriter::new();
463-
fprintf(&mut output as &mut io::Writer, fmt, args);
463+
write(&mut output as &mut io::Writer, fmt, args);
464464
return str::from_bytes_owned(output.inner());
465465
}
466466

467467
impl<'self> Formatter<'self> {
468468

469469
// First up is the collection of functions used to execute a format string
470470
// at runtime. This consumes all of the compile-time statics generated by
471-
// the ifmt! syntax extension.
471+
// the format! syntax extension.
472472

473473
fn run(&mut self, piece: &rt::Piece, cur: Option<&str>) {
474474
let setcount = |slot: &mut Option<uint>, cnt: &parse::Count| {
@@ -732,7 +732,7 @@ impl<'self> Formatter<'self> {
732732
}
733733

734734
/// This is a function which calls are emitted to by the compiler itself to
735-
/// create the Argument structures that are passed into the `sprintf` function.
735+
/// create the Argument structures that are passed into the `format` function.
736736
#[doc(hidden)]
737737
pub fn argument<'a, T>(f: extern "Rust" fn(&T, &mut Formatter),
738738
t: &'a T) -> Argument<'a> {

src/libsyntax/ext/base.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,12 @@ pub fn syntax_expander_table() -> SyntaxEnv {
139139
ext::tt::macro_rules::add_new_extension));
140140
syntax_expanders.insert(intern(&"fmt"),
141141
builtin_normal_tt(ext::fmt::expand_syntax_ext));
142-
syntax_expanders.insert(intern(&"ifmt"),
143-
builtin_normal_tt(ext::ifmt::expand_sprintf));
144-
syntax_expanders.insert(intern(&"ifmtf"),
145-
builtin_normal_tt(ext::ifmt::expand_fprintf));
142+
syntax_expanders.insert(intern(&"format"),
143+
builtin_normal_tt(ext::ifmt::expand_format));
144+
syntax_expanders.insert(intern(&"write"),
145+
builtin_normal_tt(ext::ifmt::expand_write));
146+
syntax_expanders.insert(intern(&"writeln"),
147+
builtin_normal_tt(ext::ifmt::expand_writeln));
146148
syntax_expanders.insert(
147149
intern(&"auto_encode"),
148150
@SE(ItemDecorator(ext::auto_encode::expand_auto_encode)));

src/libsyntax/ext/expand.rs

+17
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,7 @@ pub fn std_macros() -> @str {
940940
);
941941
)
942942

943+
// NOTE(acrichto): start removing this after the next snapshot
943944
macro_rules! printf (
944945
($arg:expr) => (
945946
print(fmt!(\"%?\", $arg))
@@ -949,6 +950,7 @@ pub fn std_macros() -> @str {
949950
)
950951
)
951952

953+
// NOTE(acrichto): start removing this after the next snapshot
952954
macro_rules! printfln (
953955
($arg:expr) => (
954956
println(fmt!(\"%?\", $arg))
@@ -958,6 +960,21 @@ pub fn std_macros() -> @str {
958960
)
959961
)
960962

963+
// FIXME(#6846) once stdio is redesigned, this shouldn't perform an
964+
// allocation but should rather delegate to an invocation of
965+
// write! instead of format!
966+
macro_rules! print (
967+
() => ();
968+
($arg:expr) => ( ::std::io::print(format!(\"{}\", $arg)));
969+
($fmt:expr, $($arg:tt)+) => ( ::std::io::print(format!($fmt, $($arg)+)))
970+
)
971+
972+
// FIXME(#6846) once stdio is redesigned, this shouldn't perform an
973+
// allocation but should rather delegate to an io::Writer
974+
macro_rules! println (
975+
($($arg:tt)*) => ({ print!($($arg)*); ::std::io::println(\"\"); })
976+
)
977+
961978
// NOTE: use this after a snapshot lands to abstract the details
962979
// of the TLS interface.
963980
macro_rules! local_data_key (

src/libsyntax/ext/ifmt.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -697,19 +697,24 @@ impl Context {
697697
}
698698
}
699699

700-
pub fn expand_sprintf(ecx: @ExtCtxt, sp: span,
701-
tts: &[ast::token_tree]) -> base::MacResult {
702-
expand_ifmt(ecx, sp, tts, false, "sprintf")
700+
pub fn expand_format(ecx: @ExtCtxt, sp: span,
701+
tts: &[ast::token_tree]) -> base::MacResult {
702+
expand_ifmt(ecx, sp, tts, false, false, "format")
703703
}
704704

705-
pub fn expand_fprintf(ecx: @ExtCtxt, sp: span,
706-
tts: &[ast::token_tree]) -> base::MacResult {
707-
expand_ifmt(ecx, sp, tts, true, "fprintf")
705+
pub fn expand_write(ecx: @ExtCtxt, sp: span,
706+
tts: &[ast::token_tree]) -> base::MacResult {
707+
expand_ifmt(ecx, sp, tts, true, false, "write")
708708
}
709709

710+
pub fn expand_writeln(ecx: @ExtCtxt, sp: span,
711+
tts: &[ast::token_tree]) -> base::MacResult {
712+
expand_ifmt(ecx, sp, tts, true, true, "write")
713+
}
710714

711715
fn expand_ifmt(ecx: @ExtCtxt, sp: span, tts: &[ast::token_tree],
712-
leading_arg: bool, function: &str) -> base::MacResult {
716+
leading_arg: bool, append_newline: bool,
717+
function: &str) -> base::MacResult {
713718
let mut cx = Context {
714719
ecx: ecx,
715720
args: ~[],
@@ -730,6 +735,7 @@ fn expand_ifmt(ecx: @ExtCtxt, sp: span, tts: &[ast::token_tree],
730735
cx.fmtsp = efmt.span;
731736
let fmt = expr_to_str(ecx, efmt,
732737
"format argument must be a string literal.");
738+
let fmt = if append_newline { fmt + "\n" } else { fmt.to_owned() };
733739

734740
let mut err = false;
735741
do parse::parse_error::cond.trap(|m| {

src/test/compile-fail/ifmt-bad-arg.rs

+43-43
Original file line numberDiff line numberDiff line change
@@ -9,66 +9,66 @@
99
// except according to those terms.
1010

1111
fn main() {
12-
// bad arguments to the ifmt! call
12+
// bad arguments to the format! call
1313

14-
ifmt!(); //~ ERROR: expects at least one
15-
ifmt!("{}"); //~ ERROR: invalid reference to argument
14+
format!(); //~ ERROR: requires at least a format string
15+
format!("{}"); //~ ERROR: invalid reference to argument
1616

17-
ifmt!("{1}", 1); //~ ERROR: invalid reference to argument `1`
17+
format!("{1}", 1); //~ ERROR: invalid reference to argument `1`
1818
//~^ ERROR: argument never used
19-
ifmt!("{foo}"); //~ ERROR: no argument named `foo`
19+
format!("{foo}"); //~ ERROR: no argument named `foo`
2020

21-
ifmt!("{}", 1, 2); //~ ERROR: argument never used
22-
ifmt!("{1}", 1, 2); //~ ERROR: argument never used
23-
ifmt!("{}", 1, foo=2); //~ ERROR: named argument never used
24-
ifmt!("{foo}", 1, foo=2); //~ ERROR: argument never used
25-
ifmt!("", foo=2); //~ ERROR: named argument never used
21+
format!("{}", 1, 2); //~ ERROR: argument never used
22+
format!("{1}", 1, 2); //~ ERROR: argument never used
23+
format!("{}", 1, foo=2); //~ ERROR: named argument never used
24+
format!("{foo}", 1, foo=2); //~ ERROR: argument never used
25+
format!("", foo=2); //~ ERROR: named argument never used
2626

27-
ifmt!("{0:d} {0:s}", 1); //~ ERROR: redeclared with type `s`
28-
ifmt!("{foo:d} {foo:s}", foo=1); //~ ERROR: redeclared with type `s`
27+
format!("{0:d} {0:s}", 1); //~ ERROR: redeclared with type `s`
28+
format!("{foo:d} {foo:s}", foo=1); //~ ERROR: redeclared with type `s`
2929

30-
ifmt!("{foo}", foo=1, foo=2); //~ ERROR: duplicate argument
31-
ifmt!("#"); //~ ERROR: `#` reference used
32-
ifmt!("", foo=1, 2); //~ ERROR: positional arguments cannot follow
33-
ifmt!("" 1); //~ ERROR: expected token: `,`
34-
ifmt!("", 1 1); //~ ERROR: expected token: `,`
30+
format!("{foo}", foo=1, foo=2); //~ ERROR: duplicate argument
31+
format!("#"); //~ ERROR: `#` reference used
32+
format!("", foo=1, 2); //~ ERROR: positional arguments cannot follow
33+
format!("" 1); //~ ERROR: expected token: `,`
34+
format!("", 1 1); //~ ERROR: expected token: `,`
3535

36-
ifmt!("{0, select, a{} a{} other{}}", "a"); //~ ERROR: duplicate selector
37-
ifmt!("{0, plural, =1{} =1{} other{}}", 1u); //~ ERROR: duplicate selector
38-
ifmt!("{0, plural, one{} one{} other{}}", 1u); //~ ERROR: duplicate selector
36+
format!("{0, select, a{} a{} other{}}", "a"); //~ ERROR: duplicate selector
37+
format!("{0, plural, =1{} =1{} other{}}", 1u); //~ ERROR: duplicate selector
38+
format!("{0, plural, one{} one{} other{}}", 1u); //~ ERROR: duplicate selector
3939

4040
// bad syntax of the format string
4141

42-
ifmt!("{"); //~ ERROR: unterminated format string
43-
ifmt!("\\ "); //~ ERROR: invalid escape
44-
ifmt!("\\"); //~ ERROR: expected an escape
42+
format!("{"); //~ ERROR: unterminated format string
43+
format!("\\ "); //~ ERROR: invalid escape
44+
format!("\\"); //~ ERROR: expected an escape
4545

46-
ifmt!("{0, }", 1); //~ ERROR: expected method
47-
ifmt!("{0, foo}", 1); //~ ERROR: unknown method
48-
ifmt!("{0, select}", "a"); //~ ERROR: must be followed by
49-
ifmt!("{0, plural}", 1); //~ ERROR: must be followed by
46+
format!("{0, }", 1); //~ ERROR: expected method
47+
format!("{0, foo}", 1); //~ ERROR: unknown method
48+
format!("{0, select}", "a"); //~ ERROR: must be followed by
49+
format!("{0, plural}", 1); //~ ERROR: must be followed by
5050

51-
ifmt!("{0, select, a{{}", 1); //~ ERROR: must be terminated
52-
ifmt!("{0, select, {} other{}}", "a"); //~ ERROR: empty selector
53-
ifmt!("{0, select, other{} other{}}", "a"); //~ ERROR: multiple `other`
54-
ifmt!("{0, plural, offset: other{}}", "a"); //~ ERROR: must be an integer
55-
ifmt!("{0, plural, offset 1 other{}}", "a"); //~ ERROR: be followed by `:`
56-
ifmt!("{0, plural, =a{} other{}}", "a"); //~ ERROR: followed by an integer
57-
ifmt!("{0, plural, a{} other{}}", "a"); //~ ERROR: unexpected plural
58-
ifmt!("{0, select, a{}}", "a"); //~ ERROR: must provide an `other`
59-
ifmt!("{0, plural, =1{}}", "a"); //~ ERROR: must provide an `other`
51+
format!("{0, select, a{{}", 1); //~ ERROR: must be terminated
52+
format!("{0, select, {} other{}}", "a"); //~ ERROR: empty selector
53+
format!("{0, select, other{} other{}}", "a"); //~ ERROR: multiple `other`
54+
format!("{0, plural, offset: other{}}", "a"); //~ ERROR: must be an integer
55+
format!("{0, plural, offset 1 other{}}", "a"); //~ ERROR: be followed by `:`
56+
format!("{0, plural, =a{} other{}}", "a"); //~ ERROR: followed by an integer
57+
format!("{0, plural, a{} other{}}", "a"); //~ ERROR: unexpected plural
58+
format!("{0, select, a{}}", "a"); //~ ERROR: must provide an `other`
59+
format!("{0, plural, =1{}}", "a"); //~ ERROR: must provide an `other`
6060

61-
ifmt!("{0, plural, other{{0:s}}}", "a"); //~ ERROR: previously used as
62-
ifmt!("{:s} {0, plural, other{}}", "a"); //~ ERROR: argument used to
63-
ifmt!("{0, select, other{}} \
64-
{0, plural, other{}}", "a");
61+
format!("{0, plural, other{{0:s}}}", "a"); //~ ERROR: previously used as
62+
format!("{:s} {0, plural, other{}}", "a"); //~ ERROR: argument used to
63+
format!("{0, select, other{}} \
64+
{0, plural, other{}}", "a");
6565
//~^ ERROR: declared with multiple formats
6666

6767
// It should be illegal to use implicit placement arguments nested inside of
6868
// format strings because otherwise the "internal pointer of which argument
6969
// is next" would be invalidated if different cases had different numbers of
7070
// arguments.
71-
ifmt!("{0, select, other{{}}}", "a"); //~ ERROR: cannot use implicit
72-
ifmt!("{0, plural, other{{}}}", 1); //~ ERROR: cannot use implicit
73-
ifmt!("{0, plural, other{{1:.*d}}}", 1, 2); //~ ERROR: cannot use implicit
71+
format!("{0, select, other{{}}}", "a"); //~ ERROR: cannot use implicit
72+
format!("{0, plural, other{{}}}", 1); //~ ERROR: cannot use implicit
73+
format!("{0, plural, other{{1:.*d}}}", 1, 2); //~ ERROR: cannot use implicit
7474
}

src/test/compile-fail/ifmt-bad-plural.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
// except according to those terms.
1010

1111
fn main() {
12-
ifmt!("{0, plural, other{}}", "a");
12+
format!("{0, plural, other{}}", "a");
1313
//~^ ERROR: expected uint but found
1414
}

src/test/compile-fail/ifmt-bad-select.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
// except according to those terms.
1010

1111
fn main() {
12-
ifmt!("{0, select, other{}}", 2);
12+
format!("{0, select, other{}}", 2);
1313
//~^ ERROR: expected &str but found integral
1414
}

src/test/compile-fail/ifmt-unimpl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
// except according to those terms.
1010

1111
fn main() {
12-
ifmt!("{:d}", "3");
12+
format!("{:d}", "3");
1313
//~^ ERROR: failed to find an implementation of trait std::fmt::Signed
1414
}

src/test/compile-fail/ifmt-unknown-trait.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
// except according to those terms.
1010

1111
fn main() {
12-
ifmt!("{:notimplemented}", "3");
12+
format!("{:notimplemented}", "3");
1313
//~^ ERROR: unknown format trait `notimplemented`
1414
}

0 commit comments

Comments
 (0)