@@ -36,7 +36,7 @@ Some examples of the `format!` extension are:
36
36
format!("Hello") // => ~"Hello"
37
37
format!("Hello, {:s}!", "world") // => ~"Hello, world!"
38
38
format!("The number is {:d}", 1) // => ~"The number is 1"
39
- format!("{}", ~[3, 4]) // => ~"~[3, 4]"
39
+ format!("{:? }", ~[3, 4]) // => ~"~[3, 4]"
40
40
format!("{value}", value=4) // => ~"4"
41
41
format!("{} {}", 1, 2) // => ~"1 2"
42
42
~~~
@@ -363,6 +363,32 @@ pub struct Argument<'self> {
363
363
priv value : & ' self util:: Void ,
364
364
}
365
365
366
+ impl < ' self > Arguments < ' self > {
367
+ /// When using the format_args!() macro, this function is used to generate the
368
+ /// Arguments structure. The compiler inserts an `unsafe` block to call this,
369
+ /// which is valid because the compiler performs all necessary validation to
370
+ /// ensure that the resulting call to format/write would be safe.
371
+ #[ doc( hidden) ] #[ inline]
372
+ pub unsafe fn new < ' a > ( fmt : & ' static [ rt:: Piece < ' static > ] ,
373
+ args : & ' a [ Argument < ' a > ] ) -> Arguments < ' a > {
374
+ Arguments { fmt : cast:: transmute ( fmt) , args : args }
375
+ }
376
+ }
377
+
378
+ /// This structure represents a safely precompiled version of a format string
379
+ /// and its arguments. This cannot be generated at runtime because it cannot
380
+ /// safely be done so, so no constructors are given and the fields are private
381
+ /// to prevent modification.
382
+ ///
383
+ /// The `format_args!` macro will safely create an instance of this structure
384
+ /// and pass it to a user-supplied function. The macro validates the format
385
+ /// string at compile-time so usage of the `write` and `format` functions can
386
+ /// be safely performed.
387
+ pub struct Arguments < ' self > {
388
+ priv fmt: & ' self [ rt:: Piece < ' self > ] ,
389
+ priv args : & ' self [ Argument < ' self > ] ,
390
+ }
391
+
366
392
/// When a format is not otherwise specified, types are formatted by ascribing
367
393
/// to this trait. There is not an explicit way of selecting this trait to be
368
394
/// used for formatting, it is only if no other format is specified.
@@ -410,6 +436,26 @@ pub trait Float { fn fmt(&Self, &mut Formatter); }
410
436
/// and a list of arguments. The arguments will be formatted according to the
411
437
/// specified format string into the output stream provided.
412
438
///
439
+ /// # Arguments
440
+ ///
441
+ /// * output - the buffer to write output to
442
+ /// * args - the precompiled arguments generated by `format_args!`
443
+ ///
444
+ /// # Example
445
+ ///
446
+ /// ~~~{.rust}
447
+ /// use std::fmt;
448
+ /// let w: &mut io::Writer = ...;
449
+ /// format_args!(|args| { fmt::write(w, args) }, "Hello, {}!", "world");
450
+ /// ~~~
451
+ pub fn write ( output : & mut io:: Writer , args : & Arguments ) {
452
+ unsafe { write_unsafe ( output, args. fmt , args. args ) }
453
+ }
454
+
455
+ /// The `write_unsafe` function takes an output stream, a precompiled format
456
+ /// string, and a list of arguments. The arguments will be formatted according
457
+ /// to the specified format string into the output stream provided.
458
+ ///
413
459
/// See the documentation for `format` for why this function is unsafe and care
414
460
/// should be taken if calling it manually.
415
461
///
@@ -426,8 +472,9 @@ pub trait Float { fn fmt(&Self, &mut Formatter); }
426
472
///
427
473
/// Note that this function assumes that there are enough arguments for the
428
474
/// format string.
429
- pub unsafe fn write ( output : & mut io:: Writer ,
430
- fmt : & [ rt:: Piece ] , args : & [ Argument ] ) {
475
+ pub unsafe fn write_unsafe ( output : & mut io:: Writer ,
476
+ fmt : & [ rt:: Piece ] ,
477
+ args : & [ Argument ] ) {
431
478
let mut formatter = Formatter {
432
479
flags : 0 ,
433
480
width : None ,
@@ -446,6 +493,25 @@ pub unsafe fn write(output: &mut io::Writer,
446
493
/// The format function takes a precompiled format string and a list of
447
494
/// arguments, to return the resulting formatted string.
448
495
///
496
+ /// # Arguments
497
+ ///
498
+ /// * args - a structure of arguments generated via the `format_args!` macro.
499
+ /// Because this structure can only be safely generated at
500
+ /// compile-time, this function is safe.
501
+ ///
502
+ /// # Example
503
+ ///
504
+ /// ~~~{.rust}
505
+ /// use std::fmt;
506
+ /// let s = format_args!(fmt::format, "Hello, {}!", "world");
507
+ /// assert_eq!(s, "Hello, world!");
508
+ /// ~~~
509
+ pub fn format ( args : & Arguments ) -> ~str {
510
+ unsafe { format_unsafe ( args. fmt , args. args ) }
511
+ }
512
+
513
+ /// The unsafe version of the formatting function.
514
+ ///
449
515
/// This is currently an unsafe function because the types of all arguments
450
516
/// aren't verified by immediate callers of this function. This currently does
451
517
/// not validate that the correct types of arguments are specified for each
@@ -465,9 +531,9 @@ pub unsafe fn write(output: &mut io::Writer,
465
531
///
466
532
/// Note that this function assumes that there are enough arguments for the
467
533
/// format string.
468
- pub unsafe fn format ( fmt : & [ rt:: Piece ] , args : & [ Argument ] ) -> ~str {
534
+ pub unsafe fn format_unsafe ( fmt : & [ rt:: Piece ] , args : & [ Argument ] ) -> ~str {
469
535
let mut output = MemWriter :: new ( ) ;
470
- write ( & mut output as & mut io:: Writer , fmt, args) ;
536
+ write_unsafe ( & mut output as & mut io:: Writer , fmt, args) ;
471
537
return str:: from_utf8_owned ( output. inner ( ) ) ;
472
538
}
473
539
@@ -740,7 +806,7 @@ impl<'self> Formatter<'self> {
740
806
741
807
/// This is a function which calls are emitted to by the compiler itself to
742
808
/// create the Argument structures that are passed into the `format` function.
743
- #[ doc( hidden) ]
809
+ #[ doc( hidden) ] # [ inline ]
744
810
pub fn argument < ' a , T > ( f : extern "Rust" fn ( & T , & mut Formatter ) ,
745
811
t : & ' a T ) -> Argument < ' a > {
746
812
unsafe {
@@ -753,14 +819,14 @@ pub fn argument<'a, T>(f: extern "Rust" fn(&T, &mut Formatter),
753
819
754
820
/// When the compiler determines that the type of an argument *must* be a string
755
821
/// (such as for select), then it invokes this method.
756
- #[ doc( hidden) ]
822
+ #[ doc( hidden) ] # [ inline ]
757
823
pub fn argumentstr < ' a > ( s : & ' a & str ) -> Argument < ' a > {
758
824
argument ( String :: fmt, s)
759
825
}
760
826
761
827
/// When the compiler determines that the type of an argument *must* be a uint
762
828
/// (such as for plural), then it invokes this method.
763
- #[ doc( hidden) ]
829
+ #[ doc( hidden) ] # [ inline ]
764
830
pub fn argumentuint < ' a > ( s : & ' a uint ) -> Argument < ' a > {
765
831
argument ( Unsigned :: fmt, s)
766
832
}
@@ -899,14 +965,8 @@ impl<T> Pointer for *T {
899
965
}
900
966
}
901
967
}
902
-
903
968
impl < T > Pointer for * mut T {
904
- fn fmt ( t : & * mut T , f : & mut Formatter ) {
905
- f. flags |= 1 << ( parse:: FlagAlternate as uint ) ;
906
- do :: uint:: to_str_bytes ( * t as uint , 16 ) |buf| {
907
- f. pad_integral ( buf, "0x" , true ) ;
908
- }
909
- }
969
+ fn fmt ( t : & * mut T , f : & mut Formatter ) { Pointer :: fmt ( & ( * t as * T ) , f) }
910
970
}
911
971
912
972
// Implementation of Default for various core types
@@ -940,7 +1000,6 @@ delegate!(f64 to Float)
940
1000
impl < T > Default for * T {
941
1001
fn fmt ( me : & * T , f : & mut Formatter ) { Pointer :: fmt ( me, f) }
942
1002
}
943
-
944
1003
impl < T > Default for * mut T {
945
1004
fn fmt ( me : & * mut T , f : & mut Formatter ) { Pointer :: fmt ( me, f) }
946
1005
}
0 commit comments