diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2013-08-19 10:47:38 -0700 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2013-08-24 12:48:10 -0700 |
| commit | 39207a358bdbaf3d4ba921c2b7cce786cf2c7cef (patch) | |
| tree | a58ff500afb02e8ef9de4931f0a91fdf6709b5f9 /src/libstd | |
| parent | 59ca7a88db8583199f91fec067cfc332aae33195 (diff) | |
| download | rust-39207a358bdbaf3d4ba921c2b7cce786cf2c7cef.tar.gz rust-39207a358bdbaf3d4ba921c2b7cce786cf2c7cef.zip | |
Remove ifmt hax and implement fprintf
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/fmt/mod.rs | 56 |
1 files changed, 39 insertions, 17 deletions
diff --git a/src/libstd/fmt/mod.rs b/src/libstd/fmt/mod.rs index db8a17c0bd0..03204fd30ea 100644 --- a/src/libstd/fmt/mod.rs +++ b/src/libstd/fmt/mod.rs @@ -399,6 +399,43 @@ pub trait Pointer { fn fmt(&Self, &mut Formatter); } #[allow(missing_doc)] pub trait Float { fn fmt(&Self, &mut Formatter); } +/// The fprintf function takes an output stream, a precompiled format string, +/// and a list of arguments. The arguments will be formatted according to the +/// specified format string into the output stream provided. +/// +/// See the documentation for `sprintf` for why this function is unsafe and care +/// should be taken if calling it manually. +/// +/// Thankfully the rust compiler provides the macro `fmtf!` which will perform +/// all of this validation at compile-time and provides a safe interface for +/// invoking this function. +/// +/// # Arguments +/// +/// * output - the buffer to write output to +/// * fmts - the precompiled format string to emit +/// * args - the list of arguments to the format string. These are only the +/// positional arguments (not named) +/// +/// Note that this function assumes that there are enough arguments for the +/// format string. +pub unsafe fn fprintf(output: &mut io::Writer, + fmt: &[rt::Piece], args: &[Argument]) { + let mut formatter = Formatter { + flags: 0, + width: None, + precision: None, + buf: output, + align: parse::AlignUnknown, + fill: ' ', + args: args, + curarg: args.iter(), + }; + for piece in fmt.iter() { + formatter.run(piece, None); + } +} + /// The sprintf function takes a precompiled format string and a list of /// arguments, to return the resulting formatted string. /// @@ -422,23 +459,8 @@ pub trait Float { fn fmt(&Self, &mut Formatter); } /// Note that this function assumes that there are enough arguments for the /// format string. pub unsafe fn sprintf(fmt: &[rt::Piece], args: &[Argument]) -> ~str { - let output = MemWriter::new(); - { - let mut formatter = Formatter { - flags: 0, - width: None, - precision: None, - // FIXME(#8248): shouldn't need a transmute - buf: cast::transmute(&output as &io::Writer), - align: parse::AlignUnknown, - fill: ' ', - args: args, - curarg: args.iter(), - }; - for piece in fmt.iter() { - formatter.run(piece, None); - } - } + let mut output = MemWriter::new(); + fprintf(&mut output as &mut io::Writer, fmt, args); return str::from_bytes_owned(output.inner()); } |
