From cac7a2053aba7be214d5e58e13867089638a8f50 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 28 May 2014 09:24:28 -0700 Subject: std: Remove i18n/l10n from format! * 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 #14810 [breaking-change] --- src/libcore/fmt/mod.rs | 103 ++++--------------------------------------------- src/libcore/fmt/rt.rs | 40 ++----------------- 2 files changed, 12 insertions(+), 131 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index 0e6a0d1c6f5..a8d458664b8 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -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 @@ -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(()) } @@ -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; @@ -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) } } } @@ -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. @@ -841,9 +749,14 @@ impl Show for () { } impl Show for Cell { + #[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, diff --git a/src/libcore/fmt/rt.rs b/src/libcore/fmt/rt.rs index 1feebbb35b6..6d3edeabca9 100644 --- a/src/libcore/fmt/rt.rs +++ b/src/libcore/fmt/rt.rs @@ -14,13 +14,13 @@ //! 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>), } @@ -28,7 +28,8 @@ pub enum Piece<'a> { pub struct Argument<'a> { pub position: Position, pub format: FormatSpec, - pub method: Option<&'a Method<'a>> + #[cfg(stage0)] + pub method: Option, } #[doc(hidden)] @@ -80,36 +81,3 @@ pub enum Flag { /// being aware of the sign to be printed. FlagSignAwareZeroPad, } - -#[doc(hidden)] -pub enum Method<'a> { - Plural(Option, &'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>], -} -- cgit 1.4.1-3-g733a5