Skip to content

Commit

Permalink
Remove most ~[] usage in liburl
Browse files Browse the repository at this point in the history
  • Loading branch information
sfackler committed Mar 15, 2014
1 parent 352c5e7 commit b1f5235
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions src/liburl/lib.rs
Expand Up @@ -25,6 +25,7 @@ use std::hash::Hash;
use std::io::BufReader;
use std::from_str::FromStr;
use std::uint;
use std::vec_ng::Vec;

use collections::HashMap;

Expand All @@ -42,7 +43,7 @@ use collections::HashMap;
/// host: ~"example.com",
/// port: Some(~"8080"),
/// path: ~"/foo/bar",
/// query: ~[(~"baz", ~"qux")],
/// query: vec!((~"baz", ~"qux")),
/// fragment: Some(~"quz") };
/// // https://username@example.com:8080/foo/bar?baz=qux#quz
/// ```
Expand All @@ -58,7 +59,7 @@ pub struct Url {
port: Option<~str>,
/// The path component of a URL, for example `/foo/bar`.
path: ~str,
/// The query component of a URL. `~[(~"baz", ~"qux")]` represents the
/// The query component of a URL. `vec!((~"baz", ~"qux"))` represents the
/// fragment `baz=qux` in the above example.
query: Query,
/// The fragment component, such as `quz`. Doesn't include the leading `#` character.
Expand All @@ -69,7 +70,7 @@ pub struct Url {
pub struct Path {
/// The path component of a URL, for example `/foo/bar`.
path: ~str,
/// The query component of a URL. `~[(~"baz", ~"qux")]` represents the
/// The query component of a URL. `vec!((~"baz", ~"qux"))` represents the
/// fragment `baz=qux` in the above example.
query: Query,
/// The fragment component, such as `quz`. Doesn't include the leading `#` character.
Expand All @@ -86,7 +87,7 @@ pub struct UserInfo {
}

/// Represents the query component of a URI.
pub type Query = ~[(~str, ~str)];
pub type Query = Vec<(~str, ~str)>;

impl Url {
pub fn new(scheme: ~str,
Expand Down Expand Up @@ -298,7 +299,7 @@ fn encode_plus(s: &str) -> ~str {
/**
* Encode a hashmap to the 'application/x-www-form-urlencoded' media type.
*/
pub fn encode_form_urlencoded(m: &HashMap<~str, ~[~str]>) -> ~str {
pub fn encode_form_urlencoded(m: &HashMap<~str, Vec<~str>>) -> ~str {
let mut out = ~"";
let mut first = true;

Expand All @@ -324,7 +325,7 @@ pub fn encode_form_urlencoded(m: &HashMap<~str, ~[~str]>) -> ~str {
* Decode a string encoded with the 'application/x-www-form-urlencoded' media
* type into a hashmap.
*/
pub fn decode_form_urlencoded(s: &[u8]) -> HashMap<~str, ~[~str]> {
pub fn decode_form_urlencoded(s: &[u8]) -> HashMap<~str, Vec<~str>> {
let mut rdr = BufReader::new(s);
let mut m = HashMap::new();
let mut key = ~"";
Expand All @@ -342,7 +343,7 @@ pub fn decode_form_urlencoded(s: &[u8]) -> HashMap<~str, ~[~str]> {
if key != ~"" && value != ~"" {
let mut values = match m.pop(&key) {
Some(values) => values,
None => ~[],
None => vec!(),
};

values.push(value);
Expand Down Expand Up @@ -380,7 +381,7 @@ pub fn decode_form_urlencoded(s: &[u8]) -> HashMap<~str, ~[~str]> {
if key != ~"" && value != ~"" {
let mut values = match m.pop(&key) {
Some(values) => values,
None => ~[],
None => vec!(),
};

values.push(value);
Expand Down Expand Up @@ -427,7 +428,7 @@ impl fmt::Show for UserInfo {
}

fn query_from_str(rawquery: &str) -> Query {
let mut query: Query = ~[];
let mut query: Query = vec!();
if !rawquery.is_empty() {
for p in rawquery.split('&') {
let (k, v) = split_char_first(p, '=');
Expand All @@ -443,7 +444,7 @@ fn query_from_str(rawquery: &str) -> Query {
* # Example
*
* ```rust
* let query = ~[(~"title", ~"The Village"), (~"north", ~"52.91"), (~"west", ~"4.10")];
* let query = vec!((~"title", ~"The Village"), (~"north", ~"52.91"), (~"west", ~"4.10"));
* println!("{}", url::query_to_str(&query)); // title=The%20Village&north=52.91&west=4.10
* ```
*/
Expand Down Expand Up @@ -709,9 +710,9 @@ fn get_query_fragment(rawurl: &str) ->
let f = decode_component(rawurl.slice(
1,
rawurl.len()));
return Ok((~[], Some(f)));
return Ok((vec!(), Some(f)));
} else {
return Ok((~[], None));
return Ok((vec!(), None));
}
}
let (q, r) = split_char_first(rawurl.slice(1, rawurl.len()), '#');
Expand Down Expand Up @@ -953,7 +954,7 @@ fn test_get_path() {

#[cfg(test)]
mod tests {
use {encode_form_urlencoded, decode_form_urlencoded, decode_component,
use {encode_form_urlencoded, decode_form_urlencoded,
decode, encode, from_str, encode_component, decode_component,
path_from_str, UserInfo, get_scheme};

Expand All @@ -970,7 +971,7 @@ mod tests {
assert_eq!(&u.host, &~"rust-lang.org");
assert_eq!(&u.port, &Some(~"8080"));
assert_eq!(&u.path, &~"/doc/~u");
assert_eq!(&u.query, &~[(~"s", ~"v")]);
assert_eq!(&u.query, &vec!((~"s", ~"v")));
assert_eq!(&u.fragment, &Some(~"something"));
}

Expand All @@ -981,7 +982,7 @@ mod tests {
let up = path_from_str(path);
let u = up.unwrap();
assert_eq!(&u.path, &~"/doc/~u");
assert_eq!(&u.query, &~[(~"s", ~"v")]);
assert_eq!(&u.query, &vec!((~"s", ~"v")));
assert_eq!(&u.fragment, &Some(~"something"));
}

Expand Down Expand Up @@ -1121,15 +1122,15 @@ mod tests {
let url = ~"http://rust-lang.org/doc%20uments?ba%25d%20=%23%26%2B";
let u = from_str(url).unwrap();
assert!(u.path == ~"/doc uments");
assert!(u.query == ~[(~"ba%d ", ~"#&+")]);
assert!(u.query == vec!((~"ba%d ", ~"#&+")));
}

#[test]
fn test_path_component_encoding() {
let path = ~"/doc%20uments?ba%25d%20=%23%26%2B";
let p = path_from_str(path).unwrap();
assert!(p.path == ~"/doc uments");
assert!(p.query == ~[(~"ba%d ", ~"#&+")]);
assert!(p.query == vec!((~"ba%d ", ~"#&+")));
}

#[test]
Expand Down Expand Up @@ -1256,16 +1257,16 @@ mod tests {
let mut m = HashMap::new();
assert_eq!(encode_form_urlencoded(&m), ~"");

m.insert(~"", ~[]);
m.insert(~"foo", ~[]);
m.insert(~"", vec!());
m.insert(~"foo", vec!());
assert_eq!(encode_form_urlencoded(&m), ~"");

let mut m = HashMap::new();
m.insert(~"foo", ~[~"bar", ~"123"]);
m.insert(~"foo", vec!(~"bar", ~"123"));
assert_eq!(encode_form_urlencoded(&m), ~"foo=bar&foo=123");

let mut m = HashMap::new();
m.insert(~"foo bar", ~[~"abc", ~"12 = 34"]);
m.insert(~"foo bar", vec!(~"abc", ~"12 = 34"));
assert!(encode_form_urlencoded(&m) ==
~"foo+bar=abc&foo+bar=12+%3D+34");
}
Expand All @@ -1277,7 +1278,7 @@ mod tests {
let s = "a=1&foo+bar=abc&foo+bar=12+%3D+34".as_bytes();
let form = decode_form_urlencoded(s);
assert_eq!(form.len(), 2);
assert_eq!(form.get(&~"a"), &~[~"1"]);
assert_eq!(form.get(&~"foo bar"), &~[~"abc", ~"12 = 34"]);
assert_eq!(form.get(&~"a"), &vec!(~"1"));
assert_eq!(form.get(&~"foo bar"), &vec!(~"abc", ~"12 = 34"));
}
}

0 comments on commit b1f5235

Please sign in to comment.