Skip to content

Commit cac7a20

Browse files
committed
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 rust-lang#14810 [breaking-change]
1 parent f9260d4 commit cac7a20

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
@@ -97,12 +97,6 @@ pub struct Formatter<'a> {
9797
args: &'a [Argument<'a>],
9898
}
9999

100-
enum CurrentlyFormatting<'a> {
101-
Nothing,
102-
RawString(&'a str),
103-
Number(uint),
104-
}
105-
106100
/// This struct represents the generic "argument" which is taken by the Xprintf
107101
/// family of functions. It contains a function to format the given value. At
108102
/// 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 {
279273
curarg: args.args.iter(),
280274
};
281275
for piece in args.fmt.iter() {
282-
try!(formatter.run(piece, Nothing));
276+
try!(formatter.run(piece));
283277
}
284278
Ok(())
285279
}
@@ -290,16 +284,9 @@ impl<'a> Formatter<'a> {
290284
// at runtime. This consumes all of the compile-time statics generated by
291285
// the format! syntax extension.
292286

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

317304
// Then actually do some printing
318-
match arg.method {
319-
None => (value.formatter)(value.value, self),
320-
Some(ref method) => self.execute(*method, value)
321-
}
305+
(value.formatter)(value.value, self)
322306
}
323307
}
324308
}
@@ -338,82 +322,6 @@ impl<'a> Formatter<'a> {
338322
}
339323
}
340324

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

@@ -841,9 +749,14 @@ impl Show for () {
841749
}
842750

843751
impl<T: Copy + Show> Show for Cell<T> {
752+
#[cfg(stage0)]
844753
fn fmt(&self, f: &mut Formatter) -> Result {
845754
write!(f, r"Cell \{ value: {} \}", self.get())
846755
}
756+
#[cfg(not(stage0))]
757+
fn fmt(&self, f: &mut Formatter) -> Result {
758+
write!(f, "Cell {{ value: {} }}", self.get())
759+
}
847760
}
848761

849762
// If you expected tests to be here, look instead at the run-pass/ifmt.rs test,

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)