use std::io::IoResult;
use rfc2616::is_token;
pub fn normalise_header_name(name: &String) -> String {
let mut result: String = String::with_capacity(name.len());
let mut capitalise = true;
for c in name[].chars() {
let c = match capitalise {
true => c.to_ascii().to_uppercase(),
false => c.to_ascii().to_lowercase(),
};
result.push(c.as_char());
capitalise = c.as_byte() == 45;
}
result
}
pub fn comma_split(value: &str) -> Vec<String> {
value.split(',').map(|w| String::from_str(w.trim_left())).collect()
}
pub fn comma_split_iter<'a>(value: &'a str)
-> ::std::iter::Map<'a, &'a str, &'a str, ::std::str::CharSplits<'a, char>> {
value.split(',').map(|w| w.trim_left())
}
pub trait WriterUtil: Writer {
fn write_maybe_quoted_string(&mut self, s: &String) -> IoResult<()> {
if is_token(s) {
self.write(s.as_bytes())
} else {
self.write_quoted_string(s)
}
}
fn write_quoted_string(&mut self, s: &String) -> IoResult<()> {
try!(self.write(b"\""));
for b in s.as_bytes().iter() {
if *b == b'\\' || *b == b'"' {
try!(self.write(b"\\"));
}
try!(self.write(&[*b]));
}
self.write(b"\"")
}
fn write_parameter(&mut self, k: &str, v: &String) -> IoResult<()> {
try!(self.write(k.as_bytes()));
try!(self.write(b"="));
self.write_maybe_quoted_string(v)
}
fn write_parameters(&mut self, parameters: &[(String, String)]) -> IoResult<()> {
for &(ref k, ref v) in parameters.iter() {
try!(self.write(b";"));
try!(self.write_parameter(k[], v));
}
Ok(())
}
fn write_quality(&mut self, quality: Option<f64>) -> IoResult<()> {
match quality {
Some(qvalue) => write!(&mut *self, ";q={:0.3}", qvalue),
None => Ok(()),
}
}
#[inline]
fn write_token(&mut self, token: &String) -> IoResult<()> {
assert!(is_token(token));
self.write(token.as_bytes())
}
}
impl<W: Writer> WriterUtil for W { }
#[inline]
pub fn comma_join(values: &[String]) -> String {
let mut out = String::new();
let mut iter = values.iter();
match iter.next() {
Some(s) => out.push_str(s[]),
None => return out
}
for value in iter {
out.push_str(", ");
out.push_str(value[]);
}
out
}
pub fn push_maybe_quoted_string(mut s: String, t: &String) -> String {
if is_token(t) {
s.push_str(t[]);
s
} else {
push_quoted_string(s, t)
}
}
pub fn maybe_quoted_string(s: &String) -> String {
if is_token(s) {
s.clone()
} else {
quoted_string(s)
}
}
pub fn push_quoted_string(mut s: String, t: &String) -> String {
let i = s.len();
s.reserve(t.len() + i + 2);
s.push('"');
for c in t[].chars() {
if c == '\\' || c == '"' {
s.push('\\');
}
s.push(c);
}
s.push('"');
s
}
pub fn quoted_string(s: &String) -> String {
push_quoted_string(String::new(), s)
}
pub fn unquote_string(s: &String) -> Option<String> {
enum State { Start, Normal, Escaping, End }
let mut state = State::Start;
let mut output = String::new();
output.reserve(s.len() - 2);
let mut iter = s[].chars();
loop {
state = match (state, iter.next()) {
(State::Start, Some(c)) if c == '"' => State::Normal,
(State::Start, Some(_)) => return None,
(State::Normal, Some(c)) if c == '\\' => State::Escaping,
(State::Normal, Some(c)) if c == '"' => State::End,
(State::Normal, Some(c)) | (State::Escaping, Some(c)) => {
output.push(c);
State::Normal
},
(State::End, Some(_)) => return None,
(State::End, None) => return Some(output),
(_, None) => return None,
}
}
}
pub fn maybe_unquote_string(s: &String) -> Option<String> {
if is_token(s) {
Some(s.clone())
} else {
unquote_string(s)
}
}
pub fn push_parameter(mut s: String, k: &String, v: &String) -> String {
s.push_str(k[]);
s.push('=');
push_maybe_quoted_string(s, v)
}
pub fn push_parameters(mut s: String, parameters: &[(String, String)]) -> String {
for &(ref k, ref v) in parameters.iter() {
s.push(';');
s = push_parameter(s, k, v);
}
s
}
#[cfg(test)]
mod test {
use super::{normalise_header_name, comma_split, comma_split_iter, comma_join,
push_parameter, push_parameters, push_maybe_quoted_string, push_quoted_string,
maybe_quoted_string, quoted_string, unquote_string, maybe_unquote_string};
#[test]
#[should_fail]
fn test_normalise_header_name_fail() {
normalise_header_name(&String::from_str("foö-bar"));
}
#[test]
fn test_normalise_header_name() {
assert_eq!(normalise_header_name(&String::from_str("foo-bar")), String::from_str("Foo-Bar"));
assert_eq!(normalise_header_name(&String::from_str("FOO-BAR")), String::from_str("Foo-Bar"));
}
#[test]
fn test_comma_split() {
assert_eq!(comma_split(""), vec!(String::new()));
assert_eq!(comma_split("foo"), vec!(String::from_str("foo")));
assert_eq!(comma_split("foo,bar"), vec!(String::from_str("foo"), String::from_str("bar")));
assert_eq!(comma_split("foo,bar,baz,quux"), vec!(String::from_str("foo"), String::from_str("bar"), String::from_str("baz"), String::from_str("quux")));
assert_eq!(comma_split("\"foo,bar\",baz"), vec!(String::from_str("\"foo"), String::from_str("bar\""), String::from_str("baz")));
assert_eq!(comma_split(" foo;q=0.8 , bar/* "), vec!(String::from_str("foo;q=0.8 "), String::from_str("bar/* ")));
}
#[test]
fn test_comma_split_iter() {
let s = "";
assert_eq!(comma_split_iter(s).collect::< Vec<&'static str> >(), vec![""]);
let s = "foo";
assert_eq!(comma_split_iter(s).collect::< Vec<&'static str> >(), vec!["foo"]);
let s = "foo,bar";
assert_eq!(comma_split_iter(s).collect::< Vec<&'static str> >(), vec!["foo", "bar"]);
let s = "foo,bar,baz,quux";
assert_eq!(comma_split_iter(s).collect::< Vec<&'static str> >(), vec!["foo", "bar", "baz", "quux"]);
let s = "\"foo,bar\",baz";
assert_eq!(comma_split_iter(s).collect::< Vec<&'static str> >(), vec!["\"foo", "bar\"", "baz"]);
let s = " foo;q=0.8 , bar/* ";
assert_eq!(comma_split_iter(s).collect::< Vec<&'static str> >(), vec!["foo;q=0.8 ", "bar/* "]);
}
#[test]
fn test_comma_join() {
assert_eq!(comma_join(&[String::new()]), String::new());
assert_eq!(comma_join(&[String::from_str("foo")]), String::from_str("foo"));
assert_eq!(comma_join(&[String::from_str("foo"), String::from_str("bar")]), String::from_str("foo, bar"));
assert_eq!(comma_join(&[String::from_str("foo"), String::from_str("bar"), String::from_str("baz"), String::from_str("quux")]), String::from_str("foo, bar, baz, quux"));
assert_eq!(comma_join(&[String::from_str("\"foo,bar\""), String::from_str("baz")]), String::from_str("\"foo,bar\", baz"));
assert_eq!(comma_join(&[String::from_str(" foo;q=0.8 "), String::from_str("bar/* ")]), String::from_str(" foo;q=0.8 , bar/* "));
}
#[test]
fn test_push_maybe_quoted_string() {
assert_eq!(push_maybe_quoted_string(String::from_str("foo,"), &String::from_str("bar")), String::from_str("foo,bar"));
assert_eq!(push_maybe_quoted_string(String::from_str("foo,"), &String::from_str("bar/baz")), String::from_str("foo,\"bar/baz\""));
}
#[test]
fn test_maybe_quoted_string() {
assert_eq!(maybe_quoted_string(&String::from_str("bar")), String::from_str("bar"));
assert_eq!(maybe_quoted_string(&String::from_str("bar/baz \"yay\"")), String::from_str("\"bar/baz \\\"yay\\\"\""));
}
#[test]
fn test_push_quoted_string() {
assert_eq!(push_quoted_string(String::from_str("foo,"), &String::from_str("bar")), String::from_str("foo,\"bar\""));
assert_eq!(push_quoted_string(String::from_str("foo,"), &String::from_str("bar/baz \"yay\\\"")),
String::from_str("foo,\"bar/baz \\\"yay\\\\\\\"\""));
}
#[test]
fn test_quoted_string() {
assert_eq!(quoted_string(&String::from_str("bar")), String::from_str("\"bar\""));
assert_eq!(quoted_string(&String::from_str("bar/baz \"yay\\\"")), String::from_str("\"bar/baz \\\"yay\\\\\\\"\""));
}
#[test]
fn test_unquote_string() {
assert_eq!(unquote_string(&String::from_str("bar")), None);
assert_eq!(unquote_string(&String::from_str("\"bar\"")), Some(String::from_str("bar")));
assert_eq!(unquote_string(&String::from_str("\"bar/baz \\\"yay\\\\\\\"\"")), Some(String::from_str("bar/baz \"yay\\\"")));
assert_eq!(unquote_string(&String::from_str("\"bar")), None);
assert_eq!(unquote_string(&String::from_str(" \"bar\"")), None);
assert_eq!(unquote_string(&String::from_str("\"bar\" ")), None);
assert_eq!(unquote_string(&String::from_str("\"bar\" \"baz\"")), None);
assert_eq!(unquote_string(&String::from_str("\"bar/baz \\\"yay\\\\\"\"")), None);
}
#[test]
fn test_maybe_unquote_string() {
assert_eq!(maybe_unquote_string(&String::from_str("bar")), Some(String::from_str("bar")));
assert_eq!(maybe_unquote_string(&String::from_str("\"bar\"")), Some(String::from_str("bar")));
assert_eq!(maybe_unquote_string(&String::from_str("\"bar/baz \\\"yay\\\\\\\"\"")), Some(String::from_str("bar/baz \"yay\\\"")));
assert_eq!(maybe_unquote_string(&String::from_str("\"bar")), None);
assert_eq!(maybe_unquote_string(&String::from_str(" \"bar\"")), None);
assert_eq!(maybe_unquote_string(&String::from_str("\"bar\" ")), None);
assert_eq!(maybe_unquote_string(&String::from_str("\"bar\" \"baz\"")), None);
assert_eq!(maybe_unquote_string(&String::from_str("\"bar/baz \\\"yay\\\\\"\"")), None);
}
#[test]
fn test_push_parameter() {
assert_eq!(push_parameter(String::from_str("foo"), &String::from_str("bar"), &String::from_str("baz")), String::from_str("foobar=baz"));
assert_eq!(push_parameter(String::from_str("foo"), &String::from_str("bar"), &String::from_str("baz/quux")), String::from_str("foobar=\"baz/quux\""));
}
#[test]
fn test_push_parameters() {
assert_eq!(push_parameters(String::from_str("foo"), [][]), String::from_str("foo"));
assert_eq!(push_parameters(String::from_str("foo"), [(String::from_str("bar"), String::from_str("baz"))][]), String::from_str("foo;bar=baz"));
assert_eq!(push_parameters(String::from_str("foo"), [(String::from_str("bar"), String::from_str("baz/quux"))][]), String::from_str("foo;bar=\"baz/quux\""));
assert_eq!(push_parameters(String::from_str("foo"), [(String::from_str("bar"), String::from_str("baz")), (String::from_str("quux"), String::from_str("fuzz"))][]),
String::from_str("foo;bar=baz;quux=fuzz"));
assert_eq!(push_parameters(String::from_str("foo"), [(String::from_str("bar"), String::from_str("baz")), (String::from_str("quux"), String::from_str("fuzz zee"))][]),
String::from_str("foo;bar=baz;quux=\"fuzz zee\""));
assert_eq!(push_parameters(String::from_str("foo"), [(String::from_str("bar"), String::from_str("baz/quux")), (String::from_str("fuzz"), String::from_str("zee"))][]),
String::from_str("foo;bar=\"baz/quux\";fuzz=zee"));
}
}