Skip to content

Commit 0422934

Browse files
committed
auto merge of #14831 : alexcrichton/rust/format-intl, r=brson
* 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]
2 parents c119903 + cac7a20 commit 0422934

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+736
-1087
lines changed

src/libcollections/bitv.rs

+14
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,7 @@ impl cmp::PartialEq for BitvSet {
842842
}
843843

844844
impl fmt::Show for BitvSet {
845+
#[cfg(stage0)]
845846
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
846847
try!(write!(fmt, r"\{"));
847848
let mut first = true;
@@ -854,6 +855,19 @@ impl fmt::Show for BitvSet {
854855
}
855856
write!(fmt, r"\}")
856857
}
858+
#[cfg(not(stage0))]
859+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
860+
try!(write!(fmt, "{{"));
861+
let mut first = true;
862+
for n in self.iter() {
863+
if !first {
864+
try!(write!(fmt, ", "));
865+
}
866+
try!(write!(fmt, "{}", n));
867+
first = false;
868+
}
869+
write!(fmt, "}}")
870+
}
857871
}
858872

859873
impl<S: hash::Writer> hash::Hash<S> for BitvSet {

src/libcollections/smallintmap.rs

+12
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ impl<V:Clone> SmallIntMap<V> {
185185
}
186186

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

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

196197
write!(f, r"\}")
197198
}
199+
#[cfg(not(stage0))]
200+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
201+
try!(write!(f, "{{"));
202+
203+
for (i, (k, v)) in self.iter().enumerate() {
204+
if i != 0 { try!(write!(f, ", ")); }
205+
try!(write!(f, "{}: {}", k, *v));
206+
}
207+
208+
write!(f, "}}")
209+
}
198210
}
199211

200212
macro_rules! iterator {

src/libcollections/treemap.rs

+24
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ impl<K: PartialOrd + Ord, V: PartialOrd> PartialOrd for TreeMap<K, V> {
7676
}
7777

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

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

8788
write!(f, r"\}")
8889
}
90+
#[cfg(not(stage0))]
91+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
92+
try!(write!(f, "{{"));
93+
94+
for (i, (k, v)) in self.iter().enumerate() {
95+
if i != 0 { try!(write!(f, ", ")); }
96+
try!(write!(f, "{}: {}", *k, *v));
97+
}
98+
99+
write!(f, "}}")
100+
}
89101
}
90102

91103
impl<K: Ord, V> Collection for TreeMap<K, V> {
@@ -574,6 +586,7 @@ impl<T: PartialOrd + Ord> PartialOrd for TreeSet<T> {
574586
}
575587

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

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

585598
write!(f, r"\}")
586599
}
600+
#[cfg(not(stage0))]
601+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
602+
try!(write!(f, "{{"));
603+
604+
for (i, x) in self.iter().enumerate() {
605+
if i != 0 { try!(write!(f, ", ")); }
606+
try!(write!(f, "{}", *x));
607+
}
608+
609+
write!(f, "}}")
610+
}
587611
}
588612

589613
impl<T: Ord> Collection for TreeSet<T> {

src/libcore/fmt/mod.rs

+8-95
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,6 @@ pub struct Formatter<'a> {
9898
args: &'a [Argument<'a>],
9999
}
100100

101-
enum CurrentlyFormatting<'a> {
102-
Nothing,
103-
RawString(&'a str),
104-
Number(uint),
105-
}
106-
107101
/// This struct represents the generic "argument" which is taken by the Xprintf
108102
/// family of functions. It contains a function to format the given value. At
109103
/// compile time it is ensured that the function and the value have the correct
@@ -280,7 +274,7 @@ pub fn write(output: &mut FormatWriter, args: &Arguments) -> Result {
280274
curarg: args.args.iter(),
281275
};
282276
for piece in args.fmt.iter() {
283-
try!(formatter.run(piece, Nothing));
277+
try!(formatter.run(piece));
284278
}
285279
Ok(())
286280
}
@@ -291,16 +285,9 @@ impl<'a> Formatter<'a> {
291285
// at runtime. This consumes all of the compile-time statics generated by
292286
// the format! syntax extension.
293287

294-
fn run(&mut self, piece: &rt::Piece, cur: CurrentlyFormatting) -> Result {
288+
fn run(&mut self, piece: &rt::Piece) -> Result {
295289
match *piece {
296290
rt::String(s) => self.buf.write(s.as_bytes()),
297-
rt::CurrentArgument(()) => {
298-
match cur {
299-
Nothing => Ok(()),
300-
Number(n) => secret_show(&radix(n, 10), self),
301-
RawString(s) => self.buf.write(s.as_bytes()),
302-
}
303-
}
304291
rt::Argument(ref arg) => {
305292
// Fill in the format parameters into the formatter
306293
self.fill = arg.format.fill;
@@ -316,10 +303,7 @@ impl<'a> Formatter<'a> {
316303
};
317304

318305
// Then actually do some printing
319-
match arg.method {
320-
None => (value.formatter)(value.value, self),
321-
Some(ref method) => self.execute(*method, value)
322-
}
306+
(value.formatter)(value.value, self)
323307
}
324308
}
325309
}
@@ -339,82 +323,6 @@ impl<'a> Formatter<'a> {
339323
}
340324
}
341325

342-
fn execute(&mut self, method: &rt::Method, arg: Argument) -> Result {
343-
match *method {
344-
// Pluralization is selection upon a numeric value specified as the
345-
// parameter.
346-
rt::Plural(offset, ref selectors, ref default) => {
347-
// This is validated at compile-time to be a pointer to a
348-
// '&uint' value.
349-
let value: &uint = unsafe { mem::transmute(arg.value) };
350-
let value = *value;
351-
352-
// First, attempt to match against explicit values without the
353-
// offsetted value
354-
for s in selectors.iter() {
355-
match s.selector {
356-
rt::Literal(val) if value == val => {
357-
return self.runplural(value, s.result);
358-
}
359-
_ => {}
360-
}
361-
}
362-
363-
// Next, offset the value and attempt to match against the
364-
// keyword selectors.
365-
let value = value - match offset { Some(i) => i, None => 0 };
366-
for s in selectors.iter() {
367-
let run = match s.selector {
368-
rt::Keyword(rt::Zero) => value == 0,
369-
rt::Keyword(rt::One) => value == 1,
370-
rt::Keyword(rt::Two) => value == 2,
371-
372-
// FIXME: Few/Many should have a user-specified boundary
373-
// One possible option would be in the function
374-
// pointer of the 'arg: Argument' struct.
375-
rt::Keyword(rt::Few) => value < 8,
376-
rt::Keyword(rt::Many) => value >= 8,
377-
378-
rt::Literal(..) => false
379-
};
380-
if run {
381-
return self.runplural(value, s.result);
382-
}
383-
}
384-
385-
self.runplural(value, *default)
386-
}
387-
388-
// Select is just a matching against the string specified.
389-
rt::Select(ref selectors, ref default) => {
390-
// This is validated at compile-time to be a pointer to a
391-
// string slice,
392-
let value: & &str = unsafe { mem::transmute(arg.value) };
393-
let value = *value;
394-
395-
for s in selectors.iter() {
396-
if s.selector == value {
397-
for piece in s.result.iter() {
398-
try!(self.run(piece, RawString(value)));
399-
}
400-
return Ok(());
401-
}
402-
}
403-
for piece in default.iter() {
404-
try!(self.run(piece, RawString(value)));
405-
}
406-
Ok(())
407-
}
408-
}
409-
}
410-
411-
fn runplural(&mut self, value: uint, pieces: &[rt::Piece]) -> Result {
412-
for piece in pieces.iter() {
413-
try!(self.run(piece, Number(value)));
414-
}
415-
Ok(())
416-
}
417-
418326
// Helper methods used for padding and processing formatting arguments that
419327
// all formatting traits can use.
420328

@@ -836,9 +744,14 @@ impl Show for () {
836744
}
837745

838746
impl<T: Copy + Show> Show for Cell<T> {
747+
#[cfg(stage0)]
839748
fn fmt(&self, f: &mut Formatter) -> Result {
840749
write!(f, r"Cell \{ value: {} \}", self.get())
841750
}
751+
#[cfg(not(stage0))]
752+
fn fmt(&self, f: &mut Formatter) -> Result {
753+
write!(f, "Cell {{ value: {} }}", self.get())
754+
}
842755
}
843756

844757
impl<'b, T: Show> Show for Ref<'b, T> {

src/libcore/fmt/rt.rs

+4-36
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,22 @@
1414
//! These definitions are similar to their `ct` equivalents, but differ in that
1515
//! these can be statically allocated and are slightly optimized for the runtime
1616
17+
18+
#[cfg(stage0)]
1719
use option::Option;
1820

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

2727
#[doc(hidden)]
2828
pub struct Argument<'a> {
2929
pub position: Position,
3030
pub format: FormatSpec,
31-
pub method: Option<&'a Method<'a>>
31+
#[cfg(stage0)]
32+
pub method: Option<uint>,
3233
}
3334

3435
#[doc(hidden)]
@@ -80,36 +81,3 @@ pub enum Flag {
8081
/// being aware of the sign to be printed.
8182
FlagSignAwareZeroPad,
8283
}
83-
84-
#[doc(hidden)]
85-
pub enum Method<'a> {
86-
Plural(Option<uint>, &'a [PluralArm<'a>], &'a [Piece<'a>]),
87-
Select(&'a [SelectArm<'a>], &'a [Piece<'a>]),
88-
}
89-
90-
#[doc(hidden)]
91-
pub enum PluralSelector {
92-
Keyword(PluralKeyword),
93-
Literal(uint),
94-
}
95-
96-
#[doc(hidden)]
97-
pub enum PluralKeyword {
98-
Zero,
99-
One,
100-
Two,
101-
Few,
102-
Many,
103-
}
104-
105-
#[doc(hidden)]
106-
pub struct PluralArm<'a> {
107-
pub selector: PluralSelector,
108-
pub result: &'a [Piece<'a>],
109-
}
110-
111-
#[doc(hidden)]
112-
pub struct SelectArm<'a> {
113-
pub selector: &'a str,
114-
pub result: &'a [Piece<'a>],
115-
}

0 commit comments

Comments
 (0)