]> git.lizzy.rs Git - rust.git/blob - src/libstd/fmt/mod.rs
De-~[] Mem{Reader,Writer}
[rust.git] / src / libstd / fmt / mod.rs
1 // Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 /*!
12
13 Utilities for formatting and printing strings
14
15 This module contains the runtime support for the `format!` syntax extension.
16 This macro is implemented in the compiler to emit calls to this module in order
17 to format arguments at runtime into strings and streams.
18
19 The functions contained in this module should not normally be used in everyday
20 use cases of `format!`. The assumptions made by these functions are unsafe for
21 all inputs, and the compiler performs a large amount of validation on the
22 arguments to `format!` in order to ensure safety at runtime. While it is
23 possible to call these functions directly, it is not recommended to do so in the
24 general case.
25
26 ## Usage
27
28 The `format!` macro is intended to be familiar to those coming from C's
29 printf/fprintf functions or Python's `str.format` function. In its current
30 revision, the `format!` macro returns a `~str` type which is the result of the
31 formatting. In the future it will also be able to pass in a stream to format
32 arguments directly while performing minimal allocations.
33
34 Some examples of the `format!` extension are:
35
36 ```rust
37 format!("Hello");                 // => ~"Hello"
38 format!("Hello, {:s}!", "world"); // => ~"Hello, world!"
39 format!("The number is {:d}", 1); // => ~"The number is 1"
40 format!("{:?}", ~[3, 4]);         // => ~"~[3, 4]"
41 format!("{value}", value=4);      // => ~"4"
42 format!("{} {}", 1, 2);           // => ~"1 2"
43 ```
44
45 From these, you can see that the first argument is a format string. It is
46 required by the compiler for this to be a string literal; it cannot be a
47 variable passed in (in order to perform validity checking). The compiler will
48 then parse the format string and determine if the list of arguments provided is
49 suitable to pass to this format string.
50
51 ### Positional parameters
52
53 Each formatting argument is allowed to specify which value argument it's
54 referencing, and if omitted it is assumed to be "the next argument". For
55 example, the format string `{} {} {}` would take three parameters, and they
56 would be formatted in the same order as they're given. The format string
57 `{2} {1} {0}`, however, would format arguments in reverse order.
58
59 Things can get a little tricky once you start intermingling the two types of
60 positional specifiers. The "next argument" specifier can be thought of as an
61 iterator over the argument. Each time a "next argument" specifier is seen, the
62 iterator advances. This leads to behavior like this:
63
64 ```rust
65 format!("{1} {} {0} {}", 1, 2); // => ~"2 1 1 2"
66 ```
67
68 The internal iterator over the argument has not been advanced by the time the
69 first `{}` is seen, so it prints the first argument. Then upon reaching the
70 second `{}`, the iterator has advanced forward to the second argument.
71 Essentially, parameters which explicitly name their argument do not affect
72 parameters which do not name an argument in terms of positional specifiers.
73
74 A format string is required to use all of its arguments, otherwise it is a
75 compile-time error. You may refer to the same argument more than once in the
76 format string, although it must always be referred to with the same type.
77
78 ### Named parameters
79
80 Rust itself does not have a Python-like equivalent of named parameters to a
81 function, but the `format!` macro is a syntax extension which allows it to
82 leverage named parameters. Named parameters are listed at the end of the
83 argument list and have the syntax:
84
85 ```notrust
86 identifier '=' expression
87 ```
88
89 For example, the following `format!` expressions all use named argument:
90
91 ```rust
92 format!("{argument}", argument = "test");       // => ~"test"
93 format!("{name} {}", 1, name = 2);              // => ~"2 1"
94 format!("{a:s} {c:d} {b:?}", a="a", b=(), c=3); // => ~"a 3 ()"
95 ```
96
97 It is illegal to put positional parameters (those without names) after arguments
98 which have names. Like positional parameters, it is illegal to provided named
99 parameters that are unused by the format string.
100
101 ### Argument types
102
103 Each argument's type is dictated by the format string. It is a requirement that
104 every argument is only ever referred to by one type. When specifying the format
105 of an argument, however, a string like `{}` indicates no type. This is allowed,
106 and if all references to one argument do not provide a type, then the format `?`
107 is used (the type's rust-representation is printed). For example, this is an
108 invalid format string:
109
110 ```notrust
111 {0:d} {0:s}
112 ```
113
114 Because the first argument is both referred to as an integer as well as a
115 string.
116
117 Because formatting is done via traits, there is no requirement that the
118 `d` format actually takes an `int`, but rather it simply requires a type which
119 ascribes to the `Signed` formatting trait. There are various parameters which do
120 require a particular type, however. Namely if the syntax `{:.*s}` is used, then
121 the number of characters to print from the string precedes the actual string and
122 must have the type `uint`. Although a `uint` can be printed with `{:u}`, it is
123 illegal to reference an argument as such. For example, this is another invalid
124 format string:
125
126 ```notrust
127 {:.*s} {0:u}
128 ```
129
130 ### Formatting traits
131
132 When requesting that an argument be formatted with a particular type, you are
133 actually requesting that an argument ascribes to a particular trait. This allows
134 multiple actual types to be formatted via `{:d}` (like `i8` as well as `int`).
135 The current mapping of types to traits is:
136
137 * `?` ⇒ `Poly`
138 * `d` ⇒ `Signed`
139 * `i` ⇒ `Signed`
140 * `u` ⇒ `Unsigned`
141 * `b` ⇒ `Bool`
142 * `c` ⇒ `Char`
143 * `o` ⇒ `Octal`
144 * `x` ⇒ `LowerHex`
145 * `X` ⇒ `UpperHex`
146 * `s` ⇒ `String`
147 * `p` ⇒ `Pointer`
148 * `t` ⇒ `Binary`
149 * `f` ⇒ `Float`
150 * `e` ⇒ `LowerExp`
151 * `E` ⇒ `UpperExp`
152 * *nothing* ⇒ `Show`
153
154 What this means is that any type of argument which implements the
155 `std::fmt::Binary` trait can then be formatted with `{:t}`. Implementations are
156 provided for these traits for a number of primitive types by the standard
157 library as well. If no format is specified (as in `{}` or `{:6}`), then the
158 format trait used is the `Show` trait. This is one of the more commonly
159 implemented traits when formatting a custom type.
160
161 When implementing a format trait for your own type, you will have to implement a
162 method of the signature:
163
164 ```rust
165 # use std;
166 # mod fmt { pub type Result = (); }
167 # struct T;
168 # trait SomeName<T> {
169 fn fmt(&self, f: &mut std::fmt::Formatter) -> fmt::Result;
170 # }
171 ```
172
173 Your type will be passed as `self` by-reference, and then the function should
174 emit output into the `f.buf` stream. It is up to each format trait
175 implementation to correctly adhere to the requested formatting parameters. The
176 values of these parameters will be listed in the fields of the `Formatter`
177 struct. In order to help with this, the `Formatter` struct also provides some
178 helper methods.
179
180 Additionally, the return value of this function is `fmt::Result` which is a
181 typedef to `Result<(), IoError>` (also known as `IoError<()>`). Formatting
182 implementations should ensure that they return errors from `write!` correctly
183 (propagating errors upward).
184
185 An example of implementing the formatting traits would look
186 like:
187
188 ```rust
189 use std::fmt;
190 use std::f64;
191
192 struct Vector2D {
193     x: int,
194     y: int,
195 }
196
197 impl fmt::Show for Vector2D {
198     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
199         // The `f.buf` value is of the type `&mut io::Writer`, which is what the
200         // write! macro is expecting. Note that this formatting ignores the
201         // various flags provided to format strings.
202         write!(f.buf, "({}, {})", self.x, self.y)
203     }
204 }
205
206 // Different traits allow different forms of output of a type. The meaning of
207 // this format is to print the magnitude of a vector.
208 impl fmt::Binary for Vector2D {
209     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
210         let magnitude = (self.x * self.x + self.y * self.y) as f64;
211         let magnitude = magnitude.sqrt();
212
213         // Respect the formatting flags by using the helper method
214         // `pad_integral` on the Formatter object. See the method documentation
215         // for details, and the function `pad` can be used to pad strings.
216         let decimals = f.precision.unwrap_or(3);
217         let string = f64::to_str_exact(magnitude, decimals);
218         f.pad_integral(true, "", string.as_bytes())
219     }
220 }
221
222 fn main() {
223     let myvector = Vector2D { x: 3, y: 4 };
224
225     println!("{}", myvector);       // => "(3, 4)"
226     println!("{:10.3t}", myvector); // => "     5.000"
227 }
228 ```
229
230 ### Related macros
231
232 There are a number of related macros in the `format!` family. The ones that are
233 currently implemented are:
234
235 ```ignore
236 format!      // described above
237 write!       // first argument is a &mut io::Writer, the destination
238 writeln!     // same as write but appends a newline
239 print!       // the format string is printed to the standard output
240 println!     // same as print but appends a newline
241 format_args! // described below.
242 ```
243
244
245 #### `write!`
246
247 This and `writeln` are two macros which are used to emit the format string to a
248 specified stream. This is used to prevent intermediate allocations of format
249 strings and instead directly write the output. Under the hood, this function is
250 actually invoking the `write` function defined in this module. Example usage is:
251
252 ```rust
253 # #[allow(unused_must_use)];
254 use std::io;
255
256 let mut w = io::MemWriter::new();
257 write!(&mut w as &mut io::Writer, "Hello {}!", "world");
258 ```
259
260 #### `print!`
261
262 This and `println` emit their output to stdout. Similarly to the `write!` macro,
263 the goal of these macros is to avoid intermediate allocations when printing
264 output. Example usage is:
265
266 ```rust
267 print!("Hello {}!", "world");
268 println!("I have a newline {}", "character at the end");
269 ```
270
271 #### `format_args!`
272 This is a curious macro which is used to safely pass around
273 an opaque object describing the format string. This object
274 does not require any heap allocations to create, and it only
275 references information on the stack. Under the hood, all of
276 the related macros are implemented in terms of this. First
277 off, some example usage is:
278
279 ```ignore
280 use std::fmt;
281
282 # fn lol<T>() -> T { fail!() }
283 # let my_writer: &mut ::std::io::Writer = lol();
284 # let my_fn: fn(&fmt::Arguments) = lol();
285
286 format_args!(fmt::format, "this returns {}", "~str");
287 format_args!(|args| { fmt::write(my_writer, args) }, "some {}", "args");
288 format_args!(my_fn, "format {}", "string");
289 ```
290
291 The first argument of the `format_args!` macro is a function (or closure) which
292 takes one argument of type `&fmt::Arguments`. This structure can then be
293 passed to the `write` and `format` functions inside this module in order to
294 process the format string. The goal of this macro is to even further prevent
295 intermediate allocations when dealing formatting strings.
296
297 For example, a logging library could use the standard formatting syntax, but it
298 would internally pass around this structure until it has been determined where
299 output should go to.
300
301 It is unsafe to programmatically create an instance of `fmt::Arguments` because
302 the operations performed when executing a format string require the compile-time
303 checks provided by the compiler. The `format_args!` macro is the only method of
304 safely creating these structures, but they can be unsafely created with the
305 constructor provided.
306
307 ## Internationalization
308
309 The formatting syntax supported by the `format!` extension supports
310 internationalization by providing "methods" which execute various different
311 outputs depending on the input. The syntax and methods provided are similar to
312 other internationalization systems, so again nothing should seem alien.
313 Currently two methods are supported by this extension: "select" and "plural".
314
315 Each method will execute one of a number of clauses, and then the value of the
316 clause will become what's the result of the argument's format. Inside of the
317 cases, nested argument strings may be provided, but all formatting arguments
318 must not be done through implicit positional means. All arguments inside of each
319 case of a method must be explicitly selected by their name or their integer
320 position.
321
322 Furthermore, whenever a case is running, the special character `#` can be used
323 to reference the string value of the argument which was selected upon. As an
324 example:
325
326 ```rust
327 format!("{0, select, other{#}}", "hello"); // => ~"hello"
328 ```
329
330 This example is the equivalent of `{0:s}` essentially.
331
332 ### Select
333
334 The select method is a switch over a `&str` parameter, and the parameter *must*
335 be of the type `&str`. An example of the syntax is:
336
337 ```notrust
338 {0, select, male{...} female{...} other{...}}
339 ```
340
341 Breaking this down, the `0`-th argument is selected upon with the `select`
342 method, and then a number of cases follow. Each case is preceded by an
343 identifier which is the match-clause to execute the given arm. In this case,
344 there are two explicit cases, `male` and `female`. The case will be executed if
345 the string argument provided is an exact match to the case selected.
346
347 The `other` case is also a required case for all `select` methods. This arm will
348 be executed if none of the other arms matched the word being selected over.
349
350 ### Plural
351
352 The plural method is a switch statement over a `uint` parameter, and the
353 parameter *must* be a `uint`. A plural method in its full glory can be specified
354 as:
355
356 ```notrust
357 {0, plural, offset=1 =1{...} two{...} many{...} other{...}}
358 ```
359
360 To break this down, the first `0` indicates that this method is selecting over
361 the value of the first positional parameter to the format string. Next, the
362 `plural` method is being executed. An optionally-supplied `offset` is then given
363 which indicates a number to subtract from argument `0` when matching. This is
364 then followed by a list of cases.
365
366 Each case is allowed to supply a specific value to match upon with the syntax
367 `=N`. This case is executed if the value at argument `0` matches N exactly,
368 without taking the offset into account. A case may also be specified by one of
369 five keywords: `zero`, `one`, `two`, `few`, and `many`. These cases are matched
370 on after argument `0` has the offset taken into account. Currently the
371 definitions of `many` and `few` are hardcoded, but they are in theory defined by
372 the current locale.
373
374 Finally, all `plural` methods must have an `other` case supplied which will be
375 executed if none of the other cases match.
376
377 ## Syntax
378
379 The syntax for the formatting language used is drawn from other languages, so it
380 should not be too alien. Arguments are formatted with python-like syntax,
381 meaning that arguments are surrounded by `{}` instead of the C-like `%`. The
382 actual grammar for the formatting syntax is:
383
384 ```notrust
385 format_string := <text> [ format <text> ] *
386 format := '{' [ argument ] [ ':' format_spec ] [ ',' function_spec ] '}'
387 argument := integer | identifier
388
389 format_spec := [[fill]align][sign]['#'][0][width]['.' precision][type]
390 fill := character
391 align := '<' | '>'
392 sign := '+' | '-'
393 width := count
394 precision := count | '*'
395 type := identifier | ''
396 count := parameter | integer
397 parameter := integer '$'
398
399 function_spec := plural | select
400 select := 'select' ',' ( identifier arm ) *
401 plural := 'plural' ',' [ 'offset:' integer ] ( selector arm ) *
402 selector := '=' integer | keyword
403 keyword := 'zero' | 'one' | 'two' | 'few' | 'many' | 'other'
404 arm := '{' format_string '}'
405 ```
406
407 ## Formatting Parameters
408
409 Each argument being formatted can be transformed by a number of formatting
410 parameters (corresponding to `format_spec` in the syntax above). These
411 parameters affect the string representation of what's being formatted. This
412 syntax draws heavily from Python's, so it may seem a bit familiar.
413
414 ### Fill/Alignment
415
416 The fill character is provided normally in conjunction with the `width`
417 parameter. This indicates that if the value being formatted is smaller than
418 `width` some extra characters will be printed around it. The extra characters
419 are specified by `fill`, and the alignment can be one of two options:
420
421 * `<` - the argument is left-aligned in `width` columns
422 * `>` - the argument is right-aligned in `width` columns
423
424 ### Sign/#/0
425
426 These can all be interpreted as flags for a particular formatter.
427
428 * '+' - This is intended for numeric types and indicates that the sign should
429         always be printed. Positive signs are never printed by default, and the
430         negative sign is only printed by default for the `Signed` trait. This
431         flag indicates that the correct sign (+ or -) should always be printed.
432 * '-' - Currently not used
433 * '#' - This flag is indicates that the "alternate" form of printing should be
434         used. By default, this only applies to the integer formatting traits and
435         performs like:
436     * `x` - precedes the argument with a "0x"
437     * `X` - precedes the argument with a "0x"
438     * `t` - precedes the argument with a "0b"
439     * `o` - precedes the argument with a "0o"
440 * '0' - This is used to indicate for integer formats that the padding should
441         both be done with a `0` character as well as be sign-aware. A format
442         like `{:08d}` would yield `00000001` for the integer `1`, while the same
443         format would yield `-0000001` for the integer `-1`. Notice that the
444         negative version has one fewer zero than the positive version.
445
446 ### Width
447
448 This is a parameter for the "minimum width" that the format should take up. If
449 the value's string does not fill up this many characters, then the padding
450 specified by fill/alignment will be used to take up the required space.
451
452 The default fill/alignment for non-numerics is a space and left-aligned. The
453 defaults for numeric formatters is also a space but with right-alignment. If the
454 '0' flag is specified for numerics, then the implicit fill character is '0'.
455
456 The value for the width can also be provided as a `uint` in the list of
457 parameters by using the `2$` syntax indicating that the second argument is a
458 `uint` specifying the width.
459
460 ### Precision
461
462 For non-numeric types, this can be considered a "maximum width". If the
463 resulting string is longer than this width, then it is truncated down to this
464 many characters and only those are emitted.
465
466 For integral types, this has no meaning currently.
467
468 For floating-point types, this indicates how many digits after the decimal point
469 should be printed.
470
471 ## Escaping
472
473 The literal characters `{`, `}`, or `#` may be included in a string by
474 preceding them with the `\` character. Since `\` is already an
475 escape character in Rust strings, a string literal using this escape
476 will look like `"\\{"`.
477
478 */
479
480 use any;
481 use cast;
482 use char::Char;
483 use container::Container;
484 use io::MemWriter;
485 use io;
486 use iter::{Iterator, range};
487 use num::Signed;
488 use option::{Option,Some,None};
489 use repr;
490 use result::{Ok, Err};
491 use str::StrSlice;
492 use str;
493 use slice::{Vector, ImmutableVector};
494 use slice;
495
496 pub use self::num::radix;
497 pub use self::num::Radix;
498 pub use self::num::RadixFmt;
499
500 mod num;
501 pub mod parse;
502 pub mod rt;
503
504 pub type Result = io::IoResult<()>;
505
506 /// A struct to represent both where to emit formatting strings to and how they
507 /// should be formatted. A mutable version of this is passed to all formatting
508 /// traits.
509 pub struct Formatter<'a> {
510     /// Flags for formatting (packed version of rt::Flag)
511     pub flags: uint,
512     /// Character used as 'fill' whenever there is alignment
513     pub fill: char,
514     /// Boolean indication of whether the output should be left-aligned
515     pub align: parse::Alignment,
516     /// Optionally specified integer width that the output should be
517     pub width: Option<uint>,
518     /// Optionally specified precision for numeric types
519     pub precision: Option<uint>,
520
521     /// Output buffer.
522     pub buf: &'a mut io::Writer,
523     curarg: slice::Items<'a, Argument<'a>>,
524     args: &'a [Argument<'a>],
525 }
526
527 /// This struct represents the generic "argument" which is taken by the Xprintf
528 /// family of functions. It contains a function to format the given value. At
529 /// compile time it is ensured that the function and the value have the correct
530 /// types, and then this struct is used to canonicalize arguments to one type.
531 pub struct Argument<'a> {
532     formatter: extern "Rust" fn(&any::Void, &mut Formatter) -> Result,
533     value: &'a any::Void,
534 }
535
536 impl<'a> Arguments<'a> {
537     /// When using the format_args!() macro, this function is used to generate the
538     /// Arguments structure. The compiler inserts an `unsafe` block to call this,
539     /// which is valid because the compiler performs all necessary validation to
540     /// ensure that the resulting call to format/write would be safe.
541     #[doc(hidden)] #[inline]
542     pub unsafe fn new<'a>(fmt: &'static [rt::Piece<'static>],
543                           args: &'a [Argument<'a>]) -> Arguments<'a> {
544         Arguments{ fmt: cast::transmute(fmt), args: args }
545     }
546 }
547
548 /// This structure represents a safely precompiled version of a format string
549 /// and its arguments. This cannot be generated at runtime because it cannot
550 /// safely be done so, so no constructors are given and the fields are private
551 /// to prevent modification.
552 ///
553 /// The `format_args!` macro will safely create an instance of this structure
554 /// and pass it to a user-supplied function. The macro validates the format
555 /// string at compile-time so usage of the `write` and `format` functions can
556 /// be safely performed.
557 pub struct Arguments<'a> {
558     fmt: &'a [rt::Piece<'a>],
559     args: &'a [Argument<'a>],
560 }
561
562 /// When a format is not otherwise specified, types are formatted by ascribing
563 /// to this trait. There is not an explicit way of selecting this trait to be
564 /// used for formatting, it is only if no other format is specified.
565 pub trait Show {
566     /// Formats the value using the given formatter.
567     fn fmt(&self, &mut Formatter) -> Result;
568 }
569
570 /// Format trait for the `b` character
571 pub trait Bool {
572     /// Formats the value using the given formatter.
573     fn fmt(&self, &mut Formatter) -> Result;
574 }
575
576 /// Format trait for the `c` character
577 pub trait Char {
578     /// Formats the value using the given formatter.
579     fn fmt(&self, &mut Formatter) -> Result;
580 }
581
582 /// Format trait for the `i` and `d` characters
583 pub trait Signed {
584     /// Formats the value using the given formatter.
585     fn fmt(&self, &mut Formatter) -> Result;
586 }
587
588 /// Format trait for the `u` character
589 pub trait Unsigned {
590     /// Formats the value using the given formatter.
591     fn fmt(&self, &mut Formatter) -> Result;
592 }
593
594 /// Format trait for the `o` character
595 pub trait Octal {
596     /// Formats the value using the given formatter.
597     fn fmt(&self, &mut Formatter) -> Result;
598 }
599
600 /// Format trait for the `t` character
601 pub trait Binary {
602     /// Formats the value using the given formatter.
603     fn fmt(&self, &mut Formatter) -> Result;
604 }
605
606 /// Format trait for the `x` character
607 pub trait LowerHex {
608     /// Formats the value using the given formatter.
609     fn fmt(&self, &mut Formatter) -> Result;
610 }
611
612 /// Format trait for the `X` character
613 pub trait UpperHex {
614     /// Formats the value using the given formatter.
615     fn fmt(&self, &mut Formatter) -> Result;
616 }
617
618 /// Format trait for the `s` character
619 pub trait String {
620     /// Formats the value using the given formatter.
621     fn fmt(&self, &mut Formatter) -> Result;
622 }
623
624 /// Format trait for the `?` character
625 pub trait Poly {
626     /// Formats the value using the given formatter.
627     fn fmt(&self, &mut Formatter) -> Result;
628 }
629
630 /// Format trait for the `p` character
631 pub trait Pointer {
632     /// Formats the value using the given formatter.
633     fn fmt(&self, &mut Formatter) -> Result;
634 }
635
636 /// Format trait for the `f` character
637 pub trait Float {
638     /// Formats the value using the given formatter.
639     fn fmt(&self, &mut Formatter) -> Result;
640 }
641
642 /// Format trait for the `e` character
643 pub trait LowerExp {
644     /// Formats the value using the given formatter.
645     fn fmt(&self, &mut Formatter) -> Result;
646 }
647
648 /// Format trait for the `E` character
649 pub trait UpperExp {
650     /// Formats the value using the given formatter.
651     fn fmt(&self, &mut Formatter) -> Result;
652 }
653
654 // FIXME #11938 - UFCS would make us able call the above methods
655 // directly Show::show(x, fmt).
656 macro_rules! uniform_fn_call_workaround {
657     ($( $name: ident, $trait_: ident; )*) => {
658         $(
659             #[doc(hidden)]
660             pub fn $name<T: $trait_>(x: &T, fmt: &mut Formatter) -> Result {
661                 x.fmt(fmt)
662             }
663             )*
664     }
665 }
666 uniform_fn_call_workaround! {
667     secret_show, Show;
668     secret_bool, Bool;
669     secret_char, Char;
670     secret_signed, Signed;
671     secret_unsigned, Unsigned;
672     secret_octal, Octal;
673     secret_binary, Binary;
674     secret_lower_hex, LowerHex;
675     secret_upper_hex, UpperHex;
676     secret_string, String;
677     secret_poly, Poly;
678     secret_pointer, Pointer;
679     secret_float, Float;
680     secret_lower_exp, LowerExp;
681     secret_upper_exp, UpperExp;
682 }
683
684 /// The `write` function takes an output stream, a precompiled format string,
685 /// and a list of arguments. The arguments will be formatted according to the
686 /// specified format string into the output stream provided.
687 ///
688 /// # Arguments
689 ///
690 ///   * output - the buffer to write output to
691 ///   * args - the precompiled arguments generated by `format_args!`
692 ///
693 /// # Example
694 ///
695 /// ```rust
696 /// # #[allow(unused_must_use)];
697 /// use std::fmt;
698 /// use std::io;
699 ///
700 /// let mut w = io::stdout();
701 /// format_args!(|args| { fmt::write(&mut w, args); }, "Hello, {}!", "world");
702 /// ```
703 pub fn write(output: &mut io::Writer, args: &Arguments) -> Result {
704     unsafe { write_unsafe(output, args.fmt, args.args) }
705 }
706
707 /// The `writeln` function takes the same arguments as `write`, except that it
708 /// will also write a newline (`\n`) character at the end of the format string.
709 pub fn writeln(output: &mut io::Writer, args: &Arguments) -> Result {
710     let first = unsafe { write_unsafe(output, args.fmt, args.args) };
711     first.and_then(|()| output.write(['\n' as u8]))
712 }
713
714 /// The `write_unsafe` function takes an output stream, a precompiled format
715 /// string, and a list of arguments. The arguments will be formatted according
716 /// to the specified format string into the output stream provided.
717 ///
718 /// See the documentation for `format` for why this function is unsafe and care
719 /// should be taken if calling it manually.
720 ///
721 /// Thankfully the rust compiler provides macros like `write!` and
722 /// `format_args!` which perform all of this validation at compile-time
723 /// and provide a safe interface for invoking this function.
724 ///
725 /// # Arguments
726 ///
727 ///   * output - the buffer to write output to
728 ///   * fmts - the precompiled format string to emit
729 ///   * args - the list of arguments to the format string. These are only the
730 ///            positional arguments (not named)
731 ///
732 /// Note that this function assumes that there are enough arguments for the
733 /// format string.
734 pub unsafe fn write_unsafe(output: &mut io::Writer,
735                            fmt: &[rt::Piece],
736                            args: &[Argument]) -> Result {
737     let mut formatter = Formatter {
738         flags: 0,
739         width: None,
740         precision: None,
741         buf: output,
742         align: parse::AlignUnknown,
743         fill: ' ',
744         args: args,
745         curarg: args.iter(),
746     };
747     for piece in fmt.iter() {
748         try!(formatter.run(piece, None));
749     }
750     Ok(())
751 }
752
753 /// The format function takes a precompiled format string and a list of
754 /// arguments, to return the resulting formatted string.
755 ///
756 /// # Arguments
757 ///
758 ///   * args - a structure of arguments generated via the `format_args!` macro.
759 ///            Because this structure can only be safely generated at
760 ///            compile-time, this function is safe.
761 ///
762 /// # Example
763 ///
764 /// ```rust
765 /// use std::fmt;
766 ///
767 /// let s = format_args!(fmt::format, "Hello, {}!", "world");
768 /// assert_eq!(s, ~"Hello, world!");
769 /// ```
770 pub fn format(args: &Arguments) -> ~str {
771     unsafe { format_unsafe(args.fmt, args.args) }
772 }
773
774 /// The unsafe version of the formatting function.
775 ///
776 /// This is currently an unsafe function because the types of all arguments
777 /// aren't verified by immediate callers of this function. This currently does
778 /// not validate that the correct types of arguments are specified for each
779 /// format specifier, nor that each argument itself contains the right function
780 /// for formatting the right type value. Because of this, the function is marked
781 /// as `unsafe` if this is being called manually.
782 ///
783 /// Thankfully the rust compiler provides the macro `format!` which will perform
784 /// all of this validation at compile-time and provides a safe interface for
785 /// invoking this function.
786 ///
787 /// # Arguments
788 ///
789 ///   * fmts - the precompiled format string to emit.
790 ///   * args - the list of arguments to the format string. These are only the
791 ///            positional arguments (not named)
792 ///
793 /// Note that this function assumes that there are enough arguments for the
794 /// format string.
795 pub unsafe fn format_unsafe(fmt: &[rt::Piece], args: &[Argument]) -> ~str {
796     let mut output = MemWriter::new();
797     write_unsafe(&mut output as &mut io::Writer, fmt, args).unwrap();
798     return str::from_utf8(output.unwrap().as_slice()).unwrap().to_owned();
799 }
800
801 impl<'a> Formatter<'a> {
802
803     // First up is the collection of functions used to execute a format string
804     // at runtime. This consumes all of the compile-time statics generated by
805     // the format! syntax extension.
806
807     fn run(&mut self, piece: &rt::Piece, cur: Option<&str>) -> Result {
808         match *piece {
809             rt::String(s) => self.buf.write(s.as_bytes()),
810             rt::CurrentArgument(()) => self.buf.write(cur.unwrap().as_bytes()),
811             rt::Argument(ref arg) => {
812                 // Fill in the format parameters into the formatter
813                 self.fill = arg.format.fill;
814                 self.align = arg.format.align;
815                 self.flags = arg.format.flags;
816                 self.width = self.getcount(&arg.format.width);
817                 self.precision = self.getcount(&arg.format.precision);
818
819                 // Extract the correct argument
820                 let value = match arg.position {
821                     rt::ArgumentNext => { *self.curarg.next().unwrap() }
822                     rt::ArgumentIs(i) => self.args[i],
823                 };
824
825                 // Then actually do some printing
826                 match arg.method {
827                     None => (value.formatter)(value.value, self),
828                     Some(ref method) => self.execute(*method, value)
829                 }
830             }
831         }
832     }
833
834     fn getcount(&mut self, cnt: &rt::Count) -> Option<uint> {
835         match *cnt {
836             rt::CountIs(n) => { Some(n) }
837             rt::CountImplied => { None }
838             rt::CountIsParam(i) => {
839                 let v = self.args[i].value;
840                 unsafe { Some(*(v as *any::Void as *uint)) }
841             }
842             rt::CountIsNextParam => {
843                 let v = self.curarg.next().unwrap().value;
844                 unsafe { Some(*(v as *any::Void as *uint)) }
845             }
846         }
847     }
848
849     fn execute(&mut self, method: &rt::Method, arg: Argument) -> Result {
850         match *method {
851             // Pluralization is selection upon a numeric value specified as the
852             // parameter.
853             rt::Plural(offset, ref selectors, ref default) => {
854                 // This is validated at compile-time to be a pointer to a
855                 // '&uint' value.
856                 let value: &uint = unsafe { cast::transmute(arg.value) };
857                 let value = *value;
858
859                 // First, attempt to match against explicit values without the
860                 // offsetted value
861                 for s in selectors.iter() {
862                     match s.selector {
863                         rt::Literal(val) if value == val => {
864                             return self.runplural(value, s.result);
865                         }
866                         _ => {}
867                     }
868                 }
869
870                 // Next, offset the value and attempt to match against the
871                 // keyword selectors.
872                 let value = value - match offset { Some(i) => i, None => 0 };
873                 for s in selectors.iter() {
874                     let run = match s.selector {
875                         rt::Keyword(parse::Zero) => value == 0,
876                         rt::Keyword(parse::One) => value == 1,
877                         rt::Keyword(parse::Two) => value == 2,
878
879                         // FIXME: Few/Many should have a user-specified boundary
880                         //      One possible option would be in the function
881                         //      pointer of the 'arg: Argument' struct.
882                         rt::Keyword(parse::Few) => value < 8,
883                         rt::Keyword(parse::Many) => value >= 8,
884
885                         rt::Literal(..) => false
886                     };
887                     if run {
888                         return self.runplural(value, s.result);
889                     }
890                 }
891
892                 self.runplural(value, *default)
893             }
894
895             // Select is just a matching against the string specified.
896             rt::Select(ref selectors, ref default) => {
897                 // This is validated at compile-time to be a pointer to a
898                 // string slice,
899                 let value: & &str = unsafe { cast::transmute(arg.value) };
900                 let value = *value;
901
902                 for s in selectors.iter() {
903                     if s.selector == value {
904                         for piece in s.result.iter() {
905                             try!(self.run(piece, Some(value)));
906                         }
907                         return Ok(());
908                     }
909                 }
910                 for piece in default.iter() {
911                     try!(self.run(piece, Some(value)));
912                 }
913                 Ok(())
914             }
915         }
916     }
917
918     fn runplural(&mut self, value: uint, pieces: &[rt::Piece]) -> Result {
919         ::uint::to_str_bytes(value, 10, |buf| {
920             let valuestr = str::from_utf8(buf).unwrap();
921             for piece in pieces.iter() {
922                 try!(self.run(piece, Some(valuestr)));
923             }
924             Ok(())
925         })
926     }
927
928     // Helper methods used for padding and processing formatting arguments that
929     // all formatting traits can use.
930
931     /// Performs the correct padding for an integer which has already been
932     /// emitted into a byte-array. The byte-array should *not* contain the sign
933     /// for the integer, that will be added by this method.
934     ///
935     /// # Arguments
936     ///
937     /// * is_positive - whether the original integer was positive or not.
938     /// * prefix - if the '#' character (FlagAlternate) is provided, this
939     ///   is the prefix to put in front of the number.
940     /// * buf - the byte array that the number has been formatted into
941     ///
942     /// This function will correctly account for the flags provided as well as
943     /// the minimum width. It will not take precision into account.
944     pub fn pad_integral(&mut self, is_positive: bool, prefix: &str, buf: &[u8]) -> Result {
945         use fmt::parse::{FlagAlternate, FlagSignPlus, FlagSignAwareZeroPad};
946
947         let mut width = buf.len();
948
949         let mut sign = None;
950         if !is_positive {
951             sign = Some('-'); width += 1;
952         } else if self.flags & (1 << (FlagSignPlus as uint)) != 0 {
953             sign = Some('+'); width += 1;
954         }
955
956         let mut prefixed = false;
957         if self.flags & (1 << (FlagAlternate as uint)) != 0 {
958             prefixed = true; width += prefix.len();
959         }
960
961         // Writes the sign if it exists, and then the prefix if it was requested
962         let write_prefix = |f: &mut Formatter| {
963             for c in sign.move_iter() { try!(f.buf.write_char(c)); }
964             if prefixed { f.buf.write_str(prefix) }
965             else { Ok(()) }
966         };
967
968         // The `width` field is more of a `min-width` parameter at this point.
969         match self.width {
970             // If there's no minimum length requirements then we can just
971             // write the bytes.
972             None => {
973                 try!(write_prefix(self)); self.buf.write(buf)
974             }
975             // Check if we're over the minimum width, if so then we can also
976             // just write the bytes.
977             Some(min) if width >= min => {
978                 try!(write_prefix(self)); self.buf.write(buf)
979             }
980             // The sign and prefix goes before the padding if the fill character
981             // is zero
982             Some(min) if self.flags & (1 << (FlagSignAwareZeroPad as uint)) != 0 => {
983                 self.fill = '0';
984                 try!(write_prefix(self));
985                 self.with_padding(min - width, parse::AlignRight, |f| f.buf.write(buf))
986             }
987             // Otherwise, the sign and prefix goes after the padding
988             Some(min) => {
989                 self.with_padding(min - width, parse::AlignRight, |f| {
990                     try!(write_prefix(f)); f.buf.write(buf)
991                 })
992             }
993         }
994     }
995
996     /// This function takes a string slice and emits it to the internal buffer
997     /// after applying the relevant formatting flags specified. The flags
998     /// recognized for generic strings are:
999     ///
1000     /// * width - the minimum width of what to emit
1001     /// * fill/align - what to emit and where to emit it if the string
1002     ///                provided needs to be padded
1003     /// * precision - the maximum length to emit, the string is truncated if it
1004     ///               is longer than this length
1005     ///
1006     /// Notably this function ignored the `flag` parameters
1007     pub fn pad(&mut self, s: &str) -> Result {
1008         // Make sure there's a fast path up front
1009         if self.width.is_none() && self.precision.is_none() {
1010             return self.buf.write(s.as_bytes());
1011         }
1012         // The `precision` field can be interpreted as a `max-width` for the
1013         // string being formatted
1014         match self.precision {
1015             Some(max) => {
1016                 // If there's a maximum width and our string is longer than
1017                 // that, then we must always have truncation. This is the only
1018                 // case where the maximum length will matter.
1019                 let char_len = s.char_len();
1020                 if char_len >= max {
1021                     let nchars = ::cmp::min(max, char_len);
1022                     return self.buf.write(s.slice_chars(0, nchars).as_bytes());
1023                 }
1024             }
1025             None => {}
1026         }
1027         // The `width` field is more of a `min-width` parameter at this point.
1028         match self.width {
1029             // If we're under the maximum length, and there's no minimum length
1030             // requirements, then we can just emit the string
1031             None => self.buf.write(s.as_bytes()),
1032             // If we're under the maximum width, check if we're over the minimum
1033             // width, if so it's as easy as just emitting the string.
1034             Some(width) if s.char_len() >= width => {
1035                 self.buf.write(s.as_bytes())
1036             }
1037             // If we're under both the maximum and the minimum width, then fill
1038             // up the minimum width with the specified string + some alignment.
1039             Some(width) => {
1040                 self.with_padding(width - s.len(), parse::AlignLeft, |me| {
1041                     me.buf.write(s.as_bytes())
1042                 })
1043             }
1044         }
1045     }
1046
1047     /// Runs a callback, emitting the correct padding either before or
1048     /// afterwards depending on whether right or left alingment is requested.
1049     fn with_padding(&mut self,
1050                     padding: uint,
1051                     default: parse::Alignment,
1052                     f: |&mut Formatter| -> Result) -> Result {
1053         let align = match self.align {
1054             parse::AlignUnknown => default,
1055             parse::AlignLeft | parse::AlignRight => self.align
1056         };
1057         if align == parse::AlignLeft {
1058             try!(f(self));
1059         }
1060         let mut fill = [0u8, ..4];
1061         let len = self.fill.encode_utf8(fill);
1062         for _ in range(0, padding) {
1063             try!(self.buf.write(fill.slice_to(len)));
1064         }
1065         if align == parse::AlignRight {
1066             try!(f(self));
1067         }
1068         Ok(())
1069     }
1070 }
1071
1072 /// This is a function which calls are emitted to by the compiler itself to
1073 /// create the Argument structures that are passed into the `format` function.
1074 #[doc(hidden)] #[inline]
1075 pub fn argument<'a, T>(f: extern "Rust" fn(&T, &mut Formatter) -> Result,
1076                        t: &'a T) -> Argument<'a> {
1077     unsafe {
1078         Argument {
1079             formatter: cast::transmute(f),
1080             value: cast::transmute(t)
1081         }
1082     }
1083 }
1084
1085 /// When the compiler determines that the type of an argument *must* be a string
1086 /// (such as for select), then it invokes this method.
1087 #[doc(hidden)] #[inline]
1088 pub fn argumentstr<'a>(s: &'a &str) -> Argument<'a> {
1089     argument(secret_string, s)
1090 }
1091
1092 /// When the compiler determines that the type of an argument *must* be a uint
1093 /// (such as for plural), then it invokes this method.
1094 #[doc(hidden)] #[inline]
1095 pub fn argumentuint<'a>(s: &'a uint) -> Argument<'a> {
1096     argument(secret_unsigned, s)
1097 }
1098
1099 // Implementations of the core formatting traits
1100
1101 impl<T: Show> Show for @T {
1102     fn fmt(&self, f: &mut Formatter) -> Result { secret_show(&**self, f) }
1103 }
1104 impl<T: Show> Show for ~T {
1105     fn fmt(&self, f: &mut Formatter) -> Result { secret_show(&**self, f) }
1106 }
1107 impl<'a, T: Show> Show for &'a T {
1108     fn fmt(&self, f: &mut Formatter) -> Result { secret_show(*self, f) }
1109 }
1110
1111 impl Bool for bool {
1112     fn fmt(&self, f: &mut Formatter) -> Result {
1113         secret_string(&(if *self {"true"} else {"false"}), f)
1114     }
1115 }
1116
1117 impl<'a, T: str::Str> String for T {
1118     fn fmt(&self, f: &mut Formatter) -> Result {
1119         f.pad(self.as_slice())
1120     }
1121 }
1122
1123 impl Char for char {
1124     fn fmt(&self, f: &mut Formatter) -> Result {
1125         let mut utf8 = [0u8, ..4];
1126         let amt = self.encode_utf8(utf8);
1127         let s: &str = unsafe { cast::transmute(utf8.slice_to(amt)) };
1128         secret_string(&s, f)
1129     }
1130 }
1131
1132 macro_rules! floating(($ty:ident) => {
1133     impl Float for $ty {
1134         fn fmt(&self, fmt: &mut Formatter) -> Result {
1135             // FIXME: this shouldn't perform an allocation
1136             let s = match fmt.precision {
1137                 Some(i) => ::$ty::to_str_exact(self.abs(), i),
1138                 None => ::$ty::to_str_digits(self.abs(), 6)
1139             };
1140             fmt.pad_integral(*self >= 0.0, "", s.as_bytes())
1141         }
1142     }
1143
1144     impl LowerExp for $ty {
1145         fn fmt(&self, fmt: &mut Formatter) -> Result {
1146             // FIXME: this shouldn't perform an allocation
1147             let s = match fmt.precision {
1148                 Some(i) => ::$ty::to_str_exp_exact(self.abs(), i, false),
1149                 None => ::$ty::to_str_exp_digits(self.abs(), 6, false)
1150             };
1151             fmt.pad_integral(*self >= 0.0, "", s.as_bytes())
1152         }
1153     }
1154
1155     impl UpperExp for $ty {
1156         fn fmt(&self, fmt: &mut Formatter) -> Result {
1157             // FIXME: this shouldn't perform an allocation
1158             let s = match fmt.precision {
1159                 Some(i) => ::$ty::to_str_exp_exact(self.abs(), i, true),
1160                 None => ::$ty::to_str_exp_digits(self.abs(), 6, true)
1161             };
1162             fmt.pad_integral(*self >= 0.0, "", s.as_bytes())
1163         }
1164     }
1165 })
1166 floating!(f32)
1167 floating!(f64)
1168
1169 impl<T> Poly for T {
1170     fn fmt(&self, f: &mut Formatter) -> Result {
1171         match (f.width, f.precision) {
1172             (None, None) => {
1173                 repr::write_repr(f.buf, self)
1174             }
1175
1176             // If we have a specified width for formatting, then we have to make
1177             // this allocation of a new string
1178             _ => {
1179                 let s = repr::repr_to_str(self);
1180                 f.pad(s)
1181             }
1182         }
1183     }
1184 }
1185
1186 impl<T> Pointer for *T {
1187     fn fmt(&self, f: &mut Formatter) -> Result {
1188         f.flags |= 1 << (parse::FlagAlternate as uint);
1189         secret_lower_hex::<uint>(&(*self as uint), f)
1190     }
1191 }
1192 impl<T> Pointer for *mut T {
1193     fn fmt(&self, f: &mut Formatter) -> Result {
1194         secret_pointer::<*T>(&(*self as *T), f)
1195     }
1196 }
1197 impl<'a, T> Pointer for &'a T {
1198     fn fmt(&self, f: &mut Formatter) -> Result {
1199         secret_pointer::<*T>(&(&**self as *T), f)
1200     }
1201 }
1202 impl<'a, T> Pointer for &'a mut T {
1203     fn fmt(&self, f: &mut Formatter) -> Result {
1204         secret_pointer::<*T>(&(&**self as *T), f)
1205     }
1206 }
1207
1208 // Implementation of Show for various core types
1209
1210 macro_rules! delegate(($ty:ty to $other:ident) => {
1211     impl<'a> Show for $ty {
1212         fn fmt(&self, f: &mut Formatter) -> Result {
1213             (concat_idents!(secret_, $other)(self, f))
1214         }
1215     }
1216 })
1217 delegate!(~str to string)
1218 delegate!(&'a str to string)
1219 delegate!(bool to bool)
1220 delegate!(char to char)
1221 delegate!(f32 to float)
1222 delegate!(f64 to float)
1223
1224 impl<T> Show for *T {
1225     fn fmt(&self, f: &mut Formatter) -> Result { secret_pointer(self, f) }
1226 }
1227 impl<T> Show for *mut T {
1228     fn fmt(&self, f: &mut Formatter) -> Result { secret_pointer(self, f) }
1229 }
1230
1231 // If you expected tests to be here, look instead at the run-pass/ifmt.rs test,
1232 // it's a lot easier than creating all of the rt::Piece structures here.