diff --git a/src/lib.rs b/src/lib.rs index 34233b3..2328fe2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -58,6 +58,15 @@ where let mut ita = path.components(); let mut itb = base.components(); let mut comps: Vec = vec![]; + + // ./foo and foo are the same + if let Some(Component::CurDir) = ita.clone().next() { + ita.next(); + } + if let Some(Component::CurDir) = itb.clone().next() { + itb.next(); + } + loop { match (ita.next(), itb.next()) { (None, None) => break, @@ -136,6 +145,15 @@ mod utf8_paths { let mut ita = path.components(); let mut itb = base.components(); let mut comps: Vec = vec![]; + + // ./foo and foo are the same + if let Some(Utf8Component::CurDir) = ita.clone().next() { + ita.next(); + } + if let Some(Utf8Component::CurDir) = itb.clone().next() { + itb.next(); + } + loop { match (ita.next(), itb.next()) { (None, None) => break, @@ -198,6 +216,9 @@ mod tests { assert_diff_paths("./foo", "./foo", Some("")); assert_diff_paths("/foo", "/foo", Some("")); assert_diff_paths("foo", "foo", Some("")); + assert_diff_paths("./foo", "foo", Some("")); + assert_diff_paths("foo", "./foo", Some("")); + assert_diff_paths("foo/foo", "./foo/foo", Some("")); assert_diff_paths("../foo/bar/baz", "../foo/bar/baz", Some("".into())); assert_diff_paths("foo/bar/baz", "foo/bar/baz", Some("")); @@ -221,6 +242,8 @@ mod tests { assert_diff_paths("../foo", "../bar", Some("../foo")); assert_diff_paths("../foo", "../foo/bar/baz", Some("../..")); assert_diff_paths("../foo/bar/baz", "../foo", Some("bar/baz")); + assert_diff_paths("../foo", "bar", Some("../../foo")); + assert_diff_paths("foo", "../bar", None); assert_diff_paths("foo/bar/baz", "foo", Some("bar/baz")); assert_diff_paths("foo/bar/baz", "foo/bar", Some("baz")); @@ -242,6 +265,10 @@ mod tests { assert_diff_paths(".", "foo", Some("../.")); assert_diff_paths("foo", ".", Some("foo")); assert_diff_paths("/foo", "/.", Some("foo")); + + assert_diff_paths("./foo/bar/baz", "foo", Some("bar/baz")); + assert_diff_paths("foo/bar/baz", "./foo", Some("bar/baz")); + assert_diff_paths("./foo/bar/baz", "./foo", Some("bar/baz")); } fn assert_diff_paths(path: &str, base: &str, expected: Option<&str>) {