@@ -399,6 +399,43 @@ pub trait Pointer { fn fmt(&Self, &mut Formatter); }
399
399
#[ allow( missing_doc) ]
400
400
pub trait Float { fn fmt ( & Self , & mut Formatter ) ; }
401
401
402
+ /// The fprintf function takes an output stream, a precompiled format string,
403
+ /// and a list of arguments. The arguments will be formatted according to the
404
+ /// specified format string into the output stream provided.
405
+ ///
406
+ /// See the documentation for `sprintf` for why this function is unsafe and care
407
+ /// should be taken if calling it manually.
408
+ ///
409
+ /// Thankfully the rust compiler provides the macro `fmtf!` which will perform
410
+ /// all of this validation at compile-time and provides a safe interface for
411
+ /// invoking this function.
412
+ ///
413
+ /// # Arguments
414
+ ///
415
+ /// * output - the buffer to write output to
416
+ /// * fmts - the precompiled format string to emit
417
+ /// * args - the list of arguments to the format string. These are only the
418
+ /// positional arguments (not named)
419
+ ///
420
+ /// Note that this function assumes that there are enough arguments for the
421
+ /// format string.
422
+ pub unsafe fn fprintf ( output : & mut io:: Writer ,
423
+ fmt : & [ rt:: Piece ] , args : & [ Argument ] ) {
424
+ let mut formatter = Formatter {
425
+ flags : 0 ,
426
+ width : None ,
427
+ precision : None ,
428
+ buf : output,
429
+ align : parse:: AlignUnknown ,
430
+ fill : ' ' ,
431
+ args : args,
432
+ curarg : args. iter ( ) ,
433
+ } ;
434
+ for piece in fmt. iter ( ) {
435
+ formatter. run ( piece, None ) ;
436
+ }
437
+ }
438
+
402
439
/// The sprintf function takes a precompiled format string and a list of
403
440
/// arguments, to return the resulting formatted string.
404
441
///
@@ -422,23 +459,8 @@ pub trait Float { fn fmt(&Self, &mut Formatter); }
422
459
/// Note that this function assumes that there are enough arguments for the
423
460
/// format string.
424
461
pub unsafe fn sprintf ( fmt : & [ rt:: Piece ] , args : & [ Argument ] ) -> ~str {
425
- let output = MemWriter :: new ( ) ;
426
- {
427
- let mut formatter = Formatter {
428
- flags : 0 ,
429
- width : None ,
430
- precision : None ,
431
- // FIXME(#8248): shouldn't need a transmute
432
- buf : cast:: transmute ( & output as & io:: Writer ) ,
433
- align : parse:: AlignUnknown ,
434
- fill : ' ' ,
435
- args : args,
436
- curarg : args. iter ( ) ,
437
- } ;
438
- for piece in fmt. iter ( ) {
439
- formatter. run ( piece, None ) ;
440
- }
441
- }
462
+ let mut output = MemWriter :: new ( ) ;
463
+ fprintf ( & mut output as & mut io:: Writer , fmt, args) ;
442
464
return str:: from_bytes_owned ( output. inner ( ) ) ;
443
465
}
444
466
0 commit comments