Skip to content

Commit

Permalink
std,syntax: make std::fmt::parse use Vecs.
Browse files Browse the repository at this point in the history
  • Loading branch information
huonw authored and alexcrichton committed Apr 10, 2014
1 parent 3015949 commit 1403b35
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 62 deletions.
116 changes: 58 additions & 58 deletions src/libstd/fmt/parse.rs
Expand Up @@ -131,13 +131,13 @@ pub enum Method<'a> {
///
/// The final element of this enum is the default "other" case which is
/// always required to be specified.
Plural(Option<uint>, ~[PluralArm<'a>], ~[Piece<'a>]),
Plural(Option<uint>, Vec<PluralArm<'a>>, Vec<Piece<'a>>),

/// A select method selects over a string. Each arm is a different string
/// which can be selected for.
///
/// As with `Plural`, a default "other" case is required as well.
Select(~[SelectArm<'a>], ~[Piece<'a>]),
Select(Vec<SelectArm<'a>>, Vec<Piece<'a>>),
}

/// A selector for what pluralization a plural method should take
Expand All @@ -156,7 +156,7 @@ pub struct PluralArm<'a> {
/// literal.
pub selector: PluralSelector,
/// Array of pieces which are the format of this arm
pub result: ~[Piece<'a>],
pub result: Vec<Piece<'a>>,
}

/// Enum of the 5 CLDR plural keywords. There is one more, "other", but that
Expand Down Expand Up @@ -184,7 +184,7 @@ pub struct SelectArm<'a> {
/// String selector which guards this arm
pub selector: &'a str,
/// Array of pieces which are the format of this arm
pub result: ~[Piece<'a>],
pub result: Vec<Piece<'a>>,
}

/// The parser structure for interpreting the input format string. This is
Expand All @@ -198,7 +198,7 @@ pub struct Parser<'a> {
cur: str::CharOffsets<'a>,
depth: uint,
/// Error messages accumulated during parsing
pub errors: ~[~str],
pub errors: Vec<~str>,
}

impl<'a> Iterator<Piece<'a>> for Parser<'a> {
Expand Down Expand Up @@ -236,7 +236,7 @@ impl<'a> Parser<'a> {
input: s,
cur: s.char_indices(),
depth: 0,
errors: ~[],
errors: vec!(),
}
}

Expand Down Expand Up @@ -463,7 +463,7 @@ impl<'a> Parser<'a> {
/// Parses a 'select' statement (after the initial 'select' word)
fn select(&mut self) -> ~Method<'a> {
let mut other = None;
let mut arms = ~[];
let mut arms = vec!();
// Consume arms one at a time
loop {
self.ws();
Expand Down Expand Up @@ -496,7 +496,7 @@ impl<'a> Parser<'a> {
Some(arm) => { arm }
None => {
self.err("`select` statement must provide an `other` case");
~[]
vec!()
}
};
~Select(arms, other)
Expand All @@ -506,7 +506,7 @@ impl<'a> Parser<'a> {
fn plural(&mut self) -> ~Method<'a> {
let mut offset = None;
let mut other = None;
let mut arms = ~[];
let mut arms = vec!();

// First, attempt to parse the 'offset:' field. We know the set of
// selector words which can appear in plural arms, and the only ones
Expand Down Expand Up @@ -594,7 +594,7 @@ impl<'a> Parser<'a> {
Some(arm) => { arm }
None => {
self.err("`plural` statement must provide an `other` case");
~[]
vec!()
}
};
~Plural(offset, arms, other)
Expand Down Expand Up @@ -684,9 +684,9 @@ mod tests {
use super::*;
use prelude::*;

fn same(fmt: &'static str, p: ~[Piece<'static>]) {
fn same(fmt: &'static str, p: &[Piece<'static>]) {
let mut parser = Parser::new(fmt);
assert!(p == parser.collect());
assert!(p == parser.collect::<Vec<Piece<'static>>>().as_slice());
}

fn fmtdflt() -> FormatSpec<'static> {
Expand All @@ -708,12 +708,12 @@ mod tests {

#[test]
fn simple() {
same("asdf", ~[String("asdf")]);
same("a\\{b", ~[String("a"), String("{b")]);
same("a\\#b", ~[String("a"), String("#b")]);
same("a\\}b", ~[String("a"), String("}b")]);
same("a\\}", ~[String("a"), String("}")]);
same("\\}", ~[String("}")]);
same("asdf", [String("asdf")]);
same("a\\{b", [String("a"), String("{b")]);
same("a\\#b", [String("a"), String("#b")]);
same("a\\}b", [String("a"), String("}b")]);
same("a\\}", [String("a"), String("}")]);
same("\\}", [String("}")]);
}

#[test] fn invalid01() { musterr("{") }
Expand All @@ -725,31 +725,31 @@ mod tests {

#[test]
fn format_nothing() {
same("{}", ~[Argument(Argument {
same("{}", [Argument(Argument {
position: ArgumentNext,
format: fmtdflt(),
method: None,
})]);
}
#[test]
fn format_position() {
same("{3}", ~[Argument(Argument {
same("{3}", [Argument(Argument {
position: ArgumentIs(3),
format: fmtdflt(),
method: None,
})]);
}
#[test]
fn format_position_nothing_else() {
same("{3:}", ~[Argument(Argument {
same("{3:}", [Argument(Argument {
position: ArgumentIs(3),
format: fmtdflt(),
method: None,
})]);
}
#[test]
fn format_type() {
same("{3:a}", ~[Argument(Argument {
same("{3:a}", [Argument(Argument {
position: ArgumentIs(3),
format: FormatSpec {
fill: None,
Expand All @@ -764,7 +764,7 @@ mod tests {
}
#[test]
fn format_align_fill() {
same("{3:>}", ~[Argument(Argument {
same("{3:>}", [Argument(Argument {
position: ArgumentIs(3),
format: FormatSpec {
fill: None,
Expand All @@ -776,7 +776,7 @@ mod tests {
},
method: None,
})]);
same("{3:0<}", ~[Argument(Argument {
same("{3:0<}", [Argument(Argument {
position: ArgumentIs(3),
format: FormatSpec {
fill: Some('0'),
Expand All @@ -788,7 +788,7 @@ mod tests {
},
method: None,
})]);
same("{3:*<abcd}", ~[Argument(Argument {
same("{3:*<abcd}", [Argument(Argument {
position: ArgumentIs(3),
format: FormatSpec {
fill: Some('*'),
Expand All @@ -803,7 +803,7 @@ mod tests {
}
#[test]
fn format_counts() {
same("{:10s}", ~[Argument(Argument {
same("{:10s}", [Argument(Argument {
position: ArgumentNext,
format: FormatSpec {
fill: None,
Expand All @@ -815,7 +815,7 @@ mod tests {
},
method: None,
})]);
same("{:10$.10s}", ~[Argument(Argument {
same("{:10$.10s}", [Argument(Argument {
position: ArgumentNext,
format: FormatSpec {
fill: None,
Expand All @@ -827,7 +827,7 @@ mod tests {
},
method: None,
})]);
same("{:.*s}", ~[Argument(Argument {
same("{:.*s}", [Argument(Argument {
position: ArgumentNext,
format: FormatSpec {
fill: None,
Expand All @@ -839,7 +839,7 @@ mod tests {
},
method: None,
})]);
same("{:.10$s}", ~[Argument(Argument {
same("{:.10$s}", [Argument(Argument {
position: ArgumentNext,
format: FormatSpec {
fill: None,
Expand All @@ -851,7 +851,7 @@ mod tests {
},
method: None,
})]);
same("{:a$.b$s}", ~[Argument(Argument {
same("{:a$.b$s}", [Argument(Argument {
position: ArgumentNext,
format: FormatSpec {
fill: None,
Expand All @@ -866,7 +866,7 @@ mod tests {
}
#[test]
fn format_flags() {
same("{:-}", ~[Argument(Argument {
same("{:-}", [Argument(Argument {
position: ArgumentNext,
format: FormatSpec {
fill: None,
Expand All @@ -878,7 +878,7 @@ mod tests {
},
method: None,
})]);
same("{:+#}", ~[Argument(Argument {
same("{:+#}", [Argument(Argument {
position: ArgumentNext,
format: FormatSpec {
fill: None,
Expand All @@ -893,7 +893,7 @@ mod tests {
}
#[test]
fn format_mixture() {
same("abcd {3:a} efg", ~[String("abcd "), Argument(Argument {
same("abcd {3:a} efg", [String("abcd "), Argument(Argument {
position: ArgumentIs(3),
format: FormatSpec {
fill: None,
Expand All @@ -909,42 +909,42 @@ mod tests {

#[test]
fn select_simple() {
same("{, select, other { haha } }", ~[Argument(Argument{
same("{, select, other { haha } }", [Argument(Argument{
position: ArgumentNext,
format: fmtdflt(),
method: Some(~Select(~[], ~[String(" haha ")]))
method: Some(~Select(vec![], vec![String(" haha ")]))
})]);
same("{1, select, other { haha } }", ~[Argument(Argument{
same("{1, select, other { haha } }", [Argument(Argument{
position: ArgumentIs(1),
format: fmtdflt(),
method: Some(~Select(~[], ~[String(" haha ")]))
method: Some(~Select(vec![], vec![String(" haha ")]))
})]);
same("{1, select, other {#} }", ~[Argument(Argument{
same("{1, select, other {#} }", [Argument(Argument{
position: ArgumentIs(1),
format: fmtdflt(),
method: Some(~Select(~[], ~[CurrentArgument]))
method: Some(~Select(vec![], vec![CurrentArgument]))
})]);
same("{1, select, other {{2, select, other {lol}}} }", ~[Argument(Argument{
same("{1, select, other {{2, select, other {lol}}} }", [Argument(Argument{
position: ArgumentIs(1),
format: fmtdflt(),
method: Some(~Select(~[], ~[Argument(Argument{
method: Some(~Select(vec![], vec![Argument(Argument{
position: ArgumentIs(2),
format: fmtdflt(),
method: Some(~Select(~[], ~[String("lol")]))
method: Some(~Select(vec![], vec![String("lol")]))
})])) // wat
})]);
}

#[test]
fn select_cases() {
same("{1, select, a{1} b{2} c{3} other{4} }", ~[Argument(Argument{
same("{1, select, a{1} b{2} c{3} other{4} }", [Argument(Argument{
position: ArgumentIs(1),
format: fmtdflt(),
method: Some(~Select(~[
SelectArm{ selector: "a", result: ~[String("1")] },
SelectArm{ selector: "b", result: ~[String("2")] },
SelectArm{ selector: "c", result: ~[String("3")] },
], ~[String("4")]))
method: Some(~Select(vec![
SelectArm{ selector: "a", result: vec![String("1")] },
SelectArm{ selector: "b", result: vec![String("2")] },
SelectArm{ selector: "c", result: vec![String("3")] },
], vec![String("4")]))
})]);
}

Expand All @@ -961,25 +961,25 @@ mod tests {

#[test]
fn plural_simple() {
same("{, plural, other { haha } }", ~[Argument(Argument{
same("{, plural, other { haha } }", [Argument(Argument{
position: ArgumentNext,
format: fmtdflt(),
method: Some(~Plural(None, ~[], ~[String(" haha ")]))
method: Some(~Plural(None, vec![], vec![String(" haha ")]))
})]);
same("{:, plural, other { haha } }", ~[Argument(Argument{
same("{:, plural, other { haha } }", [Argument(Argument{
position: ArgumentNext,
format: fmtdflt(),
method: Some(~Plural(None, ~[], ~[String(" haha ")]))
method: Some(~Plural(None, vec![], vec![String(" haha ")]))
})]);
same("{, plural, offset:1 =2{2} =3{3} many{yes} other{haha} }",
~[Argument(Argument{
[Argument(Argument{
position: ArgumentNext,
format: fmtdflt(),
method: Some(~Plural(Some(1), ~[
PluralArm{ selector: Literal(2), result: ~[String("2")] },
PluralArm{ selector: Literal(3), result: ~[String("3")] },
PluralArm{ selector: Keyword(Many), result: ~[String("yes")] }
], ~[String("haha")]))
method: Some(~Plural(Some(1), vec![
PluralArm{ selector: Literal(2), result: vec![String("2")] },
PluralArm{ selector: Literal(3), result: vec![String("3")] },
PluralArm{ selector: Keyword(Many), result: vec![String("yes")] }
], vec![String("haha")]))
})]);
}
}
8 changes: 4 additions & 4 deletions src/libsyntax/ext/format.rs
Expand Up @@ -242,9 +242,9 @@ impl<'a, 'b> Context<'a, 'b> {
}
}
}
self.verify_pieces(arm.result);
self.verify_pieces(arm.result.as_slice());
}
self.verify_pieces(*default);
self.verify_pieces(default.as_slice());
}
parse::Select(ref arms, ref default) => {
self.verify_arg_type(pos, String);
Expand All @@ -258,9 +258,9 @@ impl<'a, 'b> Context<'a, 'b> {
self.ecx.span_err(self.fmtsp,
"empty selector in `select`");
}
self.verify_pieces(arm.result);
self.verify_pieces(arm.result.as_slice());
}
self.verify_pieces(*default);
self.verify_pieces(default.as_slice());
}
}
self.nest_level -= 1;
Expand Down

0 comments on commit 1403b35

Please sign in to comment.