From 4c35582a9ca76ec225d2e29489062bf8800b27af Mon Sep 17 00:00:00 2001 From: cheeslide <175485195+cheeslide@users.noreply.github.com> Date: Sun, 23 Feb 2025 10:12:32 +0800 Subject: [PATCH 1/2] fix: start with . (issue #9) ./foo and foo are the same --- src/lib.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 34233b3..9be4f61 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -57,6 +57,14 @@ where } else { let mut ita = path.components(); let mut itb = base.components(); + + // ./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(); + } let mut comps: Vec = vec![]; loop { match (ita.next(), itb.next()) { @@ -198,6 +206,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 +232,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 +255,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>) { From f0be99642517c7570169f8d512ed341ec45fd712 Mon Sep 17 00:00:00 2001 From: cheeslide <175485195+cheeslide@users.noreply.github.com> Date: Tue, 25 Feb 2025 22:13:28 +0800 Subject: [PATCH 2/2] Update utf8_paths --- src/lib.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9be4f61..2328fe2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -57,6 +57,7 @@ where } else { 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() { @@ -65,7 +66,7 @@ where if let Some(Component::CurDir) = itb.clone().next() { itb.next(); } - let mut comps: Vec = vec![]; + loop { match (ita.next(), itb.next()) { (None, None) => break, @@ -144,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, @@ -255,7 +265,7 @@ 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"));