Просмотр исходного кода

Add percent encoding/decoding wrappers that return a String/Vec.

Simon Sapin 12 лет назад
Родитель
Сommit
8701d4d947
3 измененных файлов с 29 добавлено и 19 удалено
  1. 5 5
      src/form_urlencoded.rs
  2. 2 3
      src/parser.rs
  3. 22 11
      src/url.rs

+ 5 - 5
src/form_urlencoded.rs

@@ -18,7 +18,7 @@ use encoding::EncodingRef;
 use encoding::all::UTF_8;
 use encoding::label::encoding_from_whatwg_label;
 
-use super::{percent_encode, percent_decode};
+use super::{percent_encode_to, percent_decode};
 use encode_sets::FORM_URLENCODED_ENCODE_SET;
 
 
@@ -66,9 +66,9 @@ pub fn parse_bytes(input: &[u8], encoding_override: Option<EncodingRef>,
 
     #[inline]
     fn decode(input: Vec<u8>, encoding_override: EncodingRef) -> String {
-        let mut bytes = Vec::new();
-        percent_decode(input.as_slice(), &mut bytes);
-        encoding_override.decode(bytes.as_slice(), encoding::DecodeReplace).unwrap()
+        encoding_override.decode(
+            percent_decode(input.as_slice()).as_slice(),
+            encoding::DecodeReplace).unwrap()
     }
 
     Some(pairs.move_iter().map(
@@ -96,7 +96,7 @@ pub fn serialize<'a, I: Iterator<(&'a str, &'a str)>>(
             if byte == b' ' {
                 output.push_str("+")
             } else {
-                percent_encode([byte], FORM_URLENCODED_ENCODE_SET, output)
+                percent_encode_to([byte], FORM_URLENCODED_ENCODE_SET, output)
             }
         }
     }

+ 2 - 3
src/parser.rs

@@ -567,9 +567,8 @@ pub fn parse_query<'a>(input: &'a str, context: Context, parser: &UrlParser)
         },
         None => query.as_bytes()  // UTF-8
     };
-    let mut query_encoded = String::new();
-    percent_encode(query_bytes.as_slice(), QUERY_ENCODE_SET, &mut query_encoded);
-    Ok((query_encoded, remaining))
+    ;
+    Ok((percent_encode(query_bytes.as_slice(), QUERY_ENCODE_SET), remaining))
 }
 
 

+ 22 - 11
src/url.rs

@@ -363,7 +363,7 @@ impl RelativeSchemeData {
                     let mut bytes = Vec::new();
                     for path_part in self.path.iter() {
                         bytes.push(b'/');
-                        percent_decode(path_part.as_bytes(), &mut bytes);
+                        percent_decode_to(path_part.as_bytes(), &mut bytes);
                     }
                     Ok(Path::new(bytes))
                 }
@@ -587,8 +587,7 @@ impl Host {
                 Err("Invalid Ipv6 address")
             }
         } else {
-            let mut decoded = Vec::new();
-            percent_decode(input.as_bytes(), &mut decoded);
+            let decoded = percent_decode(input.as_bytes());
             let domain = String::from_utf8_lossy(decoded.as_slice());
             // TODO: Remove this check and use IDNA "domain to ASCII"
             if !domain.as_slice().is_ascii() {
@@ -817,12 +816,12 @@ fn from_hex(byte: u8) -> Option<u8> {
 
 #[inline]
 pub fn utf8_percent_encode(input: &str, encode_set: &[&str], output: &mut String) {
-    percent_encode(input.as_bytes(), encode_set, output)
+    percent_encode_to(input.as_bytes(), encode_set, output)
 }
 
 
 #[inline]
-pub fn percent_encode(input: &[u8], encode_set: &[&str], output: &mut String) {
+pub fn percent_encode_to(input: &[u8], encode_set: &[&str], output: &mut String) {
     for &byte in input.iter() {
         output.push_str(encode_set[byte as uint])
     }
@@ -830,7 +829,14 @@ pub fn percent_encode(input: &[u8], encode_set: &[&str], output: &mut String) {
 
 
 #[inline]
-pub fn percent_decode(input: &[u8], output: &mut Vec<u8>) {
+pub fn percent_encode(input: &[u8], encode_set: &[&str]) -> String {
+    let mut output = String::new();
+    percent_encode_to(input, encode_set, &mut output);
+    output
+}
+
+
+pub fn percent_decode_to(input: &[u8], output: &mut Vec<u8>) {
     let mut i = 0u;
     while i < input.len() {
         let c = input[i];
@@ -850,15 +856,20 @@ pub fn percent_decode(input: &[u8], output: &mut Vec<u8>) {
     }
 }
 
+
+#[inline]
+pub fn percent_decode(input: &[u8]) -> Vec<u8> {
+    let mut output = Vec::new();
+    percent_decode_to(input, &mut output);
+    output
+}
+
+
 // FIXME: Figure out what to do on Windows
 #[cfg(unix)]
 fn encode_file_path(path: &Path) -> Result<Vec<String>, ()> {
     if !path.is_absolute() {
         return Err(())
     }
-    Ok(path.components().map(|path_part| {
-        let mut encoded = String::new();
-        percent_encode(path_part, DEFAULT_ENCODE_SET, &mut encoded);
-        encoded
-    }).collect())
+    Ok(path.components().map(|c| percent_encode(c, DEFAULT_ENCODE_SET)).collect())
 }