about summary refs log tree commit diff
path: root/src/libsyntax
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2018-09-13 22:40:35 +0000
committerbors <bors@rust-lang.org>2018-09-13 22:40:35 +0000
commit4f921d7a8d8dee7b511e4956e06a80f550003afc (patch)
tree2669f8223a1f824c967e590624e285a31c1f6e49 /src/libsyntax
parent90d36fb5905bbe5004f5b465ea14b53d10dae260 (diff)
parent07dc4b3759f3cc0e97777fe944dbc54c967424fd (diff)
downloadrust-4f921d7a8d8dee7b511e4956e06a80f550003afc.tar.gz
rust-4f921d7a8d8dee7b511e4956e06a80f550003afc.zip
Auto merge of #54168 - kennytm:rollup, r=kennytm
Rollup of 11 pull requests

Successful merges:

 - #53371 (Do not emit E0277 on incorrect tuple destructured binding)
 - #53829 (Add rustc SHA to released DWARF debuginfo)
 - #53950 (Allow for opting out of ThinLTO and clean up LTO related cli flag handling.)
 - #53976 (Replace unwrap calls in example by expect)
 - #54070 (Add Error::description soft-deprecation to RELEASES)
 - #54076 (miri loop detector hashing)
 - #54119 (Add some unit tests for find_best_match_for_name)
 - #54147 (Add a test that tries to modify static memory at compile-time)
 - #54150 (Updated 1.29 release notes with --document-private-items flag)
 - #54163 (Update stage 0 to latest beta)
 - #54170 (COMPILER_TESTS.md has been moved)
Diffstat (limited to 'src/libsyntax')
-rw-r--r--src/libsyntax/util/lev_distance.rs42
1 files changed, 40 insertions, 2 deletions
diff --git a/src/libsyntax/util/lev_distance.rs b/src/libsyntax/util/lev_distance.rs
index e429791f2bd..feee2422cb6 100644
--- a/src/libsyntax/util/lev_distance.rs
+++ b/src/libsyntax/util/lev_distance.rs
@@ -11,7 +11,7 @@
 use std::cmp;
 use symbol::Symbol;
 
-/// To find the Levenshtein distance between two strings
+/// Find the Levenshtein distance between two strings
 pub fn lev_distance(a: &str, b: &str) -> usize {
     // cases which don't require further computation
     if a.is_empty() {
@@ -41,10 +41,12 @@ pub fn lev_distance(a: &str, b: &str) -> usize {
     } dcol[t_last + 1]
 }
 
-/// To find the best match for a given string from an iterator of names
+/// Find the best match for a given word in the given iterator
+///
 /// As a loose rule to avoid the obviously incorrect suggestions, it takes
 /// an optional limit for the maximum allowable edit distance, which defaults
 /// to one-third of the given word.
+///
 /// Besides Levenshtein, we use case insensitive comparison to improve accuracy on an edge case with
 /// a lower(upper)case letters mismatch.
 pub fn find_best_match_for_name<'a, T>(iter_names: T,
@@ -105,3 +107,39 @@ fn test_lev_distance() {
     assert_eq!(lev_distance(b, c), 1);
     assert_eq!(lev_distance(c, b), 1);
 }
+
+#[test]
+fn test_find_best_match_for_name() {
+    use with_globals;
+    with_globals(|| {
+        let input = vec![Symbol::intern("aaab"), Symbol::intern("aaabc")];
+        assert_eq!(
+            find_best_match_for_name(input.iter(), "aaaa", None),
+            Some(Symbol::intern("aaab"))
+        );
+
+        assert_eq!(
+            find_best_match_for_name(input.iter(), "1111111111", None),
+            None
+        );
+
+        let input = vec![Symbol::intern("aAAA")];
+        assert_eq!(
+            find_best_match_for_name(input.iter(), "AAAA", None),
+            Some(Symbol::intern("aAAA"))
+        );
+
+        let input = vec![Symbol::intern("AAAA")];
+        // Returns None because `lev_distance > max_dist / 3`
+        assert_eq!(
+            find_best_match_for_name(input.iter(), "aaaa", None),
+            None
+        );
+
+        let input = vec![Symbol::intern("AAAA")];
+        assert_eq!(
+            find_best_match_for_name(input.iter(), "aaaa", Some(4)),
+            Some(Symbol::intern("AAAA"))
+        );
+    })
+}