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.
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.
11 //! Utilities for formatting and printing strings
13 #![allow(unused_variable)]
16 use cell::{Cell, Ref, RefMut};
18 use collections::Collection;
19 use iter::{Iterator, range};
23 use option::{Option, Some, None};
25 use result::{Ok, Err};
27 use slice::{Vector, ImmutableSlice};
32 pub use self::num::radix;
33 pub use self::num::Radix;
34 pub use self::num::RadixFmt;
40 pub type Result = result::Result<(), FormatError>;
42 /// The error type which is returned from formatting a message into a stream.
44 /// This type does not support transmission of an error other than that an error
45 /// occurred. Any extra information must be arranged to be transmitted through
47 pub enum FormatError {
48 /// A generic write error occurred during formatting, no other information
49 /// is transmitted via this variant.
53 /// A collection of methods that are required to format a message into a stream.
55 /// This trait is the type which this modules requires when formatting
56 /// information. This is similar to the standard library's `io::Writer` trait,
57 /// but it is only intended for use in libcore.
59 /// This trait should generally not be implemented by consumers of the standard
60 /// library. The `write!` macro accepts an instance of `io::Writer`, and the
61 /// `io::Writer` trait is favored over implementing this trait.
62 pub trait FormatWriter {
63 /// Writes a slice of bytes into this writer, returning whether the write
66 /// This method can only succeed if the entire byte slice was successfully
67 /// written, and this method will not return until all data has been
68 /// written or an error occurs.
72 /// This function will return an instance of `FormatError` on error.
73 fn write(&mut self, bytes: &[u8]) -> Result;
75 /// Glue for usage of the `write!` macro with implementers of this trait.
77 /// This method should generally not be invoked manually, but rather through
78 /// the `write!` macro itself.
79 fn write_fmt(&mut self, args: &Arguments) -> Result { write(self, args) }
82 /// A struct to represent both where to emit formatting strings to and how they
83 /// should be formatted. A mutable version of this is passed to all formatting
85 pub struct Formatter<'a> {
86 /// Flags for formatting (packed version of rt::Flag)
88 /// Character used as 'fill' whenever there is alignment
90 /// Boolean indication of whether the output should be left-aligned
91 pub align: rt::Alignment,
92 /// Optionally specified integer width that the output should be
93 pub width: Option<uint>,
94 /// Optionally specified precision for numeric types
95 pub precision: Option<uint>,
97 buf: &'a mut FormatWriter,
98 curarg: slice::Items<'a, Argument<'a>>,
99 args: &'a [Argument<'a>],
104 /// This struct represents the generic "argument" which is taken by the Xprintf
105 /// family of functions. It contains a function to format the given value. At
106 /// compile time it is ensured that the function and the value have the correct
107 /// types, and then this struct is used to canonicalize arguments to one type.
108 pub struct Argument<'a> {
109 formatter: extern "Rust" fn(&Void, &mut Formatter) -> Result,
113 impl<'a> Arguments<'a> {
114 /// When using the format_args!() macro, this function is used to generate the
115 /// Arguments structure. The compiler inserts an `unsafe` block to call this,
116 /// which is valid because the compiler performs all necessary validation to
117 /// ensure that the resulting call to format/write would be safe.
118 #[doc(hidden)] #[inline]
119 pub unsafe fn new<'a>(fmt: &'static [rt::Piece<'static>],
120 args: &'a [Argument<'a>]) -> Arguments<'a> {
121 Arguments{ fmt: mem::transmute(fmt), args: args }
125 /// This structure represents a safely precompiled version of a format string
126 /// and its arguments. This cannot be generated at runtime because it cannot
127 /// safely be done so, so no constructors are given and the fields are private
128 /// to prevent modification.
130 /// The `format_args!` macro will safely create an instance of this structure
131 /// and pass it to a function or closure, passed as the first argument. The
132 /// macro validates the format string at compile-time so usage of the `write`
133 /// and `format` functions can be safely performed.
134 pub struct Arguments<'a> {
135 fmt: &'a [rt::Piece<'a>],
136 args: &'a [Argument<'a>],
139 impl<'a> Show for Arguments<'a> {
140 fn fmt(&self, fmt: &mut Formatter) -> Result {
145 /// When a format is not otherwise specified, types are formatted by ascribing
146 /// to this trait. There is not an explicit way of selecting this trait to be
147 /// used for formatting, it is only if no other format is specified.
149 /// Formats the value using the given formatter.
150 fn fmt(&self, &mut Formatter) -> Result;
153 /// Format trait for the `b` character
155 /// Formats the value using the given formatter.
156 fn fmt(&self, &mut Formatter) -> Result;
159 /// Format trait for the `c` character
161 /// Formats the value using the given formatter.
162 fn fmt(&self, &mut Formatter) -> Result;
165 /// Format trait for the `i` and `d` characters
167 /// Formats the value using the given formatter.
168 fn fmt(&self, &mut Formatter) -> Result;
171 /// Format trait for the `u` character
173 /// Formats the value using the given formatter.
174 fn fmt(&self, &mut Formatter) -> Result;
177 /// Format trait for the `o` character
179 /// Formats the value using the given formatter.
180 fn fmt(&self, &mut Formatter) -> Result;
183 /// Format trait for the `t` character
185 /// Formats the value using the given formatter.
186 fn fmt(&self, &mut Formatter) -> Result;
189 /// Format trait for the `x` character
191 /// Formats the value using the given formatter.
192 fn fmt(&self, &mut Formatter) -> Result;
195 /// Format trait for the `X` character
197 /// Formats the value using the given formatter.
198 fn fmt(&self, &mut Formatter) -> Result;
201 /// Format trait for the `s` character
203 /// Formats the value using the given formatter.
204 fn fmt(&self, &mut Formatter) -> Result;
207 /// Format trait for the `p` character
209 /// Formats the value using the given formatter.
210 fn fmt(&self, &mut Formatter) -> Result;
213 /// Format trait for the `f` character
215 /// Formats the value using the given formatter.
216 fn fmt(&self, &mut Formatter) -> Result;
219 /// Format trait for the `e` character
221 /// Formats the value using the given formatter.
222 fn fmt(&self, &mut Formatter) -> Result;
225 /// Format trait for the `E` character
227 /// Formats the value using the given formatter.
228 fn fmt(&self, &mut Formatter) -> Result;
231 // FIXME #11938 - UFCS would make us able call the above methods
232 // directly Show::show(x, fmt).
233 macro_rules! uniform_fn_call_workaround {
234 ($( $name: ident, $trait_: ident; )*) => {
237 pub fn $name<T: $trait_>(x: &T, fmt: &mut Formatter) -> Result {
243 uniform_fn_call_workaround! {
247 secret_signed, Signed;
248 secret_unsigned, Unsigned;
250 secret_binary, Binary;
251 secret_lower_hex, LowerHex;
252 secret_upper_hex, UpperHex;
253 secret_string, String;
254 secret_pointer, Pointer;
256 secret_lower_exp, LowerExp;
257 secret_upper_exp, UpperExp;
260 /// The `write` function takes an output stream, a precompiled format string,
261 /// and a list of arguments. The arguments will be formatted according to the
262 /// specified format string into the output stream provided.
266 /// * output - the buffer to write output to
267 /// * args - the precompiled arguments generated by `format_args!`
268 pub fn write(output: &mut FormatWriter, args: &Arguments) -> Result {
269 let mut formatter = Formatter {
274 align: rt::AlignUnknown,
277 curarg: args.args.iter(),
279 for piece in args.fmt.iter() {
280 try!(formatter.run(piece));
285 impl<'a> Formatter<'a> {
287 // First up is the collection of functions used to execute a format string
288 // at runtime. This consumes all of the compile-time statics generated by
289 // the format! syntax extension.
291 fn run(&mut self, piece: &rt::Piece) -> Result {
293 rt::String(s) => self.buf.write(s.as_bytes()),
294 rt::Argument(ref arg) => {
295 // Fill in the format parameters into the formatter
296 self.fill = arg.format.fill;
297 self.align = arg.format.align;
298 self.flags = arg.format.flags;
299 self.width = self.getcount(&arg.format.width);
300 self.precision = self.getcount(&arg.format.precision);
302 // Extract the correct argument
303 let value = match arg.position {
304 rt::ArgumentNext => { *self.curarg.next().unwrap() }
305 rt::ArgumentIs(i) => self.args[i],
308 // Then actually do some printing
309 (value.formatter)(value.value, self)
314 fn getcount(&mut self, cnt: &rt::Count) -> Option<uint> {
316 rt::CountIs(n) => { Some(n) }
317 rt::CountImplied => { None }
318 rt::CountIsParam(i) => {
319 let v = self.args[i].value;
320 unsafe { Some(*(v as *const _ as *const uint)) }
322 rt::CountIsNextParam => {
323 let v = self.curarg.next().unwrap().value;
324 unsafe { Some(*(v as *const _ as *const uint)) }
329 // Helper methods used for padding and processing formatting arguments that
330 // all formatting traits can use.
332 /// Performs the correct padding for an integer which has already been
333 /// emitted into a byte-array. The byte-array should *not* contain the sign
334 /// for the integer, that will be added by this method.
338 /// * is_positive - whether the original integer was positive or not.
339 /// * prefix - if the '#' character (FlagAlternate) is provided, this
340 /// is the prefix to put in front of the number.
341 /// * buf - the byte array that the number has been formatted into
343 /// This function will correctly account for the flags provided as well as
344 /// the minimum width. It will not take precision into account.
345 pub fn pad_integral(&mut self, is_positive: bool, prefix: &str,
346 buf: &[u8]) -> Result {
347 use fmt::rt::{FlagAlternate, FlagSignPlus, FlagSignAwareZeroPad};
349 let mut width = buf.len();
353 sign = Some('-'); width += 1;
354 } else if self.flags & (1 << (FlagSignPlus as uint)) != 0 {
355 sign = Some('+'); width += 1;
358 let mut prefixed = false;
359 if self.flags & (1 << (FlagAlternate as uint)) != 0 {
360 prefixed = true; width += prefix.len();
363 // Writes the sign if it exists, and then the prefix if it was requested
364 let write_prefix = |f: &mut Formatter| {
365 for c in sign.move_iter() {
366 let mut b = [0, ..4];
367 let n = c.encode_utf8(b);
368 try!(f.buf.write(b.slice_to(n)));
370 if prefixed { f.buf.write(prefix.as_bytes()) }
374 // The `width` field is more of a `min-width` parameter at this point.
376 // If there's no minimum length requirements then we can just
379 try!(write_prefix(self)); self.buf.write(buf)
381 // Check if we're over the minimum width, if so then we can also
382 // just write the bytes.
383 Some(min) if width >= min => {
384 try!(write_prefix(self)); self.buf.write(buf)
386 // The sign and prefix goes before the padding if the fill character
388 Some(min) if self.flags & (1 << (FlagSignAwareZeroPad as uint)) != 0 => {
390 try!(write_prefix(self));
391 self.with_padding(min - width, rt::AlignRight, |f| f.buf.write(buf))
393 // Otherwise, the sign and prefix goes after the padding
395 self.with_padding(min - width, rt::AlignRight, |f| {
396 try!(write_prefix(f)); f.buf.write(buf)
402 /// This function takes a string slice and emits it to the internal buffer
403 /// after applying the relevant formatting flags specified. The flags
404 /// recognized for generic strings are:
406 /// * width - the minimum width of what to emit
407 /// * fill/align - what to emit and where to emit it if the string
408 /// provided needs to be padded
409 /// * precision - the maximum length to emit, the string is truncated if it
410 /// is longer than this length
412 /// Notably this function ignored the `flag` parameters
413 pub fn pad(&mut self, s: &str) -> Result {
414 // Make sure there's a fast path up front
415 if self.width.is_none() && self.precision.is_none() {
416 return self.buf.write(s.as_bytes());
418 // The `precision` field can be interpreted as a `max-width` for the
419 // string being formatted
420 match self.precision {
422 // If there's a maximum width and our string is longer than
423 // that, then we must always have truncation. This is the only
424 // case where the maximum length will matter.
425 let char_len = s.char_len();
427 let nchars = ::cmp::min(max, char_len);
428 return self.buf.write(s.slice_chars(0, nchars).as_bytes());
433 // The `width` field is more of a `min-width` parameter at this point.
435 // If we're under the maximum length, and there's no minimum length
436 // requirements, then we can just emit the string
437 None => self.buf.write(s.as_bytes()),
438 // If we're under the maximum width, check if we're over the minimum
439 // width, if so it's as easy as just emitting the string.
440 Some(width) if s.char_len() >= width => {
441 self.buf.write(s.as_bytes())
443 // If we're under both the maximum and the minimum width, then fill
444 // up the minimum width with the specified string + some alignment.
446 self.with_padding(width - s.len(), rt::AlignLeft, |me| {
447 me.buf.write(s.as_bytes())
453 /// Runs a callback, emitting the correct padding either before or
454 /// afterwards depending on whether right or left alignment is requested.
455 fn with_padding(&mut self,
457 default: rt::Alignment,
458 f: |&mut Formatter| -> Result) -> Result {
459 let align = match self.align {
460 rt::AlignUnknown => default,
461 rt::AlignLeft | rt::AlignRight => self.align
463 if align == rt::AlignLeft {
466 let mut fill = [0u8, ..4];
467 let len = self.fill.encode_utf8(fill);
468 for _ in range(0, padding) {
469 try!(self.buf.write(fill.slice_to(len)));
471 if align == rt::AlignRight {
477 /// Writes some data to the underlying buffer contained within this
479 pub fn write(&mut self, data: &[u8]) -> Result {
483 /// Writes some formatted information into this instance
484 pub fn write_fmt(&mut self, fmt: &Arguments) -> Result {
489 /// This is a function which calls are emitted to by the compiler itself to
490 /// create the Argument structures that are passed into the `format` function.
491 #[doc(hidden)] #[inline]
492 pub fn argument<'a, T>(f: extern "Rust" fn(&T, &mut Formatter) -> Result,
493 t: &'a T) -> Argument<'a> {
496 formatter: mem::transmute(f),
497 value: mem::transmute(t)
502 /// When the compiler determines that the type of an argument *must* be a string
503 /// (such as for select), then it invokes this method.
504 #[doc(hidden)] #[inline]
505 pub fn argumentstr<'a>(s: &'a &str) -> Argument<'a> {
506 argument(secret_string, s)
509 /// When the compiler determines that the type of an argument *must* be a uint
510 /// (such as for plural), then it invokes this method.
511 #[doc(hidden)] #[inline]
512 pub fn argumentuint<'a>(s: &'a uint) -> Argument<'a> {
513 argument(secret_unsigned, s)
516 // Implementations of the core formatting traits
518 impl<'a, T: Show> Show for &'a T {
519 fn fmt(&self, f: &mut Formatter) -> Result { secret_show(*self, f) }
521 impl<'a, T: Show> Show for &'a mut T {
522 fn fmt(&self, f: &mut Formatter) -> Result { secret_show(&**self, f) }
524 impl<'a> Show for &'a Show {
525 fn fmt(&self, f: &mut Formatter) -> Result { (*self).fmt(f) }
529 fn fmt(&self, f: &mut Formatter) -> Result {
530 secret_string(&(if *self {"true"} else {"false"}), f)
534 impl<'a, T: str::Str> String for T {
535 fn fmt(&self, f: &mut Formatter) -> Result {
536 f.pad(self.as_slice())
541 fn fmt(&self, f: &mut Formatter) -> Result {
542 let mut utf8 = [0u8, ..4];
543 let amt = self.encode_utf8(utf8);
544 let s: &str = unsafe { mem::transmute(utf8.slice_to(amt)) };
549 impl<T> Pointer for *const T {
550 fn fmt(&self, f: &mut Formatter) -> Result {
551 f.flags |= 1 << (rt::FlagAlternate as uint);
552 secret_lower_hex::<uint>(&(*self as uint), f)
555 impl<T> Pointer for *mut T {
556 fn fmt(&self, f: &mut Formatter) -> Result {
557 secret_pointer::<*const T>(&(*self as *const T), f)
560 impl<'a, T> Pointer for &'a T {
561 fn fmt(&self, f: &mut Formatter) -> Result {
562 secret_pointer::<*const T>(&(&**self as *const T), f)
565 impl<'a, T> Pointer for &'a mut T {
566 fn fmt(&self, f: &mut Formatter) -> Result {
567 secret_pointer::<*const T>(&(&**self as *const T), f)
571 macro_rules! floating(($ty:ident) => {
573 fn fmt(&self, fmt: &mut Formatter) -> Result {
576 let digits = match fmt.precision {
577 Some(i) => float::DigExact(i),
578 None => float::DigMax(6),
580 float::float_to_str_bytes_common(self.abs(),
588 fmt.pad_integral(self.is_nan() || *self >= 0.0, "", bytes)
593 impl LowerExp for $ty {
594 fn fmt(&self, fmt: &mut Formatter) -> Result {
597 let digits = match fmt.precision {
598 Some(i) => float::DigExact(i),
599 None => float::DigMax(6),
601 float::float_to_str_bytes_common(self.abs(),
609 fmt.pad_integral(self.is_nan() || *self >= 0.0, "", bytes)
614 impl UpperExp for $ty {
615 fn fmt(&self, fmt: &mut Formatter) -> Result {
618 let digits = match fmt.precision {
619 Some(i) => float::DigExact(i),
620 None => float::DigMax(6),
622 float::float_to_str_bytes_common(self.abs(),
630 fmt.pad_integral(self.is_nan() || *self >= 0.0, "", bytes)
638 // Implementation of Show for various core types
640 macro_rules! delegate(($ty:ty to $other:ident) => {
641 impl<'a> Show for $ty {
642 fn fmt(&self, f: &mut Formatter) -> Result {
643 (concat_idents!(secret_, $other)(self, f))
647 delegate!(&'a str to string)
648 delegate!(bool to bool)
649 delegate!(char to char)
650 delegate!(f32 to float)
651 delegate!(f64 to float)
653 impl<T> Show for *const T {
654 fn fmt(&self, f: &mut Formatter) -> Result { secret_pointer(self, f) }
656 impl<T> Show for *mut T {
657 fn fmt(&self, f: &mut Formatter) -> Result { secret_pointer(self, f) }
660 macro_rules! peel(($name:ident, $($other:ident,)*) => (tuple!($($other,)*)))
664 ( $($name:ident,)+ ) => (
665 impl<$($name:Show),*> Show for ($($name,)*) {
666 #[allow(uppercase_variables, dead_assignment)]
667 fn fmt(&self, f: &mut Formatter) -> Result {
668 try!(write!(f, "("));
669 let ($(ref $name,)*) = *self;
673 try!(write!(f, ", "));
675 try!(write!(f, "{}", *$name));
679 try!(write!(f, ","));
688 tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, }
690 impl<'a> Show for &'a any::Any {
691 fn fmt(&self, f: &mut Formatter) -> Result { f.pad("&Any") }
694 impl<'a, T: Show> Show for &'a [T] {
695 fn fmt(&self, f: &mut Formatter) -> Result {
696 if f.flags & (1 << (rt::FlagAlternate as uint)) == 0 {
697 try!(write!(f, "["));
699 let mut is_first = true;
700 for x in self.iter() {
704 try!(write!(f, ", "));
706 try!(write!(f, "{}", *x))
708 if f.flags & (1 << (rt::FlagAlternate as uint)) == 0 {
709 try!(write!(f, "]"));
715 impl<'a, T: Show> Show for &'a mut [T] {
716 fn fmt(&self, f: &mut Formatter) -> Result {
717 secret_show(&self.as_slice(), f)
722 fn fmt(&self, f: &mut Formatter) -> Result {
727 impl<T: Copy + Show> Show for Cell<T> {
728 fn fmt(&self, f: &mut Formatter) -> Result {
729 write!(f, "Cell {{ value: {} }}", self.get())
733 impl<'b, T: Show> Show for Ref<'b, T> {
734 fn fmt(&self, f: &mut Formatter) -> Result {
739 impl<'b, T: Show> Show for RefMut<'b, T> {
740 fn fmt(&self, f: &mut Formatter) -> Result {
741 (*(self.deref())).fmt(f)
745 // If you expected tests to be here, look instead at the run-pass/ifmt.rs test,
746 // it's a lot easier than creating all of the rt::Piece structures here.