aboutsummaryrefslogtreecommitdiff
path: root/libcore/fmt
diff options
context:
space:
mode:
authorpravic <[email protected]>2016-04-12 17:47:49 +0300
committerpravic <[email protected]>2016-04-12 17:47:49 +0300
commit91d227b219446d3a8b13f5bf7eb87bfc78a8b339 (patch)
tree0e438aefd2b3cf07354a68595d5aa4ed73f81f15 /libcore/fmt
parentadd native import libraries (diff)
downloadarchived-kmd-env-rs-91d227b219446d3a8b13f5bf7eb87bfc78a8b339.tar.xz
archived-kmd-env-rs-91d227b219446d3a8b13f5bf7eb87bfc78a8b339.zip
add libcore from 2016-04-11 nightly
Diffstat (limited to 'libcore/fmt')
-rw-r--r--libcore/fmt/builders.rs406
-rw-r--r--libcore/fmt/mod.rs1636
-rw-r--r--libcore/fmt/num.rs262
-rw-r--r--libcore/fmt/rt/v1.rs58
4 files changed, 2362 insertions, 0 deletions
diff --git a/libcore/fmt/builders.rs b/libcore/fmt/builders.rs
new file mode 100644
index 0000000..d337463
--- /dev/null
+++ b/libcore/fmt/builders.rs
@@ -0,0 +1,406 @@
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use prelude::v1::*;
+use fmt::{self, Write, FlagV1};
+
+struct PadAdapter<'a, 'b: 'a> {
+ fmt: &'a mut fmt::Formatter<'b>,
+ on_newline: bool,
+}
+
+impl<'a, 'b: 'a> PadAdapter<'a, 'b> {
+ fn new(fmt: &'a mut fmt::Formatter<'b>) -> PadAdapter<'a, 'b> {
+ PadAdapter {
+ fmt: fmt,
+ on_newline: false,
+ }
+ }
+}
+
+impl<'a, 'b: 'a> fmt::Write for PadAdapter<'a, 'b> {
+ fn write_str(&mut self, mut s: &str) -> fmt::Result {
+ while !s.is_empty() {
+ if self.on_newline {
+ self.fmt.write_str(" ")?;
+ }
+
+ let split = match s.find('\n') {
+ Some(pos) => {
+ self.on_newline = true;
+ pos + 1
+ }
+ None => {
+ self.on_newline = false;
+ s.len()
+ }
+ };
+ self.fmt.write_str(&s[..split])?;
+ s = &s[split..];
+ }
+
+ Ok(())
+ }
+}
+
+/// A struct to help with `fmt::Debug` implementations.
+///
+/// Constructed by the `Formatter::debug_struct` method.
+#[must_use]
+#[allow(missing_debug_implementations)]
+#[stable(feature = "debug_builders", since = "1.2.0")]
+pub struct DebugStruct<'a, 'b: 'a> {
+ fmt: &'a mut fmt::Formatter<'b>,
+ result: fmt::Result,
+ has_fields: bool,
+}
+
+pub fn debug_struct_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>,
+ name: &str)
+ -> DebugStruct<'a, 'b> {
+ let result = fmt.write_str(name);
+ DebugStruct {
+ fmt: fmt,
+ result: result,
+ has_fields: false,
+ }
+}
+
+impl<'a, 'b: 'a> DebugStruct<'a, 'b> {
+ /// Adds a new field to the generated struct output.
+ #[stable(feature = "debug_builders", since = "1.2.0")]
+ pub fn field(&mut self, name: &str, value: &fmt::Debug) -> &mut DebugStruct<'a, 'b> {
+ self.result = self.result.and_then(|_| {
+ let prefix = if self.has_fields {
+ ","
+ } else {
+ " {"
+ };
+
+ if self.is_pretty() {
+ let mut writer = PadAdapter::new(self.fmt);
+ fmt::write(&mut writer,
+ format_args!("{}\n{}: {:#?}", prefix, name, value))
+ } else {
+ write!(self.fmt, "{} {}: {:?}", prefix, name, value)
+ }
+ });
+
+ self.has_fields = true;
+ self
+ }
+
+ /// Finishes output and returns any error encountered.
+ #[stable(feature = "debug_builders", since = "1.2.0")]
+ pub fn finish(&mut self) -> fmt::Result {
+ if self.has_fields {
+ self.result = self.result.and_then(|_| {
+ if self.is_pretty() {
+ self.fmt.write_str("\n}")
+ } else {
+ self.fmt.write_str(" }")
+ }
+ });
+ }
+ self.result
+ }
+
+ fn is_pretty(&self) -> bool {
+ self.fmt.flags() & (1 << (FlagV1::Alternate as usize)) != 0
+ }
+}
+
+/// A struct to help with `fmt::Debug` implementations.
+///
+/// Constructed by the `Formatter::debug_tuple` method.
+#[must_use]
+#[allow(missing_debug_implementations)]
+#[stable(feature = "debug_builders", since = "1.2.0")]
+pub struct DebugTuple<'a, 'b: 'a> {
+ fmt: &'a mut fmt::Formatter<'b>,
+ result: fmt::Result,
+ fields: usize,
+ empty_name: bool,
+}
+
+pub fn debug_tuple_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>, name: &str) -> DebugTuple<'a, 'b> {
+ let result = fmt.write_str(name);
+ DebugTuple {
+ fmt: fmt,
+ result: result,
+ fields: 0,
+ empty_name: name.is_empty(),
+ }
+}
+
+impl<'a, 'b: 'a> DebugTuple<'a, 'b> {
+ /// Adds a new field to the generated tuple struct output.
+ #[stable(feature = "debug_builders", since = "1.2.0")]
+ pub fn field(&mut self, value: &fmt::Debug) -> &mut DebugTuple<'a, 'b> {
+ self.result = self.result.and_then(|_| {
+ let (prefix, space) = if self.fields > 0 {
+ (",", " ")
+ } else {
+ ("(", "")
+ };
+
+ if self.is_pretty() {
+ let mut writer = PadAdapter::new(self.fmt);
+ fmt::write(&mut writer, format_args!("{}\n{:#?}", prefix, value))
+ } else {
+ write!(self.fmt, "{}{}{:?}", prefix, space, value)
+ }
+ });
+
+ self.fields += 1;
+ self
+ }
+
+ /// Finishes output and returns any error encountered.
+ #[stable(feature = "debug_builders", since = "1.2.0")]
+ pub fn finish(&mut self) -> fmt::Result {
+ if self.fields > 0 {
+ self.result = self.result.and_then(|_| {
+ if self.is_pretty() {
+ self.fmt.write_str("\n")?;
+ }
+ if self.fields == 1 && self.empty_name {
+ self.fmt.write_str(",")?;
+ }
+ self.fmt.write_str(")")
+ });
+ }
+ self.result
+ }
+
+ fn is_pretty(&self) -> bool {
+ self.fmt.flags() & (1 << (FlagV1::Alternate as usize)) != 0
+ }
+}
+
+struct DebugInner<'a, 'b: 'a> {
+ fmt: &'a mut fmt::Formatter<'b>,
+ result: fmt::Result,
+ has_fields: bool,
+}
+
+impl<'a, 'b: 'a> DebugInner<'a, 'b> {
+ fn entry(&mut self, entry: &fmt::Debug) {
+ self.result = self.result.and_then(|_| {
+ if self.is_pretty() {
+ let mut writer = PadAdapter::new(self.fmt);
+ let prefix = if self.has_fields {
+ ","
+ } else {
+ ""
+ };
+ fmt::write(&mut writer, format_args!("{}\n{:#?}", prefix, entry))
+ } else {
+ let prefix = if self.has_fields {
+ ", "
+ } else {
+ ""
+ };
+ write!(self.fmt, "{}{:?}", prefix, entry)
+ }
+ });
+
+ self.has_fields = true;
+ }
+
+ pub fn finish(&mut self) {
+ let prefix = if self.is_pretty() && self.has_fields {
+ "\n"
+ } else {
+ ""
+ };
+ self.result = self.result.and_then(|_| self.fmt.write_str(prefix));
+ }
+
+ fn is_pretty(&self) -> bool {
+ self.fmt.flags() & (1 << (FlagV1::Alternate as usize)) != 0
+ }
+}
+
+/// A struct to help with `fmt::Debug` implementations.
+///
+/// Constructed by the `Formatter::debug_set` method.
+#[must_use]
+#[allow(missing_debug_implementations)]
+#[stable(feature = "debug_builders", since = "1.2.0")]
+pub struct DebugSet<'a, 'b: 'a> {
+ inner: DebugInner<'a, 'b>,
+}
+
+pub fn debug_set_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugSet<'a, 'b> {
+ let result = write!(fmt, "{{");
+ DebugSet {
+ inner: DebugInner {
+ fmt: fmt,
+ result: result,
+ has_fields: false,
+ },
+ }
+}
+
+impl<'a, 'b: 'a> DebugSet<'a, 'b> {
+ /// Adds a new entry to the set output.
+ #[stable(feature = "debug_builders", since = "1.2.0")]
+ pub fn entry(&mut self, entry: &fmt::Debug) -> &mut DebugSet<'a, 'b> {
+ self.inner.entry(entry);
+ self
+ }
+
+ /// Adds the contents of an iterator of entries to the set output.
+ #[stable(feature = "debug_builders", since = "1.2.0")]
+ pub fn entries<D, I>(&mut self, entries: I) -> &mut DebugSet<'a, 'b>
+ where D: fmt::Debug,
+ I: IntoIterator<Item = D>
+ {
+ for entry in entries {
+ self.entry(&entry);
+ }
+ self
+ }
+
+ /// Finishes output and returns any error encountered.
+ #[stable(feature = "debug_builders", since = "1.2.0")]
+ pub fn finish(&mut self) -> fmt::Result {
+ self.inner.finish();
+ self.inner.result.and_then(|_| self.inner.fmt.write_str("}"))
+ }
+}
+
+/// A struct to help with `fmt::Debug` implementations.
+///
+/// Constructed by the `Formatter::debug_list` method.
+#[must_use]
+#[allow(missing_debug_implementations)]
+#[stable(feature = "debug_builders", since = "1.2.0")]
+pub struct DebugList<'a, 'b: 'a> {
+ inner: DebugInner<'a, 'b>,
+}
+
+pub fn debug_list_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugList<'a, 'b> {
+ let result = write!(fmt, "[");
+ DebugList {
+ inner: DebugInner {
+ fmt: fmt,
+ result: result,
+ has_fields: false,
+ },
+ }
+}
+
+impl<'a, 'b: 'a> DebugList<'a, 'b> {
+ /// Adds a new entry to the list output.
+ #[stable(feature = "debug_builders", since = "1.2.0")]
+ pub fn entry(&mut self, entry: &fmt::Debug) -> &mut DebugList<'a, 'b> {
+ self.inner.entry(entry);
+ self
+ }
+
+ /// Adds the contents of an iterator of entries to the list output.
+ #[stable(feature = "debug_builders", since = "1.2.0")]
+ pub fn entries<D, I>(&mut self, entries: I) -> &mut DebugList<'a, 'b>
+ where D: fmt::Debug,
+ I: IntoIterator<Item = D>
+ {
+ for entry in entries {
+ self.entry(&entry);
+ }
+ self
+ }
+
+ /// Finishes output and returns any error encountered.
+ #[stable(feature = "debug_builders", since = "1.2.0")]
+ pub fn finish(&mut self) -> fmt::Result {
+ self.inner.finish();
+ self.inner.result.and_then(|_| self.inner.fmt.write_str("]"))
+ }
+}
+
+/// A struct to help with `fmt::Debug` implementations.
+///
+/// Constructed by the `Formatter::debug_map` method.
+#[must_use]
+#[allow(missing_debug_implementations)]
+#[stable(feature = "debug_builders", since = "1.2.0")]
+pub struct DebugMap<'a, 'b: 'a> {
+ fmt: &'a mut fmt::Formatter<'b>,
+ result: fmt::Result,
+ has_fields: bool,
+}
+
+pub fn debug_map_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugMap<'a, 'b> {
+ let result = write!(fmt, "{{");
+ DebugMap {
+ fmt: fmt,
+ result: result,
+ has_fields: false,
+ }
+}
+
+impl<'a, 'b: 'a> DebugMap<'a, 'b> {
+ /// Adds a new entry to the map output.
+ #[stable(feature = "debug_builders", since = "1.2.0")]
+ pub fn entry(&mut self, key: &fmt::Debug, value: &fmt::Debug) -> &mut DebugMap<'a, 'b> {
+ self.result = self.result.and_then(|_| {
+ if self.is_pretty() {
+ let mut writer = PadAdapter::new(self.fmt);
+ let prefix = if self.has_fields {
+ ","
+ } else {
+ ""
+ };
+ fmt::write(&mut writer,
+ format_args!("{}\n{:#?}: {:#?}", prefix, key, value))
+ } else {
+ let prefix = if self.has_fields {
+ ", "
+ } else {
+ ""
+ };
+ write!(self.fmt, "{}{:?}: {:?}", prefix, key, value)
+ }
+ });
+
+ self.has_fields = true;
+ self
+ }
+
+ /// Adds the contents of an iterator of entries to the map output.
+ #[stable(feature = "debug_builders", since = "1.2.0")]
+ pub fn entries<K, V, I>(&mut self, entries: I) -> &mut DebugMap<'a, 'b>
+ where K: fmt::Debug,
+ V: fmt::Debug,
+ I: IntoIterator<Item = (K, V)>
+ {
+ for (k, v) in entries {
+ self.entry(&k, &v);
+ }
+ self
+ }
+
+ /// Finishes output and returns any error encountered.
+ #[stable(feature = "debug_builders", since = "1.2.0")]
+ pub fn finish(&mut self) -> fmt::Result {
+ let prefix = if self.is_pretty() && self.has_fields {
+ "\n"
+ } else {
+ ""
+ };
+ self.result.and_then(|_| write!(self.fmt, "{}}}", prefix))
+ }
+
+ fn is_pretty(&self) -> bool {
+ self.fmt.flags() & (1 << (FlagV1::Alternate as usize)) != 0
+ }
+}
diff --git a/libcore/fmt/mod.rs b/libcore/fmt/mod.rs
new file mode 100644
index 0000000..2f02f5c
--- /dev/null
+++ b/libcore/fmt/mod.rs
@@ -0,0 +1,1636 @@
+// Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+//! Utilities for formatting and printing strings.
+
+#![stable(feature = "rust1", since = "1.0.0")]
+
+use prelude::v1::*;
+
+use cell::{UnsafeCell, Cell, RefCell, Ref, RefMut, BorrowState};
+use marker::PhantomData;
+use mem;
+use num::flt2dec;
+use ops::Deref;
+use result;
+use slice;
+use str;
+
+#[unstable(feature = "fmt_flags_align", issue = "27726")]
+/// Possible alignments returned by `Formatter::align`
+#[derive(Debug)]
+pub enum Alignment {
+ /// Indication that contents should be left-aligned.
+ Left,
+ /// Indication that contents should be right-aligned.
+ Right,
+ /// Indication that contents should be center-aligned.
+ Center,
+ /// No alignment was requested.
+ Unknown,
+}
+
+#[stable(feature = "debug_builders", since = "1.2.0")]
+pub use self::builders::{DebugStruct, DebugTuple, DebugSet, DebugList, DebugMap};
+
+mod num;
+mod builders;
+
+#[unstable(feature = "fmt_internals", reason = "internal to format_args!",
+ issue = "0")]
+#[doc(hidden)]
+pub mod rt {
+ pub mod v1;
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+/// The type returned by formatter methods.
+pub type Result = result::Result<(), Error>;
+
+/// The error type which is returned from formatting a message into a stream.
+///
+/// This type does not support transmission of an error other than that an error
+/// occurred. Any extra information must be arranged to be transmitted through
+/// some other means.
+#[stable(feature = "rust1", since = "1.0.0")]
+#[derive(Copy, Clone, Debug)]
+pub struct Error;
+
+/// A collection of methods that are required to format a message into a stream.
+///
+/// This trait is the type which this modules requires when formatting
+/// information. This is similar to the standard library's `io::Write` trait,
+/// but it is only intended for use in libcore.
+///
+/// This trait should generally not be implemented by consumers of the standard
+/// library. The `write!` macro accepts an instance of `io::Write`, and the
+/// `io::Write` trait is favored over implementing this trait.
+#[stable(feature = "rust1", since = "1.0.0")]
+pub trait Write {
+ /// Writes a slice of bytes into this writer, returning whether the write
+ /// succeeded.
+ ///
+ /// This method can only succeed if the entire byte slice was successfully
+ /// written, and this method will not return until all data has been
+ /// written or an error occurs.
+ ///
+ /// # Errors
+ ///
+ /// This function will return an instance of `Error` on error.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn write_str(&mut self, s: &str) -> Result;
+
+ /// Writes a `char` into this writer, returning whether the write succeeded.
+ ///
+ /// A single `char` may be encoded as more than one byte.
+ /// This method can only succeed if the entire byte sequence was successfully
+ /// written, and this method will not return until all data has been
+ /// written or an error occurs.
+ ///
+ /// # Errors
+ ///
+ /// This function will return an instance of `Error` on error.
+ #[stable(feature = "fmt_write_char", since = "1.1.0")]
+ fn write_char(&mut self, c: char) -> Result {
+ self.write_str(unsafe {
+ str::from_utf8_unchecked(c.encode_utf8().as_slice())
+ })
+ }
+
+ /// Glue for usage of the `write!` macro with implementors of this trait.
+ ///
+ /// This method should generally not be invoked manually, but rather through
+ /// the `write!` macro itself.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn write_fmt(&mut self, args: Arguments) -> Result {
+ // This Adapter is needed to allow `self` (of type `&mut
+ // Self`) to be cast to a Write (below) without
+ // requiring a `Sized` bound.
+ struct Adapter<'a,T: ?Sized +'a>(&'a mut T);
+
+ impl<'a, T: ?Sized> Write for Adapter<'a, T>
+ where T: Write
+ {
+ fn write_str(&mut self, s: &str) -> Result {
+ self.0.write_str(s)
+ }
+
+ fn write_char(&mut self, c: char) -> Result {
+ self.0.write_char(c)
+ }
+
+ fn write_fmt(&mut self, args: Arguments) -> Result {
+ self.0.write_fmt(args)
+ }
+ }
+
+ write(&mut Adapter(self), args)
+ }
+}
+
+#[stable(feature = "fmt_write_blanket_impl", since = "1.4.0")]
+impl<'a, W: Write + ?Sized> Write for &'a mut W {
+ fn write_str(&mut self, s: &str) -> Result {
+ (**self).write_str(s)
+ }
+
+ fn write_char(&mut self, c: char) -> Result {
+ (**self).write_char(c)
+ }
+
+ fn write_fmt(&mut self, args: Arguments) -> Result {
+ (**self).write_fmt(args)
+ }
+}
+
+/// A struct to represent both where to emit formatting strings to and how they
+/// should be formatted. A mutable version of this is passed to all formatting
+/// traits.
+#[allow(missing_debug_implementations)]
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct Formatter<'a> {
+ flags: u32,
+ fill: char,
+ align: rt::v1::Alignment,
+ width: Option<usize>,
+ precision: Option<usize>,
+
+ buf: &'a mut (Write+'a),
+ curarg: slice::Iter<'a, ArgumentV1<'a>>,
+ args: &'a [ArgumentV1<'a>],
+}
+
+// NB. Argument is essentially an optimized partially applied formatting function,
+// equivalent to `exists T.(&T, fn(&T, &mut Formatter) -> Result`.
+
+enum Void {}
+
+/// This struct represents the generic "argument" which is taken by the Xprintf
+/// family of functions. It contains a function to format the given value. At
+/// compile time it is ensured that the function and the value have the correct
+/// types, and then this struct is used to canonicalize arguments to one type.
+#[derive(Copy)]
+#[allow(missing_debug_implementations)]
+#[unstable(feature = "fmt_internals", reason = "internal to format_args!",
+ issue = "0")]
+#[doc(hidden)]
+pub struct ArgumentV1<'a> {
+ value: &'a Void,
+ formatter: fn(&Void, &mut Formatter) -> Result,
+}
+
+#[unstable(feature = "fmt_internals", reason = "internal to format_args!",
+ issue = "0")]
+impl<'a> Clone for ArgumentV1<'a> {
+ fn clone(&self) -> ArgumentV1<'a> {
+ *self
+ }
+}
+
+impl<'a> ArgumentV1<'a> {
+ #[inline(never)]
+ fn show_usize(x: &usize, f: &mut Formatter) -> Result {
+ Display::fmt(x, f)
+ }
+
+ #[doc(hidden)]
+ #[unstable(feature = "fmt_internals", reason = "internal to format_args!",
+ issue = "0")]
+ pub fn new<'b, T>(x: &'b T,
+ f: fn(&T, &mut Formatter) -> Result) -> ArgumentV1<'b> {
+ unsafe {
+ ArgumentV1 {
+ formatter: mem::transmute(f),
+ value: mem::transmute(x)
+ }
+ }
+ }
+
+ #[doc(hidden)]
+ #[unstable(feature = "fmt_internals", reason = "internal to format_args!",
+ issue = "0")]
+ pub fn from_usize(x: &usize) -> ArgumentV1 {
+ ArgumentV1::new(x, ArgumentV1::show_usize)
+ }
+
+ fn as_usize(&self) -> Option<usize> {
+ if self.formatter as usize == ArgumentV1::show_usize as usize {
+ Some(unsafe { *(self.value as *const _ as *const usize) })
+ } else {
+ None
+ }
+ }
+}
+
+// flags available in the v1 format of format_args
+#[derive(Copy, Clone)]
+#[allow(dead_code)] // SignMinus isn't currently used
+enum FlagV1 { SignPlus, SignMinus, Alternate, SignAwareZeroPad, }
+
+impl<'a> Arguments<'a> {
+ /// When using the format_args!() macro, this function is used to generate the
+ /// Arguments structure.
+ #[doc(hidden)] #[inline]
+ #[unstable(feature = "fmt_internals", reason = "internal to format_args!",
+ issue = "0")]
+ pub fn new_v1(pieces: &'a [&'a str],
+ args: &'a [ArgumentV1<'a>]) -> Arguments<'a> {
+ Arguments {
+ pieces: pieces,
+ fmt: None,
+ args: args
+ }
+ }
+
+ /// This function is used to specify nonstandard formatting parameters.
+ /// The `pieces` array must be at least as long as `fmt` to construct
+ /// a valid Arguments structure. Also, any `Count` within `fmt` that is
+ /// `CountIsParam` or `CountIsNextParam` has to point to an argument
+ /// created with `argumentusize`. However, failing to do so doesn't cause
+ /// unsafety, but will ignore invalid .
+ #[doc(hidden)] #[inline]
+ #[unstable(feature = "fmt_internals", reason = "internal to format_args!",
+ issue = "0")]
+ pub fn new_v1_formatted(pieces: &'a [&'a str],
+ args: &'a [ArgumentV1<'a>],
+ fmt: &'a [rt::v1::Argument]) -> Arguments<'a> {
+ Arguments {
+ pieces: pieces,
+ fmt: Some(fmt),
+ args: args
+ }
+ }
+}
+
+/// This structure represents a safely precompiled version of a format string
+/// and its arguments. This cannot be generated at runtime because it cannot
+/// safely be done so, so no constructors are given and the fields are private
+/// to prevent modification.
+///
+/// The `format_args!` macro will safely create an instance of this structure
+/// and pass it to a function or closure, passed as the first argument. The
+/// macro validates the format string at compile-time so usage of the `write`
+/// and `format` functions can be safely performed.
+#[stable(feature = "rust1", since = "1.0.0")]
+#[derive(Copy, Clone)]
+pub struct Arguments<'a> {
+ // Format string pieces to print.
+ pieces: &'a [&'a str],
+
+ // Placeholder specs, or `None` if all specs are default (as in "{}{}").
+ fmt: Option<&'a [rt::v1::Argument]>,
+
+ // Dynamic arguments for interpolation, to be interleaved with string
+ // pieces. (Every argument is preceded by a string piece.)
+ args: &'a [ArgumentV1<'a>],
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a> Debug for Arguments<'a> {
+ fn fmt(&self, fmt: &mut Formatter) -> Result {
+ Display::fmt(self, fmt)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a> Display for Arguments<'a> {
+ fn fmt(&self, fmt: &mut Formatter) -> Result {
+ write(fmt.buf, *self)
+ }
+}
+
+/// Format trait for the `?` character.
+///
+/// `Debug` should format the output in a programmer-facing, debugging context.
+///
+/// Generally speaking, you should just `derive` a `Debug` implementation.
+///
+/// When used with the alternate format specifier `#?`, the output is pretty-printed.
+///
+/// For more information on formatters, see [the module-level documentation][module].
+///
+/// [module]: ../../std/fmt/index.html
+///
+/// This trait can be used with `#[derive]`.
+///
+/// # Examples
+///
+/// Deriving an implementation:
+///
+/// ```
+/// #[derive(Debug)]
+/// struct Point {
+/// x: i32,
+/// y: i32,
+/// }
+///
+/// let origin = Point { x: 0, y: 0 };
+///
+/// println!("The origin is: {:?}", origin);
+/// ```
+///
+/// Manually implementing:
+///
+/// ```
+/// use std::fmt;
+///
+/// struct Point {
+/// x: i32,
+/// y: i32,
+/// }
+///
+/// impl fmt::Debug for Point {
+/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+/// write!(f, "Point {{ x: {}, y: {} }}", self.x, self.y)
+/// }
+/// }
+///
+/// let origin = Point { x: 0, y: 0 };
+///
+/// println!("The origin is: {:?}", origin);
+/// ```
+///
+/// This outputs:
+///
+/// ```text
+/// The origin is: Point { x: 0, y: 0 }
+/// ```
+///
+/// There are a number of `debug_*` methods on `Formatter` to help you with manual
+/// implementations, such as [`debug_struct`][debug_struct].
+///
+/// `Debug` implementations using either `derive` or the debug builder API
+/// on `Formatter` support pretty printing using the alternate flag: `{:#?}`.
+///
+/// [debug_struct]: ../../std/fmt/struct.Formatter.html#method.debug_struct
+///
+/// Pretty printing with `#?`:
+///
+/// ```
+/// #[derive(Debug)]
+/// struct Point {
+/// x: i32,
+/// y: i32,
+/// }
+///
+/// let origin = Point { x: 0, y: 0 };
+///
+/// println!("The origin is: {:#?}", origin);
+/// ```
+///
+/// This outputs:
+///
+/// ```text
+/// The origin is: Point {
+/// x: 0,
+/// y: 0
+/// }
+/// ```
+#[stable(feature = "rust1", since = "1.0.0")]
+#[rustc_on_unimplemented = "`{Self}` cannot be formatted using `:?`; if it is \
+ defined in your crate, add `#[derive(Debug)]` or \
+ manually implement it"]
+#[lang = "debug_trait"]
+pub trait Debug {
+ /// Formats the value using the given formatter.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn fmt(&self, &mut Formatter) -> Result;
+}
+
+/// Format trait for an empty format, `{}`.
+///
+/// `Display` is similar to [`Debug`][debug], but `Display` is for user-facing
+/// output, and so cannot be derived.
+///
+/// [debug]: trait.Debug.html
+///
+/// For more information on formatters, see [the module-level documentation][module].
+///
+/// [module]: ../../std/fmt/index.html
+///
+/// # Examples
+///
+/// Implementing `Display` on a type:
+///
+/// ```
+/// use std::fmt;
+///
+/// struct Point {
+/// x: i32,
+/// y: i32,
+/// }
+///
+/// impl fmt::Display for Point {
+/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+/// write!(f, "({}, {})", self.x, self.y)
+/// }
+/// }
+///
+/// let origin = Point { x: 0, y: 0 };
+///
+/// println!("The origin is: {}", origin);
+/// ```
+#[rustc_on_unimplemented = "`{Self}` cannot be formatted with the default \
+ formatter; try using `:?` instead if you are using \
+ a format string"]
+#[stable(feature = "rust1", since = "1.0.0")]
+pub trait Display {
+ /// Formats the value using the given formatter.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn fmt(&self, &mut Formatter) -> Result;
+}
+
+/// Format trait for the `o` character.
+///
+/// The `Octal` trait should format its output as a number in base-8.
+///
+/// The alternate flag, `#`, adds a `0o` in front of the output.
+///
+/// For more information on formatters, see [the module-level documentation][module].
+///
+/// [module]: ../../std/fmt/index.html
+///
+/// # Examples
+///
+/// Basic usage with `i32`:
+///
+/// ```
+/// let x = 42; // 42 is '52' in octal
+///
+/// assert_eq!(format!("{:o}", x), "52");
+/// assert_eq!(format!("{:#o}", x), "0o52");
+/// ```
+///
+/// Implementing `Octal` on a type:
+///
+/// ```
+/// use std::fmt;
+///
+/// struct Length(i32);
+///
+/// impl fmt::Octal for Length {
+/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+/// let val = self.0;
+///
+/// write!(f, "{:o}", val) // delegate to i32's implementation
+/// }
+/// }
+///
+/// let l = Length(9);
+///
+/// println!("l as octal is: {:o}", l);
+/// ```
+#[stable(feature = "rust1", since = "1.0.0")]
+pub trait Octal {
+ /// Formats the value using the given formatter.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn fmt(&self, &mut Formatter) -> Result;
+}
+
+/// Format trait for the `b` character.
+///
+/// The `Binary` trait should format its output as a number in binary.
+///
+/// The alternate flag, `#`, adds a `0b` in front of the output.
+///
+/// For more information on formatters, see [the module-level documentation][module].
+///
+/// [module]: ../../std/fmt/index.html
+///
+/// # Examples
+///
+/// Basic usage with `i32`:
+///
+/// ```
+/// let x = 42; // 42 is '101010' in binary
+///
+/// assert_eq!(format!("{:b}", x), "101010");
+/// assert_eq!(format!("{:#b}", x), "0b101010");
+/// ```
+///
+/// Implementing `Binary` on a type:
+///
+/// ```
+/// use std::fmt;
+///
+/// struct Length(i32);
+///
+/// impl fmt::Binary for Length {
+/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+/// let val = self.0;
+///
+/// write!(f, "{:b}", val) // delegate to i32's implementation
+/// }
+/// }
+///
+/// let l = Length(107);
+///
+/// println!("l as binary is: {:b}", l);
+/// ```
+#[stable(feature = "rust1", since = "1.0.0")]
+pub trait Binary {
+ /// Formats the value using the given formatter.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn fmt(&self, &mut Formatter) -> Result;
+}
+
+/// Format trait for the `x` character.
+///
+/// The `LowerHex` trait should format its output as a number in hexadecimal, with `a` through `f`
+/// in lower case.
+///
+/// The alternate flag, `#`, adds a `0x` in front of the output.
+///
+/// For more information on formatters, see [the module-level documentation][module].
+///
+/// [module]: ../../std/fmt/index.html
+///
+/// # Examples
+///
+/// Basic usage with `i32`:
+///
+/// ```
+/// let x = 42; // 42 is '2a' in hex
+///
+/// assert_eq!(format!("{:x}", x), "2a");
+/// assert_eq!(format!("{:#x}", x), "0x2a");
+/// ```
+///
+/// Implementing `LowerHex` on a type:
+///
+/// ```
+/// use std::fmt;
+///
+/// struct Length(i32);
+///
+/// impl fmt::LowerHex for Length {
+/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+/// let val = self.0;
+///
+/// write!(f, "{:x}", val) // delegate to i32's implementation
+/// }
+/// }
+///
+/// let l = Length(9);
+///
+/// println!("l as hex is: {:x}", l);
+/// ```
+#[stable(feature = "rust1", since = "1.0.0")]
+pub trait LowerHex {
+ /// Formats the value using the given formatter.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn fmt(&self, &mut Formatter) -> Result;
+}
+
+/// Format trait for the `X` character.
+///
+/// The `UpperHex` trait should format its output as a number in hexadecimal, with `A` through `F`
+/// in upper case.
+///
+/// The alternate flag, `#`, adds a `0x` in front of the output.
+///
+/// For more information on formatters, see [the module-level documentation][module].
+///
+/// [module]: ../../std/fmt/index.html
+///
+/// # Examples
+///
+/// Basic usage with `i32`:
+///
+/// ```
+/// let x = 42; // 42 is '2A' in hex
+///
+/// assert_eq!(format!("{:X}", x), "2A");
+/// assert_eq!(format!("{:#X}", x), "0x2A");
+/// ```
+///
+/// Implementing `UpperHex` on a type:
+///
+/// ```
+/// use std::fmt;
+///
+/// struct Length(i32);
+///
+/// impl fmt::UpperHex for Length {
+/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+/// let val = self.0;
+///
+/// write!(f, "{:X}", val) // delegate to i32's implementation
+/// }
+/// }
+///
+/// let l = Length(9);
+///
+/// println!("l as hex is: {:X}", l);
+/// ```
+#[stable(feature = "rust1", since = "1.0.0")]
+pub trait UpperHex {
+ /// Formats the value using the given formatter.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn fmt(&self, &mut Formatter) -> Result;
+}
+
+/// Format trait for the `p` character.
+///
+/// The `Pointer` trait should format its output as a memory location. This is commonly presented
+/// as hexadecimal.
+///
+/// For more information on formatters, see [the module-level documentation][module].
+///
+/// [module]: ../../std/fmt/index.html
+///
+/// # Examples
+///
+/// Basic usage with `&i32`:
+///
+/// ```
+/// let x = &42;
+///
+/// let address = format!("{:p}", x); // this produces something like '0x7f06092ac6d0'
+/// ```
+///
+/// Implementing `Pointer` on a type:
+///
+/// ```
+/// use std::fmt;
+///
+/// struct Length(i32);
+///
+/// impl fmt::Pointer for Length {
+/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+/// // use `as` to convert to a `*const T`, which implements Pointer, which we can use
+///
+/// write!(f, "{:p}", self as *const Length)
+/// }
+/// }
+///
+/// let l = Length(42);
+///
+/// println!("l is in memory here: {:p}", l);
+/// ```
+#[stable(feature = "rust1", since = "1.0.0")]
+pub trait Pointer {
+ /// Formats the value using the given formatter.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn fmt(&self, &mut Formatter) -> Result;
+}
+
+/// Format trait for the `e` character.
+///
+/// The `LowerExp` trait should format its output in scientific notation with a lower-case `e`.
+///
+/// For more information on formatters, see [the module-level documentation][module].
+///
+/// [module]: ../../std/fmt/index.html
+///
+/// # Examples
+///
+/// Basic usage with `i32`:
+///
+/// ```
+/// let x = 42.0; // 42.0 is '4.2e1' in scientific notation
+///
+/// assert_eq!(format!("{:e}", x), "4.2e1");
+/// ```
+///
+/// Implementing `LowerExp` on a type:
+///
+/// ```
+/// use std::fmt;
+///
+/// struct Length(i32);
+///
+/// impl fmt::LowerExp for Length {
+/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+/// let val = self.0;
+/// write!(f, "{}e1", val / 10)
+/// }
+/// }
+///
+/// let l = Length(100);
+///
+/// println!("l in scientific notation is: {:e}", l);
+/// ```
+#[stable(feature = "rust1", since = "1.0.0")]
+pub trait LowerExp {
+ /// Formats the value using the given formatter.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn fmt(&self, &mut Formatter) -> Result;
+}
+
+/// Format trait for the `E` character.
+///
+/// The `UpperExp` trait should format its output in scientific notation with an upper-case `E`.
+///
+/// For more information on formatters, see [the module-level documentation][module].
+///
+/// [module]: ../../std/fmt/index.html
+///
+/// # Examples
+///
+/// Basic usage with `f32`:
+///
+/// ```
+/// let x = 42.0; // 42.0 is '4.2E1' in scientific notation
+///
+/// assert_eq!(format!("{:E}", x), "4.2E1");
+/// ```
+///
+/// Implementing `UpperExp` on a type:
+///
+/// ```
+/// use std::fmt;
+///
+/// struct Length(i32);
+///
+/// impl fmt::UpperExp for Length {
+/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+/// let val = self.0;
+/// write!(f, "{}E1", val / 10)
+/// }
+/// }
+///
+/// let l = Length(100);
+///
+/// println!("l in scientific notation is: {:E}", l);
+/// ```
+#[stable(feature = "rust1", since = "1.0.0")]
+pub trait UpperExp {
+ /// Formats the value using the given formatter.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ fn fmt(&self, &mut Formatter) -> Result;
+}
+
+/// The `write` 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.
+///
+/// # Arguments
+///
+/// * output - the buffer to write output to
+/// * args - the precompiled arguments generated by `format_args!`
+#[stable(feature = "rust1", since = "1.0.0")]
+pub fn write(output: &mut Write, args: Arguments) -> Result {
+ let mut formatter = Formatter {
+ flags: 0,
+ width: None,
+ precision: None,
+ buf: output,
+ align: rt::v1::Alignment::Unknown,
+ fill: ' ',
+ args: args.args,
+ curarg: args.args.iter(),
+ };
+
+ let mut pieces = args.pieces.iter();
+
+ match args.fmt {
+ None => {
+ // We can use default formatting parameters for all arguments.
+ for (arg, piece) in args.args.iter().zip(pieces.by_ref()) {
+ formatter.buf.write_str(*piece)?;
+ (arg.formatter)(arg.value, &mut formatter)?;
+ }
+ }
+ Some(fmt) => {
+ // Every spec has a corresponding argument that is preceded by
+ // a string piece.
+ for (arg, piece) in fmt.iter().zip(pieces.by_ref()) {
+ formatter.buf.write_str(*piece)?;
+ formatter.run(arg)?;
+ }
+ }
+ }
+
+ // There can be only one trailing string piece left.
+ match pieces.next() {
+ Some(piece) => {
+ formatter.buf.write_str(*piece)?;
+ }
+ None => {}
+ }
+
+ Ok(())
+}
+
+impl<'a> Formatter<'a> {
+
+ // First up is the collection of functions used to execute a format string
+ // at runtime. This consumes all of the compile-time statics generated by
+ // the format! syntax extension.
+ fn run(&mut self, arg: &rt::v1::Argument) -> Result {
+ // Fill in the format parameters into the formatter
+ self.fill = arg.format.fill;
+ self.align = arg.format.align;
+ self.flags = arg.format.flags;
+ self.width = self.getcount(&arg.format.width);
+ self.precision = self.getcount(&arg.format.precision);
+
+ // Extract the correct argument
+ let value = match arg.position {
+ rt::v1::Position::Next => { *self.curarg.next().unwrap() }
+ rt::v1::Position::At(i) => self.args[i],
+ };
+
+ // Then actually do some printing
+ (value.formatter)(value.value, self)
+ }
+
+ fn getcount(&mut self, cnt: &rt::v1::Count) -> Option<usize> {
+ match *cnt {
+ rt::v1::Count::Is(n) => Some(n),
+ rt::v1::Count::Implied => None,
+ rt::v1::Count::Param(i) => {
+ self.args[i].as_usize()
+ }
+ rt::v1::Count::NextParam => {
+ self.curarg.next().and_then(|arg| arg.as_usize())
+ }
+ }
+ }
+
+ // Helper methods used for padding and processing formatting arguments that
+ // all formatting traits can use.
+
+ /// Performs the correct padding for an integer which has already been
+ /// emitted into a str. The str should *not* contain the sign for the
+ /// integer, that will be added by this method.
+ ///
+ /// # Arguments
+ ///
+ /// * is_nonnegative - whether the original integer was either positive or zero.
+ /// * prefix - if the '#' character (Alternate) is provided, this
+ /// is the prefix to put in front of the number.
+ /// * buf - the byte array that the number has been formatted into
+ ///
+ /// This function will correctly account for the flags provided as well as
+ /// the minimum width. It will not take precision into account.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn pad_integral(&mut self,
+ is_nonnegative: bool,
+ prefix: &str,
+ buf: &str)
+ -> Result {
+ use char::CharExt;
+
+ let mut width = buf.len();
+
+ let mut sign = None;
+ if !is_nonnegative {
+ sign = Some('-'); width += 1;
+ } else if self.sign_plus() {
+ sign = Some('+'); width += 1;
+ }
+
+ let mut prefixed = false;
+ if self.alternate() {
+ prefixed = true; width += prefix.chars().count();
+ }
+
+ // Writes the sign if it exists, and then the prefix if it was requested
+ let write_prefix = |f: &mut Formatter| {
+ if let Some(c) = sign {
+ f.buf.write_str(unsafe {
+ str::from_utf8_unchecked(c.encode_utf8().as_slice())
+ })?;
+ }
+ if prefixed { f.buf.write_str(prefix) }
+ else { Ok(()) }
+ };
+
+ // The `width` field is more of a `min-width` parameter at this point.
+ match self.width {
+ // If there's no minimum length requirements then we can just
+ // write the bytes.
+ None => {
+ write_prefix(self)?; self.buf.write_str(buf)
+ }
+ // Check if we're over the minimum width, if so then we can also
+ // just write the bytes.
+ Some(min) if width >= min => {
+ write_prefix(self)?; self.buf.write_str(buf)
+ }
+ // The sign and prefix goes before the padding if the fill character
+ // is zero
+ Some(min) if self.sign_aware_zero_pad() => {
+ self.fill = '0';
+ write_prefix(self)?;
+ self.with_padding(min - width, rt::v1::Alignment::Right, |f| {
+ f.buf.write_str(buf)
+ })
+ }
+ // Otherwise, the sign and prefix goes after the padding
+ Some(min) => {
+ self.with_padding(min - width, rt::v1::Alignment::Right, |f| {
+ write_prefix(f)?; f.buf.write_str(buf)
+ })
+ }
+ }
+ }
+
+ /// This function takes a string slice and emits it to the internal buffer
+ /// after applying the relevant formatting flags specified. The flags
+ /// recognized for generic strings are:
+ ///
+ /// * width - the minimum width of what to emit
+ /// * fill/align - what to emit and where to emit it if the string
+ /// provided needs to be padded
+ /// * precision - the maximum length to emit, the string is truncated if it
+ /// is longer than this length
+ ///
+ /// Notably this function ignored the `flag` parameters
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn pad(&mut self, s: &str) -> Result {
+ // Make sure there's a fast path up front
+ if self.width.is_none() && self.precision.is_none() {
+ return self.buf.write_str(s);
+ }
+ // The `precision` field can be interpreted as a `max-width` for the
+ // string being formatted
+ if let Some(max) = self.precision {
+ // If there's a maximum width and our string is longer than
+ // that, then we must always have truncation. This is the only
+ // case where the maximum length will matter.
+ if let Some((i, _)) = s.char_indices().skip(max).next() {
+ return self.buf.write_str(&s[..i])
+ }
+ }
+ // The `width` field is more of a `min-width` parameter at this point.
+ match self.width {
+ // If we're under the maximum length, and there's no minimum length
+ // requirements, then we can just emit the string
+ None => self.buf.write_str(s),
+ // If we're under the maximum width, check if we're over the minimum
+ // width, if so it's as easy as just emitting the string.
+ Some(width) if s.chars().count() >= width => {
+ self.buf.write_str(s)
+ }
+ // If we're under both the maximum and the minimum width, then fill
+ // up the minimum width with the specified string + some alignment.
+ Some(width) => {
+ let align = rt::v1::Alignment::Left;
+ self.with_padding(width - s.chars().count(), align, |me| {
+ me.buf.write_str(s)
+ })
+ }
+ }
+ }
+
+ /// Runs a callback, emitting the correct padding either before or
+ /// afterwards depending on whether right or left alignment is requested.
+ fn with_padding<F>(&mut self, padding: usize, default: rt::v1::Alignment,
+ f: F) -> Result
+ where F: FnOnce(&mut Formatter) -> Result,
+ {
+ use char::CharExt;
+ let align = match self.align {
+ rt::v1::Alignment::Unknown => default,
+ _ => self.align
+ };
+
+ let (pre_pad, post_pad) = match align {
+ rt::v1::Alignment::Left => (0, padding),
+ rt::v1::Alignment::Right |
+ rt::v1::Alignment::Unknown => (padding, 0),
+ rt::v1::Alignment::Center => (padding / 2, (padding + 1) / 2),
+ };
+
+ let fill = self.fill.encode_utf8();
+ let fill = unsafe {
+ str::from_utf8_unchecked(fill.as_slice())
+ };
+
+ for _ in 0..pre_pad {
+ self.buf.write_str(fill)?;
+ }
+
+ f(self)?;
+
+ for _ in 0..post_pad {
+ self.buf.write_str(fill)?;
+ }
+
+ Ok(())
+ }
+
+ /// Takes the formatted parts and applies the padding.
+ /// Assumes that the caller already has rendered the parts with required precision,
+ /// so that `self.precision` can be ignored.
+ fn pad_formatted_parts(&mut self, formatted: &flt2dec::Formatted) -> Result {
+ if let Some(mut width) = self.width {
+ // for the sign-aware zero padding, we render the sign first and
+ // behave as if we had no sign from the beginning.
+ let mut formatted = formatted.clone();
+ let mut align = self.align;
+ let old_fill = self.fill;
+ if self.sign_aware_zero_pad() {
+ // a sign always goes first
+ let sign = unsafe { str::from_utf8_unchecked(formatted.sign) };
+ self.buf.write_str(sign)?;
+
+ // remove the sign from the formatted parts
+ formatted.sign = b"";
+ width = if width < sign.len() { 0 } else { width - sign.len() };
+ align = rt::v1::Alignment::Right;
+ self.fill = '0';
+ }
+
+ // remaining parts go through the ordinary padding process.
+ let len = formatted.len();
+ let ret = if width <= len { // no padding
+ self.write_formatted_parts(&formatted)
+ } else {
+ self.with_padding(width - len, align, |f| {
+ f.write_formatted_parts(&formatted)
+ })
+ };
+ self.fill = old_fill;
+ ret
+ } else {
+ // this is the common case and we take a shortcut
+ self.write_formatted_parts(formatted)
+ }
+ }
+
+ fn write_formatted_parts(&mut self, formatted: &flt2dec::Formatted) -> Result {
+ fn write_bytes(buf: &mut Write, s: &[u8]) -> Result {
+ buf.write_str(unsafe { str::from_utf8_unchecked(s) })
+ }
+
+ if !formatted.sign.is_empty() {
+ write_bytes(self.buf, formatted.sign)?;
+ }
+ for part in formatted.parts {
+ match *part {
+ flt2dec::Part::Zero(mut nzeroes) => {
+ const ZEROES: &'static str = // 64 zeroes
+ "0000000000000000000000000000000000000000000000000000000000000000";
+ while nzeroes > ZEROES.len() {
+ self.buf.write_str(ZEROES)?;
+ nzeroes -= ZEROES.len();
+ }
+ if nzeroes > 0 {
+ self.buf.write_str(&ZEROES[..nzeroes])?;
+ }
+ }
+ flt2dec::Part::Num(mut v) => {
+ let mut s = [0; 5];
+ let len = part.len();
+ for c in s[..len].iter_mut().rev() {
+ *c = b'0' + (v % 10) as u8;
+ v /= 10;
+ }
+ write_bytes(self.buf, &s[..len])?;
+ }
+ flt2dec::Part::Copy(buf) => {
+ write_bytes(self.buf, buf)?;
+ }
+ }
+ }
+ Ok(())
+ }
+
+ /// Writes some data to the underlying buffer contained within this
+ /// formatter.
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn write_str(&mut self, data: &str) -> Result {
+ self.buf.write_str(data)
+ }
+
+ /// Writes some formatted information into this instance
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn write_fmt(&mut self, fmt: Arguments) -> Result {
+ write(self.buf, fmt)
+ }
+
+ /// Flags for formatting (packed version of rt::Flag)
+ #[stable(feature = "rust1", since = "1.0.0")]
+ pub fn flags(&self) -> u32 { self.flags }
+
+ /// Character used as 'fill' whenever there is alignment
+ #[stable(feature = "fmt_flags", since = "1.5.0")]
+ pub fn fill(&self) -> char { self.fill }
+
+ /// Flag indicating what form of alignment was requested
+ #[unstable(feature = "fmt_flags_align", reason = "method was just created",
+ issue = "27726")]
+ pub fn align(&self) -> Alignment {
+ match self.align {
+ rt::v1::Alignment::Left => Alignment::Left,
+ rt::v1::Alignment::Right => Alignment::Right,
+ rt::v1::Alignment::Center => Alignment::Center,
+ rt::v1::Alignment::Unknown => Alignment::Unknown,
+ }
+ }
+
+ /// Optionally specified integer width that the output should be
+ #[stable(feature = "fmt_flags", since = "1.5.0")]
+ pub fn width(&self) -> Option<usize> { self.width }
+
+ /// Optionally specified precision for numeric types
+ #[stable(feature = "fmt_flags", since = "1.5.0")]
+ pub fn precision(&self) -> Option<usize> { self.precision }
+
+ /// Determines if the `+` flag was specified.
+ #[stable(feature = "fmt_flags", since = "1.5.0")]
+ pub fn sign_plus(&self) -> bool { self.flags & (1 << FlagV1::SignPlus as u32) != 0 }
+
+ /// Determines if the `-` flag was specified.
+ #[stable(feature = "fmt_flags", since = "1.5.0")]
+ pub fn sign_minus(&self) -> bool { self.flags & (1 << FlagV1::SignMinus as u32) != 0 }
+
+ /// Determines if the `#` flag was specified.
+ #[stable(feature = "fmt_flags", since = "1.5.0")]
+ pub fn alternate(&self) -> bool { self.flags & (1 << FlagV1::Alternate as u32) != 0 }
+
+ /// Determines if the `0` flag was specified.
+ #[stable(feature = "fmt_flags", since = "1.5.0")]
+ pub fn sign_aware_zero_pad(&self) -> bool {
+ self.flags & (1 << FlagV1::SignAwareZeroPad as u32) != 0
+ }
+
+ /// Creates a `DebugStruct` builder designed to assist with creation of
+ /// `fmt::Debug` implementations for structs.
+ ///
+ /// # Examples
+ ///
+ /// ```rust
+ /// use std::fmt;
+ ///
+ /// struct Foo {
+ /// bar: i32,
+ /// baz: String,
+ /// }
+ ///
+ /// impl fmt::Debug for Foo {
+ /// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+ /// fmt.debug_struct("Foo")
+ /// .field("bar", &self.bar)
+ /// .field("baz", &self.baz)
+ /// .finish()
+ /// }
+ /// }
+ ///
+ /// // prints "Foo { bar: 10, baz: "Hello World" }"
+ /// println!("{:?}", Foo { bar: 10, baz: "Hello World".to_string() });
+ /// ```
+ #[stable(feature = "debug_builders", since = "1.2.0")]
+ #[inline]
+ pub fn debug_struct<'b>(&'b mut self, name: &str) -> DebugStruct<'b, 'a> {
+ builders::debug_struct_new(self, name)
+ }
+
+ /// Creates a `DebugTuple` builder designed to assist with creation of
+ /// `fmt::Debug` implementations for tuple structs.
+ ///
+ /// # Examples
+ ///
+ /// ```rust
+ /// use std::fmt;
+ ///
+ /// struct Foo(i32, String);
+ ///
+ /// impl fmt::Debug for Foo {
+ /// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+ /// fmt.debug_tuple("Foo")
+ /// .field(&self.0)
+ /// .field(&self.1)
+ /// .finish()
+ /// }
+ /// }
+ ///
+ /// // prints "Foo(10, "Hello World")"
+ /// println!("{:?}", Foo(10, "Hello World".to_string()));
+ /// ```
+ #[stable(feature = "debug_builders", since = "1.2.0")]
+ #[inline]
+ pub fn debug_tuple<'b>(&'b mut self, name: &str) -> DebugTuple<'b, 'a> {
+ builders::debug_tuple_new(self, name)
+ }
+
+ /// Creates a `DebugList` builder designed to assist with creation of
+ /// `fmt::Debug` implementations for list-like structures.
+ ///
+ /// # Examples
+ ///
+ /// ```rust
+ /// use std::fmt;
+ ///
+ /// struct Foo(Vec<i32>);
+ ///
+ /// impl fmt::Debug for Foo {
+ /// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+ /// fmt.debug_list().entries(self.0.iter()).finish()
+ /// }
+ /// }
+ ///
+ /// // prints "[10, 11]"
+ /// println!("{:?}", Foo(vec![10, 11]));
+ /// ```
+ #[stable(feature = "debug_builders", since = "1.2.0")]
+ #[inline]
+ pub fn debug_list<'b>(&'b mut self) -> DebugList<'b, 'a> {
+ builders::debug_list_new(self)
+ }
+
+ /// Creates a `DebugSet` builder designed to assist with creation of
+ /// `fmt::Debug` implementations for set-like structures.
+ ///
+ /// # Examples
+ ///
+ /// ```rust
+ /// use std::fmt;
+ ///
+ /// struct Foo(Vec<i32>);
+ ///
+ /// impl fmt::Debug for Foo {
+ /// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+ /// fmt.debug_set().entries(self.0.iter()).finish()
+ /// }
+ /// }
+ ///
+ /// // prints "{10, 11}"
+ /// println!("{:?}", Foo(vec![10, 11]));
+ /// ```
+ #[stable(feature = "debug_builders", since = "1.2.0")]
+ #[inline]
+ pub fn debug_set<'b>(&'b mut self) -> DebugSet<'b, 'a> {
+ builders::debug_set_new(self)
+ }
+
+ /// Creates a `DebugMap` builder designed to assist with creation of
+ /// `fmt::Debug` implementations for map-like structures.
+ ///
+ /// # Examples
+ ///
+ /// ```rust
+ /// use std::fmt;
+ ///
+ /// struct Foo(Vec<(String, i32)>);
+ ///
+ /// impl fmt::Debug for Foo {
+ /// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+ /// fmt.debug_map().entries(self.0.iter().map(|&(ref k, ref v)| (k, v))).finish()
+ /// }
+ /// }
+ ///
+ /// // prints "{"A": 10, "B": 11}"
+ /// println!("{:?}", Foo(vec![("A".to_string(), 10), ("B".to_string(), 11)]));
+ /// ```
+ #[stable(feature = "debug_builders", since = "1.2.0")]
+ #[inline]
+ pub fn debug_map<'b>(&'b mut self) -> DebugMap<'b, 'a> {
+ builders::debug_map_new(self)
+ }
+}
+
+#[stable(since = "1.2.0", feature = "formatter_write")]
+impl<'a> Write for Formatter<'a> {
+ fn write_str(&mut self, s: &str) -> Result {
+ self.buf.write_str(s)
+ }
+
+ fn write_char(&mut self, c: char) -> Result {
+ self.buf.write_char(c)
+ }
+
+ fn write_fmt(&mut self, args: Arguments) -> Result {
+ write(self.buf, args)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Display for Error {
+ fn fmt(&self, f: &mut Formatter) -> Result {
+ Display::fmt("an error occurred when formatting an argument", f)
+ }
+}
+
+// Implementations of the core formatting traits
+
+macro_rules! fmt_refs {
+ ($($tr:ident),*) => {
+ $(
+ #[stable(feature = "rust1", since = "1.0.0")]
+ impl<'a, T: ?Sized + $tr> $tr for &'a T {
+ fn fmt(&self, f: &mut Formatter) -> Result { $tr::fmt(&**self, f) }
+ }
+ #[stable(feature = "rust1", since = "1.0.0")]
+ impl<'a, T: ?Sized + $tr> $tr for &'a mut T {
+ fn fmt(&self, f: &mut Formatter) -> Result { $tr::fmt(&**self, f) }
+ }
+ )*
+ }
+}
+
+fmt_refs! { Debug, Display, Octal, Binary, LowerHex, UpperHex, LowerExp, UpperExp }
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Debug for bool {
+ fn fmt(&self, f: &mut Formatter) -> Result {
+ Display::fmt(self, f)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Display for bool {
+ fn fmt(&self, f: &mut Formatter) -> Result {
+ Display::fmt(if *self { "true" } else { "false" }, f)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Debug for str {
+ fn fmt(&self, f: &mut Formatter) -> Result {
+ f.write_char('"')?;
+ let mut from = 0;
+ for (i, c) in self.char_indices() {
+ let esc = c.escape_default();
+ // If char needs escaping, flush backlog so far and write, else skip
+ if esc.size_hint() != (1, Some(1)) {
+ f.write_str(&self[from..i])?;
+ for c in esc {
+ f.write_char(c)?;
+ }
+ from = i + c.len_utf8();
+ }
+ }
+ f.write_str(&self[from..])?;
+ f.write_char('"')
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Display for str {
+ fn fmt(&self, f: &mut Formatter) -> Result {
+ f.pad(self)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Debug for char {
+ fn fmt(&self, f: &mut Formatter) -> Result {
+ f.write_char('\'')?;
+ for c in self.escape_default() {
+ f.write_char(c)?
+ }
+ f.write_char('\'')
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Display for char {
+ fn fmt(&self, f: &mut Formatter) -> Result {
+ if f.width.is_none() && f.precision.is_none() {
+ f.write_char(*self)
+ } else {
+ f.pad(unsafe {
+ str::from_utf8_unchecked(self.encode_utf8().as_slice())
+ })
+ }
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<T: ?Sized> Pointer for *const T {
+ fn fmt(&self, f: &mut Formatter) -> Result {
+ let old_width = f.width;
+ let old_flags = f.flags;
+
+ // The alternate flag is already treated by LowerHex as being special-
+ // it denotes whether to prefix with 0x. We use it to work out whether
+ // or not to zero extend, and then unconditionally set it to get the
+ // prefix.
+ if f.alternate() {
+ f.flags |= 1 << (FlagV1::SignAwareZeroPad as u32);
+
+ if let None = f.width {
+ f.width = Some(((mem::size_of::<usize>() * 8) / 4) + 2);
+ }
+ }
+ f.flags |= 1 << (FlagV1::Alternate as u32);
+
+ let ret = LowerHex::fmt(&(*self as *const () as usize), f);
+
+ f.width = old_width;
+ f.flags = old_flags;
+
+ ret
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<T: ?Sized> Pointer for *mut T {
+ fn fmt(&self, f: &mut Formatter) -> Result {
+ Pointer::fmt(&(*self as *const T), f)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a, T: ?Sized> Pointer for &'a T {
+ fn fmt(&self, f: &mut Formatter) -> Result {
+ Pointer::fmt(&(*self as *const T), f)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'a, T: ?Sized> Pointer for &'a mut T {
+ fn fmt(&self, f: &mut Formatter) -> Result {
+ Pointer::fmt(&(&**self as *const T), f)
+ }
+}
+
+// Common code of floating point Debug and Display.
+fn float_to_decimal_common<T>(fmt: &mut Formatter, num: &T, negative_zero: bool) -> Result
+ where T: flt2dec::DecodableFloat
+{
+ let force_sign = fmt.sign_plus();
+ let sign = match (force_sign, negative_zero) {
+ (false, false) => flt2dec::Sign::Minus,
+ (false, true) => flt2dec::Sign::MinusRaw,
+ (true, false) => flt2dec::Sign::MinusPlus,
+ (true, true) => flt2dec::Sign::MinusPlusRaw,
+ };
+
+ let mut buf = [0; 1024]; // enough for f32 and f64
+ let mut parts = [flt2dec::Part::Zero(0); 16];
+ let formatted = if let Some(precision) = fmt.precision {
+ flt2dec::to_exact_fixed_str(flt2dec::strategy::grisu::format_exact, *num, sign,
+ precision, false, &mut buf, &mut parts)
+ } else {
+ flt2dec::to_shortest_str(flt2dec::strategy::grisu::format_shortest, *num, sign,
+ 0, false, &mut buf, &mut parts)
+ };
+ fmt.pad_formatted_parts(&formatted)
+}
+
+// Common code of floating point LowerExp and UpperExp.
+fn float_to_exponential_common<T>(fmt: &mut Formatter, num: &T, upper: bool) -> Result
+ where T: flt2dec::DecodableFloat
+{
+ let force_sign = fmt.sign_plus();
+ let sign = match force_sign {
+ false => flt2dec::Sign::Minus,
+ true => flt2dec::Sign::MinusPlus,
+ };
+
+ let mut buf = [0; 1024]; // enough for f32 and f64
+ let mut parts = [flt2dec::Part::Zero(0); 16];
+ let formatted = if let Some(precision) = fmt.precision {
+ // 1 integral digit + `precision` fractional digits = `precision + 1` total digits
+ flt2dec::to_exact_exp_str(flt2dec::strategy::grisu::format_exact, *num, sign,
+ precision + 1, upper, &mut buf, &mut parts)
+ } else {
+ flt2dec::to_shortest_exp_str(flt2dec::strategy::grisu::format_shortest, *num, sign,
+ (0, 0), upper, &mut buf, &mut parts)
+ };
+ fmt.pad_formatted_parts(&formatted)
+}
+
+macro_rules! floating { ($ty:ident) => {
+
+ #[stable(feature = "rust1", since = "1.0.0")]
+ impl Debug for $ty {
+ fn fmt(&self, fmt: &mut Formatter) -> Result {
+ float_to_decimal_common(fmt, self, true)
+ }
+ }
+
+ #[stable(feature = "rust1", since = "1.0.0")]
+ impl Display for $ty {
+ fn fmt(&self, fmt: &mut Formatter) -> Result {
+ float_to_decimal_common(fmt, self, false)
+ }
+ }
+
+ #[stable(feature = "rust1", since = "1.0.0")]
+ impl LowerExp for $ty {
+ fn fmt(&self, fmt: &mut Formatter) -> Result {
+ float_to_exponential_common(fmt, self, false)
+ }
+ }
+
+ #[stable(feature = "rust1", since = "1.0.0")]
+ impl UpperExp for $ty {
+ fn fmt(&self, fmt: &mut Formatter) -> Result {
+ float_to_exponential_common(fmt, self, true)
+ }
+ }
+} }
+floating! { f32 }
+floating! { f64 }
+
+// Implementation of Display/Debug for various core types
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<T> Debug for *const T {
+ fn fmt(&self, f: &mut Formatter) -> Result { Pointer::fmt(self, f) }
+}
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<T> Debug for *mut T {
+ fn fmt(&self, f: &mut Formatter) -> Result { Pointer::fmt(self, f) }
+}
+
+macro_rules! peel {
+ ($name:ident, $($other:ident,)*) => (tuple! { $($other,)* })
+}
+
+macro_rules! tuple {
+ () => ();
+ ( $($name:ident,)+ ) => (
+ #[stable(feature = "rust1", since = "1.0.0")]
+ impl<$($name:Debug),*> Debug for ($($name,)*) {
+ #[allow(non_snake_case, unused_assignments, deprecated)]
+ fn fmt(&self, f: &mut Formatter) -> Result {
+ let mut builder = f.debug_tuple("");
+ let ($(ref $name,)*) = *self;
+ $(
+ builder.field($name);
+ )*
+
+ builder.finish()
+ }
+ }
+ peel! { $($name,)* }
+ )
+}
+
+tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, }
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<T: Debug> Debug for [T] {
+ fn fmt(&self, f: &mut Formatter) -> Result {
+ f.debug_list().entries(self.iter()).finish()
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl Debug for () {
+ fn fmt(&self, f: &mut Formatter) -> Result {
+ f.pad("()")
+ }
+}
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<T: ?Sized> Debug for PhantomData<T> {
+ fn fmt(&self, f: &mut Formatter) -> Result {
+ f.pad("PhantomData")
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<T: Copy + Debug> Debug for Cell<T> {
+ fn fmt(&self, f: &mut Formatter) -> Result {
+ f.debug_struct("Cell")
+ .field("value", &self.get())
+ .finish()
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<T: ?Sized + Debug> Debug for RefCell<T> {
+ fn fmt(&self, f: &mut Formatter) -> Result {
+ match self.borrow_state() {
+ BorrowState::Unused | BorrowState::Reading => {
+ f.debug_struct("RefCell")
+ .field("value", &self.borrow())
+ .finish()
+ }
+ BorrowState::Writing => {
+ f.debug_struct("RefCell")
+ .field("value", &"<borrowed>")
+ .finish()
+ }
+ }
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'b, T: ?Sized + Debug> Debug for Ref<'b, T> {
+ fn fmt(&self, f: &mut Formatter) -> Result {
+ Debug::fmt(&**self, f)
+ }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+impl<'b, T: ?Sized + Debug> Debug for RefMut<'b, T> {
+ fn fmt(&self, f: &mut Formatter) -> Result {
+ Debug::fmt(&*(self.deref()), f)
+ }
+}
+
+#[stable(feature = "core_impl_debug", since = "1.9.0")]
+impl<T: ?Sized + Debug> Debug for UnsafeCell<T> {
+ fn fmt(&self, f: &mut Formatter) -> Result {
+ f.pad("UnsafeCell")
+ }
+}
+
+// If you expected tests to be here, look instead at the run-pass/ifmt.rs test,
+// it's a lot easier than creating all of the rt::Piece structures here.
diff --git a/libcore/fmt/num.rs b/libcore/fmt/num.rs
new file mode 100644
index 0000000..a944c99
--- /dev/null
+++ b/libcore/fmt/num.rs
@@ -0,0 +1,262 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+//! Integer and floating-point number formatting
+
+#![allow(deprecated)]
+
+// FIXME: #6220 Implement floating point formatting
+
+use prelude::v1::*;
+
+use fmt;
+use num::Zero;
+use ops::{Div, Rem, Sub};
+use str;
+use slice;
+use ptr;
+use mem;
+
+#[doc(hidden)]
+trait Int: Zero + PartialEq + PartialOrd + Div<Output=Self> + Rem<Output=Self> +
+ Sub<Output=Self> + Copy {
+ fn from_u8(u: u8) -> Self;
+ fn to_u8(&self) -> u8;
+ fn to_u32(&self) -> u32;
+ fn to_u64(&self) -> u64;
+}
+
+macro_rules! doit {
+ ($($t:ident)*) => ($(impl Int for $t {
+ fn from_u8(u: u8) -> $t { u as $t }
+ fn to_u8(&self) -> u8 { *self as u8 }
+ fn to_u32(&self) -> u32 { *self as u32 }
+ fn to_u64(&self) -> u64 { *self as u64 }
+ })*)
+}
+doit! { i8 i16 i32 i64 isize u8 u16 u32 u64 usize }
+
+/// A type that represents a specific radix
+#[doc(hidden)]
+trait GenericRadix {
+ /// The number of digits.
+ fn base(&self) -> u8;
+
+ /// A radix-specific prefix string.
+ fn prefix(&self) -> &'static str {
+ ""
+ }
+
+ /// Converts an integer to corresponding radix digit.
+ fn digit(&self, x: u8) -> u8;
+
+ /// Format an integer using the radix using a formatter.
+ fn fmt_int<T: Int>(&self, mut x: T, f: &mut fmt::Formatter) -> fmt::Result {
+ // The radix can be as low as 2, so we need a buffer of at least 64
+ // characters for a base 2 number.
+ let zero = T::zero();
+ let is_nonnegative = x >= zero;
+ let mut buf = [0; 64];
+ let mut curr = buf.len();
+ let base = T::from_u8(self.base());
+ if is_nonnegative {
+ // Accumulate each digit of the number from the least significant
+ // to the most significant figure.
+ for byte in buf.iter_mut().rev() {
+ let n = x % base; // Get the current place value.
+ x = x / base; // Deaccumulate the number.
+ *byte = self.digit(n.to_u8()); // Store the digit in the buffer.
+ curr -= 1;
+ if x == zero {
+ // No more digits left to accumulate.
+ break
+ };
+ }
+ } else {
+ // Do the same as above, but accounting for two's complement.
+ for byte in buf.iter_mut().rev() {
+ let n = zero - (x % base); // Get the current place value.
+ x = x / base; // Deaccumulate the number.
+ *byte = self.digit(n.to_u8()); // Store the digit in the buffer.
+ curr -= 1;
+ if x == zero {
+ // No more digits left to accumulate.
+ break
+ };
+ }
+ }
+ let buf = unsafe { str::from_utf8_unchecked(&buf[curr..]) };
+ f.pad_integral(is_nonnegative, self.prefix(), buf)
+ }
+}
+
+/// A binary (base 2) radix
+#[derive(Clone, PartialEq)]
+struct Binary;
+
+/// An octal (base 8) radix
+#[derive(Clone, PartialEq)]
+struct Octal;
+
+/// A decimal (base 10) radix
+#[derive(Clone, PartialEq)]
+struct Decimal;
+
+/// A hexadecimal (base 16) radix, formatted with lower-case characters
+#[derive(Clone, PartialEq)]
+struct LowerHex;
+
+/// A hexadecimal (base 16) radix, formatted with upper-case characters
+#[derive(Clone, PartialEq)]
+struct UpperHex;
+
+macro_rules! radix {
+ ($T:ident, $base:expr, $prefix:expr, $($x:pat => $conv:expr),+) => {
+ impl GenericRadix for $T {
+ fn base(&self) -> u8 { $base }
+ fn prefix(&self) -> &'static str { $prefix }
+ fn digit(&self, x: u8) -> u8 {
+ match x {
+ $($x => $conv,)+
+ x => panic!("number not in the range 0..{}: {}", self.base() - 1, x),
+ }
+ }
+ }
+ }
+}
+
+radix! { Binary, 2, "0b", x @ 0 ... 2 => b'0' + x }
+radix! { Octal, 8, "0o", x @ 0 ... 7 => b'0' + x }
+radix! { Decimal, 10, "", x @ 0 ... 9 => b'0' + x }
+radix! { LowerHex, 16, "0x", x @ 0 ... 9 => b'0' + x,
+ x @ 10 ... 15 => b'a' + (x - 10) }
+radix! { UpperHex, 16, "0x", x @ 0 ... 9 => b'0' + x,
+ x @ 10 ... 15 => b'A' + (x - 10) }
+
+macro_rules! int_base {
+ ($Trait:ident for $T:ident as $U:ident -> $Radix:ident) => {
+ #[stable(feature = "rust1", since = "1.0.0")]
+ impl fmt::$Trait for $T {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ $Radix.fmt_int(*self as $U, f)
+ }
+ }
+ }
+}
+
+macro_rules! debug {
+ ($T:ident) => {
+ #[stable(feature = "rust1", since = "1.0.0")]
+ impl fmt::Debug for $T {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ fmt::Display::fmt(self, f)
+ }
+ }
+ }
+}
+
+macro_rules! integer {
+ ($Int:ident, $Uint:ident) => {
+ int_base! { Binary for $Int as $Uint -> Binary }
+ int_base! { Octal for $Int as $Uint -> Octal }
+ int_base! { LowerHex for $Int as $Uint -> LowerHex }
+ int_base! { UpperHex for $Int as $Uint -> UpperHex }
+ debug! { $Int }
+
+ int_base! { Binary for $Uint as $Uint -> Binary }
+ int_base! { Octal for $Uint as $Uint -> Octal }
+ int_base! { LowerHex for $Uint as $Uint -> LowerHex }
+ int_base! { UpperHex for $Uint as $Uint -> UpperHex }
+ debug! { $Uint }
+ }
+}
+integer! { isize, usize }
+integer! { i8, u8 }
+integer! { i16, u16 }
+integer! { i32, u32 }
+integer! { i64, u64 }
+
+const DEC_DIGITS_LUT: &'static[u8] =
+ b"0001020304050607080910111213141516171819\
+ 2021222324252627282930313233343536373839\
+ 4041424344454647484950515253545556575859\
+ 6061626364656667686970717273747576777879\
+ 8081828384858687888990919293949596979899";
+
+macro_rules! impl_Display {
+ ($($t:ident),*: $conv_fn:ident) => ($(
+ #[stable(feature = "rust1", since = "1.0.0")]
+ impl fmt::Display for $t {
+ #[allow(unused_comparisons)]
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ let is_nonnegative = *self >= 0;
+ let mut n = if is_nonnegative {
+ self.$conv_fn()
+ } else {
+ // convert the negative num to positive by summing 1 to it's 2 complement
+ (!self.$conv_fn()).wrapping_add(1)
+ };
+ let mut buf: [u8; 20] = unsafe { mem::uninitialized() };
+ let mut curr = buf.len() as isize;
+ let buf_ptr = buf.as_mut_ptr();
+ let lut_ptr = DEC_DIGITS_LUT.as_ptr();
+
+ unsafe {
+ // eagerly decode 4 characters at a time
+ if <$t>::max_value() as u64 >= 10000 {
+ while n >= 10000 {
+ let rem = (n % 10000) as isize;
+ n /= 10000;
+
+ let d1 = (rem / 100) << 1;
+ let d2 = (rem % 100) << 1;
+ curr -= 4;
+ ptr::copy_nonoverlapping(lut_ptr.offset(d1), buf_ptr.offset(curr), 2);
+ ptr::copy_nonoverlapping(lut_ptr.offset(d2), buf_ptr.offset(curr + 2), 2);
+ }
+ }
+
+ // if we reach here numbers are <= 9999, so at most 4 chars long
+ let mut n = n as isize; // possibly reduce 64bit math
+
+ // decode 2 more chars, if > 2 chars
+ if n >= 100 {
+ let d1 = (n % 100) << 1;
+ n /= 100;
+ curr -= 2;
+ ptr::copy_nonoverlapping(lut_ptr.offset(d1), buf_ptr.offset(curr), 2);
+ }
+
+ // decode last 1 or 2 chars
+ if n < 10 {
+ curr -= 1;
+ *buf_ptr.offset(curr) = (n as u8) + 48;
+ } else {
+ let d1 = n << 1;
+ curr -= 2;
+ ptr::copy_nonoverlapping(lut_ptr.offset(d1), buf_ptr.offset(curr), 2);
+ }
+ }
+
+ let buf_slice = unsafe {
+ str::from_utf8_unchecked(
+ slice::from_raw_parts(buf_ptr.offset(curr), buf.len() - curr as usize))
+ };
+ f.pad_integral(is_nonnegative, "", buf_slice)
+ }
+ })*);
+}
+
+impl_Display!(i8, u8, i16, u16, i32, u32: to_u32);
+impl_Display!(i64, u64: to_u64);
+#[cfg(target_pointer_width = "32")]
+impl_Display!(isize, usize: to_u32);
+#[cfg(target_pointer_width = "64")]
+impl_Display!(isize, usize: to_u64);
diff --git a/libcore/fmt/rt/v1.rs b/libcore/fmt/rt/v1.rs
new file mode 100644
index 0000000..6b31e04
--- /dev/null
+++ b/libcore/fmt/rt/v1.rs
@@ -0,0 +1,58 @@
+// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+//! This is an internal module used by the ifmt! runtime. These structures are
+//! emitted to static arrays to precompile format strings ahead of time.
+//!
+//! These definitions are similar to their `ct` equivalents, but differ in that
+//! these can be statically allocated and are slightly optimized for the runtime
+#![allow(missing_debug_implementations)]
+
+#[derive(Copy, Clone)]
+pub struct Argument {
+ pub position: Position,
+ pub format: FormatSpec,
+}
+
+#[derive(Copy, Clone)]
+pub struct FormatSpec {
+ pub fill: char,
+ pub align: Alignment,
+ pub flags: u32,
+ pub precision: Count,
+ pub width: Count,
+}
+
+/// Possible alignments that can be requested as part of a formatting directive.
+#[derive(Copy, Clone, PartialEq)]
+pub enum Alignment {
+ /// Indication that contents should be left-aligned.
+ Left,
+ /// Indication that contents should be right-aligned.
+ Right,
+ /// Indication that contents should be center-aligned.
+ Center,
+ /// No alignment was requested.
+ Unknown,
+}
+
+#[derive(Copy, Clone)]
+pub enum Count {
+ Is(usize),
+ Param(usize),
+ NextParam,
+ Implied,
+}
+
+#[derive(Copy, Clone)]
+pub enum Position {
+ Next,
+ At(usize),
+}