浏览代码

Make implementation details of percent-encoding encode sets private.

Simon Sapin 12 年之前
父节点
当前提交
e18f7e460c
共有 5 个文件被更改,包括 47 次插入24 次删除
  1. 7 7
      make_encode_sets.py
  2. 7 7
      src/encode_sets.rs
  3. 1 2
      src/form_urlencoded.rs
  4. 2 2
      src/parser.rs
  5. 30 6
      src/url.rs

+ 7 - 7
make_encode_sets.py

@@ -22,13 +22,13 @@ print('''\
 // Generated by make_encode_sets.py
 ''')
 for name, encoded in [
-    ('SIMPLE_ENCODE_SET',   ''),
-    ('QUERY_ENCODE_SET',    r''' "#<>`'''),
-    ('DEFAULT_ENCODE_SET',  r''' "#<>`?'''),
-    ('USERINFO_ENCODE_SET', r''' "#<>`?@'''),
-    ('PASSWORD_ENCODE_SET', r''' "#<>`?@\/'''),
-    ('USERNAME_ENCODE_SET', r''' "#<>`?@\/:'''),
-    ('FORM_URLENCODED_ENCODE_SET', r''' !"#$%&\'()+,/:;<=>?@[\]^`{|}'''),
+    ('SIMPLE',   ''),
+    ('QUERY',    r''' "#<>`'''),
+    ('DEFAULT',  r''' "#<>`?'''),
+    ('USERINFO', r''' "#<>`?@'''),
+    ('PASSWORD', r''' "#<>`?@\/'''),
+    ('USERNAME', r''' "#<>`?@\/:'''),
+    ('FORM_URLENCODED', r''' !"#$%&\'()+,/:;<=>?@[\]^`{|}'''),
 ]:
     print(
         "pub static %s: [&'static str, ..256] = [\n%s\n];\n\n"

+ 7 - 7
src/encode_sets.rs

@@ -8,7 +8,7 @@
 
 // Generated by make_encode_sets.py
 
-pub static SIMPLE_ENCODE_SET: [&'static str, ..256] = [
+pub static SIMPLE: [&'static str, ..256] = [
    "%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07",
    "%08", "%09", "%0A", "%0B", "%0C", "%0D", "%0E", "%0F",
    "%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17",
@@ -44,7 +44,7 @@ pub static SIMPLE_ENCODE_SET: [&'static str, ..256] = [
 ];
 
 
-pub static QUERY_ENCODE_SET: [&'static str, ..256] = [
+pub static QUERY: [&'static str, ..256] = [
    "%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07",
    "%08", "%09", "%0A", "%0B", "%0C", "%0D", "%0E", "%0F",
    "%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17",
@@ -80,7 +80,7 @@ pub static QUERY_ENCODE_SET: [&'static str, ..256] = [
 ];
 
 
-pub static DEFAULT_ENCODE_SET: [&'static str, ..256] = [
+pub static DEFAULT: [&'static str, ..256] = [
    "%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07",
    "%08", "%09", "%0A", "%0B", "%0C", "%0D", "%0E", "%0F",
    "%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17",
@@ -116,7 +116,7 @@ pub static DEFAULT_ENCODE_SET: [&'static str, ..256] = [
 ];
 
 
-pub static USERINFO_ENCODE_SET: [&'static str, ..256] = [
+pub static USERINFO: [&'static str, ..256] = [
    "%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07",
    "%08", "%09", "%0A", "%0B", "%0C", "%0D", "%0E", "%0F",
    "%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17",
@@ -152,7 +152,7 @@ pub static USERINFO_ENCODE_SET: [&'static str, ..256] = [
 ];
 
 
-pub static PASSWORD_ENCODE_SET: [&'static str, ..256] = [
+pub static PASSWORD: [&'static str, ..256] = [
    "%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07",
    "%08", "%09", "%0A", "%0B", "%0C", "%0D", "%0E", "%0F",
    "%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17",
@@ -188,7 +188,7 @@ pub static PASSWORD_ENCODE_SET: [&'static str, ..256] = [
 ];
 
 
-pub static USERNAME_ENCODE_SET: [&'static str, ..256] = [
+pub static USERNAME: [&'static str, ..256] = [
    "%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07",
    "%08", "%09", "%0A", "%0B", "%0C", "%0D", "%0E", "%0F",
    "%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17",
@@ -224,7 +224,7 @@ pub static USERNAME_ENCODE_SET: [&'static str, ..256] = [
 ];
 
 
-pub static FORM_URLENCODED_ENCODE_SET: [&'static str, ..256] = [
+pub static FORM_URLENCODED: [&'static str, ..256] = [
    "%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07",
    "%08", "%09", "%0A", "%0B", "%0C", "%0D", "%0E", "%0F",
    "%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17",

+ 1 - 2
src/form_urlencoded.rs

@@ -20,8 +20,7 @@ use encoding::EncodingRef;
 use encoding::all::UTF_8;
 use encoding::label::encoding_from_whatwg_label;
 
-use super::{percent_encode_to, percent_decode};
-use encode_sets::FORM_URLENCODED_ENCODE_SET;
+use super::{percent_encode_to, percent_decode, FORM_URLENCODED_ENCODE_SET};
 
 
 /// Convert a string in the `application/x-www-form-urlencoded` format

+ 2 - 2
src/parser.rs

@@ -15,8 +15,8 @@ use encoding;
 use super::{
     ParseResult, UrlParser, Url, RelativeSchemeData, NonRelativeSchemeData, Host, Domain,
     SchemeType, FileLikeScheme, RelativeScheme, NonRelativeScheme,
-    utf8_percent_encode_to, percent_encode};
-use encode_sets::{SIMPLE_ENCODE_SET, DEFAULT_ENCODE_SET, USERINFO_ENCODE_SET, QUERY_ENCODE_SET};
+    utf8_percent_encode_to, percent_encode,
+    SIMPLE_ENCODE_SET, DEFAULT_ENCODE_SET, USERINFO_ENCODE_SET, QUERY_ENCODE_SET};
 
 
 macro_rules! is_match(

+ 30 - 6
src/url.rs

@@ -918,28 +918,52 @@ fn from_hex(byte: u8) -> Option<u8> {
 }
 
 
-#[inline]
-pub fn utf8_percent_encode_to(input: &str, encode_set: &[&str], output: &mut String) {
-    percent_encode_to(input.as_bytes(), encode_set, output)
+pub struct EncodeSet {
+    map: &'static [&'static str, ..256],
 }
 
+pub static SIMPLE_ENCODE_SET: EncodeSet = EncodeSet { map: &encode_sets::SIMPLE };
+pub static QUERY_ENCODE_SET: EncodeSet = EncodeSet { map: &encode_sets::QUERY };
+pub static DEFAULT_ENCODE_SET: EncodeSet = EncodeSet { map: &encode_sets::DEFAULT };
+pub static USERINFO_ENCODE_SET: EncodeSet = EncodeSet { map: &encode_sets::USERINFO };
+pub static PASSWORD_ENCODE_SET: EncodeSet = EncodeSet { map: &encode_sets::PASSWORD };
+pub static USERNAME_ENCODE_SET: EncodeSet = EncodeSet { map: &encode_sets::USERNAME };
+pub static FORM_URLENCODED_ENCODE_SET: EncodeSet = EncodeSet { map: &encode_sets::FORM_URLENCODED };
+
 
 #[inline]
-pub fn percent_encode_to(input: &[u8], encode_set: &[&str], output: &mut String) {
+pub fn percent_encode_to(input: &[u8], encode_set: EncodeSet, output: &mut String) {
     for &byte in input.iter() {
-        output.push_str(encode_set[byte as uint])
+        output.push_str(encode_set.map[byte as uint])
     }
 }
 
 
+/// Percent-encode the given bytes.
+///
+/// The returned string. is within the ASCII range.
 #[inline]
-pub fn percent_encode(input: &[u8], encode_set: &[&str]) -> String {
+pub fn percent_encode(input: &[u8], encode_set: EncodeSet) -> String {
     let mut output = String::new();
     percent_encode_to(input, encode_set, &mut output);
     output
 }
 
 
+#[inline]
+pub fn utf8_percent_encode_to(input: &str, encode_set: EncodeSet, output: &mut String) {
+    percent_encode_to(input.as_bytes(), encode_set, output)
+}
+
+
+#[inline]
+pub fn utf8_percent_encode(input: &str, encode_set: EncodeSet) -> String {
+    let mut output = String::new();
+    utf8_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() {