Przeglądaj źródła

idna: add a fast path for mapping simple ASCII characters

Dirkjan Ochtman 6 lat temu
rodzic
commit
8c22586d68
2 zmienionych plików z 25 dodań i 1 usunięć
  1. 0 1
      idna/Cargo.toml
  2. 25 0
      idna/src/uts46.rs

+ 0 - 1
idna/Cargo.toml

@@ -10,7 +10,6 @@ edition = "2018"
 
 [lib]
 doctest = false
-test = false
 
 [[test]]
 name = "tests"

+ 25 - 0
idna/src/uts46.rs

@@ -83,6 +83,11 @@ fn find_char(codepoint: char) -> &'static Mapping {
 }
 
 fn map_char(codepoint: char, config: Config, output: &mut String, errors: &mut Vec<Error>) {
+    if let '.' | '-' | 'a'..='z' | '0'..='9' = codepoint {
+        output.push(codepoint);
+        return;
+    }
+
     match *find_char(codepoint) {
         Mapping::Valid => output.push(codepoint),
         Mapping::Ignored => {}
@@ -558,3 +563,23 @@ impl fmt::Display for Errors {
         Ok(())
     }
 }
+
+#[cfg(test)]
+mod tests {
+    use super::{find_char, Mapping};
+
+    #[test]
+    fn mapping_fast_path() {
+        assert_matches!(find_char('-'), &Mapping::Valid);
+        assert_matches!(find_char('.'), &Mapping::Valid);
+        for c in &['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] {
+            assert_matches!(find_char(*c), &Mapping::Valid);
+        }
+        for c in &[
+            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
+            'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
+        ] {
+            assert_matches!(find_char(*c), &Mapping::Valid);
+        }
+    }
+}