Skip to content

Commit

Permalink
std: Remove i18n/l10n from format!
Browse files Browse the repository at this point in the history
* The select/plural methods from format strings are removed
* The # character no longer needs to be escaped
* The \-based escapes have been removed
* '{{' is now an escape for '{'
* '}}' is now an escape for '}'

Closes rust-lang#14810
[breaking-change]
  • Loading branch information
alexcrichton committed Jun 11, 2014
1 parent f9260d4 commit cac7a20
Show file tree
Hide file tree
Showing 57 changed files with 736 additions and 1,087 deletions.
14 changes: 14 additions & 0 deletions src/libcollections/bitv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,7 @@ impl cmp::PartialEq for BitvSet {
}

impl fmt::Show for BitvSet {
#[cfg(stage0)]
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
try!(write!(fmt, r"\{"));
let mut first = true;
Expand All @@ -854,6 +855,19 @@ impl fmt::Show for BitvSet {
}
write!(fmt, r"\}")
}
#[cfg(not(stage0))]
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
try!(write!(fmt, "{{"));
let mut first = true;
for n in self.iter() {
if !first {
try!(write!(fmt, ", "));
}
try!(write!(fmt, "{}", n));
first = false;
}
write!(fmt, "}}")
}
}

impl<S: hash::Writer> hash::Hash<S> for BitvSet {
Expand Down
12 changes: 12 additions & 0 deletions src/libcollections/smallintmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ impl<V:Clone> SmallIntMap<V> {
}

impl<V: fmt::Show> fmt::Show for SmallIntMap<V> {
#[cfg(stage0)]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, r"\{"));

Expand All @@ -195,6 +196,17 @@ impl<V: fmt::Show> fmt::Show for SmallIntMap<V> {

write!(f, r"\}")
}
#[cfg(not(stage0))]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "{{"));

for (i, (k, v)) in self.iter().enumerate() {
if i != 0 { try!(write!(f, ", ")); }
try!(write!(f, "{}: {}", k, *v));
}

write!(f, "}}")
}
}

macro_rules! iterator {
Expand Down
24 changes: 24 additions & 0 deletions src/libcollections/treemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ impl<K: PartialOrd + Ord, V: PartialOrd> PartialOrd for TreeMap<K, V> {
}

impl<K: Ord + Show, V: Show> Show for TreeMap<K, V> {
#[cfg(stage0)]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, r"\{"));

Expand All @@ -86,6 +87,17 @@ impl<K: Ord + Show, V: Show> Show for TreeMap<K, V> {

write!(f, r"\}")
}
#[cfg(not(stage0))]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "{{"));

for (i, (k, v)) in self.iter().enumerate() {
if i != 0 { try!(write!(f, ", ")); }
try!(write!(f, "{}: {}", *k, *v));
}

write!(f, "}}")
}
}

impl<K: Ord, V> Collection for TreeMap<K, V> {
Expand Down Expand Up @@ -574,6 +586,7 @@ impl<T: PartialOrd + Ord> PartialOrd for TreeSet<T> {
}

impl<T: Ord + Show> Show for TreeSet<T> {
#[cfg(stage0)]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, r"\{"));

Expand All @@ -584,6 +597,17 @@ impl<T: Ord + Show> Show for TreeSet<T> {

write!(f, r"\}")
}
#[cfg(not(stage0))]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "{{"));

for (i, x) in self.iter().enumerate() {
if i != 0 { try!(write!(f, ", ")); }
try!(write!(f, "{}", *x));
}

write!(f, "}}")
}
}

impl<T: Ord> Collection for TreeSet<T> {
Expand Down
103 changes: 8 additions & 95 deletions src/libcore/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,6 @@ pub struct Formatter<'a> {
args: &'a [Argument<'a>],
}

enum CurrentlyFormatting<'a> {
Nothing,
RawString(&'a str),
Number(uint),
}

/// 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
Expand Down Expand Up @@ -279,7 +273,7 @@ pub fn write(output: &mut FormatWriter, args: &Arguments) -> Result {
curarg: args.args.iter(),
};
for piece in args.fmt.iter() {
try!(formatter.run(piece, Nothing));
try!(formatter.run(piece));
}
Ok(())
}
Expand All @@ -290,16 +284,9 @@ impl<'a> Formatter<'a> {
// at runtime. This consumes all of the compile-time statics generated by
// the format! syntax extension.

fn run(&mut self, piece: &rt::Piece, cur: CurrentlyFormatting) -> Result {
fn run(&mut self, piece: &rt::Piece) -> Result {
match *piece {
rt::String(s) => self.buf.write(s.as_bytes()),
rt::CurrentArgument(()) => {
match cur {
Nothing => Ok(()),
Number(n) => secret_show(&radix(n, 10), self),
RawString(s) => self.buf.write(s.as_bytes()),
}
}
rt::Argument(ref arg) => {
// Fill in the format parameters into the formatter
self.fill = arg.format.fill;
Expand All @@ -315,10 +302,7 @@ impl<'a> Formatter<'a> {
};

// Then actually do some printing
match arg.method {
None => (value.formatter)(value.value, self),
Some(ref method) => self.execute(*method, value)
}
(value.formatter)(value.value, self)
}
}
}
Expand All @@ -338,82 +322,6 @@ impl<'a> Formatter<'a> {
}
}

fn execute(&mut self, method: &rt::Method, arg: Argument) -> Result {
match *method {
// Pluralization is selection upon a numeric value specified as the
// parameter.
rt::Plural(offset, ref selectors, ref default) => {
// This is validated at compile-time to be a pointer to a
// '&uint' value.
let value: &uint = unsafe { mem::transmute(arg.value) };
let value = *value;

// First, attempt to match against explicit values without the
// offsetted value
for s in selectors.iter() {
match s.selector {
rt::Literal(val) if value == val => {
return self.runplural(value, s.result);
}
_ => {}
}
}

// Next, offset the value and attempt to match against the
// keyword selectors.
let value = value - match offset { Some(i) => i, None => 0 };
for s in selectors.iter() {
let run = match s.selector {
rt::Keyword(rt::Zero) => value == 0,
rt::Keyword(rt::One) => value == 1,
rt::Keyword(rt::Two) => value == 2,

// FIXME: Few/Many should have a user-specified boundary
// One possible option would be in the function
// pointer of the 'arg: Argument' struct.
rt::Keyword(rt::Few) => value < 8,
rt::Keyword(rt::Many) => value >= 8,

rt::Literal(..) => false
};
if run {
return self.runplural(value, s.result);
}
}

self.runplural(value, *default)
}

// Select is just a matching against the string specified.
rt::Select(ref selectors, ref default) => {
// This is validated at compile-time to be a pointer to a
// string slice,
let value: & &str = unsafe { mem::transmute(arg.value) };
let value = *value;

for s in selectors.iter() {
if s.selector == value {
for piece in s.result.iter() {
try!(self.run(piece, RawString(value)));
}
return Ok(());
}
}
for piece in default.iter() {
try!(self.run(piece, RawString(value)));
}
Ok(())
}
}
}

fn runplural(&mut self, value: uint, pieces: &[rt::Piece]) -> Result {
for piece in pieces.iter() {
try!(self.run(piece, Number(value)));
}
Ok(())
}

// Helper methods used for padding and processing formatting arguments that
// all formatting traits can use.

Expand Down Expand Up @@ -841,9 +749,14 @@ impl Show for () {
}

impl<T: Copy + Show> Show for Cell<T> {
#[cfg(stage0)]
fn fmt(&self, f: &mut Formatter) -> Result {
write!(f, r"Cell \{ value: {} \}", self.get())
}
#[cfg(not(stage0))]
fn fmt(&self, f: &mut Formatter) -> Result {
write!(f, "Cell {{ value: {} }}", self.get())
}
}

// If you expected tests to be here, look instead at the run-pass/ifmt.rs test,
Expand Down
40 changes: 4 additions & 36 deletions src/libcore/fmt/rt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,22 @@
//! These definitions are similar to their `ct` equivalents, but differ in that
//! these can be statically allocated and are slightly optimized for the runtime


#[cfg(stage0)]
use option::Option;

#[doc(hidden)]
pub enum Piece<'a> {
String(&'a str),
// FIXME(#8259): this shouldn't require the unit-value here
CurrentArgument(()),
Argument(Argument<'a>),
}

#[doc(hidden)]
pub struct Argument<'a> {
pub position: Position,
pub format: FormatSpec,
pub method: Option<&'a Method<'a>>
#[cfg(stage0)]
pub method: Option<uint>,
}

#[doc(hidden)]
Expand Down Expand Up @@ -80,36 +81,3 @@ pub enum Flag {
/// being aware of the sign to be printed.
FlagSignAwareZeroPad,
}

#[doc(hidden)]
pub enum Method<'a> {
Plural(Option<uint>, &'a [PluralArm<'a>], &'a [Piece<'a>]),
Select(&'a [SelectArm<'a>], &'a [Piece<'a>]),
}

#[doc(hidden)]
pub enum PluralSelector {
Keyword(PluralKeyword),
Literal(uint),
}

#[doc(hidden)]
pub enum PluralKeyword {
Zero,
One,
Two,
Few,
Many,
}

#[doc(hidden)]
pub struct PluralArm<'a> {
pub selector: PluralSelector,
pub result: &'a [Piece<'a>],
}

#[doc(hidden)]
pub struct SelectArm<'a> {
pub selector: &'a str,
pub result: &'a [Piece<'a>],
}
Loading

0 comments on commit cac7a20

Please sign in to comment.